H
Hoov
I am writing (in C#) a transaction processing middle-tier server that
needs to handle 2000+ concurrent connections. The server receives a
connection and routes the connection to an appropriate upstream
server. After routing is done, this mid-tier server simply acts as a
pass-though between the client and the upstream server. The
communication protocol is well defined and non-variable.
The pseudo code for each connection looks like this:
Read routing data from client.
Open connection to upstream server.
Send primer data to server.
Read message length from client.
Read message payload from client.
Send length to upstream server.
Send payload to upstream server.
Read response length from upstream server.
Read response message from upstream server.
Terminate upstream server connection.
Send response length to client.
Send response payload to client.
Terminate client connection.
I have already figured out that Asynchronous sockets should be used
for this implementation as apposed to a multi-threaded, blocking
socket implementation.
I started writing this and came up with a question: After the initial
Stream.BeginRead, why shouldn't I use blocking calls once my initial
AsyncCallback has been entered? Are there any advantages to doing
"chained" Asynchronous calls?
Ex: Chained callbacks
Socket.BeginRead(state.buffer, 0, kRoutingDataSize,
ReadRoutingCallback);
Void ReadRoutingCallback(IAsyncResult ar)
{
int iBytesRead = ar.state.stream.EndRead(ar);
//Processing code
int iPayloadLen = GetLenFromBuffer(ar.AsyncState);
ar.state.stream.BeginRead(state.buffer, 0, iPayloadLen,
ReadPayloadCallback, state);
}
Etc, etc.
Ex: Blocking in initial callback
Socket.BeginRead(state.buffer, 0, kRoutingDataSize,
ReadRoutingCallback);
Void ReadRoutingCallback(IAsyncResult ar)
{
int iBytesRead = ar.state.stream.EndRead(ar);
//Processing code
int iPayloadLen = GetLenFromBuffer(ar.AsyncState);
ar.state.stream.Read(state.buffer, 0, iPayloadLen,
ReadPayloadCallback, state);
//Continue complete message processing using blocking calls
}
Etc, etc.
Processing time is very important in this implementation. Why not use
blocking calls in the initial callback?
Are Async socket calls always faster?
Any opinions/suggestions are very much appreciated.
Thanks,
John
needs to handle 2000+ concurrent connections. The server receives a
connection and routes the connection to an appropriate upstream
server. After routing is done, this mid-tier server simply acts as a
pass-though between the client and the upstream server. The
communication protocol is well defined and non-variable.
The pseudo code for each connection looks like this:
Read routing data from client.
Open connection to upstream server.
Send primer data to server.
Read message length from client.
Read message payload from client.
Send length to upstream server.
Send payload to upstream server.
Read response length from upstream server.
Read response message from upstream server.
Terminate upstream server connection.
Send response length to client.
Send response payload to client.
Terminate client connection.
I have already figured out that Asynchronous sockets should be used
for this implementation as apposed to a multi-threaded, blocking
socket implementation.
I started writing this and came up with a question: After the initial
Stream.BeginRead, why shouldn't I use blocking calls once my initial
AsyncCallback has been entered? Are there any advantages to doing
"chained" Asynchronous calls?
Ex: Chained callbacks
Socket.BeginRead(state.buffer, 0, kRoutingDataSize,
ReadRoutingCallback);
Void ReadRoutingCallback(IAsyncResult ar)
{
int iBytesRead = ar.state.stream.EndRead(ar);
//Processing code
int iPayloadLen = GetLenFromBuffer(ar.AsyncState);
ar.state.stream.BeginRead(state.buffer, 0, iPayloadLen,
ReadPayloadCallback, state);
}
Etc, etc.
Ex: Blocking in initial callback
Socket.BeginRead(state.buffer, 0, kRoutingDataSize,
ReadRoutingCallback);
Void ReadRoutingCallback(IAsyncResult ar)
{
int iBytesRead = ar.state.stream.EndRead(ar);
//Processing code
int iPayloadLen = GetLenFromBuffer(ar.AsyncState);
ar.state.stream.Read(state.buffer, 0, iPayloadLen,
ReadPayloadCallback, state);
//Continue complete message processing using blocking calls
}
Etc, etc.
Processing time is very important in this implementation. Why not use
blocking calls in the initial callback?
Are Async socket calls always faster?
Any opinions/suggestions are very much appreciated.
Thanks,
John