Exemple #1
0
        private void EndRecv(IAsyncResult result)
        {
            try {
                EndPoint remoteAddress = new IPEndPoint(IPAnyAddress, 0);
                int      count         = fSocket.EndReceiveFrom(result, ref remoteAddress);
                if (count > 0)
                {
                    byte[] buffer = new byte[count];
                    Buffer.BlockCopy(fBuffer, 0, buffer, 0, count);
                    OnRecvMessage((IPEndPoint)remoteAddress, buffer);
                }
            } catch (Exception ex) {
                fLogger.WriteError("EndRecv.1()", ex);
            }

            if (!fConnected)
            {
                return;
            }

            bool notsuccess;

            do
            {
                try {
                    BeginRecv();
                    notsuccess = false;
                } catch (Exception ex) {
                    fLogger.WriteError("EndRecv.2()", ex);
                    notsuccess = true;
                }
            } while (notsuccess);
        }
        private void DeviceFound(object sender, DeviceEventArgs args)
        {
            try {
                INatDevice device = args.Device;

                fLogger.WriteInfo("Device found, type: {0}", device.GetType().Name);
                if (device.GetType().Name == "PmpNatDevice")
                {
                    fLogger.WriteInfo("Device skipped");
                    return;
                }

                fLogger.WriteInfo("External IP: {0}", device.GetExternalIP());

                try {
                    Mapping m;

                    /*Mapping m = device.GetSpecificMapping(Mono.Nat.Protocol.Tcp, ProtocolHelper.PublicTCPPort);
                     * if (m != null) {
                     *  fLogger.WriteInfo("Specific Mapping: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort, m.PrivatePort);
                     * } else {*/
                    /*m = new Mapping(Protocol.Tcp, ProtocolHelper.PublicTCPPort, ProtocolHelper.PublicTCPPort);
                     * device.CreatePortMap(m);
                     * fLogger.WriteInfo("Create Mapping: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort, m.PrivatePort);*/
                    //}

                    m = device.GetSpecificMapping(Protocol.Udp, DHTClient.PublicDHTPort);
                    if (m != null)
                    {
                        try {
                            device.DeletePortMap(m);
                        } catch {
                        }
                    }
                    m = new Mapping(Protocol.Udp, DHTClient.PublicDHTPort, DHTClient.PublicDHTPort);
                    device.CreatePortMap(m);
                    fLogger.WriteInfo("Create Mapping: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort, m.PrivatePort);
                } catch (Exception ex) {
                    fLogger.WriteError("Couldn't create specific mapping", ex);
                }

                foreach (Mapping mp in device.GetAllMappings())
                {
                    fLogger.WriteInfo("Existing Mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort);
                }

                fUPnPSem.Release();
            } catch (Exception ex) {
                fLogger.WriteError("NATMapper.DeviceFound()", ex);
            }
        }
        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;
        }