public void ReceiveDataCallBack(TcpService tcpService, byte[] data) { try { DataPacket dataPacket = null; if (tcpService == TcpService.JGFW) { Console.WriteLine("1"); } switch (tcpService) { case TcpService.SSHQ: case TcpService.LSHQ: case TcpService.WPFW: dataPacket = RealTimeDataPacket.DecodePacket(data, data.Length); break; case TcpService.DPZS: case TcpService.JGFW: case TcpService.GPZS: case TcpService.JGLS: dataPacket = OrgDataPacket.DecodePacket(data, data.Length); break; case TcpService.HQZX: dataPacket = InfoDataPacket.DecodePacket(data, data.Length); break; } if (dataPacket != null && dataPacket.IsResult) { if (DoCMReceiveData != null) //通知界面而已,对于数据的响应在这个类中完成。 { DoCMReceiveData(this, new CMRecvDataEventArgs(tcpService, dataPacket, data.Length)); } } } catch (Exception e) { LogUtilities.LogMessage("ReceiveDataCallBack Error" + e.Message); } }
private int _msLengthOrg; //_ms里的有效长度 ///<summary> /// 网络中接收到数据的事件发生 ///</summary> ///<param name="sender"></param> ///<param name="e"></param> public void RecvOrgData(object sender, RecvDataEventArgs e) { try { while (_packetStartPosOrg < e.Length) { if (_bodyLenOrg == 0) { if ((_packetStartPosOrg + 3) < e.Length) { //获取包体长度 byte[] len = new byte[] { e.Data[_packetStartPosOrg], e.Data[_packetStartPosOrg + 1], e.Data[_packetStartPosOrg + 2], e.Data[_packetStartPosOrg + 3] }; _bodyLenOrg = BitConverter.ToInt32(len, 0); } else { break; } } //如果有一个完整包,解包 if (_packetStartPosOrg + 16 + _bodyLenOrg - _msLengthOrg < e.Length) { _msOrg.Write(e.Data, _packetStartPosOrg, 17 + _bodyLenOrg - _msLengthOrg); _packetStartPosOrg += 17 + _bodyLenOrg - _msLengthOrg; OrgDataPacket dataPacket = OrgDataPacket.DecodePacket(_msOrg.ToArray(), 17 + _bodyLenOrg); _bodyLenOrg = 0; _msOrg.Position = 0; _msLengthOrg = 0; if (dataPacket != null && dataPacket.IsResult) { //(new Thread(ProcessDataPacket)).Start(new ClientRecvDataEventArgs(_ipAddressPort, // dataPacket, e.Length)); } } else { break; } } //有多余的字节,且不是一个完整包,存起来 if (e.Length > _packetStartPosOrg) { _msOrg.Write(e.Data, _packetStartPosOrg, e.Length - _packetStartPosOrg); _msLengthOrg += e.Length - _packetStartPosOrg; } else { _msLengthOrg = 0; } _packetStartPosOrg = 0; } catch (Exception ex) { LogUtilities.LogMessage(ex.Message); } }