implementing more connection to network functionality

This commit is contained in:
michael-bailey 2021-03-21 12:25:36 +00:00
parent 41c545de50
commit bd24c1e615
2 changed files with 8 additions and 4 deletions

View File

@ -10,5 +10,6 @@ pub enum NetworkSockIn {
#[derive(Serialize, Deserialize)]
pub enum NetworkSockOut<'a> {
Request,
GotInfo {server_name: &'a str, server_owner: &'a str}
GotInfo {server_name: &'a str, server_owner: &'a str},
Connecting,
}

View File

@ -49,7 +49,7 @@ impl ICooperative for NetworkManager {
let mut buffer = String::new();
// request is always sent on new connection
// send request message to connection
writer.write_all(
serde_json::to_string(&NetworkSockOut::Request).unwrap().as_bytes()
).unwrap_or_default();
@ -58,13 +58,15 @@ impl ICooperative for NetworkManager {
// read the new request into a buffer
let res = reader.read_line(&mut buffer);
// if reading caused an error skip the connection
if res.is_err() {continue;}
// turn into enum for pattern matching
// turn into enum and perform pattern matching
if let Ok(request) = serde_json::from_str::<NetworkSockIn>(&buffer) {
// perform action based on the enum
match request {
NetworkSockIn::Info => {
// send back server info to the connection
writer.write_all(
serde_json::to_string(
&NetworkSockOut::GotInfo {
@ -76,6 +78,7 @@ impl ICooperative for NetworkManager {
writer.flush().unwrap();
}
NetworkSockIn::Connect { uuid, username, address } => {
// create client and send to server
let new_client = Client::new(
uuid,
username,