-
Notifications
You must be signed in to change notification settings - Fork 2
Receiving objects
SimplMessage is a library that facilitates sending on any type of objects. It uses MessagePack for high performance serialization to binary format and uses SimplSockets to send the binary data. It also adds a descriptor to each package so that the receiving side knows how to deserialize the data to the correct class.
SimplMessage uses callbacks to pass received messages to the application
client.AddCallBack<ClassA>((receivedMessage) =>
{
// get data from received message cast to ClassA
var receivedObject = receivedMessage.GetContent<ClassA>();
});This works both on client and server side. The callback passes the argument containing the received pooledMessage. Remember to return it to the pool when done.
If you want to determine how to deal with an object yourself, you can also use the generic callback, by not providing a type. This will function as a catch-all for any message that does not have typed callback attached.
client.AddCallBack(GenericCallback) =>
{
// Check for type ourselves
if (receivedMessage.Identifier == "ClassA")
{
var message = receivedMessage.GetContent<ClassA>();
}
});A simple echo server is made with a few lines
// Create a callback for received data of type classA
server.AddCallBack<ClassA>((receivedMessage) =>
{
// get data from received message cast to ClassA
var receivedObject = receivedMessage.GetContent<ClassA>();
Console.WriteLine($"Server received message: {receivedObject.VarDouble}, {receivedObject.VarInt}");
// Reply to the message with the same content (echo)
server.Reply(receivedObject, receivedMessage);
Console.WriteLine($"Server replied to message");
});These features are shown in more detail in this example and this example