private void StartSend(SocketAsyncEventArgs e, byte[] buffer) { AsyncUserToken token = (AsyncUserToken)e.UserToken; if (token != null && token.Socket.Connected) { var sendSocketArgs = token.SendSocket; sendSocketArgs.AcceptSocket = e.AcceptSocket; sendSocketArgs.SetBuffer(buffer, 0, buffer.Length); bool willRaiseEvent = sendSocketArgs.AcceptSocket.SendAsync(sendSocketArgs); if (!willRaiseEvent) { ProcessSend(sendSocketArgs); } } }
protected override void CloseSocket(SocketAsyncEventArgs e) { AsyncUserToken token = e.UserToken as AsyncUserToken; // close the socket associated with the client try { token.Socket.Shutdown(SocketShutdown.Send); } // throws if client process has already closed catch (Exception err) { Console.WriteLine($"Exception:{err.Message}"); } token.Socket?.Close(); // Free the SocketAsyncEventArg so they can be reused by another client FreeSocketAsyncEventArgsToPool(e); Console.WriteLine("Connection break"); }
/// <summary> /// 从buffer池中读取消息 即拆包 /// </summary> /// <returns>剩余待处理字节数</returns> /// <param name="bufferPool">Buffer pool.</param> /// <param name="connection">Connection.</param> /// <param name="remainLength">Remain length.</param> public int ReadBufferFromPool(byte[] bufferPool, AsyncUserToken connection, int remainLength) { if (connection.BytesOfDoneHead < 4) { // 读取消息头:若当前消息头未读取完整则继续读取 var remainHeadLength = 4 - connection.BytesOfDoneHead; if (remainLength - remainHeadLength >= 0) { for (var i = 0; i < remainHeadLength; ++i) { connection.MessageHead[connection.BytesOfDoneHead + i] = bufferPool[connection.OffsetInBufferPool + connection.SkipBufferBytes + i]; } remainLength -= remainHeadLength; connection.BytesOfDoneHead += remainHeadLength; connection.SkipBufferBytes += remainHeadLength; connection.MessageLength = BitConverter.ToInt32(connection.MessageHead); } else { for (var i = 0; i < remainLength; ++i) { connection.MessageHead[connection.BytesOfDoneHead + i] = bufferPool[connection.OffsetInBufferPool + connection.SkipBufferBytes + i]; } remainLength = 0; connection.BytesOfDoneHead += remainHeadLength; connection.SkipBufferBytes = 0; } } if (connection.MessageLength > MaxProtocolLengthLimit || connection.MessageLength <= 0) { connection.Clear(); Console.WriteLine($"ERROR: protocol lengh over limit, length:{connection.MessageLength}"); } // 读取消息体:若当前剩余buffer长度大于待读取的消息长度 则读取的消息长度为 待读取的消息长度 if (connection.BytesOfDoneBody == 0) { connection.MessageBody = new byte[connection.MessageLength]; } var waitDoneBytes = connection.MessageLength - connection.BytesOfDoneBody; if (remainLength >= waitDoneBytes) { Array.Copy(bufferPool, connection.OffsetInBufferPool + connection.SkipBufferBytes , connection.MessageBody, connection.BytesOfDoneBody, connection.MessageLength - connection.BytesOfDoneBody); connection.BytesOfDoneBody = connection.MessageLength; connection.SkipBufferBytes += waitDoneBytes; remainLength -= waitDoneBytes; } else { Array.Copy(bufferPool, connection.OffsetInBufferPool + connection.SkipBufferBytes , connection.MessageBody, connection.BytesOfDoneBody, remainLength); connection.BytesOfDoneBody += remainLength; remainLength = 0; } // 处理读取到到消息 if (connection.MessageLength == connection.BytesOfDoneBody && connection.MessageLength != 0) { MessageQueue.Enqueue(DeserializeMessage(connection.MessageBody, connection.Guid)); if (remainLength == 0) { connection.SkipBufferBytes = 0; } connection.MessageLength = 0; connection.BytesOfDoneBody = 0; connection.BytesOfDoneHead = 0; } else { connection.SkipBufferBytes = 0; } return(remainLength); }