[ENet-discuss] enet, epoll and libevent

Mihai Draghicioiu mihai.draghicioiu at gmail.com
Sat Aug 1 06:45:45 PDT 2009


Hi all. I'm working on frogmod, a sauerbraten server. At first it used the
original sauer main loop, then I changed it to use a loop similar to
BloodFrontier (checks for events then sleeps for 1ms), but now I want to
make it work with libevent. The way libevent works -- you receive a callback
for each event that happens on the socket. That way the most cpu is saved
when there are no connections, and everything is asynchronous (including the
dns). From what I can see in the enet code -- enet_host_service() and
enet_socket_wait(), the way to replace the main loop would be something like
this, for reading:

// basically set a callback for when data is available for reading
event_set(&evinfo, socket, EV_READ | EV_PERSIST, readcb, NULL);
event_add(&evinfo, NULL);

void readcb(...) { // handle incoming data. the code is pasted
        if (event != NULL)
    {
        event -> type = ENET_EVENT_TYPE_NONE;
        event -> peer = NULL;
        event -> packet = NULL;

        switch (enet_protocol_dispatch_incoming_commands (host, event))
        {
        case 1:
            return 1;

        case -1:
            perror ("Error dispatching incoming packets");

            return -1;

        default:
            break;
        }
    }
}

(and something similar for writing)

So basically, it would mean replacing enet_host_service() in the program
with the libevent setup.

The reason I'm doing this is because libevent also provides me with async
DNS, http server/client code, and I've already written IRC client code. This
would also make frogmod modular, able to easily support other enet based
servers (like blood frontier).

My questions are:
1. Is this code the good way to go about it? Basically with libevent you add
some fds for reading and some for writing, then enter its loop, from where
it dispatches.
2. What would you recommend instead?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20090801/83e7cf5f/attachment.htm>


More information about the ENet-discuss mailing list