private void ReceiveEvent(ITcpClientAsync client, List <byte[]> data) { if (data == null || data.Count == 0) { return; } ThreadPool.QueueUserWorkItem((o) => { List <byte[]> list = o as List <byte[]>; foreach (var buffer in list) { var arr = OnDecrypt(buffer); MsgData msg = MsgData.Deserialize(arr); if (msg != null) { if (msg.Id != 0) { var callModel = this.GetMsgIdCall(msg.Id); if (callModel != null) { this.OnMsgIdCall(msg, callModel); return; } } var call = this.GetCmdCall(msg.Cmd); this.OnMsgCmdCall(msg, call); } } list.Clear(); }, data); }
private void ReceiveEvent(TcpSocketAsync client, byte[] data, int length) { data = OnDecrypt(data); MsgData msg = MsgData.Deserialize(data); if (msg != null) { ThreadPool.QueueUserWorkItem((o) => { MsgData _msg = o as MsgData; if (_msg.Id != 0) { var callModel = this.GetMsgIdCall(_msg.Id); if (callModel != null) { this.OnMsgIdCall(_msg, callModel); return; } } var call = this.GetCmdCall(_msg.Cmd); this.OnMsgCmdCall(_msg, call); }, msg); } }
private void OnMsgCmdCall(MsgData msg, MsgCmdCallModel callInfo) { if (callInfo == null || callInfo.Call == null) { return; } callInfo.Call(this, msg, callInfo.State); callInfo.State = null; }
/// <summary> /// 同步发送数据 /// </summary> /// <param name="cmd"></param> /// <param name="data">消息内容</param> /// <param name="millisecondsTimeout"></param> /// <returns></returns> public virtual MsgData Send <T>(int cmd, T data, int millisecondsTimeout) { MsgData result = null; using (ManualResetEvent manualResetEvent = new ManualResetEvent(false)) { int msgId = this.SendAsync(cmd, data, (client, m, o) => { try { result = m; manualResetEvent.Set(); } catch { } }); if (!manualResetEvent.WaitOne(millisecondsTimeout)) { this.RemoveMsgIdCall(msgId); } } return(result); }
/// <summary> /// 异步发送消息 /// </summary> /// <param name="cmd">需要发送的消息</param> /// <param name="data"></param> /// <param name="call">回调函数</param> /// <param name="state">传入数据</param> /// <returns>msgId</returns> public virtual int SendAsync <T>(int cmd, T data, MsgDataCall call, object state = null) { int msgId = this.GetMsgId(); MsgData msg = new MsgData() { Id = msgId, Cmd = cmd }; if (data != null) { msg.SetData(data); } if (call != null) { this.AddMsgIdCall(msgId, call, state); } var buffer = msg.Serialize(); buffer = OnEncrypt(buffer); this.tcpClient.Send(buffer); return(msgId); }