Ejemplo n.º 1
0
        /// <summary>
        /// Функция сбора пакета
        /// </summary>
        /// <param name="lowCommand">Низкоуровневая команда</param>
        /// <param name="data">Данные, усекается до 65529</param>
        /// <returns></returns>
        public static byte[] AssemblyRawPack(LowCommandsRequest lowCommand, params byte[] data)
        {
            int length = 0;

            byte[] pack = null;

            if (data != null)
            {
                int dataLength = 0;
                dataLength = data.Length > 65530 ? 65530 : data.Length;
                length     = 2 + 1 + 1 + dataLength + 1;
                pack       = new byte[length];
                System.Buffer.BlockCopy(data, 0, pack, 4, dataLength);
                pack[pack.Length - 1] = CRC8.ComputeChecksum(4, dataLength, pack);
            }
            else
            {
                length = 2 + 1 + 1;
                pack   = new byte[length];
            }

            System.Buffer.BlockCopy(BitConverter.GetBytes(length), 0, pack, 0, 2);
            pack[2] = (byte)lowCommand;
            pack[3] = CRC8.ComputeChecksum(0, 3, pack);
            return(pack);
        }
Ejemplo n.º 2
0
        private void SystemPacket(byte[] message)
        {
            LowCommandsRequest lCommand = (LowCommandsRequest)message[0];
            bool crc8 = CRC8.ComputeChecksum(0, message.Length - 1, message) == message[message.Length - 1];

            switch (lCommand)
            {
            case LowCommandsRequest.Ping:
                this.Send(Protocol.AssemblyRawPack(LowCommandsRequest.Version));
                break;

            case LowCommandsRequest.Version:
                string version = Encoding.ASCII.GetString(message, 1, message.Length - 2);

                this.Send(Protocol.AssemblyRawPack(LowCommandsRequest.RunType));
                break;

            case LowCommandsRequest.RunType:
                int runType = BitConverter.ToInt32(message, 1);
                break;

            default:
                this.Disconnect();
                break;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Функция сбора пакета
        /// </summary>
        /// <param name="lowCommand">Низкоуровневая команда</param>
        /// <param name="data">Данные, усекается до 65529</param>
        /// <returns></returns>
        private byte[] AssemblyRawPack(LowCommandsRequest lowCommand, byte[] data)
        {
            buffWriter.Clear();

            buffWriter.WriteUInt16((ushort)(2 + 1 + 1 + (data == null ? 0 : data.Length > 65530 ? 65530 : data.Length + 1)));
            buffWriter.WriteUInt8((byte)lowCommand);
            buffWriter.WriteUInt8(CRC8.ComputeChecksum(0, 3, buffWriter.ReadBytes(3)));

            if (data != null)
            {
                buffWriter.WriteBytes(data);
                buffWriter.WriteUInt8(CRC8.ComputeChecksum(0, data.Length, data));
            }

            return(buffWriter.ToByteArray());
        }
Ejemplo n.º 4
0
        private bool SystemPacket()
        {
            LowCommandsRequest lCommand = (LowCommandsRequest)buffReader.ReadUInt8();

            //bool crc8 = CRC8.ComputeChecksum(0, message.Length - 1, message) == message[message.Length - 1];

            switch (lCommand)
            {
            case LowCommandsRequest.Ping:
                if (!IsInitConnection)
                {
                    return(false);
                }
                break;

            case LowCommandsRequest.Version:
                if (IsInitConnection)
                {
                    return(false);
                }

                Version clientProtoVer = new Version(Encoding.ASCII.GetString(buffReader.ReadBytes((int)(buffReader.IncomingBytesUnread - 1))));
                if (this.ProtoVersion != clientProtoVer)
                {
                    return(false);
                }

                pingTimer.Reset();

                IsInitConnection = true;
                break;

            case LowCommandsRequest.RunType:
                int runType = buffReader.ReadInt32();
                break;

            default:
                return(false);
            }

            return(true);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Функция сбора пакета
 /// </summary>
 /// <param name="lowCommand">Низкоуровневая команда</param>
 /// <returns></returns>
 private byte[] AssemblyRawPack(LowCommandsRequest lowCommand)
 {
     return(AssemblyRawPack(lowCommand, null));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Функция сбора пакета
 /// </summary>
 /// <param name="lowCommand">Низкоуровневая команда</param>
 /// <returns></returns>
 public static byte[] AssemblyRawPack(LowCommandsRequest lowCommand)
 {
     return(AssemblyRawPack(lowCommand, null));
 }