Struct zeronet_protocol::zero_connection::ZeroConnection [−][src]
pub struct ZeroConnection { pub connection: Connection<ZeroMessage>, pub target_address: Option<PeerAddr>, pub next_req_id: Arc<Mutex<usize>>, }
Fields
connection: Connection<ZeroMessage>
A ZeroNet Protocol connection
The ZeroNet Protocol is specified at https://zeronet.io/docs/help_zeronet/network_protocol/
Examples
use std::net::{TcpStream, TcpListener}; use futures::executor::block_on; use zeronet_protocol::{ZeroConnection, ZeroMessage, PeerAddr}; fn handle_connection(stream: TcpStream) { let mut connection = ZeroConnection::new(Box::new(stream.try_clone().unwrap()), Box::new(stream)).unwrap(); let request = block_on(connection.recv()).unwrap(); let body = "anything serializable".to_string(); block_on(connection.respond(request.req_id, body)); } fn main() { let listener = TcpListener::bind("127.0.0.1:15442").unwrap(); for stream in listener.incoming() { if let Ok(stream) = stream { handle_connection(stream) } } }
target_address: Option<PeerAddr>
next_req_id: Arc<Mutex<usize>>
Implementations
Creates a new ZeroConnection from a given reader and writer
Creates a new ZeroConnection from a given address
Connect to an ip and port and perform the handshake, then return the ZeroConnection.
Returns a future that will read from the internal reader and attempt to decode valid ZeroMessages. The future returns the first Request that gets decoded.
Respond to a request.
The body
variable is flattened into the ZeroMessage,
therefore it should be an object, a map or a pair.
Returns a future that will send a request with
a new req_id
and then read from internal reader
and attempt to decode valid ZeroMessages.
The future returns the first Response that
has the corresponding to
field.
Get the req_id of the last request
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for ZeroConnection
impl Send for ZeroConnection
impl Sync for ZeroConnection
impl Unpin for ZeroConnection
impl UnwindSafe for ZeroConnection
Blanket Implementations
Mutably borrows from an owned value. Read more