From 487486695eb470aa0e506fa493662952f2566228 Mon Sep 17 00:00:00 2001 From: Lukas Holst Date: Mon, 12 Aug 2024 06:29:00 -0400 Subject: [PATCH] TCPConnection, moved exception handling --- .../src/main/scala/dtn/TCPConnection.scala | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Modules/DTN/jvm/src/main/scala/dtn/TCPConnection.scala b/Modules/DTN/jvm/src/main/scala/dtn/TCPConnection.scala index 01f610079..df434860c 100644 --- a/Modules/DTN/jvm/src/main/scala/dtn/TCPConnection.scala +++ b/Modules/DTN/jvm/src/main/scala/dtn/TCPConnection.scala @@ -24,18 +24,13 @@ class TCPConnection(socket: Socket) { def close(): Unit = socket.close() def receive: Array[Byte] = { - try { - val size = inputStream.readInt() + val size = inputStream.readInt() - val bytes = new Array[Byte](size) + val bytes = new Array[Byte](size) - inputStream.readFully(bytes, 0, size) + inputStream.readFully(bytes, 0, size) - bytes - } catch { - case e: IOException => println(s"read attempted on closed socket: $e"); throw e - case e: EOFException => println(s"socket closed down while reading: $e"); throw e - } + bytes } } object TCPConnection { @@ -99,9 +94,16 @@ class TCPReadonlyServer(socket: ServerSocket) { var keepRunning: Boolean = true override def run(): Unit = { - while keepRunning do { - queue.put((connection, connection.receive)) + try { + while keepRunning do { + queue.put((connection, connection.receive)) + } + } catch { + case e: IOException => println(s"read attempted on closed socket: $e") + case e: EOFException => println(s"socket closed down while reading: $e") } + println("closing socket") + connection.close() } } }