[ENet-discuss] A query about channels

Jay Sprenkle jsprenkle at gmail.com
Sun Jan 17 16:46:43 PST 2010


The following works for me. It may not be the best code in the world but
it's simple, lightweight, and works. It's what others suggested I think as
well.

// A class you can serialize/deserialize:
class Version
{
public:
    Version();
    char v;
    unsigned long Number;

    friend std::ostream& operator<<( std::ostream& stream, const Version&
v);
    friend std::istream& operator>>( std::istream& stream, Version& v);
};

// write to the stream
std::ostream& operator<<( std::ostream& stream, const Version& ver )
{
   stream << ver.v << ver.Number;
   return stream;
}

// read from the stream
std::istream& operator>>( std::istream& stream, Version& ver )
{
   stream >> ver.v >> ver.Number;
   if ( ver.v != 'v' )
      throw new std::exception;
   return stream;
}


To deserialize a received packet:

void deserialize( ENetPacket* packet )
{
   // copy packet data to a stream
   std::stringstream stream;
   stream.write( (char*)packet->data, packet->dataLength );

   switch ( *packet->data )
   {
      case 'v':
         {
          Version v;
         stream >> v;
         }
         break;
      default:
         throw new std::runtime_error( "unknown packet data received from
peer" );
         break;
   }
}




On Sun, Jan 17, 2010 at 4:31 PM, Eugene Marcotte <eugene at emarcotte.com>wrote:

> On Sun, Jan 17, 2010 at 02:54:10PM -0500, Alex Milstead wrote:
> > In that case, is there a solid way to extract that identifier? Or will
> > I need to do some I'm aware of sprintf calls, is it common practice to
> > simply "sprintf" the first integer of the received binary data into a
> > buffer and (effectively) switch-case on that buffer, then memcpy the
> > remaining data that follows? Or is there a better way of doing this?
>
> What I do in my project is send google protocal buffer messages back and
> forth. To figure out which is which, each time I send I wrap them in
> another message that has two fields, a name and a blob where I serialize
> whatever the real message is.
>
> Using the built-in reflection of protocol buffers I can figure out what to
> call each message, and using some templates and maps I am able to make use
> of the protocol buffers builders on the receive side to build messages up
> as objects. From there its just a matter of dispatching to various event
> handlers.
>
> It took a bit of work to get functional, but now that it works it's pretty
> nice to just say send(whateverObject) and hook up some event handler for
> that type on the other end.
>
> It does feel rather heavy compared to what other people are suggesting
> about enums and structs too :)
>
> Eugene
> _______________________________________________
> ENet-discuss mailing list
> ENet-discuss at cubik.org
> http://lists.cubik.org/mailman/listinfo/enet-discuss
>



-- 
Cause united breaks guitars
http://www.youtube.com/watch?v=5YGc4zOqozo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20100117/aca904f5/attachment.htm>


More information about the ENet-discuss mailing list