pub struct Server<H>{ /* private fields */ }
Expand description
The server manages client connections, verifies message formats, and handles sending and receiving messages.
§Type Parameters
H
: Type that implements theRequestHandler
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>
impl<H> Server<H>
Sourcepub fn get_addr(&self) -> SocketAddr
pub fn get_addr(&self) -> SocketAddr
Gets the socket address of the server.
§Returns
The socket address on which the server is listening.
Sourcepub fn add_conn(&mut self, conn_stream: TcpStream)
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.
Sourcepub fn send_all(&self, message: String)
pub fn send_all(&self, message: String)
Sends a message to all connected clients.
§Arguments
message
- The message to be sent to all clients.
Sourcepub async fn listen(&mut self)
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}
Sourcepub fn new(address: SocketAddr, message_handler: H) -> Self
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more