From 7094849bf164ff6f853c8f6812a0e831a66762f3 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 14 Jun 2018 10:18:22 -0500 Subject: updated dependancies, made it so server still processes events if client connection is disconnected --- src/server/connection.rs | 4 ++-- src/server/dashboard.rs | 15 ++++++++----- src/server/engines.rs | 58 ++++++++++++++++++++++++------------------------ src/server/mining.rs | 53 +++++++++++++++++++++++++------------------ src/server/navigation.rs | 32 +++++++++++++------------- src/server/refinery.rs | 55 ++++++++++++++++++++++++++------------------- 6 files changed, 120 insertions(+), 97 deletions(-) (limited to 'src/server') diff --git a/src/server/connection.rs b/src/server/connection.rs index d8e0eef..80ecde8 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -44,12 +44,12 @@ impl ServerConnection { } pub fn process(&mut self, mut masses : &mut HashMap) { - self.open = match self.module_type { + match self.module_type { ModuleType::Mining => self.server_mining(&mut masses), ModuleType::Engines => self.server_engines(&mut masses), ModuleType::Refinery => self.server_refinery(&mut masses), ModuleType::Dashboard => self.server_dashboard(&mut masses), ModuleType::Navigation => self.server_navigation(&mut masses), - }; + } } } diff --git a/src/server/dashboard.rs b/src/server/dashboard.rs index 91d8831..d1aa8a4 100644 --- a/src/server/dashboard.rs +++ b/src/server/dashboard.rs @@ -7,12 +7,15 @@ use mass::Mass; use server::connection::ServerConnection; impl ServerConnection { - pub fn server_dashboard(&mut self, masses : &mut HashMap) -> bool { - let ship = masses.get(&self.name).unwrap(); - let send = serde_json::to_string(&ship).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Ok(_result) => true, - Err(_error) => false, + pub fn server_dashboard(&mut self, masses : &mut HashMap) { + if self.open { + let ship = masses.get(&self.name).unwrap(); + let send = serde_json::to_string(&ship).unwrap() + "\n"; + + self.open = match self.stream.write(send.as_bytes()) { + Ok(_result) => true, + Err(_error) => false, + }; } } } diff --git a/src/server/engines.rs b/src/server/engines.rs index 710ed3a..2c96920 100644 --- a/src/server/engines.rs +++ b/src/server/engines.rs @@ -9,39 +9,39 @@ use modules::navigation::NavigationStatus; use server::connection::ServerConnection; impl ServerConnection { - pub fn server_engines(&mut self, masses : &mut HashMap) -> bool { - let mut ship = masses.remove(&self.name).unwrap(); - let ship_clone = ship.clone(); - let mut connection_good = true; + pub fn server_engines(&mut self, masses : &mut HashMap) { + if self.open { + let mut ship = masses.remove(&self.name).unwrap(); + let ship_clone = ship.clone(); - if let MassType::Ship{ref mut engines, ref navigation, ..} = ship.mass_type { - let navigation = navigation.clone().unwrap(); - let engines = engines.as_mut().unwrap(); - let targeted = navigation.status == NavigationStatus::Targeted; + if let MassType::Ship{ref mut engines, ref navigation, ..} = ship.mass_type { + let navigation = navigation.clone().unwrap(); + let engines = engines.as_mut().unwrap(); + let targeted = navigation.status == NavigationStatus::Targeted; - let send = serde_json::to_string(&targeted).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => connection_good = false, - } + let send = serde_json::to_string(&targeted).unwrap() + "\n"; + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => self.open = false, + } - let target = match navigation.target_name { - Some(name) => masses.get(&name), - None => None, - }; - let mut recv = String::new(); - match self.buff_r.read_line(&mut recv) { - Ok(result) => { - engines.give_client_data(&ship_clone, target, recv); - if result == 0 { - connection_good = false; - } - }, - Err(_error) => (), + let target = match navigation.target_name { + Some(name) => masses.get(&name), + None => None, + }; + let mut recv = String::new(); + match self.buff_r.read_line(&mut recv) { + Ok(result) => { + engines.give_client_data(&ship_clone, target, recv); + if result == 0 { + self.open = false; + } + }, + Err(_error) => (), + } } - } - masses.insert(self.name.clone(), ship); - connection_good + masses.insert(self.name.clone(), ship); + } } } diff --git a/src/server/mining.rs b/src/server/mining.rs index 91210f8..a54ce33 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -19,10 +19,9 @@ pub struct MiningData { } impl ServerConnection { - pub fn server_mining(&mut self, masses : &mut HashMap) -> bool { + pub fn server_mining(&mut self, masses : &mut HashMap) { let mut ship = masses.remove(&self.name).unwrap(); let ship_clone = ship.clone(); - let mut connection_good = true; let mut item = None; if let MassType::Ship{ref mut mining, ref navigation, ..} = ship.mass_type { @@ -30,27 +29,13 @@ impl ServerConnection { let mut navigation = navigation.as_ref().unwrap(); let mining_data = get_mining_data(ship_clone, mining, navigation, masses); - let send = serde_json::to_string(&mining_data).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => connection_good = false, - } - let mut recv = String::new(); - match self.buff_r.read_line(&mut recv) { - Ok(result) => match recv.as_bytes() { - b"F\n" => { - if mining_data.is_within_range { - mining.toggle(); - } - }, - _ => { - if result == 0 { - connection_good = false; - } - }, + if self.open { + if self.txrx_mining(&mining_data) { + if mining_data.is_within_range { + mining.toggle(); + } } - Err(_error) => (), } if !mining_data.is_within_range { @@ -76,7 +61,31 @@ impl ServerConnection { } masses.insert(self.name.clone(), ship); - connection_good + } + + fn txrx_mining(&mut self, mining_data : &MiningData) -> bool { + let send = serde_json::to_string(mining_data).unwrap() + "\n"; + match self.stream.write(send.as_bytes()) { + Err(_error) => self.open = false, + _ => (), + } + + let mut recv = String::new(); + match self.buff_r.read_line(&mut recv) { + Ok(result) => match recv.as_bytes() { + b"F\n" => { + return true; + }, + _ => { + if result == 0 { + self.open = false; + } + }, + } + _ => (), + } + + false } } diff --git a/src/server/navigation.rs b/src/server/navigation.rs index 2a2f105..461a2b8 100644 --- a/src/server/navigation.rs +++ b/src/server/navigation.rs @@ -9,10 +9,9 @@ use mass::{Mass, MassType}; use server::connection::ServerConnection; impl ServerConnection { - pub fn server_navigation(&mut self, masses : &mut HashMap) -> bool { + pub fn server_navigation(&mut self, masses : &mut HashMap) { let mut ship = masses.remove(&self.name).unwrap(); let ship_clone = ship.clone(); - let mut connection_good = true; if let MassType::Ship{ref mut navigation, ..} = ship.mass_type { let mut navigation = navigation.as_mut().unwrap(); @@ -21,23 +20,26 @@ impl ServerConnection { distance(ship_clone.position, mass.position) < navigation.range) .collect(); within_range.insert(&self.name, &ship_clone); - let send = serde_json::to_string(&within_range).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => connection_good = false, - } - let mut recv = String::new(); - match self.buff_r.read_line(&mut recv) { - Ok(_result) => (), - Err(_error) => (), - } - if !recv.is_empty() { - navigation.give_target(recv.replace("\n", "")); + if self.open { + let send = serde_json::to_string(&within_range).unwrap() + "\n"; + + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => self.open = false, + }; + + let mut recv = String::new(); + match self.buff_r.read_line(&mut recv) { + Ok(_result) => (), + Err(_error) => (), + } + if !recv.is_empty() { + navigation.give_target(recv.replace("\n", "")); + } } } masses.insert(self.name.clone(), ship); - connection_good } } diff --git a/src/server/refinery.rs b/src/server/refinery.rs index bb0627f..b1af971 100644 --- a/src/server/refinery.rs +++ b/src/server/refinery.rs @@ -15,11 +15,10 @@ pub struct RefineryData { } impl ServerConnection { - pub fn server_refinery(&mut self, masses : &mut HashMap) -> bool { + pub fn server_refinery(&mut self, masses : &mut HashMap) { let mut ship = masses.remove(&self.name).unwrap(); let ship_clone = ship.clone(); let mut refine = false; - let mut connection_good = true; if let MassType::Ship{ref mut refinery, ..} = ship.mass_type { let mut refinery = refinery.as_mut().unwrap(); @@ -29,27 +28,10 @@ impl ServerConnection { status : refinery.status, }; - let send = serde_json::to_string(&refinery_data).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => connection_good = false, - } - - let mut recv = String::new(); - match self.buff_r.read_line(&mut recv) { - Ok(result) => match recv.as_bytes() { - b"R\n" => { - if refinery_data.has_minerals { - refinery.toggle(); - } - }, - _ => { - if result == 0 { - connection_good = false; - } - }, + if self.open { + if self.txrx_refinery(&refinery_data) { + refinery.toggle(); } - Err(_error) => (), } if !refinery_data.has_minerals { @@ -68,6 +50,33 @@ impl ServerConnection { } masses.insert(self.name.clone(), ship); - connection_good + } + + fn txrx_refinery(&mut self, refinery_data : &RefineryData) -> bool { + let send = serde_json::to_string(refinery_data).unwrap() + "\n"; + match self.stream.write(send.as_bytes()) { + Err(_error) => self.open = false, + _ => (), + } + + let mut recv = String::new(); + match self.buff_r.read_line(&mut recv) { + Ok(result) => match recv.as_bytes() { + b"R\n" => { + if refinery_data.has_minerals { + return true + } + }, + _ => { + if result == 0 { + self.open = false; + } + }, + } + _ => (), + } + + false } } + -- cgit v1.2.3