Skip to content

Commit

Permalink
TCPConnection, moved exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lh70 committed Aug 12, 2024
1 parent 1af0b2b commit 4874866
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Modules/DTN/jvm/src/main/scala/dtn/TCPConnection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}
}
Expand Down

0 comments on commit 4874866

Please sign in to comment.