forked from haneytron/simplsockets
-
Notifications
You must be signed in to change notification settings - Fork 2
Receiving bytes
Thijs Elenbaas edited this page Mar 13, 2019
·
6 revisions
SimplSockets is responsible for sending packages consisting of an array of bytes. SimplSockets will not do any interpretation of these byte arrays, that is all up to the user. The library will append a message size and thread id, so the message will be processed in the correct thread and will have correct size.
SimplSockets uses callbacks to pass received messages to the application
client.MessageReceived += (sender, argument) =>
{
// Get the message
var receivedMessage = argument.ReceivedMessage;
}
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.
A simple echo server is made with a few lines
server.MessageReceived += (s, e) =>
{
// Get the message
var receivedMessage = e.ReceivedMessage;
Console.WriteLine($"Server received message of {receivedMessage.Length} bytes");
// Reply to the message with the same message (echo)
server.Reply(receivedMessage, receivedMessage);
Console.WriteLine($"Server replied to message");
// We cannot not dispose the received message directly, since it may still need to be send
// Instead we use the ReturnAfterSend, which will return the message to the pool after sending
receivedMessage.ReturnAfterSend();
};
These features are shown in more detail in this example and this example