Struct zeronet_protocol::zero_connection::ZeroConnection
source · [−]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
sourceimpl ZeroConnection
impl ZeroConnection
sourcepub fn new(
reader: Box<dyn Read + Send>,
writer: Box<dyn Write + Send>
) -> Result<ZeroConnection, Error>
pub fn new(
reader: Box<dyn Read + Send>,
writer: Box<dyn Write + Send>
) -> Result<ZeroConnection, Error>
Creates a new ZeroConnection from a given reader and writer
sourcepub fn from_address(address: PeerAddr) -> Result<ZeroConnection, Error>
pub fn from_address(address: PeerAddr) -> Result<ZeroConnection, Error>
Creates a new ZeroConnection from a given address
sourcepub fn connect(
address: String
) -> impl Future<Output = Result<ZeroConnection, Error>>
pub fn connect(
address: String
) -> impl Future<Output = Result<ZeroConnection, Error>>
Connect to an ip and port and perform the handshake, then return the ZeroConnection.
sourcepub fn recv(&mut self) -> impl Future<Output = Result<Request, Error>>
pub fn recv(&mut self) -> impl Future<Output = Result<Request, Error>>
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.
sourcepub fn respond<T: DeserializeOwned + Serialize>(
&mut self,
to: usize,
body: T
) -> impl Future<Output = Result<(), Error>>
pub fn respond<T: DeserializeOwned + Serialize>(
&mut self,
to: usize,
body: T
) -> impl Future<Output = Result<(), Error>>
Respond to a request.
The body
variable is flattened into the ZeroMessage,
therefore it should be an object, a map or a pair.
sourcepub fn request<T: DeserializeOwned + Serialize>(
&mut self,
cmd: &str,
body: T
) -> impl Future<Output = Result<Response, Error>>
pub fn request<T: DeserializeOwned + Serialize>(
&mut self,
cmd: &str,
body: T
) -> impl Future<Output = Result<Response, Error>>
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.
sourcepub fn last_req_id(&self) -> usize
pub fn last_req_id(&self) -> usize
Get the req_id of the last request
Trait Implementations
sourceimpl Clone for ZeroConnection
impl Clone for ZeroConnection
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more