Socket communication layer as a DLL - how?

R

Roodie

Hello,

I have a quite specific problem, and after half a day spent with
Google, still no solid answer.

The task:

I have to write a C# class Library ( DLL ) to handle TCP socket
communication. It will be called by a multithreaded server application
to send some data to an another server ( which is a linux server ).

There was already a very old ( unmanaged ) C++ dll, which used win32
sockets to do the communication, and I am required to replace it with a
managed C# DLL. The only information I have from this C++ DLL is the
data structures and response / error codes, and the function names and
parameters.

The question is - how?

My first idea was a static class with a single TcpClient to send the
data. Is this a viable method? Or I have to use asynchronous socket
functions? Or a spearate working thread with a Queue implented?

All the examples I found were:
- multithreaded SERVERS
- simple applications, not thread-safe class libraries

The problem with the current C++ comm layer DLL is that it gets struck
with more than 50 concurrent users.

Any help appreciated, thanks!

--
 
W

William Stacey [MVP]

From each server thread (i.e. user connection), will it be a single
request/response pattern then back to the client thread? Could you expand
the server requirements a bit? TIA
 
G

Guest

If the class is going to be used by multiple threads making calls on it
simultaneously, you should consider using asynchronous methods. This means
that every time a send request comes in, it gets spun off on a threadpool
thread which gets freed up as soon as the work in the callback method is
complete.
if you still have issues, then you may need a custom threadpool because of
the default number of threads avaialable in the .NET threadpool (this may
have changed in .NET 2.0) One that I've used with great success is "Smart
Threadpool" by Ami Bar, you can find it on codeproject.com.
Peter
 
R

Roodie

Hello,
From each server thread (i.e. user connection), will it be a single
request/response pattern then back to the client thread? Could you
expand the server requirements a bit? TIA

Finally I was able to get some more information about the project.

The first - fix - part is a server written in visual basic.net. What it
does: using async methods, it receives requests from a mobile
communication device, parses the messages, validates them, and send
them to an another server ( using an unmanaged DLL currently), waits
for the response, and then sends back the info to the mobile comm
device.

My part is replacing the unmanaged c++ communcation layer with a
managed C# one.

My current plan is to implement a queue in this DLL: when the VB
application calls the sendMessage method, it will put that message into
a queue, and a separate working thread will regularly check that queue,
and if there are any messages there, it will send them ( using a socket
connection ), and it will put the results into an another queue.

The interface is fixed, I HAVE TO work with the following functions (
the VB application uses these ):

CreateConnection( server_ip, server_port) // connects to the server
CloseConnection( connection_handler ) // disconnects
Send( connection_handler, message ) // sends a command
Listen( timeout_in_secs ) // waits for the reply
GetQueuedMessageCount( connection_handler )
ReadNextMessage( connection_handler )

Is my plan feasible? OR did I miss something? Unfortunately I have not
too much experience creating these kind of communication layers.

Thanks for any suggestions or comments!

--
 
W

William Stacey [MVP]

So you want to multiplex 1 socket connection (using the dll) to the other
server? So there will be 1 worker thread in the dll. Just confirming.
Also, are those methods static? This communication object will be a
singleton?

--
William Stacey [MVP]

| Hello,
|
| William Stacey [MVP] wrote:
| > From each server thread (i.e. user connection), will it be a single
| > request/response pattern then back to the client thread? Could you
| > expand the server requirements a bit? TIA
|
| Finally I was able to get some more information about the project.
|
| The first - fix - part is a server written in visual basic.net. What it
| does: using async methods, it receives requests from a mobile
| communication device, parses the messages, validates them, and send
| them to an another server ( using an unmanaged DLL currently), waits
| for the response, and then sends back the info to the mobile comm
| device.
|
| My part is replacing the unmanaged c++ communcation layer with a
| managed C# one.
|
| My current plan is to implement a queue in this DLL: when the VB
| application calls the sendMessage method, it will put that message into
| a queue, and a separate working thread will regularly check that queue,
| and if there are any messages there, it will send them ( using a socket
| connection ), and it will put the results into an another queue.
|
| The interface is fixed, I HAVE TO work with the following functions (
| the VB application uses these ):
|
| CreateConnection( server_ip, server_port) // connects to the server
| CloseConnection( connection_handler ) // disconnects
| Send( connection_handler, message ) // sends a command
| Listen( timeout_in_secs ) // waits for the reply
| GetQueuedMessageCount( connection_handler )
| ReadNextMessage( connection_handler )
|
| Is my plan feasible? OR did I miss something? Unfortunately I have not
| too much experience creating these kind of communication layers.
|
| Thanks for any suggestions or comments!
|
| --
|
 
R

Roodie

Hello,
So you want to multiplex 1 socket connection (using the dll) to the
other server? So there will be 1 worker thread in the dll.
Yes, that's the current plan.
confirming. Also, are those methods static? This communication
object will be a singleton?
Currently - in the old C++ version of the DLL - the methods are noot
static. The comm object will be a singleton, yes - or it is how I
planned so far.
 
R

Roodie

William said:
So you want to multiplex 1 socket connection (using the dll) to the
other server? So there will be 1 worker thread in the dll. Just
confirming. Also, are those methods static? This communication
object will be a singleton?
One more thing - nothing is carved into stone :) I am totally
inexperienced with network programming, thats why I am asking for
opinions and help!
So if you think that I should use a different approach, just tell me (
and explain of course :) )
 
W

William Stacey [MVP]

I might keep it straight forward and simple as possible especially as this
may be your first network program. I am thinking:
1) Use a lock(). That "queues" up other caller threads.
2) Inside the lock, do your write and read to/from the server using the
single open socket.
3) release lock and the next thread goes.

public class Client
{
private object syncLock = new object();
//...
public string Send(string msg)
{
lock(syncLock)
{
byte[] buf = ...
s.Send(buf, 0, buf.length);
buf = Receive();
// return the string.
}
}
}

When you get this working, you can then decide if you need to get into
queues and/or async methods.

--
William Stacey [MVP]

| William Stacey [MVP] wrote:
|
| > So you want to multiplex 1 socket connection (using the dll) to the
| > other server? So there will be 1 worker thread in the dll. Just
| > confirming. Also, are those methods static? This communication
| > object will be a singleton?
| One more thing - nothing is carved into stone :) I am totally
| inexperienced with network programming, thats why I am asking for
| opinions and help!
| So if you think that I should use a different approach, just tell me (
| and explain of course :) )
|
| --
| Roodie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top