Struct reactive_messaging::prelude::GenericSocketServer
source · pub struct GenericSocketServer<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 Server, full of generic parameters.
Probably you want to instantiate this structure through the sugared macro new_socket_server!() instead.
Generic Parameters:
CONFIG: theu64version of the [ConstConfig] instance used to build this struct – from whichProcessorUniTypeandSenderChannelderive;RemoteMessages: the messages that are generated by the clients (usually anenum);LocalMessages: the messages that are generated by the server (usually anenum);ProcessorUniType: an instance of areactive-mutiny’s [Uni] type (using one of the zero-copy channels) – This [Uni] will execute the given server reactive logic for each incoming message (see how it is used in new_socket_server!());SenderChannel: an instance of areactive-mutiny’s Uni movableChannel, which will provide aStreamof messages to be sent to the client.
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> GenericSocketServer<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> GenericSocketServer<CONFIG, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel>
sourcepub fn new<IntoString: Into<String>>(
interface_ip: IntoString,
port: u16
) -> Self
pub fn new<IntoString: Into<String>>( interface_ip: IntoString, port: u16 ) -> Self
Creates a new server instance listening on TCP/IP:
interface_ip: the interface’s IP to listen to – 0.0.0.0 will cause listening to all network interfaces
port: what port to listen 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 server, returning immediately.
The given dialog_processor_builder_fn will be called for each new connection and should return a Stream
that will produce non-futures & non-fallible items that won’t be sent to the client:
connection_events_callback: – a generic function (or closure) to handle connected, disconnected and shutdown events (possibly to manage sessions). 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 client messages and returns anotherStream, which won’t be sent out to clients – 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 CLIENT]>> (client_addr: String, connected_port: u16, peer: Arc<Peer<CONFIG, LocalMessages, SenderChannel>>, client_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 clients, 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 server, returning immediately,
The given dialog_processor_builder_fn will be called for each new connection and will return a Stream
that will produce non-futures & non-fallible items that will be sent to the client:
connection_events_callback: – a generic function (or closure) to handle connected, disconnected and shutdown events (possibly to manage sessions). 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 client messages and returns theStreamof server messages to be sent to the clients – 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 CLIENT]>> (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_server(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 server 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 server
uses this time to inform all clients that a remote-initiated disconnection (due to a shutdown) is happening.