public void SendData(byte[] data) { Socket sk = this.UserToken as Socket; if (data != null) { writeDataBuffer.Push(data); } if ((writeEventArgs.Buffer == null || writeEventArgs.Offset == writeEventArgs.Buffer.Length - 1) && writeDataBuffer.Count > 0) { byte[] sendData = writeDataBuffer.Pop(); writeEventArgs.SetBuffer(sendData, 0, sendData.Length); if (!sk.SendAsync(writeEventArgs)) { this.SendCallBack?.Invoke(writeEventArgs); } } else if ((writeEventArgs.Buffer != null && writeEventArgs.Offset == writeEventArgs.Buffer.Length - 1)) { if (!sk.SendAsync(writeEventArgs)) { this.SendCallBack?.Invoke(writeEventArgs); } } }
// This method is invoked when an asynchronous send operation completes. // The method issues another receive on the socket to read any additional // data sent from the client // // <param name="e"></param> public void ProcessSend(SocketWrite e) { if (e.BytesTransferred != 0 && e.SocketError == SocketError.Success) { Console.WriteLine("成功发送了{0}字节数", e.BytesTransferred); if (e.BytesTransferred == e.Buffer.Length) { e.SetBuffer(null, 0, 0); } else { e.SetBuffer(e.BytesTransferred + e.Offset, e.Buffer.Length - e.BytesTransferred); } e?.ReacSocket?.SendData(null); } else { CloseClientSocket(e.ReacSocket as T); } }