implement ClientHello

This commit is contained in:
Horhik 2021-01-05 19:54:22 +02:00
parent 18b666b604
commit bbd79d2b00

View file

@ -1,9 +1,9 @@
use async_trait::async_trait; use async_trait::async_trait;
use std::error::Error; use std::error::Error;
use std::io; use std::net::{IpAddr, SocketAddr};
use std::io::prelude::*; use tokio::io::{self, AsyncBufRead, AsyncReadExt, AsyncWriteExt};
use std::net::TcpStream; use tokio::net::TcpListener;
use std::net::{IpAddr, SocketAddr, TcpListener}; use tokio::net::TcpStream;
struct Fcp { struct Fcp {
connected: bool, connected: bool,
stream: TcpStream, stream: TcpStream,
@ -14,15 +14,17 @@ struct Fcp {
struct NodeHello { struct NodeHello {
responce: String, responce: String,
} }
#[async_trait]
trait FcpConnection { trait FcpConnection {
fn new(addr: &'static str, port: u16, name: String) -> Result<Box<Self>, Box<dyn Error>>; async fn new(addr: &'static str, port: u16, name: String) -> Result<Box<Self>, Box<dyn Error>>;
} }
#[async_trait]
impl FcpConnection for Fcp { impl FcpConnection for Fcp {
fn new(addr: &'static str, port: u16, name: String) -> Result<Box<Self>, Box<dyn Error>> { async fn new(addr: &'static str, port: u16, name: String) -> Result<Box<Self>, Box<dyn Error>> {
let socket = SocketAddr::new(IpAddr::V4(addr.parse().unwrap()), port); let socket = SocketAddr::new(IpAddr::V4(addr.parse().unwrap()), port);
match TcpStream::connect(&socket) { match TcpStream::connect(&socket).await {
Ok(mut stream) => Ok(Box::new(Fcp { Ok(mut stream) => Ok(Box::new(Fcp {
connected: true, connected: true,
stream: stream, stream: stream,
@ -33,31 +35,27 @@ impl FcpConnection for Fcp {
} }
} }
} }
/*
use futures::try_join;
use tokio::io::{self, AsyncBufRead, AsyncReadExt};
use tokio::net::TcpStream;
#[async_trait] #[async_trait]
trait FCP { trait FCP {
async fn connect(&self) -> io::Result<()>; async fn client_hello(&mut self) -> io::Result<NodeHello>;
} }
#[async_trait] #[async_trait]
impl FCP for Fcp { impl FCP for Fcp {
async fn connect(&self) -> io::Result<()> { async fn client_hello(&mut self) -> io::Result<NodeHello> {
let stream = self.stream; let _ = self
let _ = .stream
stream.write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes()); .write_all(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes());
let mut buffer = String::new(); let mut buffer = String::new();
let smth = stream.read_to_string(&mut buffer).await?; match self.stream.read_to_string(&mut buffer).await {
println!("{:?}", smth); Ok(text) => Ok(NodeHello {
responce: text.to_string(),
Ok(()) }),
// } Err(e) => Err(e),
}
} }
} }
*/
/* /*
// TODO add error if freenet not connected // TODO add error if freenet not connected