Exemplo n.º 1
0
    public void init()
    {
        // protocol init
        NetworkTransport.Init();
        ClientNET_RecieaveFunction.init(this);
        ClientNET_SendFunction.init(this);

        ConnectionConfig cc = new ConnectionConfig();

        reliableChanel   = cc.AddChannel(QosType.Reliable); // ReliableSequenced, Reliable
        unReliableChanel = cc.AddChannel(QosType.Unreliable);

        HostTopology topo = new HostTopology(cc, MAX_USER);

        hostId = NetworkTransport.AddHost(topo, 0);

        /*--------------------------------------------------*/

        Debug.Log(ServerHost);
        ConnectionMenu.setConnectionStatusMessage("connecting to " + ServerHost);
        if (ServerHost != "localhost" && ServerHost != "127.0.0.1")   // FIXME: check if is on local area
        // check if player is connected
        {
            try {
                IPAdress = System.Net.Dns.GetHostEntry("google.com").AddressList[0].ToString();
                ConnectionMenu.setConnectionStatusMessage("Checking connection status.");
                Debug.Log("Checking connection status.");
            } catch (System.Exception e) {
                ConnectionMenu.setConnectionStatusMessage("error: you are not connected to the internet.");
                Debug.Log("error: you are not connected to the internet.");
                disconnect(); // disconnect client
            }
        }

        // resolve hostname
        try {
            IPAdress = System.Net.Dns.GetHostEntry(ServerHost).AddressList[0].ToString();
            ConnectionMenu.setConnectionStatusMessage("Resolving hostname " + ServerHost);
            canConnect = true;
        } catch (System.Exception e) {
            ConnectionMenu.setConnectionStatusMessage("error: hostname " + ServerHost + " dosen't exist");
            Debug.Log("error: hostname " + ServerHost + "dosen't exist");
            disconnect(); // disconnect client
        }

        /*--------------------------------------------------*/
        if (canConnect)
        {
            // try to connect
            connectionId = NetworkTransport.Connect(hostId, IPAdress, PORT, 0, out error);
            Debug.Log("try to connect to " + ServerHost + " |  " + IPAdress + ":" + PORT);
            ConnectionMenu.setConnectionStatusMessage("Connecting to " + ServerHost + ":" + PORT + "...");

            if (error != 0)
            {
                Debug.Log("can't connect to server | error: " + error + " | retriying...");
                ConnectionMenu.setConnectionStatusMessage("can't connect to server | error: " + error + " | retriying...");
                init();
            }
            else
            {
                isStarted = true;
            }
        }
    }