_receiveBuffer is not thread safe when called from OnAsyncCompleted->ProcessReceive->OnReceived
there is no lock on it,so Is there a problem that When i m dealing with the data in OnReceived,then the data changed by other thread?
_receiveBuffer does not require locking, it should not be used in multithread. Please check the whole cycle:
ReceiveAsync() -> TryReceive() -> Socket.ReceiveAsync() -> OnAsyncCompleted() -> ProcessReceive() -> OnReceived() -> TryReceive() -> ...
Thx @chronoxor ,but I m working with TCP async workflow,and as you mentioned,I should call receive or receiveAsync manually only in sync workflow. So I think the correct async workflow entry is from SocketAsyncEventArgs.Complete‘s callback->OnAsyncCompleted->ProcessReceive->OnReceived. Iocp will callback with a random thread in threadpool in theory(also i have tested it).So I still think there will be some problem here.
In sync mode you should call only sync methods - Connect(), Disconnect(), Send(), Receive(). I suggest use sync scenarios only for TCP/SSL client.
For server I suggest considering async processing. As a reference please see async TcpChatServer
TcpServer.Start() will start socket accepting and will call TcpSession.TryReceive() automatically for each connected session.
Iocp will callback with a random thread in threadpool in theory(also i have tested it).So I still think there will be some problem here.
Yes receiving loop will be called on a threadpool, but sequentially, so you'll never get _receiveBuffer be used in several threads at the same time, only one by one.
I see,Thanks for your explaining.