Example #1
0
        public static PoolCommand CreateCommand(string commandName, BasePayload payload)
        {
            var command = new PoolCommand();

            command.Prefix      = DefaultPrefixBytes;
            command.Suffix      = DefaultSuffixBytes;
            command.CommandName = commandName;

            if (payload != null)
            {
                var bytes = payload.Serialize();
                command.Payload  = bytes;
                command.Size     = bytes.Length;
                command.Checksum = GetChecksum(bytes);
            }
            else
            {
                var bytes = new byte[0];
                command.Payload  = bytes;
                command.Size     = bytes.Length;
                command.Checksum = GetChecksum(bytes);
            }

            return(command);
        }
Example #2
0
        public static PoolCommand ConvertBytesToMessage(byte[] bytes)
        {
            var prefixBytes = new byte[4];
            var suffixBytes = new byte[4];

            Array.Copy(bytes, 0, prefixBytes, 0, prefixBytes.Length);
            Array.Copy(bytes, bytes.Length - suffixBytes.Length, suffixBytes, 0, suffixBytes.Length);

            if (!BytesEquals(DefaultPrefixBytes, prefixBytes) || !BytesEquals(DefaultSuffixBytes, suffixBytes))
            {
                return(null);
            }

            var commandBytes = new byte[12];
            var sizeBytes    = new byte[4];

            Array.Copy(bytes, 4, commandBytes, 0, 12);
            Array.Copy(bytes, 16, sizeBytes, 0, 4);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(sizeBytes);
            }

            var commandTextBytes = new List <byte>();

            foreach (var b in commandBytes)
            {
                if (b > 0x00)
                {
                    commandTextBytes.Add(b);
                }
                else
                {
                    break;
                }
            }

            var commandName = Encoding.UTF8.GetString(commandTextBytes.ToArray());
            int size        = BitConverter.ToInt32(sizeBytes, 0);

            var payloadBytes  = new byte[size];
            var checksumBytes = new byte[4];

            Array.Copy(bytes, 20, payloadBytes, 0, size);
            Array.Copy(bytes, 20 + size, checksumBytes, 0, 4);

            if (!BytesEquals(checksumBytes, GetChecksum(payloadBytes)))
            {
                return(null);
            }


            var command = new PoolCommand();

            command.Prefix      = prefixBytes;
            command.CommandName = commandName;
            command.Size        = size;
            command.Payload     = payloadBytes;
            command.Checksum    = checksumBytes;
            command.Suffix      = suffixBytes;

            return(command);
        }