Search code examples
monoxsockets.net

Inferior behavior of XSockets under Mono compared to MS.NET


Given

I am using XSockets 3.0.6 which I think is the latest stable version. Under MS.NET the behavior is as expected. On Ubuntu 14.04 and Mono 3.6.1 though the server has some kind of delays before sending messages to clients.

Problem

On MS.NET when I type a string in the client and send it, all clients are immediately notified. On Mono though message is received by the server and clients were not notified immediately. With only 1 message I waited for 5 minutes and clients were still not notified. When messages become 5-6 then all clients become notified about all messages at once. It seems like the server uses some kind of buffering but conditionally - depending on the .NET runtime, which is very strange.

Question

Am I doing something wrong? How to change the code so that all clients are immediately notified as in MS.NET?

Code

I followed (and slightly modified) the quick start example as follows...

Server

Initialization

using (var container = Composable.GetExport<IXSocketServerContainer>())
{
    container.StartServers();

    foreach (var server in container.Servers)
    {
        Console.WriteLine(server.ConfigurationSetting.Endpoint);
    }

    Console.Write("Started! Hit 'Enter' to quit.");
    Console.ReadLine();

    container.StopServers();
}

Custom controller

public class CustomController : XSocketController
{
    public override void OnMessage(ITextArgs textArgs)
    {
        Console.WriteLine ("No delay = {0}", this.Socket.Socket.NoDelay);
        if (!this.Socket.Socket.NoDelay)
        {
            Socket.Socket.NoDelay = true;
        }

        Console.WriteLine("Received {0} about {1}.", textArgs.data, textArgs.@event);
        this.SendToAll(textArgs);
    }
}

Client

var client = new XSocketClient("ws://127.0.0.1:4502/CustomController", "*");

client.OnOpen += (sender, eventArgs) => System.Console.WriteLine("OPEN");
client.Bind("foo", message => System.Console.WriteLine(message.data));
Thread.Sleep(1000);
client.Open();

string input;
System.Console.WriteLine("Type 'quit' to quit and any other string to send a message:");
do
{
    input = System.Console.ReadLine();
    if (input != "quit")
    {
        client.Send(input, "foo");
    }
} while (input != "quit");

Solution

  • I experienced this my self when running XSockets on a raspberry pi. After some investigation I realized that it had to do with the fact that the pi is single core and that internal queue did not send the message out until 5 messages was sent in.... Then all messages was sent out.

    How many cores does your computer have ?

    This issue is resolved in 4.0 (in alpha right now)

    Edit: I have only had this issue on single core machines with Mono, on my Mac Book Air everything works great on Mono