You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LineBufferedSocket.kt 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.dmdirc.ktirc.io
  2. import io.ktor.network.selector.ActorSelectorManager
  3. import io.ktor.network.sockets.Socket
  4. import io.ktor.network.sockets.aSocket
  5. import io.ktor.network.sockets.openReadChannel
  6. import io.ktor.network.sockets.openWriteChannel
  7. import io.ktor.network.util.ioCoroutineDispatcher
  8. import kotlinx.coroutines.CoroutineScope
  9. import kotlinx.coroutines.channels.ReceiveChannel
  10. import kotlinx.coroutines.channels.produce
  11. import kotlinx.coroutines.io.ByteReadChannel
  12. import kotlinx.coroutines.io.ByteWriteChannel
  13. import java.net.InetSocketAddress
  14. interface LineBufferedSocket {
  15. // TODO: This is a bit pants.
  16. var debugReceiver: ((String) -> Unit)?
  17. suspend fun connect()
  18. fun disconnect()
  19. suspend fun sendLine(line: ByteArray, offset: Int = 0, length: Int = line.size)
  20. suspend fun sendLine(line: String)
  21. fun readLines(coroutineScope: CoroutineScope): ReceiveChannel<ByteArray>
  22. }
  23. /**
  24. * Asynchronous socket that buffers incoming data and emits individual lines.
  25. */
  26. // TODO: TLS options
  27. class KtorLineBufferedSocket(private val host: String, private val port: Int): LineBufferedSocket {
  28. companion object {
  29. const val CARRIAGE_RETURN = '\r'.toByte()
  30. const val LINE_FEED = '\n'.toByte()
  31. }
  32. // TODO: This is a bit pants.
  33. override var debugReceiver: ((String) -> Unit)? = null
  34. private lateinit var socket: Socket
  35. private lateinit var readChannel: ByteReadChannel
  36. private lateinit var writeChannel: ByteWriteChannel
  37. override suspend fun connect() {
  38. socket = aSocket(ActorSelectorManager(ioCoroutineDispatcher)).tcp().connect(InetSocketAddress(host, port))
  39. readChannel = socket.openReadChannel()
  40. writeChannel = socket.openWriteChannel()
  41. }
  42. override fun disconnect() {
  43. socket.close()
  44. }
  45. override suspend fun sendLine(line: ByteArray, offset: Int, length: Int) {
  46. with (writeChannel) {
  47. debugReceiver?.let { it(">>> ${String(line, offset, length)}") }
  48. writeAvailable(line, offset, length)
  49. writeByte(CARRIAGE_RETURN)
  50. writeByte(LINE_FEED)
  51. flush()
  52. }
  53. }
  54. override suspend fun sendLine(line: String) = sendLine(line.toByteArray())
  55. override fun readLines(coroutineScope: CoroutineScope) = coroutineScope.produce {
  56. val lineBuffer = ByteArray(4096)
  57. var index = 0
  58. while (!readChannel.isClosedForRead) {
  59. var start = index
  60. val count = readChannel.readAvailable(lineBuffer, index, lineBuffer.size - index)
  61. for (i in index until index + count) {
  62. if (lineBuffer[i] == CARRIAGE_RETURN || lineBuffer[i] == LINE_FEED) {
  63. if (start < i) {
  64. val line = lineBuffer.sliceArray(start until i)
  65. debugReceiver?.let { it("<<< ${String(line)}") }
  66. send(line)
  67. }
  68. start = i + 1
  69. }
  70. }
  71. lineBuffer.copyInto(lineBuffer, 0, start)
  72. index = count + index - start
  73. }
  74. }
  75. }