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: the u64 version of the [ConstConfig] instance used to build this struct – from which ProcessorUniType and SenderChannel derive;
  • RemoteMessages: the messages that are generated by the clients (usually an enum);
  • LocalMessages: the messages that are generated by the server (usually an enum);
  • ProcessorUniType: an instance of a reactive-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 a reactive-mutiny’s Uni movable Channel, which will provide a Stream of 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>

source

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

source

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 the Stream of client messages and returns another Stream, 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():

source

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 the Stream of client messages and returns the Stream of 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().

source

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;
source

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.

Auto Trait Implementations§

§

impl<const CONFIG: u64, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel> !RefUnwindSafe for GenericSocketServer<CONFIG, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel>

§

impl<const CONFIG: u64, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel> Send for GenericSocketServer<CONFIG, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel>

§

impl<const CONFIG: u64, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel> Sync for GenericSocketServer<CONFIG, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel>

§

impl<const CONFIG: u64, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel> Unpin for GenericSocketServer<CONFIG, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel>where LocalMessages: Unpin, ProcessorUniType: Unpin, RemoteMessages: Unpin, SenderChannel: Unpin,

§

impl<const CONFIG: u64, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel> !UnwindSafe for GenericSocketServer<CONFIG, RemoteMessages, LocalMessages, ProcessorUniType, SenderChannel>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V