implementing more connection to network functionality
This commit is contained in:
parent
41c545de50
commit
8c72b6e6ee
|
|
@ -4,12 +4,24 @@ use serde::{Serialize, Deserialize};
|
|||
/// # ClientMessage
|
||||
/// This enum defined the message that a client can receive from the server
|
||||
/// This uses the serde library to transform to and from json.
|
||||
///
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum ClientStreamIn {
|
||||
Disconnect {id: String},
|
||||
Connected,
|
||||
|
||||
Update,
|
||||
SendMessage {to_uuid: String, contents: String},
|
||||
SendGlobalMessage {contents: String},
|
||||
|
||||
Disconnect,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum ClientStreamOut {
|
||||
Message {from_uuid: String},
|
||||
Disconnect,
|
||||
Connected,
|
||||
|
||||
UserMessage {from_uuid: String, contents: String},
|
||||
GlobalMessage {contents: String},
|
||||
|
||||
Disconnected,
|
||||
}
|
||||
|
|
@ -10,5 +10,7 @@ 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,
|
||||
}
|
||||
|
|
@ -118,7 +118,7 @@ impl ICooperative for Client {
|
|||
let command = serde_json::from_str::<ClientStreamIn>(buffer.as_str()).unwrap();
|
||||
|
||||
match command {
|
||||
ClientStreamIn::Disconnect {id} => println!("got Disconnect from id: {:?}", id),
|
||||
ClientStreamIn::Disconnect => println!("got Disconnect"),
|
||||
_ => println!("New command found"),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,12 @@ pub enum ClientMessage {
|
|||
Disconnect
|
||||
}
|
||||
|
||||
pub enum ClientMgrMessage {
|
||||
Remove(Uuid),
|
||||
Add(Arc<Client>),
|
||||
SendMessage(Uuid, String),
|
||||
}
|
||||
|
||||
pub enum ServerMessage {
|
||||
ClientConnected(Arc<Client>),
|
||||
ClientDisconnected(Uuid)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ use crate::messages::ServerMessage;
|
|||
use std::io::BufWriter;
|
||||
use std::io::BufReader;
|
||||
use std::sync::Arc;
|
||||
use crate::server::ServerMessages;
|
||||
use std::net::TcpListener;
|
||||
use std::io::Write;
|
||||
use std::io::BufRead;
|
||||
|
|
@ -49,7 +48,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 +57,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 +77,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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue