public void OnReceiveComplete(ReceiveEventArgs args) { if (args.SocketError != System.Net.Sockets.SocketError.Success || args.BytesTransferred == 0) { //Release back to the pool args.Socket = null; _receivePool.PushBack(args); Close(); } else { // give to terraria args.ReceiveCallback(args.UserToken, args.BytesTransferred); args.Socket = null; _receivePool.PushBack(args); } }
protected void OnReceiveComplete(ReceiveEventArgs arg) { if (arg.SocketError != System.Net.Sockets.SocketError.Success) { //Release back to the pool arg.Socket = null; _receivePool.PushBack(arg); Close(); } else if (arg.BytesTransferred == 0) { //Release back to the pool arg.Socket = null; _receivePool.PushBack(arg); Close(); } else { //receivedBytes += arg.BytesTransferred; //lock (storeBuffer) { //var free = storeBuffer.Length - storeWriteOffset; //if (free < arg.BytesTransferred) //{ // throw new InvalidOperationException("Receive buffer has no room for this segment"); //} //Migrate to the storeBuffer //Array.Copy(receiveBuffer, arg.Offset, storeBuffer, storeWriteOffset, arg.BytesTransferred); //storeWriteOffset += arg.BytesTransferred; lock (_rxSyncRoot) //lock (storeBuffer) { storeWriteOffset += arg.BytesTransferred; var free = arg.Buffer.Length - storeWriteOffset; if (free == 0) { //Move back to the start. This can overwrite data, so make sure terraria reading isn't slow! storeWriteOffset = 0; free = receiveBuffer.Length; reset = true; Console.WriteLine("Reader reset"); } arg.SetBuffer(storeWriteOffset, free); available += arg.BytesTransferred; } } if (!_socket.ReceiveAsync(arg)) { //The receive was processed synchronously which means the callback wont be executed. OnReceiveComplete(arg); } } }