Struct rustdtp::Server

source ·
pub struct Server<S, R>
where S: Serialize + Send + 'static, R: DeserializeOwned + Send + 'static,
{ /* private fields */ }
Expand description

A socket server.

The server takes two generic parameters:

  • S: the type of data that will be sent to clients.
  • R: the type of data that will be received from clients.

Both types must be serializable in order to be sent through the socket. When creating clients, the types should be swapped, since the server’s send type will be the client’s receive type and vice versa.

use rustdtp::*;

#[tokio::main]
async fn main() {
    // Create a server that receives strings and returns the length of each string
    let (mut server, mut server_events) = Server::builder()
        .sending::<usize>()
        .receiving::<String>()
        .with_event_channel()
        .start(("0.0.0.0", 0))
        .await
        .unwrap();

    // Iterate over events
    while let Some(event) = server_events.next().await {
        match event {
            ServerEvent::Connect { client_id } => {
                println!("Client with ID {} connected", client_id);
            }
            ServerEvent::Disconnect { client_id } => {
                println!("Client with ID {} disconnected", client_id);
            }
            ServerEvent::Receive { client_id, data } => {
                // Send back the length of the string
                server.send(client_id, data.len()).await.unwrap();
            }
            ServerEvent::Stop => {
                // No more events will be sent, and the loop will end
                println!("Server closed");
            }
        }
    }
}

Implementations§

source§

impl Server<(), ()>

source

pub fn builder( ) -> ServerBuilder<ServerSendingUnknown, ServerReceivingUnknown, ServerEventReportingUnknown>

Constructs a server builder. Use this for a clearer, more explicit, and more featureful server configuration. See ServerBuilder for more information.

source§

impl<S, R> Server<S, R>
where S: Serialize + Send + 'static, R: DeserializeOwned + Send + 'static,

source

pub async fn start<A>( addr: A ) -> Result<(ServerHandle<S>, EventStream<ServerEvent<R>>)>
where A: ToSocketAddrs,

Start a socket server.

addr: the address for the server to listen on.

Returns a result containing a handle to the server and a channel from which to receive server events, or the error variant if an error occurred while starting the server.

use rustdtp::*;

#[tokio::main]
async fn main() {
    let (mut server, mut server_events) = Server::builder()
        .sending::<()>()
        .receiving::<()>()
        .with_event_channel()
        .start(("0.0.0.0", 0))
        .await
        .unwrap();
}

Neither the server handle nor the event receiver should be dropped until the server has been stopped. Prematurely dropping either one can cause unintended behavior.

Auto Trait Implementations§

§

impl<S, R> RefUnwindSafe for Server<S, R>

§

impl<S, R> Send for Server<S, R>

§

impl<S, R> Sync for Server<S, R>
where R: Sync, S: Sync,

§

impl<S, R> Unpin for Server<S, R>
where R: Unpin, S: Unpin,

§

impl<S, R> UnwindSafe for Server<S, R>
where R: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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 T
where V: MultiLane<T>,

§

fn vzip(self) -> V