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 _msLengthRealTime; //_ms里的有效长度 ///<summary> /// 网络中接收到数据的事件发生 ///</summary> ///<param name="sender"></param> ///<param name="e"></param> public void RecvRealTimeData(object sender, RecvDataEventArgs e) { try { while (_packetStartPosRealTime < e.Length) { if (_bodyLenRealTime == 0) { if ((_packetStartPosRealTime + 3) < e.Length) { //获取包体长度 byte[] len = new byte[] { e.Data[_packetStartPosRealTime], e.Data[_packetStartPosRealTime + 1], e.Data[_packetStartPosRealTime + 2], e.Data[_packetStartPosRealTime + 3] }; _bodyLenRealTime = BitConverter.ToInt32(len, 0); } else { break; } } //如果有一个完整包,解包 if (_packetStartPosRealTime + 16 + _bodyLenRealTime - _msLengthRealTime < e.Length) { _msRealTime.Write(e.Data, _packetStartPosRealTime, 17 + _bodyLenRealTime - _msLengthRealTime); _packetStartPosRealTime += 17 + _bodyLenRealTime - _msLengthRealTime; RealTimeDataPacket dataPacket = RealTimeDataPacket.DecodePacket(_msRealTime.ToArray(), 17 + _bodyLenRealTime); _bodyLenRealTime = 0; _msRealTime.Position = 0; _msLengthRealTime = 0; if (dataPacket != null && (dataPacket.IsResult || dataPacket.RequestType == FuncTypeRealTime.Init)) { //(new Thread(ProcessDataPacket)).Start(new CMRecvDataEventArgs(_ipAddressPort, // dataPacket, e.Length)); } } else { break; } } //有多余的字节,且不是一个完整包,存起来 if (e.Length > _packetStartPosRealTime) { _msRealTime.Write(e.Data, _packetStartPosRealTime, e.Length - _packetStartPosRealTime); _msLengthRealTime += e.Length - _packetStartPosRealTime; } else { _msLengthRealTime = 0; } _packetStartPosRealTime = 0; } catch (Exception ex) { LogUtilities.LogMessage(ex.Message); } }