Пример #1
0
        public void Connect(string host, ServiceFlags serviceFlag)
        {
            if (string.IsNullOrWhiteSpace(host))
            {
                throw new ArgumentNullException(nameof(host));
            }

            var       iid   = Interop.Constants.InterfaceId;
            var       port  = PortsHelper.GetPort(_network);
            IPAddress ipAdr = null;

            if (!IPAddress.TryParse(host, out ipAdr))
            {
                // TODO : Throw an exception.
            }

            var adrBytes = ipAdr.MapToIPv6().GetAddressBytes();

            _serviceFlag      = serviceFlag;
            _currentIpAddress = new IpAddress(serviceFlag, adrBytes, ushort.Parse(port));
            // _client = new RpcClientApi(iid, RpcProtseq.ncacn_ip_tcp, host, port);
            _client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, host);
            // Connection to peers : https://bitcoin.org/en/developer-guide#connecting-to-peers
            var instance         = PeersStore.Instance();
            var transmittingNode = instance.GetMyIpAddress();
            var nonce            = NonceHelper.GetNonceUInt64();
            var versionMessage   = new VersionMessage(transmittingNode, _currentIpAddress, nonce, string.Empty, 0, false, _network);

            try
            {
                _peerConnection = new PeerConnection(adrBytes);
                var result = _messageCoordinator.Launch(this, versionMessage);
                if (result != null && result is VerackMessage)
                {
                    _peerConnection.Connect();
                    if (ConnectEvent != null)
                    {
                        ConnectEvent(this, new IpAddressEventArgs(_currentIpAddress));
                    }

                    _timer = new Timer(TimerElapsed, _autoEvent, CHECK_INTERVAL, CHECK_INTERVAL); // CHECK PEERS AVAILABILITY EVERY 60 SECONDS.
                }
            }
            catch (Interop.RpcException)
            {
                throw new PeerConnectorException(ErrorCodes.PeerRpcError);
            }
        }
        public IEnumerable <Message> Broadcast(Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var result = new List <Message>();

            foreach (var activePeer in _peers)
            {
                result.Add(_messageCoordinator.Launch(activePeer, message));
            }

            return(result);
        }