aboutsummaryrefslogtreecommitdiff
path: root/src/net/server/connection.rs
diff options
context:
space:
mode:
authorArne Dußin2021-01-27 14:01:50 +0100
committerArne Dußin2021-02-02 22:16:15 +0100
commitf92e9f6f07b1e3834c2ca58ce3510734819d08e4 (patch)
tree20e3d3afce342a56ae98f6c20491482ccd2b5c6b /src/net/server/connection.rs
parentc60a6d07efb120724b308e29e8e70f27c87c952d (diff)
downloadgraf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.tar.gz
graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.zip
Rework graf karto to fit the client/server structure
Diffstat (limited to 'src/net/server/connection.rs')
-rw-r--r--src/net/server/connection.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/net/server/connection.rs b/src/net/server/connection.rs
new file mode 100644
index 0000000..801eb4b
--- /dev/null
+++ b/src/net/server/connection.rs
@@ -0,0 +1,84 @@
+//! A TCP-connection from a client to the server.
+
+use super::super::packet::{Packet, PacketRwError};
+use super::connection_manager::ConnId;
+use serde::de::DeserializeOwned;
+use serde::Serialize;
+use std::fmt::Debug;
+use std::net::TcpStream;
+use std::sync::mpsc::Sender;
+use std::thread::{self, JoinHandle};
+
+/// Holds a stream to the client and manages receiving packets from said client
+/// in an extra thread.
+pub struct Connection {
+ stream: TcpStream,
+ rcv_thread_handle: JoinHandle<()>,
+}
+
+impl Connection {
+ /// Start up the receiving thread after a client connection has been detected.
+ /// Will create a ghost disconnect packet in case the client disconnects.
+ pub(super) fn start_rcv<P>(
+ conn_id: ConnId,
+ stream: TcpStream,
+ packet_tx: Sender<(ConnId, Packet<P>)>,
+ ) -> Self
+ where
+ P: 'static + Debug + Send + DeserializeOwned + Serialize,
+ {
+ let mut stream_cl = stream
+ .try_clone()
+ .expect("Unable to clone TcpStream handle");
+ let rcv_thread_handle = thread::spawn(move || {
+ let mut running = true;
+ while running {
+ // Read the newest packet from the stream.
+ let packet = match Packet::read_from_stream(&mut stream_cl) {
+ Ok(packet) => dbg!(packet),
+ Err(PacketRwError::Closed) => {
+ // Stop the thread after this packet.
+ running = false;
+
+ /* Generate an internal disconnection packet, so the connection
+ * manager can call cleanup code if necessary.
+ */
+ Packet::Disconnect
+ }
+ Err(err) => {
+ error!(
+ "Receiving packet failed. Connection `{}`. {:?}",
+ conn_id, err
+ );
+
+ // Ignore the received data.
+ continue;
+ }
+ };
+
+ /* Try sending the packet to the Connection manager. If it has already
+ * stopped and hung up on the channel, stop this receive thread as well.
+ */
+ if packet_tx.send((conn_id, packet)).is_err() {
+ info!("Shutting down connection `{}`", conn_id);
+ running = false;
+ }
+ }
+
+ info!("Packet receive thread has stopped running.");
+ });
+
+ Self {
+ stream,
+ rcv_thread_handle,
+ }
+ }
+
+ /// Send a packet to the client via TCP.
+ pub(super) fn send<P>(&mut self, packet: &Packet<P>) -> Result<(), PacketRwError>
+ where
+ P: 'static + Debug + Send + DeserializeOwned + Serialize,
+ {
+ packet.write_to_stream(&mut self.stream)
+ }
+}