implementing more connection to network functionality
This commit is contained in:
parent
41c545de50
commit
bd24c1e615
|
|
@ -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,
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue