[−][src]Crate networking
Async client
use networking::{asyncronous::AsyncHost, ArtificeConfig, ArtificePeer}; use std::fs::File; use std::io::Read; #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let mut config_file = File::open("host.json").unwrap(); let mut conf_vec = String::new(); config_file.read_to_string(&mut conf_vec).unwrap(); let config: ArtificeConfig = serde_json::from_str(&conf_vec).unwrap(); let mut file = File::open("peer.json").unwrap(); let mut invec = Vec::new(); file.read_to_end(&mut invec).unwrap(); let string = String::from_utf8(invec).unwrap(); // println!("invec: {}", invec); let peer: ArtificePeer = serde_json::from_str(&string).unwrap(); let host = AsyncHost::client_only(&config).await.unwrap(); let mut stream = host.connect(peer).await.unwrap(); let mut buffer = Vec::new(); println!("about to read from sream"); println!( "got {} bytes from server", stream.recv(&mut buffer).await.unwrap() ); println!("read from stream"); let string = String::from_utf8(buffer).unwrap(); println!("got message: {} from server", string); Ok(()) }
Async Server
use networking::{asyncronous::AsyncHost, ArtificeConfig, ArtificePeer}; use std::fs::File; use std::io::Read; #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { // currently not functioning let mut config_file = File::open("host.json").unwrap(); let mut conf_vec = String::new(); config_file.read_to_string(&mut conf_vec).unwrap(); let config: ArtificeConfig = serde_json::from_str(&conf_vec).unwrap(); let mut host = AsyncHost::from_host_config(&config).await.unwrap(); let mut file = File::open("peer.json").unwrap(); let mut invec = String::new(); file.read_to_string(&mut invec).unwrap(); let peer: ArtificePeer = serde_json::from_str(&invec).unwrap(); while let Some(Ok(mut stream)) = host.incoming()?.await { // make sure you got a connection from the correct peer assert_eq!(peer, *stream.peer()); println!("sending message hello world"); stream.send(b"hello world").await.unwrap(); } Ok(()) }
Sync Client
use networking::{ArtificeConfig, ArtificeHost, ArtificePeer}; use std::fs::File; use std::io::Read; fn main() { let mut config_file = File::open("host.json").unwrap(); let mut conf_vec = String::new(); config_file.read_to_string(&mut conf_vec).unwrap(); let config: ArtificeConfig = serde_json::from_str(&conf_vec).unwrap(); let mut file = File::open("peer.json").unwrap(); let mut invec = Vec::new(); file.read_to_end(&mut invec).unwrap(); let string = String::from_utf8(invec).unwrap(); // println!("invec: {}", invec); let peer: ArtificePeer = serde_json::from_str(&string).unwrap(); let host = ArtificeHost::client_only(&config); let mut stream = host.connect(peer).unwrap(); let mut buffer = Vec::new(); println!("about to read from sream"); println!( "got {} bytes from server", stream.recv(&mut buffer).unwrap() ); println!("read from stream"); let string = String::from_utf8(buffer).unwrap(); println!("got message: {} from server", string); //stream.write(&buffer).unwrap(); }
Sync Server
use networking::{ArtificeConfig, ArtificeHost, ArtificePeer}; use std::fs::File; use std::io::{Read}; fn main() { let mut config_file = File::open("host.json").unwrap(); let mut conf_vec = String::new(); config_file.read_to_string(&mut conf_vec).unwrap(); let config: ArtificeConfig = serde_json::from_str(&conf_vec).unwrap(); let host = ArtificeHost::from_host_data(&config).unwrap(); let mut file = File::open("peer.json").unwrap(); let mut invec = String::new(); file.read_to_string(&mut invec).unwrap(); let peer: ArtificePeer = serde_json::from_str(&invec).unwrap(); for netstream in host { let mut stream = netstream.unwrap(); println!("about to write to stream"); stream .send(&"hello world".to_string().into_bytes()) .unwrap(); // do something with the stream example: if *stream.peer() == peer { // correct peer } } }
Re-exports
pub use encryption::*; |
pub use peers::*; |
Modules
asyncronous | |
encryption | contains blowfish encryption wrapper, as well as storage solution (serde) for BigUint principly BigNum |
error | |
peers | contains the ArtificePeer struct |
query | used for permission requests in the manager crate |
syncronous |
Structs
ArtificeConfig | used to build and configure the local host |
ArtificeHostData | provides a means of saving private keys to files, because the process of generating the keys takes a really long time, but creating them from existing values does not |
Header | contains peer information sent accross the network in an effort to prevent man in the middle attacks |
StreamHeader |
Traits
ArtificeHost | |
ArtificeStream | trait used to implement common features between async and sync implementations of networking |