Advertisement

Is anyone using Yojimbo for networking?

Started by July 04, 2018 02:16 PM
4 comments, last by Hodgman 6 years, 4 months ago

I would like to use Glenn Fiedler's Yojimbo library as It looks like it is perfect for my application but the documentation is pretty sparse and I have yet to see a real-world source example of how to use it.  From what I can discern the examples show to connect but not how to actually send/receive messages.  Does anyone have any insight on how to use it properly?

 

I'm using it on my racing game and am very happy; it seems like a very solid network stack and was very quick to add into my game. But yes, the docs are pretty light at the moment. You can build doxygen documentation from the source code, but that's just a reference, not a how-to. There's a few discussions with Glenn in the github issues, which are kind of useful to brush up: e.g. https://github.com/networkprotocol/yojimbo/issues/25
Glenn is quite busy these days, but is also a very friendly guy if you get a hold of him :)

There are a few examples in the distro, which define their messages in shared.h.

For each message type in your game, sub-class yojimbo::Message for small messages with serialization callbacks, or yojimbo::BlockMessage if you want to attach a blob of variable-sized data onto the message. Then implement the templated Serialize function as in the samples, and add the YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS macro within your class body.

Then you use the YOJIMBO_MESSAGE_FACTORY_STARTYOJIMBO_DECLARE_MESSAGE_TYPE / YOJIMBO_MESSAGE_FACTORY_FINISH macros to create a message factory that can create all of your message types.

Then sub-class yojimbo::Adapter and override CreateMessageFactory with a function that returns an instance of your custom message factory type (which was declared by those macros, above).

Then, when you make your yojimbo::Client or yojimbo::Server objects, you pass in your own Adapter-sub-class, which allows the yojimbo client/server to create your custom message types.

The server/client can then allocate messages by passing a message type ID number to CreateMessage, then optionally allocate data-blocks and attach them onto your BlockMessage message types (with AllocateBlock / AttachBlockToMessage), then send them with SendMessage

The server/client should call AdvanceTime/ReceivePackets/SendPackets regularly to actually push packets through the network, and also call ReceiveMessage in a loop to decode received packets into your custom message types (use GetType, perhaps in a switch statement, to determine exactly which type of message class has been instantiated).

Advertisement

Thanks!  That's very helpful, I will give it a go.  I used to work in professional racing, your game looks very interesting.

Do you know if CreateMessage, SendMessage, ReceiveMessage, ReleaseMessage, etc are thread safe?   Can I call them from a separate thread than the one that is doing the AdvanceTime/ReceivePackets/SendPackets sequence? 

 

 

No. In that situation, you should either wrap your client/server objects in a mutex so you can share them it with your other threads, or, the main thread should receive all messages immediately after calling ReceivePackets and then re-push them into your own thread-safe queue for later processing (and also push your own message types into a thread-safe queue for later conversion to yojimbo messages and sending).

This topic is closed to new replies.

Advertisement