Better disconnection logging in psoserv.

This commit is contained in:
Daan Vanden Bosch 2021-08-15 16:54:29 +02:00
parent c11b84c950
commit 2a05650ac8
2 changed files with 17 additions and 4 deletions
psoserv/src/main/kotlin/world/phantasmal/psoserv/servers

View File

@ -4,6 +4,7 @@ import world.phantasmal.psolib.buffer.Buffer
import world.phantasmal.psoserv.encryption.Cipher
import world.phantasmal.psoserv.messages.*
import java.net.Socket
import java.net.SocketException
class ProxyServer(
name: String,
@ -129,5 +130,9 @@ class ProxyServer(
override fun logMessageReceived(message: Message) {
logger.trace { "Sent $message." }
}
override fun logUnexpectedSocketException(e: SocketException) {
// Do nothing, we expect both server and client to close connections.
}
}
}

View File

@ -37,6 +37,7 @@ abstract class SocketHandler<MessageType : Message>(
fun listen() {
logger.info { "Listening to $name ($sockName)." }
running = true
var clientDisconnected = false
try {
val readBuffer = Buffer.withCapacity(BUFFER_CAPACITY, Endianness.Little)
@ -49,6 +50,7 @@ abstract class SocketHandler<MessageType : Message>(
if (readSize == -1) {
// Close the connection if no more bytes available.
logger.debug { "$name ($sockName) end of stream." }
clientDisconnected = true
break@readLoop
}
@ -165,6 +167,7 @@ abstract class SocketHandler<MessageType : Message>(
}
// Close the connection.
clientDisconnected = true
break@readLoop
}
@ -198,9 +201,8 @@ abstract class SocketHandler<MessageType : Message>(
// Don't log if we're not running anymore because that means this exception was probably
// generated by a socket.close() call.
if (running) {
logger.error(e) {
"Error while listening to $name ($sockName), closing connection."
}
logUnexpectedSocketException(e)
clientDisconnected = true
}
} catch (e: Throwable) {
logger.error(e) { "Error while listening to $name ($sockName), closing connection." }
@ -208,7 +210,7 @@ abstract class SocketHandler<MessageType : Message>(
running = false
try {
if (socket.isClosed) {
if (socket.isClosed || socket.isInputShutdown || clientDisconnected) {
logger.info { "Connection to $name ($sockName) was closed." }
} else {
logger.info { "Closing connection to $name ($sockName)." }
@ -282,6 +284,12 @@ abstract class SocketHandler<MessageType : Message>(
logger.trace { "Received $message." }
}
protected open fun logUnexpectedSocketException(e: SocketException) {
logger.error(e) {
"Error while listening to $name ($sockName), closing connection."
}
}
protected enum class ProcessResult {
Ok, Changed, Done
}