I just got an unexpected problem. I try to use raw...
# ktor
a
I just got an unexpected problem. I try to use raw sockets from ktor-network to work with home-made TCP-based protocol. And I can't see how I can use Input/Output API with it. It seems like socket IO lives in a completely different world. For example, instead WritableByteChannel I have ByteWriteChannel, which seems to be unrelated. The problem is that I already have ktor-io based code that work with files and it would be very sad to have to re-do it for Ktor sockets. Are there any solutions to this?
e
Hey @altavir, it looks like you’re trying to use sockets from
java.nio.channels
. You can use
aSocket
method to create a socket from ktor-network
a
I am using
Copy code
connection = aSocket(ActorSelectorManager(<http://Dispatchers.IO|Dispatchers.IO>))
    .tcp().connect(InetSocketAddress(host, port)).connection()
and trying to use connection
e
Yep, and the connection has
input
and
output
primitives from ktor-io
a
Copy code
public class Connection(
    public val socket: Socket,
    public val input: ByteReadChannel,
    public val output: ByteWriteChannel
)
I don't see any way of converting ByteWriteChannel to Output
Those type are similar to Input/Output but seems to be incompatible.
e
Yep, there are no common blocking adapters on this
The only thing you can do is to use
BytePacketBuilder
to prepare the message(with
buildPacket
) and transfer it with
writePacket
.
BytePacketBuilder
is
Output
a
Is it possible to somehow convert them to Input/Output? I can avoid using Ktor and wrap regular java channels, but it seems like a bit of waste (not using ktor to access ktor).
Yeah, I know about builders and I even have my own Binary API which makes it easier. But it does not solve the problem completely. For example streaming read won't work this way
e
There are no working adapters like this
a
😞
e
I’m still not sure that making blocking adapters for streaming cases is a good idea
a
I am not saying you should. But having single abstraction for input/ouput is very important.
e
Could you give me a hint on the use cases you have?
I’m not sure that it’s possible to have the same abstraction for
suspend
and not
suspend
API
a
If you have blocking calls they could be called from suspend functions. Not vice versa of course. But it seems to be possbile to have suspending writers implement blocking calls .
e
I’m not sure that I clearly get it
Could you give me an example of having single abstraction for both with a few methods?
a
I will see if I have time to implement an Input/Output wrapper on top of ByteRead/WriteChannel. By the way, the name is misleading because it is easy to confuse with WritableByteChannel
🙏 1
👍 1