Example #1
0
    public void Cancel()
    {
        mResult = null;

        string publicIP = NetworkHost.GetLocalAddress();
        string url      = ServerLookupHostname + "/api/removeserver?ip=" + publicIP;

        Debug.Log("Making webrequest to: " + url);
        WebRequest req = WebRequest.Create(url);

        req.ContentType = "application/json; charset=utf-8";
        req.Timeout     = mTimeout_ms;

        WebResponse res = req.GetResponse();

        Stream       resStream = res.GetResponseStream();
        StreamReader reader    = new StreamReader(resStream);
        string       resData   = reader.ReadToEnd();

        reader.Close();
        res.Close();

        Dictionary <string, string> data = JsonConvert.DeserializeObject <Dictionary <string, string> >(resData);

        if (data["status"] == "ok")
        {
            mResult = "success";
        }
        else
        {
            mResult = "failure";
        }
    }
Example #2
0
        /// <summary>
        /// Start up the network manager. This should be called by Lobby.cs
        /// when the server host IP has been discovered and is connectable.
        /// </summary>
        ///
        /// <param name="serverAddr">
        /// As server, local IP address to host on.
        /// As client, remote server IP address to connect to.
        /// </param>
        ///
        /// <param name="nwMode">
        /// Are you the server or client?
        /// </param>
        ///
        /// <param name="useDNS">
        /// If serverAddr is a local IP, convert it via DNS? Set to false if using wifi-direct IP address.
        /// </param>
        ///
        /// <returns>
        /// True if successful.
        /// If client, use this.NetworkHost.ConnectionCount == 1 afterwards to confirm connection to server.
        /// </returns>
        public bool Run(string serverAddr, NetworkMode nwMode, bool useDNS = true)
        {
            ServerAddress = serverAddr;
            Mode          = nwMode;

            // Construct serializer and packet queue
            Serializer  = new XMLPacketSerializer();
            PacketQueue = new PacketQueue(Serializer);

            // Create and configure host
            if (Mode == NetworkMode.Server)
            {
                // Convert any "local address" to the "dns local address"
                if (useDNS && NetworkHost.IsLocalAddress(ServerAddress))
                {
                    ServerAddress = NetworkHost.GetLocalAddress();
                }

                // Server, configures socket to be "known" at this address
                Debug.Log("NetworkManager: As a server, I am using server IP: " + ServerAddress);
                NetworkHost = new NetworkHost(ServerAddress, ServerPort, 4);
            }
            else
            {
                // Client, connect to server
                NetworkHost = new NetworkHost(4);
            }

            // Configure channels and open/allow connections.
            NetworkHost.ConfigureChannel("info", ChannelType.ReliableSequenced);
            NetworkHost.ConfigureChannel("state", ChannelType.UnreliableStateUpdate);
            NetworkHost.ConfigureChannel("input", ChannelType.ReliableStateUpdate);
            bool rv = NetworkHost.Open();

            if (!rv)
            {
                NetworkHost.Dispose();
                NetworkHost = null;
                return(false);
            }

            // Connect client to server
            if (Mode == NetworkMode.Client)
            {
                Debug.Log("NetworkManager: As a client, I am using server IP: " + ServerAddress);
                NetworkHost.Connect(ServerAddress, ServerPort);
            }

            // Allow FixedUpdate() to run.
            IsRunning = true;

            return(true);
        }
Example #3
0
    // Use this for initialization
    void Start()
    {
        KnownPeople = new HashSet <NetworkInfo>();
        Serializer  = new JSONPacketSerializer();

        //
        Network = new NetworkHost(9560, 1);
        Network.ConfigureChannel("broadcast", ChannelType.UnreliableUnordered);
        Network.Open();

        // Mark broadcast credentials
        Network.SetBroadcastCredentials(1234, 1);

        //
        NetworkHost.StartBroadcast(Network, Encoding.ASCII.GetBytes(NetworkHost.GetLocalAddress()));

        //
        var textUi = GetComponent <Text>();

        textUi.text = "";
    }
Example #4
0
    public void GetServerIP(string roomCode)
    {
        // If host is wanting its own local IP address
        if (roomCode == null)
        {
            mResult = NetworkHost.GetLocalAddress();
            return;
        }

        mResult = null;

        string url = ServerLookupHostname + "/api/queryserver" +
                     "?roomCode=" + roomCode;

        Debug.Log("Making webrequest to: " + url);
        WebRequest req = WebRequest.Create(url);

        req.ContentType = "application/json; charset=utf-8";

        WebResponse res = req.GetResponse();

        Stream       resStream = res.GetResponseStream();
        StreamReader reader    = new StreamReader(resStream);
        string       resData   = reader.ReadToEnd();

        Debug.Log(resData);
        reader.Close();
        res.Close();

        Dictionary <string, string> data = JsonConvert.DeserializeObject <Dictionary <string, string> >(resData);

        if (data["status"] == "ok")
        {
            mResult = data["ip"];
        }
        else
        {
            mResult = "failure";
        }
    }
Example #5
0
    /// <summary>
    /// Step 2/3 of hosting a lobby.
    /// Get the result of HostGame_CreateNetwork().
    /// Menu should display a loading screen while repeatedly calling this function until success.
    /// Upon success, network manager will have been started to listen and accept client connections.
    /// </summary>
    ///
    /// <returns>
    /// If success, returns lobby code that is either an IP address or 4-digit room code. Menu should
    /// make this visible. Lobby code will an IP address if InternetIP was passed to HostGame_CreateNetwork().
    /// If failure, returns "failure".
    /// If not ready yet, returns null.
    /// </returns>
    public string HostLobby_CreateNetworkResult()
    {
        if (this.nwType == NetworkType.InternetIP)
        {
            // Use "127.0.0.1" as our server IP.
            bool isSuccess = NwMgr.Run(NwMgr.ServerAddress, NetworkMode.Server);
            if (!isSuccess)
            {
                return("failure");
            }

            // But display public IP to share with clients to connect.
            string hostIP = NetworkHost.GetLocalAddress();
            if (IsDebug)
            {
                Debug.Log("HostGame_CreateNetworkResult: Public IP - " + hostIP);
            }
            return(hostIP);
        }
        else if (this.nwType == NetworkType.InternetRoomCode)
        {
            string roomCode = this.serverLookup.AdvertiseServerResult();
            if (roomCode == null || roomCode == "failure")
            {
                return(roomCode);
            }
            else
            {
                // Use "127.0.0.1" as our server IP.
                bool isSuccess = NwMgr.Run(NwMgr.ServerAddress, NetworkMode.Server);
                if (!isSuccess)
                {
                    return("failure");
                }

                // But display 4-digit room code to share with clients to connect.
                return(roomCode);
            }
        }
        else
        {
            string roomCode = this.serverLookup.AdvertiseServerResult();
            if (roomCode == null || roomCode == "failure")
            {
                return(roomCode);
            }
            else
            {
                this.serverLookup.GetServerIP(null);

                string hostIP    = this.serverLookup.GetServerIPResult();
                bool   useDnsIP  = this.nwType != NetworkType.LocalWifiDirect;
                bool   isSuccess = NwMgr.Run(hostIP, NetworkMode.Server, useDnsIP);
                if (!isSuccess)
                {
                    return("failure");
                }

                return(roomCode);
            }
        }
    }
 // Use this for initialization
 void Start()
 {
     LocalAddress = NetworkHost.GetLocalAddress();
     Network      = FindObjectOfType <NetworkManager>();
 }