[][src]Struct packets::server::Server

pub struct Server {
    pub listener: TcpListener,
    // some fields omitted
}

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]

pub fn connections(&self) -> Vec<&Connection>[src]

pub fn receive<A: Serialize + DeserializeOwned>(
    &mut self
) -> Result<Option<(SocketAddr, A)>>
[src]

Iterate over connections until we find one that is

pub fn receive_all<A: Serialize + DeserializeOwned>(
    &mut self
) -> Result<Vec<(SocketAddr, A)>>
[src]

pub fn receive_from<A: Serialize + DeserializeOwned>(
    &mut self,
    addr: SocketAddr
) -> Result<Option<A>>
[src]

pub fn send<A: Serialize + DeserializeOwned>(
    &mut self,
    addr: SocketAddr,
    packet: &A
) -> Result<()>
[src]

Send a packet to a specific connection identified by its address.

pub fn send_global<A: Serialize + DeserializeOwned>(
    &mut self,
    packet: &A
) -> Result<()>
[src]

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]

Run a closure on a

Trait Implementations

impl Debug for Server[src]

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.