public void DetectPacket(byte dataByte) { // Recognize header fields and change status if (_status == StreamsStatus.UNSYNC && dataByte == 0x02) { _status = StreamsStatus.FIRST_HEADER_RECEIVED; _completePacket = new List<byte>(); _completePacket.Add(dataByte); } else if (_status == StreamsStatus.FIRST_HEADER_RECEIVED && dataByte == 0x10 || _status == StreamsStatus.RECEIVING_DATA && dataByte == 0x10) { _status = StreamsStatus.FIRST_END_RECEIVED; _completePacket.Add(dataByte); } else if (_status == StreamsStatus.FIRST_END_RECEIVED && dataByte == 0x03) { _status = StreamsStatus.SECOND_END_RECEIVED; _completePacket.Add(dataByte); List<Byte> _temp = new List<Byte>(_completePacket.Count - 3); _temp.AddRange(_completePacket.GetRange(1, _completePacket.Count - 3)); CompletePacketReceivedArgs args = new CompletePacketReceivedArgs(_temp); if (CompletePacketReceived != null) CompletePacketReceived(this, args); _status = StreamsStatus.HOLE_PACKET_RECEIVED; resetStream(); } else if (_status == StreamsStatus.FIRST_END_RECEIVED && dataByte == 0x10) { _status = StreamsStatus.RECEIVING_DATA; } else { switch (_status) { case StreamsStatus.FIRST_HEADER_RECEIVED: _completePacket.Add(dataByte); _status = StreamsStatus.RECEIVING_DATA; break; case StreamsStatus.RECEIVING_DATA: _completePacket.Add(dataByte); break; default: resetStream(); break; } } }
public PacketHandler() { _status = StreamsStatus.UNSYNC; }
/// <summary> /// Method that resets the stream status /// </summary> private void resetStream() { if (_status != StreamsStatus.HOLE_PACKET_RECEIVED) { if (ByteOutOfSync != null) ByteOutOfSync(this, new EventArgs()); } _status = StreamsStatus.UNSYNC; _completePacket = null; }