Is it possible to use pure sockets with kotlin-nat...
# kotlin-native
e
Is it possible to use pure sockets with kotlin-native? Normal client socket. After some searching it seems my best bet is ktor-network, but I'm not quite able to get the example to run. At the moment it's stuck at not being able to resolve "ActorSelectorManager". Could be it's Android-specific? I got a normal console app
Made it run! Had to make some changes, replaced
ActorSelectorManager
with
SelectorManager
,
<http://Dispatchers.IO|Dispatchers.IO>
with
Dispatchers.Default
and
w.write
with
w.writeStringUtf8
. Got some runtime errors, but it did connect and write some stuff 🙂
👌 1
Also works in debian and archlinux containers, so it will probably work in most distros. Does not work in alpine, but I think that is because of missing glibc, which can be solved later by building with musl (or something, there is an issue about it). This is unlike ktor-client which depend on curl
m
Fwiw, you can also use plain posix syscalls
e
@mbonnin Will that work in Windows?
m
Mm good question. ~Most likely not~Edit: it does, windows is posix 🎉
e
My solution would? Haven't tested yet, but I hope it will. Multi OS support is important. To test I have to start up my Windows VM, and start installing JDK 😛
Tried building on Windows now:
> No matching variant of io.ktorktor network1.5.3 was found.
😢
Finding packages/libraries seems exceedingly difficult. For ktor-network I can find it here: https://package-search.jetbrains.com/search?query=ktor-network&amp;onlyMpp=true but I have no idea what platforms each package support, and every description is identical. I can't see anything like -mingw or -win in that list so I guess it's not supported
Using another search engine kotlin.link I found another library; rsocket. This search also doesn't tell me what I want to know about platform details. Anyway, rsocket also has no support for Windows:
Native - TCP client only (linux x64, macos, ios, watchos, tvos)
From Kotlin docs it seems posix should work on Windows
For all Unix- or Windows-based targets (including Android and iOS targets) we provide the
posix
platform lib. It contains bindings to platform's implementation of the POSIX standard.
Sorry for the ranting. I will try this next 🙂
v
e
@Victor ADĂSCĂLIȚEI This is pure TCP MPP? I want pure sockets. Had a quick look at posix. It seems very complicated
👍 1
m
Ah, if there are posix apis for windows then using the plain sockets directly will work
You can copy/paste that
val socketFd = socket(AF_INET, SOCK_STREAM, 0)
in a mingw sourceSet and see if that compiles
posix API have some verbosity but they have years of documentation and sample material so I find that it's worth the tradeoff
e
Not that many samples for Kotlin though. I haven't found a working client sample yet
m
Right not much Kotlin samples...
e
Found two different servers on https://github.com/JetBrains/kotlin-native/tree/master/samples/ (echo and nonBlocking). I can try to write a client based on these (or your server)
I found one client on stackoverflow, but it was a bit old, and didn't compile out of the box
m
If you're willing to add okio into the mix the apollo-android code has implementations of
Source
and
Sinks
that take a file descriptor as input: https://github.com/apollographql/apollo-android/blob/3588729f16d6c6f5e4c2eec9139bf[…]n/com/apollographql/apollo3/integration/FileDescriptorSource.kt
That's super handy because then you get all the readUtf8, encoding, buffering, etc... from okio
Client shouldn't be way different from server. Calling
connect
instead of
listen
and then get a fileDescriptor that you turn into a
okio.Source
e
okio is just a dependency which will be linked in?
As long as I end up with a single standalone executable I'm happy
m
I think so (it's 100% Kotlin)
I've been hit in the past by libcurl not being linked in K/N executables, that hurts...
e
Sounds good. Amazing there's no wrapper for posix tcp-stuff, I see the same horrible code over and over on GitHub
m
I guess every one writes HTTP these days 😅
e
Given HTTP in Native today expects people to install curl, or libcurl I would expect a pure TCP wrapper to be the first step towards a pure Kotlin Http client - or am I wrong?
m
100% amen to that, I never really understood how come that wasn't a bigger issue
I think most people have the expectation that libcurl is preinstalled because it is on mac
But that sounds super unsafe TBH
e
ktor-network uses posix from the looks of it. Wonder why they don't have windows, or if my test did something wrong
m
There's no mingw on MavenCentral: https://repo1.maven.org/maven2/io/ktor/
e
Same as I saw on package-search. If they use posix that is strange
m
Yep, maybe open an issue?
e
Trying to find an old issue about this now, but it's a bit hard to find any
m
TBH, I would still use the posix APIs even if ktor-network had mingw support. Sure there's a bit of messing up with
memScoped
and low level APIs but you're pretty much guaranteed stable and battle-tested API whereas ktor, especially ktor-network seems way younger
e
Writing
alloc<sockaddr_in>()
could affect my sleep cycle 😛
m
😂
Fair 🙂
e
I'm starting to think recent changes have made the package able to build for mingw and they simply haven't pushed it (set up a build)
m
Ah nice!
e
Tried building ktor-network from master on my WIndows VM without luck, also tried the UDP-branch by @Thomas without luck
t
The Udp branch is still a work in progress. Need to rebase it on ktor 1.6 branch. I am already using it in production on iOS.
e
@Thomas Do you believe it will work on Windows? Should it work now? I think some tests just failed for me, could be it actually built. What command should I use?
t
The UDP sockets PR I am working on has not been tested on Windows yet. It also misses an implementation for inet_ntop here: https://github.com/ktorio/ktor/blob/6dceed335d2de689dd30e3cb1d62d571c8061041/ktor-network/posix/interop/network.def#L32. I am not familiar with the Windows APIs so I need some help here. I will rebase this PR on branch 1.6.0-eap soon so that should fix some tests. By the way, what type of sockets are you interested in? My PR only adds support for UDP sockets on native. However, TCP sockets on native are already there in Ktor so I think they should work on Windows, but I have not tested that.
e
I'm only interested in TCP. I didn't find a ktor-network-mingw package, I'm not able to build on Windows and KTOR-1270 seems to state it's not possible. I could be wrong though, all of this is new to me
I found your branch interesting because I thought it might build on Windows. I guessed as long as posix built then mingw would build and it would "just work"
t
I did not change the supported targets in the PR so that should be the same
Looks like the network module was excluded for mingw, see: https://github.com/ktorio/ktor/blob/main/gradle/posix.gradle#L36-L40
I am not using Ktor on Windows so I do not know why it is not supported.
I am not sure if any changes are needed to get Ktor network working on Windows
e
Wonder what happens if I add mingw there and try to build
t
You could try building Ktor for Windows after editing the linked posix.gradle file and see what is not working. Maybe you can add support for it in a PR. I would not advice using my PR as that is still a work in progress. I think the main branch or 1.6.0-eap branch would be better.
e
Need to fire up my VM again. Worth a try. Thanks
Stupid question; how do I build only ktor-network, is that possible? I've tried standing in the older and running
../gradlew build -x test
without luck
t
You can add the
--continue
flag so it ignores failing tests. Is that what you want?
417 Views