Struct reactive_messaging::prelude::GenericSocketClient
source · pub struct GenericSocketClient<const CONFIG: u64, RemoteMessages: ReactiveMessagingDeserializer<RemoteMessages> + Send + Sync + PartialEq + Debug + 'static, LocalMessages: ReactiveMessagingSerializer<LocalMessages> + Send + Sync + PartialEq + Debug + 'static, ProcessorUniType: GenericUni<ItemType = RemoteMessages> + Send + Sync + 'static, SenderChannel: FullDuplexUniChannel<ItemType = LocalMessages, DerivedItemType = LocalMessages> + Send + Sync> { /* private fields */ }Expand description
Real definition & implementation for our Socket Client, full of generic parameters.
Probably you want to instantiate this structure through the sugared macro new_socket_client!() instead.
Generic Parameters:
CONFIG: theu64version of the [ConstConfig] instance used to build this struct – from whichProcessorUniTypeandSenderChannelderive;RemoteMessages: the messages that are generated by the server (usually anenum);LocalMessages: the messages that are generated by this client (usually anenum);ProcessorUniType: an instance of areactive-mutiny’s [Uni] type (using one of the zero-copy channels) – This [Uni] will execute the given client reactive logic for each incoming message (see how it is used in new_socket_client!());SenderChannel: an instance of areactive-mutiny’s Uni movableChannel, which will provide aStreamof messages to be sent to the server.
Implementations§
source§impl<const CONFIG: u64, RemoteMessages: ReactiveMessagingDeserializer<RemoteMessages> + Send + Sync + PartialEq + Debug + 'static, LocalMessages: ReactiveMessagingSerializer<LocalMessages> + Send + Sync + PartialEq + Debug + 'static, ProcessorUniType: GenericUni<ItemType = RemoteMessages> + Send + Sync + 'static, SenderChannel: FullDuplexUniChannel<ItemType = LocalMessages, DerivedItemType = LocalMessages> + Send + Sync + 'static> GenericSocketClient<CONFIG, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel>
impl<const CONFIG: u64, RemoteMessages: ReactiveMessagingDeserializer<RemoteMessages> + Send + Sync + PartialEq + Debug + 'static, LocalMessages: ReactiveMessagingSerializer<LocalMessages> + Send + Sync + PartialEq + Debug + 'static, ProcessorUniType: GenericUni<ItemType = RemoteMessages> + Send + Sync + 'static, SenderChannel: FullDuplexUniChannel<ItemType = LocalMessages, DerivedItemType = LocalMessages> + Send + Sync + 'static> GenericSocketClient<CONFIG, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel>
sourcepub fn new<IntoString: Into<String>>(ip: IntoString, port: u16) -> Self
pub fn new<IntoString: Into<String>>(ip: IntoString, port: u16) -> Self
Instantiates a client to connect to a TCP/IP Server:
ip: the server IP to connect to
port: the server port to connect to
sourcepub async fn spawn_unresponsive_processor<OutputStreamItemsType: Send + Sync + Debug + 'static, ServerStreamType: Stream<Item = OutputStreamItemsType> + Send + 'static, ConnectionEventsCallbackFuture: Future<Output = ()> + Send + 'static, ConnectionEventsCallback: Fn(ConnectionEvent<CONFIG, LocalMessages, SenderChannel>) -> ConnectionEventsCallbackFuture + Send + Sync + 'static, ProcessorBuilderFn: Fn(String, u16, Arc<Peer<CONFIG, LocalMessages, SenderChannel>>, MessagingMutinyStream<ProcessorUniType>) -> ServerStreamType + Send + Sync + 'static>(
&mut self,
connection_events_callback: ConnectionEventsCallback,
dialog_processor_builder_fn: ProcessorBuilderFn
) -> Result<(), Box<dyn Error + Sync + Send>>
pub async fn spawn_unresponsive_processor<OutputStreamItemsType: Send + Sync + Debug + 'static, ServerStreamType: Stream<Item = OutputStreamItemsType> + Send + 'static, ConnectionEventsCallbackFuture: Future<Output = ()> + Send + 'static, ConnectionEventsCallback: Fn(ConnectionEvent<CONFIG, LocalMessages, SenderChannel>) -> ConnectionEventsCallbackFuture + Send + Sync + 'static, ProcessorBuilderFn: Fn(String, u16, Arc<Peer<CONFIG, LocalMessages, SenderChannel>>, MessagingMutinyStream<ProcessorUniType>) -> ServerStreamType + Send + Sync + 'static>( &mut self, connection_events_callback: ConnectionEventsCallback, dialog_processor_builder_fn: ProcessorBuilderFn ) -> Result<(), Box<dyn Error + Sync + Send>>
Spawns a task to start the local client, returning immediately.
The given dialog_processor_builder_fn will be called when the connection is established and should return a Stream
that will produce non-futures & non-fallible items that won’t be sent to the server:
connection_events_callback: – a generic function (or closure) to handle connected, disconnected and shutdown events. Sign it as:async fn connection_events_handler<const CONFIG: u64, LocalMessages: ReactiveMessagingSerializer<LocalMessages> + Send + Sync + PartialEq + Debug, SenderChannel: FullDuplexUniChannel<ItemType=LocalMessages, DerivedItemType=LocalMessages> + Send + Sync> (_event: ConnectionEvent<CONFIG, LocalMessages, SenderChannel>) {...}dialog_processor_builder_fn– the generic function (or closure) that receives theStreamof server messages and returns anotherStream, which won’t be sent out to the server – called once for each connection. Sign it as:fn unresponsive_processor<const CONFIG: u64, LocalMessages: ReactiveMessagingSerializer<LocalMessages> + Send + Sync + PartialEq + Debug, SenderChannel: FullDuplexUniChannel<ItemType=LocalMessages, DerivedItemType=LocalMessages> + Send + Sync, StreamItemType: Deref<Target=[your type for messages produced by the SERVER]>> (server_addr: String, connected_port: u16, peer: Arc<Peer<CONFIG, LocalMessages, SenderChannel>>, server_messages_stream: impl Stream<Item=StreamItemType>) -> impl Stream<Item=()> {...}
– if you want the processor to produce answer messages of type LocalMessages to be sent to the server, see Self::spawn_responsive_processor():
sourcepub async fn spawn_responsive_processor<ServerStreamType: Stream<Item = LocalMessages> + Send + 'static, ConnectionEventsCallbackFuture: Future<Output = ()> + Send + 'static, ConnectionEventsCallback: Fn(ConnectionEvent<CONFIG, LocalMessages, SenderChannel>) -> ConnectionEventsCallbackFuture + Send + Sync + 'static, ProcessorBuilderFn: Fn(String, u16, Arc<Peer<CONFIG, LocalMessages, SenderChannel>>, MessagingMutinyStream<ProcessorUniType>) -> ServerStreamType + Send + Sync + 'static>(
&mut self,
connection_events_callback: ConnectionEventsCallback,
dialog_processor_builder_fn: ProcessorBuilderFn
) -> Result<(), Box<dyn Error + Sync + Send>>where
LocalMessages: ResponsiveMessages<LocalMessages>,
pub async fn spawn_responsive_processor<ServerStreamType: Stream<Item = LocalMessages> + Send + 'static, ConnectionEventsCallbackFuture: Future<Output = ()> + Send + 'static, ConnectionEventsCallback: Fn(ConnectionEvent<CONFIG, LocalMessages, SenderChannel>) -> ConnectionEventsCallbackFuture + Send + Sync + 'static, ProcessorBuilderFn: Fn(String, u16, Arc<Peer<CONFIG, LocalMessages, SenderChannel>>, MessagingMutinyStream<ProcessorUniType>) -> ServerStreamType + Send + Sync + 'static>( &mut self, connection_events_callback: ConnectionEventsCallback, dialog_processor_builder_fn: ProcessorBuilderFn ) -> Result<(), Box<dyn Error + Sync + Send>>where LocalMessages: ResponsiveMessages<LocalMessages>,
Spawns a task to start the local client, returning immediately,
The given dialog_processor_builder_fn will be called when the connection is established and should return a Stream
that will produce non-futures & non-fallible items that will be sent to the server:
connection_events_callback: – a generic function (or closure) to handle connected, disconnected and shutdown events. Sign it as:async fn connection_events_handler<const CONFIG: u64, LocalMessages: ReactiveMessagingSerializer<LocalMessages> + Send + Sync + PartialEq + Debug, SenderChannel: FullDuplexUniChannel<ItemType=LocalMessages, DerivedItemType=LocalMessages> + Send + Sync> (_event: ConnectionEvent<CONFIG, LocalMessages, SenderChannel>) {...}dialog_processor_builder_fn– the generic function (or closure) that receives theStreamof server messages and returns theStreamof client messages to be sent to the server – called once for each connection. Sign it as:fn responsive_processor<const CONFIG: u64, LocalMessages: ReactiveMessagingSerializer<LocalMessages> + Send + Sync + PartialEq + Debug, SenderChannel: FullDuplexUniChannel<ItemType=LocalMessages, DerivedItemType=LocalMessages> + Send + Sync, StreamItemType: Deref<Target=[your type for messages produced by the SERVER]>> (client_addr: String, connected_port: u16, peer: Arc<Peer<CONFIG, LocalMessages, SenderChannel>>, client_messages_stream: impl Stream<Item=StreamItemType>) -> impl Stream<Item=LocalMessages> {...}
Notice that this method requires that LocalMessages implements, additionally, [ResponsiveMessages<>].
– if you don’t want the processor to produce answer messages, see Self::spawn_unresponsive_processor().
sourcepub fn shutdown_waiter(
&mut self
) -> impl FnOnce() -> BoxFuture<'static, Result<(), Box<dyn Error + Send + Sync>>>
pub fn shutdown_waiter( &mut self ) -> impl FnOnce() -> BoxFuture<'static, Result<(), Box<dyn Error + Send + Sync>>>
Returns an async closure that blocks until Self::shutdown() is called. Example:
self.spawn_the_client(logic...);
self.shutdown_waiter()().await;sourcepub fn shutdown(
self,
timeout_ms: u32
) -> Result<(), Box<dyn Error + Send + Sync>>
pub fn shutdown( self, timeout_ms: u32 ) -> Result<(), Box<dyn Error + Send + Sync>>
Notifies the client it is time to shutdown.
A shutdown is considered graceful if it could be accomplished in less than timeout_ms milliseconds.
It is a good practice that the connection_events_handler() you provided when starting the client
uses this time to inform the server that a client-initiated disconnection (due to a local shutdown) is happening.