예제 #1
0
파일: Protocol.cs 프로젝트: gonznak/Socket
        public void HandleMessage(byte[] packet, int length)
        {
            pingTimer.Reset();

            if (length < 2 + 2 || true)
            {
                //Disconnected();
                return;
            }

            int dataLength = BitConverter.ToUInt16(packet, 0);
            LowCommandsResponse command = (LowCommandsResponse)packet[2];
            bool crc8 = CRC8.ComputeChecksum(0, 3, packet) == packet[3];

            byte[] message = new byte[dataLength - 4];

            /*
             * 0-1	- data length
             * 2		- LowCommandsRequest
             * 3		- crc8 (0-2)
             * N		- LowCommands
             * D		- data
             * N+D+1	- data crc8
             */

            if (crc8 == false)
            {
                Disconnect();
                return;
            }

            switch (command)
            {
            case LowCommandsResponse.System:
                Array.Copy(packet, 4, message, 0, message.Length);
                this.SystemPacket(message);
                break;

            case LowCommandsResponse.Data:
            { }
            break;

            case LowCommandsResponse.DataBroken:
            { }
            break;

            case LowCommandsResponse.ErrorCRC:
            { }
            break;

            case LowCommandsResponse.NotSupported:
            { }
            break;

            default:
            { }
            break;
            }
        }
예제 #2
0
        public bool HandleMessage(byte[] packet, int length)
        {
            /*
             *      0-1	- data length
             *      2		- LowCommandsRequest
             *      3		- crc8 (0-2)
             *      N		- LowCommands
             *      D		- data
             *      N+D+1	- data crc8
             */

            buffReader.SetBuffer(false, packet, length);

            if (buffReader.IncomingBytes < 4)
            {
                return(false);
            }

            int fullLength = buffReader.ReadUInt16();

            if (fullLength != buffReader.IncomingBytes)
            {
                return(false);
            }

            byte defCommand = buffReader.ReadUInt8();

            if (!Enum.IsDefined(typeof(LowCommandsResponse), defCommand))
            {
                return(false);
            }

            LowCommandsResponse command = (LowCommandsResponse)defCommand;

            buffReader.SetPosition(true, 0);
            if (CRC8.ComputeChecksum(0, 3, buffReader.ReadBytes(3)) != buffReader.ReadUInt8())
            {
                return(false);
            }

            switch (command)
            {
            case LowCommandsResponse.System:
                return(this.SystemPacket());

            case LowCommandsResponse.Data:
                break;

            case LowCommandsResponse.DataBroken:
                break;

            case LowCommandsResponse.ErrorCRC:
                break;

            case LowCommandsResponse.NotSupported:
                break;

            default:
                break;
            }

            return(true);
        }