[ENet-discuss] New experimental feature: packet intercept callbacks

Lee Salzman lsalzman at gmail.com
Tue Oct 2 03:48:53 PDT 2012


Because there were some requests on github for things that would otherwise
use the functionality, or direct appeals for the functionality itself, I
finally bit the bullet and added a simple packet intercept callback to
ENetHost, usage looks sorta like this:


int intercept_callback(ENetHost *host, ENetEvent *event)
{
   // the first two bytes of a valid ENet protocol packet will never be
0xFFFF, so you should ideally use this to distinguish user data packets
from ENet protocol packets
   if(host->receivedDataLength >= 2 && *(unsigned short
*)host->receivedData == 0xFFFF)
   {
      // host->receivedAddress contains the address the UDP packet came from
      // do magical stuff with packet here...
      // if event is non-NULL, you can also set event->type with your own
non-zero event number that ENet will pass out of enet_host_service(), but
this is not required, just optional
      // if event->type is untouched, ENet will still skip the packet so
long as you return 1...
      return 1; // tell ENet to skip the packet
   }
   return 0; // tell ENet to continue processing the packet
   // returning -1 will propagate the -1 return value out of
enet_host_service...
}

....
host -> intercept = intercept_callback;
....
enet_host_service(host, event, timeout); // intercept gets called from in
here when ENet receives a raw udp packet
// if you propagated a custom event type from within the intercept
callback, it will get returned out here too with enet_host_service
returning 1 like for any other event

This system can be used to implement all manner of fun things, from
simulating packet loss to multiplexing your own data like pings or whatever
over the ENet socket.
This is currently in the github repo, going to let it stew a bit before I
release anything in case people have suggestions.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20121002/eddef02e/attachment.html>


More information about the ENet-discuss mailing list