Пример #1
0
        private static void Run(int port, int maxConnections, int statusInterval, string logFile)
        {
            Console.WriteLine("Note: Press <CTRL-C> to stop server.");

            // Create and run application, which does all the real work.
            _app = new Application(
                port, maxConnections, statusInterval,
                Path.Combine(Directory.GetCurrentDirectory(), logFile), nodeListManager);
            _app.Run(TerminateCommandReceived);

            Console.CancelKeyPress += delegate {
                Console.WriteLine("<CTRL-C> received.");
                StopServer();
                _exitSignal.Set();
            };


            //Get peers from a DNS
            var ipAddresses = Dns.GetHostAddresses("protochain.org");

            foreach (var peerIPAddress in ipAddresses)
            {
                try
                {
                    Console.WriteLine($"Trying to connect to peer with IP: '{peerIPAddress.ToString()}'");
                    var peer = new Peer(peerIPAddress);

                    peer.Connect();
                    var nodeList = peer.GetNodeList();
                    nodeListManager.SyncNodeList(nodeList);
                    Console.WriteLine(nodeListManager.ToString());

                    Console.WriteLine($"Finished syncing with peer: '{peerIPAddress.ToString()}'");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"An error occurred while trying to connect to peer with IP: '{peerIPAddress.ToString()}'");
                }
            }

            // Block on exit signal to keep process running until exit event encountered
            _exitSignal.Wait();
        }
Пример #2
0
        public void Handle(NodeListManager nodeListManager)
        {
            int bytesRead;

            // Read 4 byte header
            var buffer = new byte[_headerSize];

            while ((bytesRead = TryReadChunk(buffer)) == _headerSize)
            {
                string commandText = Encoding.UTF8.GetString(buffer);
                Console.WriteLine($"Command '{commandText}' received");
                switch (commandText)
                {
                case "GNDS":
                    var bytesToSend = nodeListManager.GetNodeList().ToByteArray();
                    //send payload length
                    _socketConnection.Send(BitConverter.GetBytes(bytesToSend.Length));
                    _socketConnection.Send(bytesToSend);
                    _socketConnection.Send(endBytes);
                    Console.WriteLine($"Sent nodelist.");

                    //add this requester to my node list
                    IPEndPoint remoteIpEndPoint = _socketConnection.RemoteEndPoint as IPEndPoint;
                    if (remoteIpEndPoint != null)
                    {
                        nodeListManager.AddNodeToList(remoteIpEndPoint.Address);
                    }

                    Console.WriteLine(nodeListManager.ToString());

                    break;

                default:
                    break;
                }
            }
        }