Esempio n. 1
0
        public CommunicatorCore(IChatForm form)
        {
            if (form == null)
            {
                throw new ArgumentNullException("form");
            }

            fConnected = false;
            fForm      = form;
            fLogger    = LogManager.GetLogger(ProtocolHelper.LOG_FILE, ProtocolHelper.LOG_LEVEL, "ChatDHTCP");
            fPeers     = new List <Peer>();
            fSTUNInfo  = null;

            fProfile = new UserProfile();

            fDatabase = IDatabase.CreateDefault();
            fDatabase.Connect();
            fDatabase.LoadProfile(fProfile);

            fDHTClient                   = new DHTClient(DHTClient.IPAnyAddress, DHTClient.PublicDHTPort, this, ProtocolHelper.CLIENT_VER);
            fDHTClient.PeersFound       += OnPeersFound;
            fDHTClient.PeerPinged       += OnPeerPinged;
            fDHTClient.QueryReceived    += OnQueryReceive;
            fDHTClient.ResponseReceived += OnResponseReceive;

            NATHolePunching();

            fTCPClient              = new TCPDuplexClient();
            fTCPClient.DataReceive += OnDataReceive;

            fTCPListenerPort = ProtocolHelper.PublicTCPPort;
        }
        public CommunicatorCore(IChatForm form)
        {
            if (form == null)
            {
                throw new ArgumentNullException("form");
            }

            fConnectionState = ConnectionState.Disconnected;
            fForm            = form;
            fLogger          = LogManager.GetLogger(ProtocolHelper.LOG_FILE, ProtocolHelper.LOG_LEVEL, "CommCore");
            fPeers           = new List <Peer>();

            fProfile = new UserProfile();

            fDatabase = new GKNetDatabase();
            fDatabase.Connect();
            fDatabase.LoadProfile(fProfile);

            fBlockchainNode = new BlockchainNode(this, fDatabase);

            fDataPlugins = new List <IDataPlugin>();
            LoadPlugins(Utilities.GetAppPath());

            Port = DHTClient.PublicDHTPort;
            fLogger.WriteInfo("Port: {0}", Port);

            try {
                fSTUNInfo       = STUNUtility.Detect(Port);
                fPublicEndPoint = (fSTUNInfo.NetType != STUN_NetType.UdpBlocked) ? fSTUNInfo.PublicEndPoint : null;
            } catch (Exception ex) {
                fLogger.WriteError("DetectSTUN() error", ex);
                fPublicEndPoint = null;
            }

            if (UPnPEnabled)
            {
                CreatePortMapping();
            }

            fDHTClient = new DHTClient(new IPEndPoint(DHTClient.IPAnyAddress, Port), this, ProtocolHelper.CLIENT_VER);
            fDHTClient.PublicEndPoint    = fPublicEndPoint;
            fDHTClient.PeersFound       += OnPeersFound;
            fDHTClient.PeerPinged       += OnPeerPinged;
            fDHTClient.QueryReceived    += OnQueryReceive;
            fDHTClient.ResponseReceived += OnResponseReceive;

            InitializePeers();

            fTCPClient              = new TCPDuplexClient();
            fTCPClient.DataReceive += OnDataReceive;

            fTCPListenerPort = ProtocolHelper.PublicTCPPort;
        }
        public CommunicatorCore(IChatForm form)
        {
            if (form == null)
            {
                throw new ArgumentNullException("form");
            }

            fConnected = false;
            fForm      = form;
            fLogger    = LogManager.GetLogger(ProtocolHelper.LOG_FILE, ProtocolHelper.LOG_LEVEL, "ChatDHTCP");
            fProfile   = new UserProfile();
            fParser    = new BencodeParser();
            fPeers     = new List <Peer>();
            fSTUNInfo  = null;

            int dhtPort = DHTClient.PublicDHTPort;

            fDHTClient             = new DHTClient(DHTClient.IPAnyAddress, dhtPort, this, CLIENT_VER);
            fDHTClient.PeersFound += delegate(object sender, PeersFoundEventArgs e) {
                fLogger.WriteInfo(string.Format("Found DHT peers: {0}", e.Peers.Count));

                bool changed = false;
                foreach (var p in e.Peers)
                {
                    changed = UpdatePeer(p);
                }

                if (changed)
                {
                    fForm.OnPeersListChanged();
                }
            };
            fDHTClient.PeerPinged += delegate(object sender, PeerPingedEventArgs e) {
                fLogger.WriteInfo(string.Format("Peer pinged: {0}", e.EndPoint));

                bool changed = CheckPeer(e.EndPoint);

                if (changed)
                {
                    fForm.OnPeersListChanged();
                    SendData(e.EndPoint, ProtocolHelper.CreateGetPeerInfoQuery());
                }
            };
            fDHTClient.QueryReceived    += OnQueryReceive;
            fDHTClient.ResponseReceived += OnResponseReceive;

            NATHolePunching();

            fTCPClient              = new TCPDuplexClient();
            fTCPClient.DataReceive += OnDataReceive;
        }