public virtual IMessage Decode(byte[] bytes) { header.Clear(); header.Decode(bytes, 0); switch (header.GetMessageType()) { case MessageType.Keyboard: keyboard.Clear(); keyboard.Decode(bytes, 0); return(keyboard); case MessageType.Rocker: rocker.Clear(); rocker.Decode(bytes, 0); return(rocker); case MessageType.Gyro: gyro.Clear(); gyro.Decode(bytes, 0); return(gyro); } return(null); }
/// <summary> /// 内部解析 /// </summary> private void OnKcpRead(KChannel channel, byte[] bytes) { //处理连接 if (!this.IsConnected) { //兼容1.3.1旧版本时,改用SimulationRecvConnected //OnConnectedSuccess(); } else { //解析数据 header.Clear(); header.Decode(bytes, 0); if (header.GetMessageType() != MessageType.Msg) { int ts = (SystemTime.LowClientNow() - header.TimeStamp) / 2; Log.Debug("当前延迟:" + ts); if (ts < 0) { ts = 10; } timeStampList.Add((uint)ts); } OnReadEventHandler?.Invoke(bytes); } }
private void dealwithMessage(byte[] bytes, int length, bool isMessageComplelte = false) { if (isMessageComplelte == true) { } else { //var newBuffer = new byte[DecoderBufferSize]; //Array.Copy(bytes, newBuffer, DecoderBufferSize); //Array.Reverse(newBuffer); //int indexOfZero = 0; //for (; indexOfZero< DecoderBufferSize; indexOfZero++) //{ // if (newBuffer[indexOfZero] != 0) // { // break; // } //} //int length = DecoderBufferSize - indexOfZero; //if (length < 14) //{ // throw new Exception("the length of message received is wrong."); //} if (length == 14 && isHeader == true) { //it is a header //if(hasCompleted == true) //{ // hasCompleted = false; // _msg = null; //} isHeader = false; Array.Copy(bytes, messageBytes, length); byte[] messageLengths = new byte[4]; Array.Copy(bytes, 0, messageLengths, 0, 4); Array.Reverse(messageLengths, 0, 4); _messageDataLength = (int)BitConverter.ToUInt32(messageLengths, 0); _msgHeader = MessageHeader.Decode(bytes, 4); if (_euip.DeviceID == 0) { StreamTextBox.Text = _msgHeader.S.ToString(); FunctionTextBox.Text = _msgHeader.F.ToString(); StatusTextBox.AppendText("get msg header for device 0.\n"); } else { Stream2TextBox.Text = _msgHeader.S.ToString(); Function2TextBox.Text = _msgHeader.F.ToString(); StatusTextBox.AppendText("get msg header for device 1.\n"); } } else { isHeader = true; //Array.Copy(bytes, 0, messageBytes,14, length); //_secsDecoder.Buffer = messageBytes; //_secsDecoder.Decode(14 + length); if (_messageDataLength - 10 == length) { //it is the message int index = 0; Item item = decoder.BufferedDecodeItem(bytes, ref index); _msg = new SecsMessage(_msgHeader.S, _msgHeader.F, _msgHeader.ReplyExpected, "", item); if (_euip.DeviceID == 0) { MessageTextBox.Text = _msg.ToString(); MessageTextBox.AppendText("\n"); MessageTextBox.AppendText(_msg.SecsItem.ToString()); StatusTextBox.AppendText("get msg data for device 0.\n"); } else { Message2TextBox.Text = _msg.ToString(); Message2TextBox.AppendText("\n"); Message2TextBox.AppendText(_msg.SecsItem.ToString()); StatusTextBox.AppendText("get msg data for device 1.\n"); } } } } }