Beispiel #1
0
        public override byte[] Encode()
        {
            LoadOptions();
            int length = 0;

            byte[] header = new byte[4 + TokenLength];

            int index = 0;

            header[index++] = (byte)((0x01 << 0x06) | (byte)(Convert.ToByte((int)RequestType) << 0x04) | TokenLength);
            header[index++] = (byte)(int)Method;
            header[index++] = (byte)((MessageId >> 8) & 0x00FF);
            header[index++] = (byte)(MessageId & 0x00FF);

            if (TokenLength > 0)
            {
                Buffer.BlockCopy(Token, 0, header, 4, TokenLength);
            }

            length += header.Length;

            byte[] options = null;

            if (Options.Count > 0)
            {
                OptionBuilder builder = new OptionBuilder(Options.ToArray());
                options = builder.Encode();
                length += options.Length;
            }

            byte[] buffer;
            if (Payload != null)
            {
                length += Payload.Length + 1;
                buffer  = new byte[length];
                Buffer.BlockCopy(header, 0, buffer, 0, header.Length);
                if (options != null)
                {
                    Buffer.BlockCopy(options, 0, buffer, header.Length, options.Length);
                    Buffer.BlockCopy(new byte[] { 0xFF }, 0, buffer, header.Length + options.Length, 1);
                    Buffer.BlockCopy(Payload, 0, buffer, header.Length + options.Length + 1, Payload.Length);
                }
                else
                {
                    Buffer.BlockCopy(new byte[] { 0xFF }, 0, buffer, header.Length, 1);
                    Buffer.BlockCopy(Payload, 0, buffer, header.Length + 1, Payload.Length);
                }
            }
            else
            {
                buffer = new byte[length];
                Buffer.BlockCopy(header, 0, buffer, 0, header.Length);
                if (options != null)
                {
                    Buffer.BlockCopy(options, 0, buffer, header.Length, options.Length);
                }
            }

            return(buffer);
        }
Beispiel #2
0
        public override byte[] Encode()
        {
            LoadOptions();
            int length = 0;

            byte[] header = new byte[4 + this.TokenLength];

            int index = 0;

            header[index++] = (byte)((byte)(0x01 << 0x06) | (byte)(Convert.ToByte((int)ResponseType) << 0x04) | (byte)(this.TokenLength));

            int code = (int)this.Code;

            header[index++] = code < 10 ? (byte)code : (byte)((byte)(Convert.ToByte(Convert.ToString((int)this.ResponseCode).Substring(0, 1)) << 0x05) |
                                                              (byte)(Convert.ToByte(Convert.ToString((int)this.ResponseCode).Substring(1, 2))));

            //header[index++] = (byte)((byte)(Convert.ToByte(Convert.ToString((int)this.ResponseCode).Substring(0, 1)) << 0x05) |
            //                  (byte)(Convert.ToByte(Convert.ToString((int)this.ResponseCode).Substring(1, 2))));
            header[index++] = (byte)((this.MessageId >> 8) & 0x00FF); //MSB
            header[index++] = (byte)(this.MessageId & 0x00FF);        //LSB

            if (this.TokenLength > 0)
            {
                Buffer.BlockCopy(this.Token, 0, header, 4, this.TokenLength);
            }

            length += header.Length;

            byte[] options = null;

            if (this.Options.Count > 0)
            {
                OptionBuilder builder = new OptionBuilder(this.Options.ToArray());
                options = builder.Encode();
                length += options.Length;
            }

            byte[] buffer = null;

            if (this.Payload != null)
            {
                length += this.Payload.Length + 1;
                buffer  = new byte[length];
                Buffer.BlockCopy(header, 0, buffer, 0, header.Length);
                if (options != null)
                {
                    Buffer.BlockCopy(options, 0, buffer, header.Length, options.Length);
                    Buffer.BlockCopy(new byte[] { 0xFF }, 0, buffer, header.Length + options.Length, 1);
                    Buffer.BlockCopy(this.Payload, 0, buffer, header.Length + options.Length + 1, this.Payload.Length);
                }
                else
                {
                    Buffer.BlockCopy(new byte[] { 0xFF }, 0, buffer, header.Length, 1);
                    Buffer.BlockCopy(this.Payload, 0, buffer, header.Length + 1, this.Payload.Length);
                }
            }
            else
            {
                buffer = new byte[length];
                Buffer.BlockCopy(header, 0, buffer, 0, header.Length);
                if (options != null)
                {
                    Buffer.BlockCopy(options, 0, buffer, header.Length, options.Length);
                }
            }

            return(buffer);
        }