예제 #1
0
        private void HandleHandshake()
        {
            char[] protocolIdentifier = _reader.ReadChars(_reader.ReadByte());
            Debug($"Remote Protocol: {new string(protocolIdentifier)}");
            if (!ProtocolIdentifier.SequenceEqual(protocolIdentifier))
            {
                Debug("Invalid remote protocol");
                Disconnect();
                return;
            }

            byte[] extensionBytes = _reader.ReadBytes(8);
            if (!extensionBytes.SequenceEqual(new byte[8]))
            {
                Debug("Remote client has extensions");
            }

            byte[] torrentHash = _reader.ReadBytes(20);
            Debug($"Remote Torrent Hash: {Encoding.UTF8.GetString(torrentHash)}");
            if (_initiator)
            {
                if (!_torrent.InfoHashBytes.SequenceEqual(torrentHash))
                {
                    Debug("Invalid remote torrent hash");
                    Disconnect();
                    return;
                }
            }
            else
            {
                // TODO: Check we have this torrent
                throw new NotImplementedException("Unable to handle incoming requests");
            }

            if (!_initiator)
            {
                SendHandshake();
            }

            string id = Encoding.UTF8.GetString(_reader.ReadBytes(20));

            Debug($"Peer Id: {id}");
            // TODO: Check for banned peers
            PeerId = id;

            if (_initiator)
            {
                _writer.Write(_client.PeerId.ToCharArray());
            }
        }
예제 #2
0
 public static string Parse(this BeBinaryReader br, out string b, int maxLength)
 {
     b = new string(br.ReadChars(maxLength)).Replace("\0", "");
     return(b);
 }