Beispiel #1
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);
        }
 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);
     }
 }