I'm having some trouble figuring out what I'm doin...
# ktor
z
I'm having some trouble figuring out what I'm doing wrong in my code using sockets to communicate with the X window system protocol
Copy code
val selectorManager = SelectorManager(Dispatchers.Main)
val socketPath = "/tmp/.X11-unix/X2"
val tcp = aSocket(selectorManager).tcp()
val clientConnection = try {
    tcp.connect(UnixSocketAddress(socketPath))
} catch (e: Exception) {
    selectorManager.close()
    println(e.message)
    exitProcess(1)
}

val readChannel = clientConnection.openReadChannel()
val sendChannel = clientConnection.openWriteChannel()

val authName = ""
val authData = ""

fun BytePacketBuilder.writePadding(bytes: Int) {
    repeat((4 - bytes % 4) % 4) { writeByte(0) }
}

sendChannel.writePacket {
    writeUByte(102u)
    writeByte(0)
    writeUShort(11u)
    writeUShort(0u)
    writeUShort(authName.length.toUShort())
    writeUShort(authData.length.toUShort())
    writeByte(0)
    writeText(authName)
    writePadding(authName.length)
    writeText(authData)
    writePadding(authData.length)
}

sendChannel.flush()

println(readChannel.readByte())
It just suspends at the readByte call
r
i experienced something similar a while back on a project (running on osx tho). I was able to successfully read with something like the following
Copy code
receiveChannel.read { respBuffer ->
  // consume respBuffer
}
but this leads to some really ugly nested code, particularly if you are performing many read / write ops