[−][src]Struct packets::server::Server
A simple server
Examples
Echo server
use packets::*; use packets::server::*; use std::net::SocketAddr; use std::time::Instant; // Bind the server to localhost at port 60000. let mut server = Server::bind("localhost:60000", &ServerConfig::default()).unwrap(); let start_time = Instant::now(); loop { server.accept_all(); // Accept all incoming connections. // Using String as our packet type for this example, receive all incoming packets along with // their addresses. Any Serde-compatible type can be used as the packet type. let packets: Vec<(SocketAddr, String)> = server.receive_all().unwrap(); for (addr, packet) in packets { server.send(addr, &packet); // Echo the data back to the client. } // For this example we shut the server down after 1 second. if start_time.elapsed().as_secs() > 1 { break; } } server.shutdown(); // Close all connections to the server.
Fields
listener: TcpListener
Implementations
impl Server
[src]
pub fn accept(&mut self) -> Result<Option<SocketAddr>>
[src]
Attempt to accept a new connection.
pub fn accept_all(&mut self) -> Result<Vec<SocketAddr>>
[src]
Use this function to accept all pending connections.
pub fn accept_blocking(&mut self) -> Result<SocketAddr>
[src]
Block until a new connection is accepted.
pub fn bind<B: ToSocketAddrs>(addr: B, config: &ServerConfig) -> Result<Server>
[src]
pub fn block_until_receive_from(
&mut self,
addr: SocketAddr,
timeout: Duration
) -> Result<PacketReceiveStatus>
[src]
&mut self,
addr: SocketAddr,
timeout: Duration
) -> Result<PacketReceiveStatus>
pub fn connections(&self) -> Vec<&Connection>
[src]
pub fn receive<A: Serialize + DeserializeOwned>(
&mut self
) -> Result<Option<(SocketAddr, A)>>
[src]
&mut self
) -> Result<Option<(SocketAddr, A)>>
Iterate over connections until we find one that is
pub fn receive_all<A: Serialize + DeserializeOwned>(
&mut self
) -> Result<Vec<(SocketAddr, A)>>
[src]
&mut self
) -> Result<Vec<(SocketAddr, A)>>
pub fn receive_from<A: Serialize + DeserializeOwned>(
&mut self,
addr: SocketAddr
) -> Result<Option<A>>
[src]
&mut self,
addr: SocketAddr
) -> Result<Option<A>>
pub fn send<A: Serialize + DeserializeOwned>(
&mut self,
addr: SocketAddr,
packet: &A
) -> Result<()>
[src]
&mut self,
addr: SocketAddr,
packet: &A
) -> Result<()>
Send a packet to a specific connection identified by its address.
pub fn send_global<A: Serialize + DeserializeOwned>(
&mut self,
packet: &A
) -> Result<()>
[src]
&mut self,
packet: &A
) -> Result<()>
Send a packet to all connections.
pub fn shutdown(self) -> Result<()>
[src]
Shuts this server down, closing all connections, then drops this object. The server cannot be used after it has been shut down as the Server object is moved into this function and then dropped.
pub fn with_connection<A, F>(&mut self, addr: SocketAddr, f: F) -> Result<A> where
F: FnOnce(&mut Connection) -> Result<A>,
[src]
F: FnOnce(&mut Connection) -> Result<A>,
Run a closure on a
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Server
impl Send for Server
impl Sync for Server
impl Unpin for Server
impl UnwindSafe for Server
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,