public override void Parse(CodeyPacket packet) { Debug.Assert(packet.Body[0] == (int)CodeyPacketType.Variable); int index = ParseName(packet.Body); List <byte> byteList = new List <byte>(); //获取变量标识,区分是数值还是字符串 ValueType = (VariableType)packet.Body[index++]; // 从后续的标识开始 for (; index < packet.Body.Length; index++) { byteList.Add(packet.Body[index]); } switch (ValueType) { case VariableType.Int32: // littleendian 整形数 Value = BitConverter.ToInt32(byteList.ToArray(), 0).ToString(); break; case VariableType.String: // 字符串 Value = Encoding.UTF8.GetString(byteList.ToArray()); break; default: Debug.Assert(false); break; } }
public static ICodeyShareable Generate(CodeyPacket packet) { ICodeyShareable ret = null; switch ((CodeyPacketType)packet.Body[0]) { case CodeyPacketType.Variable: ret = new SharedVariable(); break; case CodeyPacketType.Message: ret = new BroadcastMessage(); break; case CodeyPacketType.Heartbeat: ret = new Heartbeat(); break; default: Debug.Assert(false); break; } ret.Parse(packet); return(ret); }
/// <summary> /// 发送变量/消息统一接口 /// </summary> /// <param name="var"></param> public void Send(ICodeyShareable var) { CodeyPacket packet = new CodeyPacket { Body = var.ToArray() }; WriteDataAsync(CodeyProtocolSerializer.Serialize(packet)); }
public void Parse(CodeyPacket packet) { Debug.Assert(packet.Body[0] == (int)CodeyPacketType.Heartbeat); List <byte> byteList = new List <byte>(); // 从后续的标识开始 for (int index = 1; index < packet.Body.Length; index++) { byteList.Add(packet.Body[index]); } Code = byteList.ToArray(); }
public static byte[] Serialize(CodeyPacket packet) { List <byte> byteList = new List <byte>(); packet.Len = (byte)packet.Body.Length; packet.LenCheck = (byte)(packet.Len + CodeyPacket.Header); packet.CheckSum = (byte)packet.Body.Sum(s => s); byteList.Add(CodeyPacket.Header); byteList.Add(packet.LenCheck); byteList.Add(packet.Len); byteList.Add(0x00); //长度占位 byteList.AddRange(packet.Body); byteList.Add(packet.CheckSum); byteList.Add(CodeyPacket.EndCode); return(byteList.ToArray()); }
public abstract void Parse(CodeyPacket packet);
/// <summary> /// 不断的放入数据,如果数据可以凑成一个句子,就返回true /// </summary> /// <returns></returns> public bool PushData(byte data) { var ret = false; switch (_state) { case State.None: if (data == CodeyPacket.Header) { _state = State.StartFirst; _packet = new CodeyPacket(); } break; case State.StartFirst: _state = State.LenCheck; _packet.LenCheck = data; break; case State.LenCheck: _state = State.Len; _packet.Len = data; if ((byte)(_packet.Len + CodeyPacket.Header) == _packet.LenCheck) { _packet.Body = new byte[_packet.Len]; _state = State.Len; _dateCnt = 0; } else { _state = State.None; } break; case State.Len: // 这部分应该是长度的一部分 _state = State.Body; break; case State.Body: _packet.Body[_dateCnt] = data; if (_dateCnt >= _packet.Len - 1) { _state = State.CheckSum; } _dateCnt++; break; case State.CheckSum: if (data == CheckSum(_packet.Body)) { _packet.CheckSum = data; _state = State.End; } else { _state = State.None; } break; case State.End: if (data == CodeyPacket.EndCode) { ret = true; } _state = State.None; break; } return(ret); }
public override void Parse(CodeyPacket packet) { Debug.Assert(packet.Body[0] == (int)CodeyPacketType.Message); ParseName(packet.Body); }