There are many things that I would change there. Read this as a preamble .
First of all, whenever you send any data on a socket, you must handle this event: either stop the entire sending process, or perform another send socket operation to send the remaining data.
So, it makes sense to have 3 methods, for example:
1) public void Send(byte[] buffer);
, , , () SocketAsyncEventArgs, :
2) private void Send(SocketAsyncEventArgs e);
Socket.SendAsync(SocketAsyncEventArgs e)
(, ?) SAEA. - , (2) , , . , SAEA.
3) private void ProcessSent(SocketAsyncEventArgs e);
, . , SAEA . , (2) . , SAEA.BytesTransferred. , , ( "this" ).
SocketError SAEA.
:
, Begin/EndXXX ( IAsyncResult). , ( ) StackOverflow .
SocketAsyncEventArgs 1- (byte []) 3- , .
( ) SocketAsyncEventArgs.UserToken , , Socket.SendAsync().
, , , SAEA, Send() Receive(), . , : ( ) .
, 1st Send(), , . , , Send (byte []) " " Socket.SendAsync(), , 1- . , . -, " " , send op - . concurrency .
, , , , . SocketAsyncEventArgs, , .
, , , .
Edit:
,
unchecked
{
_bytesSent += (uint)length;
}
Interlocked.Add(ref _totalBytesSent, length);
ProcessSend (SAEA) args.BytesTransferred "length".