Exemple #1
0
        private void HandleConnection(Socket socket)
        {
            var peer = new TCPPeer(socket);

            // this initial message is not only used to fetch chains but also to verify identity of peers
            var requestKind = RequestKind.Chains | RequestKind.Peers;

            if (Capabilities.HasFlag(PeerCaps.Mempool))
            {
                requestKind |= RequestKind.Mempool;
            }

            var request = new RequestMessage(this.Address, this.PublicEndpoint, requestKind, Nexus.Name);
            var active  = SendMessage(peer, request);

            string ip = ((IPEndPoint)(socket.RemoteEndPoint)).Address.ToString();

            Logger.Debug($"Incoming connection from " + ip);

            while (active)
            {
                var msg = peer.Receive();
                if (msg == null)
                {
                    break;
                }

                Logger.Debug($"Got {msg.GetType().Name} from: {msg.Address.Text}");
                foreach (var line in msg.GetDescription())
                {
                    Logger.Debug(line);
                }

                var answer = HandleMessage(peer, msg);
                if (answer != null)
                {
                    if (!SendMessage(peer, answer))
                    {
                        break;
                    }
                }
            }

            Logger.Debug("Disconnected from peer: " + peer.Endpoint);
            lock (_peers)
            {
                var entry = new EndpointEntry(peer.Endpoint);
                _knownEndpoints.Remove(entry);
                Logger.Debug("removed endpoint: " + entry.endpoint);

                var peerKey = peer.Endpoint.ToString();
                if (_peers.ContainsKey(peerKey))
                {
                    _peers.Remove(peerKey);
                    Logger.Message("Removed peer: " + peerKey);
                }
            }

            socket.Close();
        }
Exemple #2
0
        private void HandleConnection(Socket socket)
        {
            var peer = new TCPPeer(socket);

            lock (_peers)
            {
                Logger.Debug("add Peer: " + peer.Endpoint);
                _peers.Add(peer);
            }

            // this initial message is not only used to fetch chains but also to verify identity of peers
            var requestKind = RequestKind.Chains | RequestKind.Peers;

            if (Capabilities.HasFlag(PeerCaps.Mempool))
            {
                requestKind |= RequestKind.Mempool;
            }

            var request = new RequestMessage(requestKind, Nexus.Name, this.Address);
            var active  = SendMessage(peer, request);

            while (active)
            {
                var msg = peer.Receive();
                if (msg == null)
                {
                    break;
                }

                Logger.Debug($"Got {msg.GetType().Name} from: {msg.Address.Text}");
                foreach (var line in msg.GetDescription())
                {
                    Logger.Debug(line);
                }

                var answer = HandleMessage(peer, msg);
                if (answer != null)
                {
                    if (!SendMessage(peer, answer))
                    {
                        break;
                    }
                }
            }

            Logger.Debug("Disconnected from peer2: " + peer.Endpoint);
            lock (_peers)
            {
                var endpoint = new EndpointEntry(peer.Endpoint);
                _knownEndpoints.Remove(endpoint);
                Logger.Debug("endpoint removed ");
                _peers.Remove(peer);
                Logger.Debug("peer removed");
            }

            socket.Close();
        }