Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I group socket connections by the client application instance they propagated from?

Most recently I have been working on an application that is leveraged by a TCP client-server model (reverse connection).

In order to improve the performance of long-running operations, I have made it so that a single instance of the client application can make a number of outgoing socket connections to the server.

When the server application accepts an incoming connection, the child socket representing that connection is encapsulated in a new instance of a class called a ServerChildSocket.

I need some way to effectively group all of the ServerChildSocket instances that propagated from the same client application instance but I am struggling to develop a working approach let alone a good approach.

My objective is to group ServerChildSocket instances accordingly in a class similar to this...

class UserState
{
     ServerChildSocket MainConnection {get; set;}
     ServerChildSocket FileDownloadConnection {get; set;}
     ServerChildSocket VoiceChatConnection {get; set;}
}

How can I identify which connections originated from the same client application instance and then assign the connection to an instance of the UserState class accordingly?


I believe that the client application would need to send some preliminary information about the connections' intent (main connection? download connection? etc.) as soon as the connection has been established. I am struggling to think of a way to bring this all together, though.

like image 811
Caster Troy Avatar asked Sep 26 '13 17:09

Caster Troy


2 Answers

Include the assignment of a "client id" from the server to each client in your protocol and also the idea of tagging each connection based on its intended use.

When the client makes its main connection to the server the relevant part of the handshake would look like this:

             CLIENT                          SERVER

           Type = Main    =============>
           Id   = null

                          <=============    Id = 123

Then, when the client makes additional side-channel connections it will include the id returned by the server in the initial handshake:

             CLIENT                          SERVER

           Type = Chat    =============>
           Id   = 123

This way the server knows what client each accepted connection belongs to and there is also no danger of accidental collisions (the server will not assign the same id to multiple clients). Finally, you can make the id as large and unpredictable as you like in order to guard against clients spoofing the sidechannel connections of one another.

like image 135
Jon Avatar answered Sep 17 '22 14:09

Jon


It seems what you are seeking is something like server side session management.
A description of architectural road map, implementation in Web Servers could be seen hear.

Stan Kirk have contributed an advanced post called C# SocketAsyncEventArgs High Performance Socket Code , handling UserToken acknowledgment by session-id.

I think combining the implementation of Stan Kirk to create the session-id and storing it in a concurrent-Hash-Map, like ConcurrentDictionary (TKey as session-id, TValue as UserState) will help you.

like image 36
Mohsen Heydari Avatar answered Sep 19 '22 14:09

Mohsen Heydari