pub struct ZeroConnection {
    pub connection: Connection<ZeroMessage>,
    pub next_req_id: Arc<Mutex<usize>>,
    pub target_address: Option<PeerAddr>,
}

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)
		}
	}
}
next_req_id: Arc<Mutex<usize>>target_address: Option<PeerAddr>

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.