Exemplo n.º 1
0
        public bool Start(RaknetIPAddress localAddress = null, ushort maxConnCount = ushort.MaxValue)
        {
            rakPeer.AttachPlugin(natPunchthroughServer);
            rakPeer.SetMaximumIncomingConnections(maxConnCount);
            SocketDescriptor socketDescriptor = new SocketDescriptor();

            if (localAddress != null && !string.IsNullOrWhiteSpace(localAddress.Address) && localAddress.Port > 0)
            {
                socketDescriptor.hostAddress = localAddress.Address;
                socketDescriptor.port        = localAddress.Port;
            }
            var startResult = rakPeer.Startup(maxConnCount, socketDescriptor, 1);

            if (startResult == StartupResult.SOCKET_PORT_ALREADY_IN_USE)
            {
                RaknetExtension.WriteWarning(string.Format(@"{0}端口被占用", socketDescriptor.port));
                return(false);
            }
            ReceiveThreadStart();
            List <int> startList = new List <int>()
            {
                (int)StartupResult.RAKNET_STARTED,
                (int)StartupResult.RAKNET_ALREADY_STARTED,
            };

            if (startList.Any(m => m == (int)startResult))
            {
                return(true);
            }
            isThreadRunning = false;
            return(false);
        }
Exemplo n.º 2
0
        internal bool Start(RaknetIPAddress localAddress = null, ushort maxConnCount = ushort.MaxValue)
        {
            rakPeer.AttachPlugin(udpProxyCoordinator);
            udpProxyCoordinator.SetRemoteLoginPassword(RaknetConfig.COORDINATOR_PASSWORD);
            rakPeer.SetMaximumIncomingConnections(maxConnCount);
            SocketDescriptor socketDescriptor = new SocketDescriptor();

            if (localAddress != null && !string.IsNullOrWhiteSpace(localAddress.Address) && localAddress.Port > 0)
            {
                socketDescriptor.hostAddress = localAddress.Address;
                socketDescriptor.port        = localAddress.Port;
            }
            var startResult = rakPeer.Startup(maxConnCount, socketDescriptor, 1);

            if (startResult == StartupResult.SOCKET_PORT_ALREADY_IN_USE)
            {
                RaknetExtension.WriteWarning(string.Format(@"{0}端口被占用", socketDescriptor.port));
                return(false);
            }
            OnUdpProxyGeneral += RaknetUdpProxyCoordinator_OnUdpProxyGeneral;
            ReceiveThreadStart();
            List <int> startList = new List <int>()
            {
                (int)StartupResult.RAKNET_STARTED,
                (int)StartupResult.RAKNET_ALREADY_STARTED,
            };

            if (startList.Any(m => m == (int)startResult))
            {
                return(true);
            }
            isThreadRunning = false;
            return(false);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="beforeAction"></param>
        internal void Stop(Action beforeAction = null)
        {
            beforeAction?.Invoke();
            string myAddress = GetMyAddress().ToString();

            isThreadRunning = false;
            rakPeer.Shutdown(10);
            RakPeerInterface.DestroyInstance(rakPeer);
            RaknetExtension.WriteWarning(string.Format("coordinator停止了:{0}", myAddress));
        }
Exemplo n.º 4
0
        // <summary>
        /// 停止
        /// </summary>
        /// <param name="beforeAction"></param>
        internal void Stop(Action beforeAction = null)
        {
            beforeAction?.Invoke();
            string myAddress = GetMyAddress().ToString();

            rakPeer.CloseConnection(new AddressOrGUID(new SystemAddress(_coordinatorAddress.Address, _coordinatorAddress.Port)), true);
            isThreadRunning = false;
            rakPeer.Shutdown(10);
            RakPeerInterface.DestroyInstance(rakPeer);
            RaknetExtension.WriteWarning(string.Format("UdpProxyServer停止了:{0}", myAddress));
        }
Exemplo n.º 5
0
        public RaknetUdpPeerClient Start(RaknetIPAddress localAddress = null, ushort maxConnCount = ushort.MaxValue)
        {
            rakPeer.AttachPlugin(natPunchthroughClient);
            rakPeer.AttachPlugin(udpProxyClient);
            SocketDescriptor socketDescriptor = new SocketDescriptor();

            if (localAddress != null && !string.IsNullOrWhiteSpace(localAddress.Address) && localAddress.Port > 0)
            {
                socketDescriptor.hostAddress = localAddress.Address;
                socketDescriptor.port        = localAddress.Port;
            }
            var startResult = rakPeer.Startup(maxConnCount, socketDescriptor, 1);

            if (startResult == StartupResult.SOCKET_PORT_ALREADY_IN_USE)
            {
                RaknetExtension.WriteWarning(string.Format(@"{0}端口被占用", socketDescriptor.port));
            }
            return(this);
        }
Exemplo n.º 6
0
        internal RaknetUdpProxyServer Start(RaknetIPAddress localAddress = null, ushort maxConnCount = ushort.MaxValue)
        {
            rakPeer.AttachPlugin(udpProxyServer);
            udpProxyServer.SetResultHandler(new MyUDPProxyServerResultHandler());
            rakPeer.SetMaximumIncomingConnections(maxConnCount);
            SocketDescriptor socketDescriptor = new SocketDescriptor();

            if (localAddress != null && !string.IsNullOrWhiteSpace(localAddress.Address) && localAddress.Port > 0)
            {
                socketDescriptor.hostAddress = localAddress.Address;
                socketDescriptor.port        = localAddress.Port;
            }
            var startResult = rakPeer.Startup(maxConnCount, socketDescriptor, 1);

            if (startResult == StartupResult.SOCKET_PORT_ALREADY_IN_USE)
            {
                RaknetExtension.WriteWarning(string.Format(@"proxyServer {0}端口被占用", socketDescriptor.port));
                return(this);
            }
            return(this);
        }
Exemplo n.º 7
0
        public bool Connect(RaknetIPAddress natServerAddress, RaknetIPAddress coordinatorAddress)
        {
            _natServerAddress            = natServerAddress;
            _coordinatorAddress          = coordinatorAddress;
            OnNewIncomingConnection     += RaknetUdpPeerServer_OnNewIncomingConnection;
            OnDisconnectionNotification += RaknetUdpPeerServer_OnDisconnectionNotification;
            OnRaknetReceive             += RaknetUdpPeerServer_OnRaknetReceive;
            OnConnectionAttemptFailed   += RaknetUdpPeerServer_OnConnectionAttemptFailed;
            OnNoFreeIncomingConnections += RaknetUdpPeerServer_OnNoFreeIncomingConnections;
            OnConnectionRequestAccepted += RaknetUdpPeerServer_OnConnectionRequestAccepted;
            OnConnectionLost            += RaknetUdpPeerServer_OnConnectionLost;
            ReceiveThreadStart();

            var connectNatServerResult = rakPeer.Connect(_natServerAddress.Address, _natServerAddress.Port, RaknetConfig.natServerPwd, RaknetConfig.natServerPwd.Length);

            if (connectNatServerResult == ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)            //尝试连接穿透服务器开始
            {
                //连接coordinator,为走Proxy流程备用
                udpProxyClient.SetResultHandler(new MyUDPProxyClientResultHandler());
                var connectCoordinatorResult = rakPeer.Connect(_coordinatorAddress.Address, _coordinatorAddress.Port, "", 0);
                if (connectCoordinatorResult == ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)                 //尝试连接协调器开始
                {
                    return(true);
                }
                else
                {
                    RaknetExtension.WriteWarning("peerServer尝试连接协调器失败");
                }
            }
            else
            {
                RaknetExtension.WriteWarning("peerServer尝试连接穿透服务器失败");
            }

            isThreadRunning = false;
            return(false);
        }