Esempio n. 1
0
        public bool Connect(RaknetIPAddress natServerAddress, RaknetIPAddress coordinatorAddress, RaknetIPAddress peerServerAddress, ulong udpPeerServerGuid)
        {
            _natServerAddress            = natServerAddress;
            _coordinatorAddress          = coordinatorAddress;
            _peerServerAddress           = peerServerAddress;
            _udpPeerServerGuid           = udpPeerServerGuid;
            OnConnectionRequestAccepted += RaknetUdpPeerClient_OnConnectionRequestAccepted;
            OnNatPunchthroughSucceeded  += RaknetUdpPeerClient_OnNatPunchthroughSucceeded;
            OnNatPunchthroughFailed     += RaknetUdpPeerClient_OnNatPunchthroughFailed;
            OnDisconnectionNotification += RaknetUdpPeerClient_OnDisconnectionNotification;
            OnConnectionLost            += RaknetUdpPeerClient_OnConnectionLost;
            OnRaknetReceive             += RaknetUdpPeerClient_OnRaknetReceive;
            OnConnectionAttemptFailed   += RaknetUdpPeerClient_OnConnectionAttemptFailed;
            OnNoFreeIncomingConnections += RaknetUdpPeerClient_OnNoFreeIncomingConnections;
            ReceiveThreadStart();
            ////启动NATPunchthrough连接
            var connectResult = rakPeer.Connect(_natServerAddress.Address, _natServerAddress.Port, RaknetConfig.natServerPwd, RaknetConfig.natServerPwd.Length);

            if (connectResult == ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)
            {
                return(true);
            }
            isThreadRunning = false;
            return(false);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
            public override void OnForwardingSuccess(string proxyIPAddress, ushort proxyPort, SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, UDPProxyClient proxyClientPlugin)
            {
                RaknetExtension.WriteInfo("▲▲▲OnForwardingSuccess");
                _proxyServerAddress = new RaknetIPAddress(targetAddress.ToString(false), targetAddress.GetPort());
                var peer          = proxyClientPlugin.GetRakPeerInterface();
                var systemAddress = peer.GetMyBoundAddress();

                peer.Connect(_proxyServerAddress.Address, _proxyServerAddress.Port, "", 0);
            }
Esempio n. 5
0
        public bool Start(RaknetIPAddress localAddress = null, ushort maxConnCount = ushort.MaxValue)
        {
            var proxyCoordinatorStarted = raknetUdpProxyCoordinator.Start(localAddress, maxConnCount);

            if (proxyCoordinatorStarted)
            {
                var proxyServerStarted = raknetUdpProxyServer.Start().Connect(raknetUdpProxyCoordinator.GetMyIpAddress());
                if (proxyServerStarted)
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 6
0
        internal bool Connect(RaknetIPAddress coordinatorAddress)
        {
            _coordinatorAddress          = coordinatorAddress;
            OnConnectionRequestAccepted += RaknetUdpProxyServer_OnConnectionRequestAccepted;
            OnConnectionAttemptFailed   += RaknetUdpProxyServer_OnConnectionAttemptFailed;
            ReceiveThreadStart();
            var connectResult = rakPeer.Connect(coordinatorAddress.Address, coordinatorAddress.Port, "", 0);

            if (connectResult == ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)             //尝试连接开始
            {
                return(true);
            }
            isThreadRunning = false;
            return(false);
        }
Esempio n. 7
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);
        }
Esempio n. 8
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);
        }
Esempio n. 9
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);
        }
Esempio n. 10
0
 private void RaknetUdpPeerClient_OnConnectionRequestAccepted(string address, ushort port)
 {
     if (address == _natServerAddress.Address && port == _natServerAddress.Port)
     {
         //OpenNAT
         RakNetGUID peerServerRakNetGUID = new RakNetGUID(_udpPeerServerGuid);
         RakNetGUID myGuid = rakPeer.GetMyGUID();
         if (myGuid.g != _udpPeerServerGuid)
         {
             natPunchthroughClient.OpenNAT(peerServerRakNetGUID, new SystemAddress(address, port));
         }
     }
     else if (address == _coordinatorAddress.Address && port == _coordinatorAddress.Port)
     {
         udpProxyClient.SetResultHandler(new MyUDPProxyClientResultHandler());
         SystemAddress coordinatorAddress = new SystemAddress();
         coordinatorAddress.SetBinaryAddress(_coordinatorAddress.Address);
         coordinatorAddress.SetPortHostOrder(_coordinatorAddress.Port);
         udpProxyClient.RequestForwarding(coordinatorAddress, rakPeer.GetMyBoundAddress(), new SystemAddress(_peerServerAddress.Address, _peerServerAddress.Port), 7000);
     }
     else if (_proxyServerAddress != null && address == _proxyServerAddress.Address && port == _proxyServerAddress.Port)
     {
         _isConnectPeerServer = true;
         new Thread(n =>
         {
             OnConnect(address, port, this);
         })
         {
             IsBackground = true, Priority = ThreadPriority.Highest
         }.Start();
         //代理连接成功,对_peerServerAddress重新赋值,该值实际是proxy的一个临时地址,通过该值直接发送消息给peerServer,但通过代理的方式要持续发送消息给proxy才能保持连接,如果一段时间内不发送消息,则连接主动断开
         _peerServerAddress = new RaknetIPAddress(address, port);
         //开启线程,持续向proxy发送消息,只能发送一个字节,并且消息类型必须为 DefaultMessageIDTypes.ID_USER_PACKET_ENUM
         ThreadPool.QueueUserWorkItem(obj =>
         {
             isProxyMsgSending = true;
             while (true)
             {
                 if (!isProxyMsgSending)
                 {
                     break;
                 }
                 RaknetExtension.WriteInfo("dddddddd");
                 //循环发送消息以保持连接
                 var tempByte = new byte[] { (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM };
                 rakPeer.Send(tempByte, tempByte.Length,
                              PacketPriority.LOW_PRIORITY,
                              PacketReliability.RELIABLE_ORDERED,
                              (char)0,
                              new AddressOrGUID(new SystemAddress(_peerServerAddress.Address, _peerServerAddress.Port)),
                              false);
                 Thread.Sleep(4000);
             }
         });
     }
     else
     {
         _isConnectPeerServer = true;
         new Thread(n =>
         {
             OnConnect(address, port, this);
         })
         {
             IsBackground = true, Priority = ThreadPriority.Highest
         }.Start();
         //通过NAT与peerServer连接成功后,立即断开与natServer的连接
         rakPeer.CloseConnection(new AddressOrGUID(new SystemAddress(_natServerAddress.Address, _natServerAddress.Port)), true);
     }
 }