[][src]Crate knet

A Server and Client with parrallel read/write with async_std library.

Internal data will be automatically serialized and de-serialized by the trait KnetTransform.

It is advised to use the internal derive macro DeriveKnet on enum.

Here is a basic example:

-> Server/Client

-> DeriveKnet

Server/Client

The Server and the Client has the run that allows you to start asynchronously. It return (Self, Receiver)

You can read the Event with the receiver in a loop.

Example

loop {
    match receiver.try_next() {
       Ok(Some(event)) => {
            println!("Receive event<T> {:?} ", event);
         }
        Ok(None) => {
            eprintln!("Connection is down");
            break;
        }
        Err(e) => {
            error!("Nothing receive from receiver", e);
        }
    }
}

There is small difference between them :

  • Server::write_all and Client::write have similar prototype but just difference name. It allow you to send the T over the network.
  • Server::write is the same as above but have a Id paramater to precise on which connection you want to send the data.
  • The Receiver from the Server have a wrapper Event.

DeriveKnet

The derive macro allow you to automatically implement the trait KnetTransform.

Example

#[derive(DeriveKnet, Debug, PartialEq, Clone, Copy)]
enum Data {
    Byte(u8),
    Integer(i32),
    Char(char),
    Float(f64),
}

Structs

Client
Server

Enums

Event

The wrapper event for the server. It allow you to know when there is a new connection, a data or and drop receive by the server

Traits

KnetTransform

Type Definitions

Id

Derive Macros

DeriveKnet