Beispiel #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);
        }
Beispiel #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);
        }
            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);
            }
Beispiel #4
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));
        }
Beispiel #5
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));
        }
        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);
        }
Beispiel #7
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);
        }
Beispiel #8
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);
        }
 public override void OnRecipientNotConnected(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, UDPProxyClient proxyClientPlugin)
 {
     RaknetExtension.WriteInfo("▲▲▲OnRecipientNotConnected");
 }
 public override void OnNoServersOnline(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, UDPProxyClient proxyClientPlugin)
 {
     RaknetExtension.WriteInfo("▲▲▲OnNoServersOnline");
 }
 public override void OnForwardingNotification(string proxyIPAddress, ushort proxyPort, SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, UDPProxyClient proxyClientPlugin)
 {
     RaknetExtension.WriteInfo("▲▲▲OnForwardingNotification");
 }
 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);
     }
 }
Beispiel #13
0
 public override void OnNoPasswordSet(RakString usedPassword, UDPProxyServer proxyServerPlugin)
 {
     RaknetExtension.WriteInfo("▲▲▲OnNoPasswordSet");
 }
Beispiel #14
0
 public override void OnAlreadyLoggedIn(RakString usedPassword, UDPProxyServer proxyServerPlugin)
 {
     RaknetExtension.WriteInfo("▲▲▲OnAlreadyLoggedIn");
 }
Beispiel #15
0
 public override void OnLoginSuccess(RakString usedPassword, UDPProxyServer proxyServerPlugin)
 {
     RaknetExtension.WriteInfo("▲▲▲OnLoginSuccess");
 }
Beispiel #16
0
 private void RaknetUdpProxyCoordinator_OnUdpProxyGeneral(string address, ushort port, byte theByte)
 {
     RaknetExtension.WriteInfo(string.Format(@"ID_UDP_PROXY_GENERAL类型:{0}", theByte));
 }