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:<br>
<br>// basically set a callback for when data is available for reading<br>event_set(&evinfo, socket, EV_READ | EV_PERSIST, readcb, NULL);<br>event_add(&evinfo, NULL);<br><br>void readcb(...) { // handle incoming data. the code is pasted<br>
if (event != NULL)<br> {<br> event -> type = ENET_EVENT_TYPE_NONE;<br> event -> peer = NULL;<br> event -> packet = NULL;<br><br> switch (enet_protocol_dispatch_incoming_commands (host, event))<br>
{<br> case 1:<br> return 1;<br><br> case -1:<br> perror ("Error dispatching incoming packets");<br><br> return -1;<br><br> default:<br> break;<br>
}<br> }<br>
}<br>
<br>
(and something similar for writing)<br><br>So basically, it would mean replacing enet_host_service() in the program with the libevent setup.<br><br>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).<br>
<br>My questions are:<br>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.<br>2. What would you recommend instead?<br>
<br>