mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
feat: add delegate() to connection
This commit is contained in:
@@ -4,6 +4,7 @@ use std::net::SocketAddr;
|
|||||||
|
|
||||||
use log::{debug, error, warn};
|
use log::{debug, error, warn};
|
||||||
use postcard::from_bytes;
|
use postcard::from_bytes;
|
||||||
|
use tokio::net::TcpSocket;
|
||||||
use tokio::{net::TcpStream, sync::mpsc::Sender};
|
use tokio::{net::TcpStream, sync::mpsc::Sender};
|
||||||
|
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
@@ -45,6 +46,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Meant for clients
|
||||||
pub async fn connect_to_server(&mut self, url: SocketAddr) -> bool {
|
pub async fn connect_to_server(&mut self, url: SocketAddr) -> bool {
|
||||||
match self.conn_type {
|
match self.conn_type {
|
||||||
ConnectionType::Server => {
|
ConnectionType::Server => {
|
||||||
@@ -65,6 +67,18 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Meant for server
|
||||||
|
pub fn delegate(&mut self, tcp: TcpStream, client_addr: SocketAddr) -> bool {
|
||||||
|
match self.conn_type {
|
||||||
|
ConnectionType::Server => false, // only server can delegate
|
||||||
|
ConnectionType::Client => {
|
||||||
|
self.peer = Some(client_addr);
|
||||||
|
self.tcp = Some(tcp);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn send(&mut self, msg: Message<Outbound>) {
|
pub async fn send(&mut self, msg: Message<Outbound>) {
|
||||||
let size = msg.size;
|
let size = msg.size;
|
||||||
let content = msg.content;
|
let content = msg.content;
|
||||||
|
|||||||
Reference in New Issue
Block a user