https://kotlinlang.org logo
#ktor
Title
# ktor
a

altavir

10/12/2021, 1:37 PM
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

e5l

10/12/2021, 1:38 PM
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

altavir

10/12/2021, 1:40 PM
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

e5l

10/12/2021, 1:41 PM
Yep, and the connection has
input
and
output
primitives from ktor-io
a

altavir

10/12/2021, 1:43 PM
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

e5l

10/12/2021, 1:44 PM
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

altavir

10/12/2021, 1:46 PM
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

e5l

10/12/2021, 1:48 PM
There are no working adapters like this
a

altavir

10/12/2021, 1:48 PM
😞
e

e5l

10/12/2021, 1:48 PM
I’m still not sure that making blocking adapters for streaming cases is a good idea
a

altavir

10/12/2021, 1:49 PM
I am not saying you should. But having single abstraction for input/ouput is very important.
e

e5l

10/12/2021, 1:50 PM
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

altavir

10/12/2021, 1:54 PM
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

e5l

10/12/2021, 1:55 PM
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

altavir

10/12/2021, 1:58 PM
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