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 --- Cargo.lock | 117 +++++++++++++++++++--------------------------- README.md | 1 + src/bin/server.rs | 17 +++++-- src/modules/navigation.rs | 5 ++ 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 +++++++++++++--------- 10 files changed, 186 insertions(+), 171 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6160261..43b3a98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [[package]] name = "bitflags" -version = "1.0.1" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -13,7 +13,7 @@ name = "fuchsia-zircon" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -24,31 +24,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "itoa" -version = "0.3.4" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libc" -version = "0.2.36" +version = "0.2.42" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "num-traits" -version = "0.1.43" +name = "proc-macro2" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "num-traits 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "num-traits" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "quote" -version = "0.3.15" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "rand" @@ -56,13 +54,13 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "redox_syscall" -version = "0.1.37" +version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -70,42 +68,32 @@ name = "redox_termios" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde" -version = "1.0.27" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde_derive" -version = "1.0.27" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive_internals 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "serde_derive_internals" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", - "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde_json" -version = "1.0.9" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -113,29 +101,21 @@ name = "space" version = "0.1.0" dependencies = [ "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "syn" -version = "0.11.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", - "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "synom" -version = "0.11.3" +version = "0.14.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -143,8 +123,8 @@ name = "termion" version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", - "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -153,17 +133,17 @@ name = "toml" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "unicode-xid" -version = "0.0.4" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "winapi" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -181,27 +161,24 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] -"checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf" +"checksum bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d0c54bb8f454c567f21197eefcdbf5679d0bd99f2ddbe52e84c77061952e6789" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" -"checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" -"checksum libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "1e5d97d6708edaa407429faa671b942dc0f2727222fb6b6539bf1db936e4b121" -"checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" -"checksum num-traits 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e7de20f146db9d920c45ee8ed8f71681fd9ade71909b48c3acbd766aa504cf10" -"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" +"checksum itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c069bbec61e1ca5a596166e55dfe4773ff745c3d16b700013bcaff9a6df2c682" +"checksum libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)" = "b685088df2b950fccadf07a7187c8ef846a959c142338a48f9dc0b94517eb5f1" +"checksum proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "effdb53b25cdad54f8f48843d67398f7ef2e14f12c1b4cb4effc549a6462a4d6" +"checksum quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e44651a0dc4cdd99f71c83b561e221f714912d11af1a4dff0631f923d53af035" "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" -"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" +"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" -"checksum serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "db99f3919e20faa51bb2996057f5031d8685019b5a06139b1ce761da671b8526" -"checksum serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "f4ba7591cfe93755e89eeecdbcc668885624829b020050e6aec99c2a03bd3fd0" -"checksum serde_derive_internals 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6e03f1c9530c3fb0a0a5c9b826bdd9246a5921ae995d75f512ac917fc4dd55b5" -"checksum serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c9db7266c7d63a4c4b7fe8719656ccdd51acf1bed6124b174f933b009fb10bcb" -"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" -"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" +"checksum serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "e9a2d9a9ac5120e0f768801ca2b58ad6eec929dc9d1d616c162f208869c2ce95" +"checksum serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "0a90213fa7e0f5eac3f7afe2d5ff6b088af515052cc7303bd68c7e3b91a3fb79" +"checksum serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "fc97cccc2959f39984524026d760c08ef0dd5f0f5948c8d31797dbfae458c875" +"checksum syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c67da57e61ebc7b7b6fff56bb34440ca3a83db037320b0507af4c10368deda7d" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a0263c6c02c4db6c8f7681f9fd35e90de799ebd4cfdeab77a38f4ff6b3d8c0d9" -"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" -"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3" +"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/README.md b/README.md index 05ec88c..700827e 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,4 @@ ideas - some sort of energy / fuel mechanic - server ability to quit and import/export all data - collisions +- figure how position floats overflow and how to improve it diff --git a/src/bin/server.rs b/src/bin/server.rs index 1fad1d3..e20acf6 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -25,22 +25,31 @@ fn main() { let mut masses = populate(); - let mut connections = Vec::new(); + let mut connections : Vec = Vec::new(); for stream in listener.incoming() { match stream { - Ok(stream) => connections.push(ServerConnection::new(stream, &mut masses)), + Ok(stream) => { + let new_connection = ServerConnection::new(stream, &mut masses); + let exists = connections.iter().position(|connection| + connection.name == new_connection.name && + connection.module_type == new_connection.module_type); + match exists { + Some(index) => { connections.remove(index); }, + _ => (), + } + connections.push(new_connection); + }, _ => { for i in 0..connections.len() { connections[i].process(&mut masses); } - connections.retain(|connection| connection.open); for mass in masses.values_mut() { mass.process(); } sleep(Duration::from_millis(100)); - } + }, } } } diff --git a/src/modules/navigation.rs b/src/modules/navigation.rs index 179a766..c4b3224 100644 --- a/src/modules/navigation.rs +++ b/src/modules/navigation.rs @@ -41,6 +41,11 @@ impl Navigation { } _ => (), } + + match self.target_name { + None => self.status = NavigationStatus::None, + _ => (), + } } pub fn give_target(&mut self, target_name : String) { 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