Open Geod24 opened 2 years ago
Additionally, calling listenUDP
before the event loop is running results in an invalidHandle
error.
I had the following code:
if (cmdline_args.dns)
{
auto udp = listenUDP(53);
runTask({
while (true) {
auto pack = udp.recv();
logInfo("Got packet: %s", pack);
}
});
}
return runEventLoop();
And got an error. Moving the listenUDP
inside runTask
cleared it.
To be precise, it seems like the UDP handle's validationCounter
stays at 0
while the common
is 1
.
I've tried this recently and it seems to be working properly now.
you shoud use listenUDP on run Task. like this:
if (cmdline_args.dns)
{
runTask({
auto udp = listenUDP(53);
ubyte[] buffer = cast(ubyte[]) Mallocator.instance.allocate(2048);
scope(exit) Mallocator.instance.deallocate(cast(void[]) buffer);
while (true) {
auto pack = udp.recv(buffer,null);
logInfo("Got packet: %s", pack);
}
});
}
return runEventLoop();
If the fiber is in the same thread it should not make a difference. Also in this case I think we were saving the listener somewhere so we could shut it down when the user pressed Ctrl+C, so it wasn't actually an option.
Creating a TCP server is rather straightforward and documented. However, as I tried to create an UDP server, I hit a few issues:
recv
, which will throw on timeout.