private void SendAsync(SendingQueue queue) { try { if (queue.Count > 1) { if (_asyncEventArgsSend.Buffer != null) { _asyncEventArgsSend.SetBuffer(null, 0, 0); } _asyncEventArgsSend.BufferList = queue; } else { if (_asyncEventArgsSend.BufferList != null) { _asyncEventArgsSend.BufferList = null; } var item = queue[0]; _asyncEventArgsSend.SetBuffer(item.Array, item.Offset, item.Count); } if (_socket == null) { return; } //Interlocked.Add(ref _statistic.TcpSentCount, queue.Count); //Interlocked.Add(ref SendedCount, queue.Count); if (_socket.SendAsync(_asyncEventArgsSend) == false) { OnSendCompleted(_asyncEventArgsSend); } } catch (Exception ex) { _logger.LogError(ex, "Exception happened in SendAsync"); Close(); } }
public TcpChannel(IChannelOption channelOption, ILogger logger, NetStatistic statistic) : base(channelOption, logger, statistic) { _asyncEventArgsSend = new SocketAsyncEventArgs(); _asyncEventArgsSend.Completed += OnIoCompleted; _asyncEventArgsSend.UserToken = this; _receivedBuffer = new byte[NetPacket.MaxTcpPacketSize]; _asyncEventArgsReceive = new SocketAsyncEventArgs(); _asyncEventArgsReceive.Completed += OnIoCompleted; _asyncEventArgsReceive.UserToken = this; _asyncEventArgsReceive.SetBuffer(_receivedBuffer, 0, _receivedBuffer.Length); _sendedList = new List <NetPacket>(); _sendWaitQueue = new List <NetPacket>(); _sendedBufferList = new SendingQueue(); _receivedSize = 0; }