R
Ricardo Vazquez
Hello everybody,
I'm programming a TCP server.
During the stress tests I noticed that maybe my socket-receiving thread
became deaf after an hour of heavy stress.
I think that the reason could be this: The processing of the received
messages. Something goes wrong in this processing and the code gets stuck
here. As the processing is within the receiving "while" loop, the loop gets
also stuck, so that we will never reach again the Socket.Receive invoke;
thereafter we are deaf.
Besides I disliked the idea of processing the message within the receiving
loop. Somehow I understand that receving should be more "disconnected" from
the rest of the code.
So I thought of a different thread, asynchronously processing the received
message.
And, as after heavy stress this thread could get stuck as I said, I thought
of using the ThreadPool (even though one processing-thread gets stuck, the
following messages will be processed anyway, because ThreadPool will give us
a new thread to do this).
So, code would be something like this:
------------------------
int nRes = 0;
Byte[] buffer = new Byte[MAX_RECV_SIZE];
try
{
nRes = m_pSocket.Receive(buffer, MAX_RECV_SIZE, SocketFlags.None);
}
catch ...
....
string msg = Encoding.ASCII.GetString(buffer, 0, nRes);
sLog = String.Format("[pbx] CTIServer {0} < recv: '{1}'",
getPbxStringId(), msg);
m_maq.log.logNormal(3, sLog);
try
{
ThreadPool.QueueUserWorkItem(new WaitCallback(processingThread), msg);
}
catch (ApplicationException appEx)
{
sLog = String.Format("[pbx] Exception thrown while creating received
buffer processing thread: " + appEx.Message);
m_maq.log.logWarning(1, sLog);
}
catch (Exception)
{
sLog = String.Format("[pbx] Generic exception thrown while creating
received buffer processing thread");
m_maq.log.logWarning(1, sLog);
}
------------------------
It works just fine, and I think that this way I achieve my goal: receiving
and processing are not coupled.
BUT it is the first time I use the ThreadPool, and I'm quite new to C# (I'm
arriving from C++), so I would like to ask you if you think I'm using
ThreadPool the right way, if you think that this usage could end up with
some other problem or serious inconvenience, and to ask you for hints and
advices.
Thank you!
Ricardo Vázquez.
Madrid, Spain.
I'm programming a TCP server.
During the stress tests I noticed that maybe my socket-receiving thread
became deaf after an hour of heavy stress.
I think that the reason could be this: The processing of the received
messages. Something goes wrong in this processing and the code gets stuck
here. As the processing is within the receiving "while" loop, the loop gets
also stuck, so that we will never reach again the Socket.Receive invoke;
thereafter we are deaf.
Besides I disliked the idea of processing the message within the receiving
loop. Somehow I understand that receving should be more "disconnected" from
the rest of the code.
So I thought of a different thread, asynchronously processing the received
message.
And, as after heavy stress this thread could get stuck as I said, I thought
of using the ThreadPool (even though one processing-thread gets stuck, the
following messages will be processed anyway, because ThreadPool will give us
a new thread to do this).
So, code would be something like this:
------------------------
int nRes = 0;
Byte[] buffer = new Byte[MAX_RECV_SIZE];
try
{
nRes = m_pSocket.Receive(buffer, MAX_RECV_SIZE, SocketFlags.None);
}
catch ...
....
string msg = Encoding.ASCII.GetString(buffer, 0, nRes);
sLog = String.Format("[pbx] CTIServer {0} < recv: '{1}'",
getPbxStringId(), msg);
m_maq.log.logNormal(3, sLog);
try
{
ThreadPool.QueueUserWorkItem(new WaitCallback(processingThread), msg);
}
catch (ApplicationException appEx)
{
sLog = String.Format("[pbx] Exception thrown while creating received
buffer processing thread: " + appEx.Message);
m_maq.log.logWarning(1, sLog);
}
catch (Exception)
{
sLog = String.Format("[pbx] Generic exception thrown while creating
received buffer processing thread");
m_maq.log.logWarning(1, sLog);
}
------------------------
It works just fine, and I think that this way I achieve my goal: receiving
and processing are not coupled.
BUT it is the first time I use the ThreadPool, and I'm quite new to C# (I'm
arriving from C++), so I would like to ask you if you think I'm using
ThreadPool the right way, if you think that this usage could end up with
some other problem or serious inconvenience, and to ask you for hints and
advices.
Thank you!
Ricardo Vázquez.
Madrid, Spain.