Struct Server

Source
pub struct Server<H>
where H: RequestHandler + Sync + Send + 'static,
{ /* private fields */ }
Expand description

The server manages client connections, verifies message formats, and handles sending and receiving messages.

§Type Parameters

  • H: Type that implements the RequestHandler trait for handling incoming requests.

§Fields

  • address: The socket address on which the server is listening.
  • handles: Vector of join handles for spawned connection tasks.
  • send_all_tx: Sender channel for broadcasting messages to all clients.
  • cmd_rx: Receiver channel for receiving commands from clients.
  • cmd_tx: Sender channel for sending commands to clients.
  • message_handler: Shared handler function to handle all incoming messages.

Implementations§

Source§

impl<H> Server<H>
where H: RequestHandler + Sync + Send,

Source

pub fn get_addr(&self) -> SocketAddr

Gets the socket address of the server.

§Returns

The socket address on which the server is listening.

Source

pub fn add_conn(&mut self, conn_stream: TcpStream)

Adds a new connection to the server.

§Arguments
  • conn_stream - The TCP stream representing the connection to the client.
Source

pub fn send_all(&self, message: String)

Sends a message to all connected clients.

§Arguments
  • message - The message to be sent to all clients.
Source

pub async fn listen(&mut self)

Starts listening for incoming connections and commands.

Examples found in repository?
examples/chat_room_server.rs (line 52)
41async fn main() {
42    // Getting a socket addr obj
43    let addr: SocketAddr = "127.0.0.1:5555".parse().expect("Could not parse ip addr");
44
45    // Creating instance of a handler
46    let handler = ChatRoomHandler::new();
47
48    // Creating a server obj
49    let mut server = Server::new(addr, handler);
50
51    // Start server listener
52    server.listen().await;
53}
Source

pub fn new(address: SocketAddr, message_handler: H) -> Self

Creates a new instance of Server.

§Arguments
  • address - The socket address on which the server will listen for incoming connections.
  • message_handler - The handler for processing incoming requests.
§Returns

A new instance of Server.

Examples found in repository?
examples/chat_room_server.rs (line 49)
41async fn main() {
42    // Getting a socket addr obj
43    let addr: SocketAddr = "127.0.0.1:5555".parse().expect("Could not parse ip addr");
44
45    // Creating instance of a handler
46    let handler = ChatRoomHandler::new();
47
48    // Creating a server obj
49    let mut server = Server::new(addr, handler);
50
51    // Start server listener
52    server.listen().await;
53}

Auto Trait Implementations§

§

impl<H> Freeze for Server<H>

§

impl<H> RefUnwindSafe for Server<H>

§

impl<H> Send for Server<H>

§

impl<H> Sync for Server<H>

§

impl<H> Unpin for Server<H>

§

impl<H> UnwindSafe for Server<H>

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.