/// <summary> /// 触发客户端数据完成事件 /// </summary> public void OnClientCommand(RoadGateFrame frame) { if (ClientCommand != null) { ClientCommand(frame); } }
/// <summary> /// 线程工作函数 /// </summary> protected override void WorkerRun() { try { RoadGateFrameCodec frameCodec = new RoadGateFrameCodec(); bool timeout = false; //using(var timer = new Timer(_=>timeout=true,null,20000,Timeout.Infinite)) //{ while (isRun == true && timeout == false) { RoadGateFrame frame = frameCodec.ReceiveFrame(client); if (frame != null) { System.Console.Write(frameCodec.FrameBuffer); OnClientCommand(frame); } Thread.Sleep(0); } client.Close(); //} } catch (Exception e) { client.Close(); System.Console.Write(e.Message); } }
/// <summary> /// 接收数据帧 /// </summary> /// <param name="socket"></param> /// <returns></returns> public RoadGateFrame ReceiveFrame(Socket socket) { int len = socket.Available; if (len > 0)//接收Socket数据 { if (len + Length > FrameBuffer.Length) { len = FrameBuffer.Length - Length; } socket.Receive(FrameBuffer, len, SocketFlags.None); Length = Length + len; } //缓存区中有数据,判断帧头 if (Length > RoadGateFrame.HeardLength) { if ((FrameBuffer[0] == (byte)RoadGateFrame.Heard && FrameBuffer[1] == (byte)(RoadGateFrame.Heard >> 8)) == false) { Remove(RoadGateFrame.HeardLength);//移除当前帧长 return(null); } } //判断帧数据接收完成 if ((Length > RoadGateFrame.DataLengthIndex) == false) { return(null); } // int datalen = FrameBuffer[RoadGateFrame.DataLengthIndex]; //数据长度 int framelen = RoadGateFrame.HeardLength + RoadGateFrame.DataLengthIndex + datalen + RoadGateFrame.endLength; //帧长度 //帧数据接收完成 if (Length >= framelen) { if (FrameBuffer[framelen - 1] == RoadGateFrame.End)//判断帧尾 { //校验帧数据 if (CheckFrame(FrameBuffer, framelen) == FrameBuffer[framelen - 2]) { RoadGateFrame frmae = Decode(FrameBuffer, framelen); Remove(framelen);//移除当前帧长 return(frmae); } } Remove(framelen);//移除当前帧长 } return(null); }
/// <summary> /// 接收数据处理事件 /// </summary> /// <param name="frame"></param> void RoadGateClient_ClientCommand(RoadGateFrame frame) { switch (frame.cmdtype) { case 0: break; default: break; } }
/// <summary> /// 帧编码 /// </summary> /// <param name="frame"></param> /// <returns></returns> public byte[] Encode(RoadGateFrame frame) { byte[] _byte; using (MemoryStream mem = new MemoryStream()) { BinaryWriter writer = new BinaryWriter(mem); writer.Write(frame.heard); writer.Write(frame.clientid); writer.Write(frame.cmdcode); writer.Write(frame.end); _byte = mem.ToArray(); writer.Close(); } return(EnEscape(_byte, _byte.Length)); }
/// <summary> /// 帧解码 /// </summary> /// <param name="array"></param> /// <returns></returns> public RoadGateFrame Decode(byte[] array, int len) { RoadGateFrame frame = new RoadGateFrame(); byte[] buffer = DeEscape(array, len); using (MemoryStream mem = new MemoryStream(buffer)) { BinaryReader reader = new BinaryReader(mem); frame.heard = reader.ReadUInt16(); frame.length = reader.ReadByte(); frame.clientid = reader.ReadUInt16(); frame.cmdtype = reader.ReadByte(); frame.cmdcode = reader.ReadByte(); frame.datecount = reader.ReadByte(); frame.date = reader.ReadBytes(frame.datecount); frame.check = reader.ReadByte(); frame.end = reader.ReadByte(); reader.Close(); } return(frame); }