Exemplo n.º 1
0
        public static NatPunchthroughClient GetInstance()
        {
            global::System.IntPtr cPtr = RakNetPINVOKE.CSharp_RakNet_NatPunchthroughClient_GetInstance();
            NatPunchthroughClient ret  = (cPtr == global::System.IntPtr.Zero) ? null : new NatPunchthroughClient(cPtr, false);

            return(ret);
        }
Exemplo n.º 2
0
        private bool isProxyMsgSending = false;                    //(内部用)启动代理转发消息时,是否持续发送消息来保持连接

        public RaknetUdpPeerClient()
        {
            natPunchthroughClient = new NatPunchthroughClient();
            udpProxyClient        = new UDPProxyClient();
            OnConnect            += RaknetUdpPeerClient_OnConnect;
            OnConnectFailed      += RaknetUdpPeerClient_OnConnectFailed;
            OnReceive            += RaknetUdpPeerClient_OnReceive;
            OnDisConnect         += RaknetUdpPeerClient_OnDisConnect;
        }
    IEnumerator connectToNATFacilitatorTest()
    {
        StartupResult startResult = rakPeerTest.Startup(2, new SocketDescriptor(), 1);

        if (startResult != StartupResult.RAKNET_STARTED)
        {
            Debug.Log("Failed to initialize network interface 2: " + startResult.ToString());
            yield break;
        }
        ConnectionAttemptResult connectResult = rakPeerTest.Connect(facilitatorIP, (ushort)facilitatorPort, null, 0);

        if (connectResult != ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)
        {
            Debug.Log("Failed to initialize connection to NAT Facilitator 2: " + connectResult.ToString());
            yield break;
        }

        Packet packet;

        while ((packet = rakPeerTest.Receive()) == null)
        {
            yield return(new WaitForEndOfFrame());
        }

        if (packet.data[0] != (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED)
        {
            Debug.Log("Failed to connect to NAT Facilitator 2: " + ((DefaultMessageIDTypes)packet.data[0]));
            yield break;
        }

        guidTest = rakPeerTest.GetMyGUID().g.ToString();
        Debug.Log("Connected 2: " + guidTest);

        facilitatorSystemAddressTest = packet.systemAddress;

        externalIP = rakPeerTest.GetExternalID(packet.systemAddress).ToString(false);

        rakPeerTest.SetMaximumIncomingConnections(2);

        if (firstTimeConnectTest)
        {
            firstTimeConnectTest = false;

            natPunchthroughClientTest = new NatPunchthroughClient();
            rakPeerTest.AttachPlugin(natPunchthroughClientTest);

            natPunchthroughClientTest.FindRouterPortStride(facilitatorSystemAddressTest);
        }
        else
        {
            StartCoroutine("waitForIncomingNATPunchThroughOnServerTest");
        }
        connectedToFacTest = true;

        isReadyTest = true;
    }
Exemplo n.º 4
0
    /**
     * Connect to the externally hosted NAT Facilitator (NATCompleteServer from the RakNet samples)
     * This is called initially and also after each succesfull punchthrough received on the server.
     */
    IEnumerator connectToNATFacilitator()
    {
        // Start the RakNet interface listening on a random port
        // We never need more than 2 connections, one to the Facilitator and one to either the server or the latest incoming client
        // Each time a client connects on the server RakNet is shut down and restarted with two fresh connections
        StartupResult startResult = rakPeer.Startup(2, new SocketDescriptor(), 1);

        if (startResult != StartupResult.RAKNET_STARTED)
        {
            Debug.Log("Failed to initialize network interface: " + startResult.ToString());
            yield break;
        }

        // Connect to the Facilitator
        ConnectionAttemptResult connectResult = rakPeer.Connect(facilitatorIP, (ushort)facilitatorPort, null, 0);

        if (connectResult != ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)
        {
            Debug.Log("Failed to initialize connection to NAT Facilitator: " + connectResult.ToString());
            yield break;
        }

        // Connecting, wait for response
        Packet packet;

        while ((packet = rakPeer.Receive()) == null)
        {
            yield return(new WaitForEndOfFrame());
        }

        // Was the connection accepted?
        if (packet.data[0] != (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED)
        {
            Debug.Log("Failed to connect to NAT Facilitator: " + ((DefaultMessageIDTypes)packet.data[0]));
            yield break;
        }

        // Success, we are connected to the Facilitator
        guid = rakPeer.GetMyGUID().g.ToString();
        Debug.Log("Connected: " + guid);

        // We store this for later so that we can tell which incoming messages are coming from the facilitator
        facilitatorSystemAddress = packet.systemAddress;

        // Now that we have an external connection we can get the externalIP
        externalIP = rakPeer.GetExternalID(packet.systemAddress).ToString(false);

        if (firstTimeConnect)
        {
            firstTimeConnect = false;

            // Attach RakNet punchthrough client
            // This is really what does all the heavy lifting
            natPunchthroughClient = new NatPunchthroughClient();
            rakPeer.AttachPlugin(natPunchthroughClient);
            // Punchthrough can't happen until RakNet is done finding router port stride
            // so we start it asap. If we didn't call this here RakNet would handle it
            // when we actually try and punch through.
            natPunchthroughClient.FindRouterPortStride(facilitatorSystemAddress);
        }
        else
        {
            // If this is not the first time connecting to the facilitor it means the server just received
            // a successful punchthrough and it reconnecting to prepare for more punching. We can start
            // listening immediately.
            StartCoroutine(waitForIncomingNATPunchThroughOnServer(onHolePunched));
        }

        isReady = true;
    }
    IEnumerator waitForIncomingNATPunchThroughOnServer()
    {
        ushort natListenPort                = 0;
        NatPunchthroughClient cli           = natPunchthroughClient;
        RakPeerInterface      peer          = rakPeer;
        SystemAddress         facAddr       = facilitatorSystemAddress;
        Action <int>          onHolePunched = onHolePunchedServer;

        while (true)
        {
            yield return(new WaitForEndOfFrame());

            // Check for incoming packet
            Packet packet = peer.Receive();

            // No packet, maybe next time
            if (packet == null)
            {
                continue;
            }

            // Got a packet, see what it is
            RakNet.DefaultMessageIDTypes messageType = (DefaultMessageIDTypes)packet.data[0];
            switch (messageType)
            {
            case DefaultMessageIDTypes.ID_NAT_PUNCHTHROUGH_SUCCEEDED:
                bool weAreTheSender = packet.data[1] == 1;
                if (!weAreTheSender)
                {
                    // Someone successfully punched through to us
                    // natListenPort is the port that the server should listen on
                    natListenPort = peer.GetInternalID().GetPort();
                    // Now we're waiting for the client to try and connect to us
                    Debug.Log("Received punch through from " + packet.guid + " " + packet.systemAddress.ToString());


                    // Reconnect to Facilitator for next punchthrough
                    NATNetworkManager_PHP.connectedToFac = false;
                    //firstTimeConnect = true;
                    //rakPeer = null;
                    ConnectFac();
                }
                break;

            case DefaultMessageIDTypes.ID_NEW_INCOMING_CONNECTION:
                // Cool we've got a connection. The client now knows which port to connect from / to
                Debug.Log("Received incoming RakNet connection.");
                // Close the connection to the client
                peer.CloseConnection(packet.guid, true);
                // And also to the Facilitator
                peer.CloseConnection(facAddr, true);
                break;

            case DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION:
                // Once the Facilitator has disconnected we shut down raknet
                // so that UNet can listen on the port that RakNet is currently listening on
                // At this point the hole is succesfully punched
                // RakNet is then reconnected to the facilitator on a new random port.
                if (packet.systemAddress == facAddr)
                {
                    peer.Shutdown(0);

                    // Hole is punched, UNet can start listening
                    onHolePunched(natListenPort);

                    yield break;                     // Totally done
                }
                break;

            default:
                Debug.Log(((DefaultMessageIDTypes)packet.data[0]).ToString());
                break;
            }
        }
    }
    IEnumerator connectToNATFacilitator()
    {
        StartupResult startResult = rakPeer.Startup(2, new SocketDescriptor(), 1);

        if (startResult != StartupResult.RAKNET_STARTED)
        {
            NATNetworkManager_PHP.DisplayLog("Failed to initialize network interface: " + startResult.ToString());
            yield break;
        }
        ConnectionAttemptResult connectResult = rakPeer.Connect(facilitatorIP, (ushort)facilitatorPort, null, 0);

        if (connectResult != ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)
        {
            NATNetworkManager_PHP.DisplayLog("Failed to initialize connection to NAT Facilitator: " + connectResult.ToString());
            yield break;
        }

        Packet packet;

        while ((packet = rakPeer.Receive()) == null)
        {
            yield return(new WaitForEndOfFrame());
        }

        if (packet.data[0] != (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED)
        {
            NATNetworkManager_PHP.DisplayLog("Failed to connect to NAT Facilitator: " + ((DefaultMessageIDTypes)packet.data[0]));
            NATNetworkManager_PHP.RefreshInfo();
            yield break;
        }

        guid = rakPeer.GetMyGUID().g.ToString();
        Debug.Log("Connected: " + guid);


        externalIP = rakPeer.GetExternalID(packet.systemAddress).ToString(false);

        NATNetworkManager_PHP.RefreshInfo();



        //if (firstTimeConnect) {
        //	firstTimeConnect = false;
        natPunchthroughClient = new NatPunchthroughClient();
        rakPeer.AttachPlugin(natPunchthroughClient);

        natPunchthroughClient.FindRouterPortStride(packet.systemAddress);
        facilitatorSystemAddress = packet.systemAddress;

        /*} else {
         *      rakPeer.AttachPlugin (natPunchthroughClient);
         *      //if (!facilitatorSystemAddress.EqualsExcludingPort(packet.systemAddress)) {
         *              natPunchthroughClient.FindRouterPortStride (packet.systemAddress);
         *      //}
         *      facilitatorSystemAddress = packet.systemAddress;
         * }*/
        if (NetworkServer.active && NATNetworkManager_PHP.singleton.myRoom.isNetGame)
        {
            StartCoroutine("waitForIncomingNATPunchThroughOnServer");
        }

        if (string.IsNullOrEmpty(guid))
        {
            NATNetworkManager_PHP.DisplayLog("GUID IS NULL OR EMPTY");
        }
        else
        {
            NATNetworkManager_PHP.singleton.StartCoroutine("setNewGUID", guid);
        }

        NATNetworkManager_PHP.connectedToFac = true;
        rakPeer.SetMaximumIncomingConnections(2);
        isReady = true;
    }
Exemplo n.º 7
0
 public static void DestroyInstance(NatPunchthroughClient i)
 {
     RakNetPINVOKE.CSharp_RakNet_NatPunchthroughClient_DestroyInstance(NatPunchthroughClient.getCPtr(i));
 }
Exemplo n.º 8
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(NatPunchthroughClient obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }