dotnet/WatsonTcp

Improvement to multiclients

luki5417 opened this issue · 2 comments

Overall the WatsonTcp project is great and covers my needs quite well.
However, I have one suggestion that I think will be useful to many who use many customers.

The case concerns the Server, specifically the WatsonTcpServer class.

Now we have that all ready packets go to MessageReceived and MessageReceivedEventArgs class contains IpPort.
At this point, we need to handle the search for our client, which can be quite problematic.

I was thinking that since we have the IpPort of a given client in MessageReceivedEventArgs, why not add a new property, e.g. Action<byte[]>, which would give us the ability to jump directly to our client.

Example below:

Assignment

public WatsonTcpServer MyServer = new WatsonTcpServer("127.0.0.1", 1234);

MyServer.Events.ClientConnected += Events_ClientConnected;
MyServer.Events.MessageReceived += Events_MessageReceived;

private void Events_ClientConnected(object? sender, MessageReceivedEventArgs e)
{
    var myClient = new MyClientClass();
    MyServer.SetClientAction(e.IpPort, myClient.MessageReceived);//Action<byte[]>
}

Use

private void Events_MessageReceived(object? sender, MessageReceivedEventArgs e)
{
    e.ClientAction?.Invoke(e.Data);
}

Adding this capability would be compatible with earlier versions and it seems that the performance so far would not be affected.

That would be a strong enhancement.

Cheers

Hi @luki5417 thanks for the suggestion, I'd be happy to take a PR on this.

Hi, I'm writing again because I found a solution by trying to make changes to the library :)

Just bind your object with WatsonTcp ClientMetadata object:

private void Events_ClientConnected(object? sender, MessageReceivedEventArgs e)
{
    var myClient = new MyClientClass();
    e.Client.Metadata = myClient;
}

Thanks again for a great library :)

Regards