/// <summary> /// 异步发送,前一步次发送没完成,不影响后一次发送,强连接使用,比如帧同步战斗数据发送 /// </summary> /// <param name="buffer">发送数据</param> /// <param name="invokeElement">发送完成回调元素</param> public void BeginSend(byte[] buffer, TcpSocketInvokeElement invokeElement) { if (UserToken.TcpSocket == null) { return; } if (!UserToken.TcpSocket.Connected) { invokeElement.SocketErrorInvoke(UserToken, SocketError.Shutdown); return; } TcpSocketSendState sendState = new TcpSocketSendState(UserToken.TcpSocket); sendState.SendInvokeElement = invokeElement; sendState.Buffer = buffer; UserToken.TcpSocket.BeginSend(sendState.Buffer, sendState.CurrentLength, sendState.TargetLength, SocketFlags.None, out sendState.errorCode, new AsyncCallback(EndSend), sendState); }
/// <summary> /// 完成端口的异步发送,前一步次发送没完成前,后一次不能发送,建议发送用这种方法 /// </summary> /// <param name="buffer">发送数据</param> /// <param name="invokeElement">发送完成回调元素</param> /// <returns></returns> public bool SendAsync(byte[] buffer, TcpSocketInvokeElement invokeElement) { if (UserToken.TcpSocket == null || !UserToken.LockSendState()) { return(false); } if (!UserToken.TcpSocket.Connected) { invokeElement.SocketErrorInvoke(UserToken, SocketError.Shutdown); return(false); } UserToken.SendInvokeElement = invokeElement; UserToken.SendEventArgs.SetBuffer(buffer, 0, buffer.Length); bool willRaiseEvent = UserToken.TcpSocket.SendAsync(UserToken.SendEventArgs); if (!willRaiseEvent) { ProcessSend(UserToken.SendEventArgs); } return(true); }