vibe-d / vibe-core

Repository for the next generation of vibe.d's core package.
MIT License
61 stars 47 forks source link

Creating an UDP server is cumbersome #289

Open Geod24 opened 2 years ago

Geod24 commented 2 years ago

Creating a TCP server is rather straightforward and documented. However, as I tried to create an UDP server, I hit a few issues:

Geod24 commented 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.

mkykadir commented 2 years ago

I've tried this recently and it seems to be working properly now.

dushibaiyu commented 9 months ago

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();
Geod24 commented 9 months ago

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.