Skip to content

Commit

Permalink
fix: jvm unix implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vyfor committed May 8, 2024
1 parent f792f14 commit 25ff2a0
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/jvmMain/kotlin/io/github/vyfor/kpresence/ipc/Connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,24 @@ actual class Connection {
val length = stream.readInt().reverseBytes()
val buffer = ByteBuffer.allocate(length)

stream.read(buffer)
val bytesRead = stream.read(buffer)
if (bytesRead == 0) throw ConnectionClosedException("The pipe has been closed")

return Message(
opcode,
buffer.array()
)
} catch (e: AsynchronousCloseException) {
return null
} catch (e: ConnectionClosedException) {
throw e
} catch (e: ClosedChannelException) {
throw ConnectionClosedException(e.message.orEmpty())
} catch (e: SocketException) {
if (e.message == "Connection reset") throw ConnectionClosedException(e.message.orEmpty())
throw PipeReadException(e.message.orEmpty())
} catch (e: Exception) {
if (e.message == "Broken pipe") throw ConnectionClosedException(e.message.orEmpty())
throw PipeReadException(e.message.orEmpty())
}
} ?: throw NotConnectedException()
Expand All @@ -199,16 +204,19 @@ actual class Connection {
buffer.putInt(bytes.size.reverseBytes(), 4)
bytes.copyInto(buffer, 8)
}

stream.write(ByteBuffer.wrap(buffer))
val bytesWritten = stream.write(ByteBuffer.wrap(buffer))
if (bytesWritten == 0) throw ConnectionClosedException("The pipe has been closed")
} catch (e: AsynchronousCloseException) {
return
} catch (e: ConnectionClosedException) {
throw e
} catch (e: ClosedChannelException) {
throw ConnectionClosedException(e.message.orEmpty())
} catch (e: SocketException) {
if (e.message == "Connection reset") throw ConnectionClosedException(e.message.orEmpty())
throw PipeWriteException(e.message.orEmpty())
} catch (e: Exception) {
if (e.message == "Broken pipe") throw ConnectionClosedException(e.message.orEmpty())
throw PipeWriteException(e.message.orEmpty())
}
} ?: throw NotConnectedException()
Expand All @@ -222,7 +230,9 @@ actual class Connection {
private fun SocketChannel.readInt(): Int {
val buffer = ByteBuffer.allocate(4)

read(buffer)
val bytesRead = read(buffer)
if (bytesRead == 0) throw ConnectionClosedException("The pipe has been closed")

return ((buffer[0].toUInt() shl 24) +
(buffer[1].toUInt() shl 16) +
(buffer[2].toUInt() shl 8) +
Expand Down

0 comments on commit 25ff2a0

Please sign in to comment.