As in the simplified code below, is checking that SendAsync() has finished transmitting all intended bytes in the buffer the correct thing to do? Or is that redundant? This is for a TCP connection socket. I am particularly concerned with having to issue a second sub-call to SendAsync() inside ProcessSend().
For example, if I had multiple threads transmitting data to a client using a new (pooled) SocketAsyncEventArg for each message, isn't it possible that a simultaneous send might interject itself in-between a partially transmitted message? Or is this a good reason for controlled access to the client by only allowing one SocketAsyncEventArg to be used in data sending?
private void ProcessSend(SocketAsyncEventArgs e)
{
// Check that data was sent
if ((e.SocketError == SocketError.Success) && (e.BytesTransferred > 0))
{
AsyncSendUserToken token = (AsyncSendUserToken)e.UserToken;
// Check that all the data was sent
int offset = e.Offset + e.BytesTransferred;
if (offset < e.Count)
{
// Call send again to finish the send
e.SetBuffer(offset, e.Count - offset);
bool _willRaiseEvent = token.Socket.SendAsync(e);
if (!_willRaiseEvent)
ProcessSend(e);
}
}
}