示例#1
0
        public static P2PCommand CreateCommand(string identity, string commandName, int nonce, BasePayload payload)
        {
            var command = new P2PCommand();

            command.Prefix      = DefaultPrefixBytes;
            command.Suffix      = DefaultSuffixBytes;
            command.Identity    = identity;
            command.Nonce       = nonce;
            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);
        }
示例#2
0
        public static P2PCommand 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 identityBytes = new byte[16];
            var commandBytes  = new byte[12];
            var nonceBytes    = new byte[4];
            var sizeBytes     = new byte[4];

            Array.Copy(bytes, 4, identityBytes, 0, 16);
            Array.Copy(bytes, 20, commandBytes, 0, 12);
            Array.Copy(bytes, 32, nonceBytes, 0, 4);
            Array.Copy(bytes, 36, sizeBytes, 0, 4);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(nonceBytes);
                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 nonce       = BitConverter.ToInt32(nonceBytes, 0);
            int size        = BitConverter.ToInt32(sizeBytes, 0);

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

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

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


            var command = new P2PCommand();

            command.Prefix      = prefixBytes;
            command.Identity    = new Guid(identityBytes).ToString();
            command.CommandName = commandName;
            command.Nonce       = nonce;
            command.Size        = size;
            command.Payload     = payloadBytes;
            command.Checksum    = checksumBytes;
            command.Suffix      = suffixBytes;

            return(command);
        }