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.<br><br>// A class you can serialize/deserialize:<br>class Version<br>
{<br>public:<br> Version();<br> char v;<br> unsigned long Number;<br><br> friend std::ostream& operator<<( std::ostream& stream, const Version& v);<br> friend std::istream& operator>>( std::istream& stream, Version& v);<br>
};<br><br>// write to the stream<br>std::ostream& operator<<( std::ostream& stream, const Version& ver )<br>{<br> stream << ver.v << ver.Number;<br> return stream;<br>}<br><br>// read from the stream<br>
std::istream& operator>>( std::istream& stream, Version& ver )<br>{<br> stream >> ver.v >> ver.Number;<br> if ( ver.v != 'v' )<br> throw new std::exception;<br> return stream;<br>
}<br><br><br>To deserialize a received packet:<br><br>void deserialize( ENetPacket* packet )<br>{<br> // copy packet data to a stream<br> std::stringstream stream;<br> stream.write( (char*)packet->data, packet->dataLength );<br>
<br> switch ( *packet->data )<br> {<br> case 'v':<br> {<br> Version v;<br> stream >> v;<br> }<br> break;<br> default:<br> throw new std::runtime_error( "unknown packet data received from peer" );<br>
break;<br> }<br>}<br><br><br><br><br><div class="gmail_quote">On Sun, Jan 17, 2010 at 4:31 PM, Eugene Marcotte <span dir="ltr"><<a href="mailto:eugene@emarcotte.com">eugene@emarcotte.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">On Sun, Jan 17, 2010 at 02:54:10PM -0500, Alex Milstead wrote:<br>
> In that case, is there a solid way to extract that identifier? Or will<br>
> I need to do some I'm aware of sprintf calls, is it common practice to<br>
> simply "sprintf" the first integer of the received binary data into a<br>
> buffer and (effectively) switch-case on that buffer, then memcpy the<br>
> remaining data that follows? Or is there a better way of doing this?<br>
<br>
</div>What I do in my project is send google protocal buffer messages back and<br>
forth. To figure out which is which, each time I send I wrap them in<br>
another message that has two fields, a name and a blob where I serialize<br>
whatever the real message is.<br>
<br>
Using the built-in reflection of protocol buffers I can figure out what to<br>
call each message, and using some templates and maps I am able to make use<br>
of the protocol buffers builders on the receive side to build messages up<br>
as objects. From there its just a matter of dispatching to various event<br>
handlers.<br>
<br>
It took a bit of work to get functional, but now that it works it's pretty<br>
nice to just say send(whateverObject) and hook up some event handler for<br>
that type on the other end.<br>
<br>
It does feel rather heavy compared to what other people are suggesting<br>
about enums and structs too :)<br>
<font color="#888888"><br>
Eugene<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
ENet-discuss mailing list<br>
<a href="mailto:ENet-discuss@cubik.org">ENet-discuss@cubik.org</a><br>
<a href="http://lists.cubik.org/mailman/listinfo/enet-discuss" target="_blank">http://lists.cubik.org/mailman/listinfo/enet-discuss</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Cause united breaks guitars<br><a href="http://www.youtube.com/watch?v=5YGc4zOqozo">http://www.youtube.com/watch?v=5YGc4zOqozo</a><br><br>