예제 #1
0
        public UDPSocket(IPEndPoint localEndPoint)
        {
            fLocalEndPoint = localEndPoint;
            fBuffer        = new byte[65535];
            fLogger        = LogManager.GetLogger(ProtocolHelper.LOG_FILE, ProtocolHelper.LOG_LEVEL, "UDPSocket");

            fSocket = new Socket(IPAddressFamily, SocketType.Dgram, ProtocolType.Udp);
            fSocket.SetIPProtectionLevelUnrestricted();

            #if !MONO
            #if !IP6
            const long IOC_IN            = 0x80000000;
            const long IOC_VENDOR        = 0x18000000;
            const long SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
            byte[]     optionInValue     = { Convert.ToByte(false) };
            byte[]     optionOutValue    = new byte[4];
            fSocket.IOControl((IOControlCode)SIO_UDP_CONNRESET, optionInValue, optionOutValue);
            #else
            #endif
            #endif

#if !IP6
            fSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, false);
            fSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            fSocket.Ttl = 255;
#else
            fSocket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
#endif

            fSocket.Bind(fLocalEndPoint);

            fConnected = false;
        }
        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;
        }