示例#1
0
        private void RegisterOnMasterServer(JSONNode masterServerData)
        {
            // The Master Server communicates over TCP
            TCPMasterClient client = new TCPMasterClient();

            // Once this client has been accepted by the master server it should send it's get request
            client.serverAccepted += (sender) =>
            {
                try
                {
                    Text temp = Text.CreateFromString(client.Time.Timestep, masterServerData.ToString(), true, Receivers.Server, MessageGroupIds.MASTER_SERVER_REGISTER, true);

                    //Debug.Log(temp.GetData().Length);
                    // Send the request to the server
                    client.Send(temp);

                    Networker.disconnected += s =>
                    {
                        client.Disconnect(false);
                        MasterServerNetworker = null;
                    };
                }
                catch
                {
                    // If anything fails, then this client needs to be disconnected
                    client.Disconnect(true);
                    client = null;
                }
            };

            client.Connect(_masterServerHost, _masterServerPort);

            Networker.disconnected += NetworkerDisconnected;
            MasterServerNetworker   = client;
        }
        private void UpdateMasterServerListing(JSONNode masterServerData)
        {
            if (string.IsNullOrEmpty(_masterServerHost))
            {
                throw new System.Exception("This server is not registered on a master server, please ensure that you are passing a master server host and port into the initialize");
            }

            if (MasterServerNetworker == null)
            {
                throw new System.Exception("Connection to master server is closed. Make sure to be connected to master server before update trial");
            }

            // The Master Server communicates over TCP
            TCPMasterClient client = new TCPMasterClient();

            // Once this client has been accepted by the master server it should send it's update request
            client.serverAccepted += (sender) =>
            {
                try
                {
                    Text temp = Text.CreateFromString(client.Time.Timestep, masterServerData.ToString(), true, Receivers.Server, MessageGroupIds.MASTER_SERVER_UPDATE, true);

                    // Send the request to the server
                    client.Send(temp);
                }
                finally
                {
                    // If anything fails, then this client needs to be disconnected
                    client.Disconnect(true);
                    client = null;
                }
            };

            client.Connect(_masterServerHost, _masterServerPort);
        }
示例#3
0
        private void RegisterOnMasterServer(ushort port, int maxPlayers, string masterServerHost, ushort masterServerPort, bool useElo = false, int eloRequired = 0)
        {
            // The Master Server communicates over TCP
            TCPMasterClient client = new TCPMasterClient();

            // Once this client has been accepted by the master server it should send it's get request
            client.serverAccepted += () =>
            {
                try
                {
                    // Create the get request with the desired filters
                    JSONNode  sendData     = JSONNode.Parse("{}");
                    JSONClass registerData = new JSONClass();
                    registerData.Add("id", "myGame");
                    registerData.Add("name", "Forge Game");
                    registerData.Add("port", new JSONData(port));
                    registerData.Add("playerCount", new JSONData(0));
                    registerData.Add("maxPlayers", new JSONData(maxPlayers));
                    registerData.Add("comment", "Demo comment...");
                    registerData.Add("type", "Deathmatch");
                    registerData.Add("mode", "Teams");
                    registerData.Add("protocol", "udp");
                    registerData.Add("elo", new JSONData(eloRequired));
                    registerData.Add("useElo", new JSONData(useElo));

                    sendData.Add("register", registerData);

                    Frame.Text temp = Frame.Text.CreateFromString(client.Time.Timestep, sendData.ToString(), true, Receivers.Server, MessageGroupIds.MASTER_SERVER_REGISTER, true);

                    //Debug.Log(temp.GetData().Length);
                    // Send the request to the server
                    client.Send(temp);
                }
                catch
                {
                    // If anything fails, then this client needs to be disconnected
                    client.Disconnect(true);
                    client = null;
                }
            };

            client.Connect(masterServerHost, masterServerPort);

            Networker.disconnected += () =>
            {
                client.Disconnect(false);
                MasterServerNetworker = null;
            };

            MasterServerNetworker = client;
        }
示例#4
0
        public virtual void MatchmakingServersFromMasterServer(string masterServerHost,
                                                               ushort masterServerPort,
                                                               int elo,
                                                               System.Action <MasterServerResponse> callback = null,
                                                               string gameId   = "myGame",
                                                               string gameType = "any",
                                                               string gameMode = "all")
        {
            // The Master Server communicates over TCP
            TCPMasterClient client = new TCPMasterClient();

            // Once this client has been accepted by the master server it should send it's get request
            client.serverAccepted += (sender) =>
            {
                try
                {
                    // Create the get request with the desired filters
                    JSONNode  sendData = JSONNode.Parse("{}");
                    JSONClass getData  = new JSONClass();
                    getData.Add("id", gameId);
                    getData.Add("type", gameType);
                    getData.Add("mode", gameMode);
                    getData.Add("elo", new JSONData(elo));

                    sendData.Add("get", getData);

                    // Send the request to the server
                    client.Send(Text.CreateFromString(client.Time.Timestep, sendData.ToString(), true, Receivers.Server, MessageGroupIds.MASTER_SERVER_GET, true));
                }
                catch
                {
                    // If anything fails, then this client needs to be disconnected
                    client.Disconnect(true);
                    client = null;

                    MainThreadManager.Run(() =>
                    {
                        if (callback != null)
                        {
                            callback(null);
                        }
                    });
                }
            };

            // An event that is raised when the server responds with hosts
            client.textMessageReceived += (player, frame, sender) =>
            {
                try
                {
                    // Get the list of hosts to iterate through from the frame payload
                    JSONNode data = JSONNode.Parse(frame.ToString());
                    MainThreadManager.Run(() =>
                    {
                        if (data["hosts"] != null)
                        {
                            MasterServerResponse response = new MasterServerResponse(data["hosts"].AsArray);
                            if (callback != null)
                            {
                                callback(response);
                            }
                        }
                        else
                        {
                            if (callback != null)
                            {
                                callback(null);
                            }
                        }
                    });
                }
                finally
                {
                    if (client != null)
                    {
                        // If we succeed or fail the client needs to disconnect from the Master Server
                        client.Disconnect(true);
                        client = null;
                    }
                }
            };

            try
            {
                client.Connect(masterServerHost, masterServerPort);
            }
            catch (System.Exception ex)
            {
                Debug.LogError(ex.Message);
                MainThreadManager.Run(() =>
                {
                    if (callback != null)
                    {
                        callback(null);
                    }
                });
            }
        }
示例#5
0
    public void Refresh()
    {
        ClearServers();

        if (lan)
        {
            NetWorker.RefreshLocalUdpListings(ushort.Parse(portNumber.text));
            return;
        }

        // The Master Server communicates over TCP
        TCPMasterClient client = new TCPMasterClient();

        // Once this client has been accepted by the master server it should sent it's get request
        client.serverAccepted += x =>
        {
            try
            {
                // The overall game id to select from
                string gameId = "myGame";

                // The game type to choose from, if "any" then all types will be returned
                string gameType = "any";

                // The game mode to choose from, if "all" then all game modes will be returned
                string gameMode = "all";

                // Create the get request with the desired filters
                JSONNode  sendData = JSONNode.Parse("{}");
                JSONClass getData  = new JSONClass();

                // The id of the game to get
                getData.Add("id", gameId);
                getData.Add("type", gameType);
                getData.Add("mode", gameMode);

                sendData.Add("get", getData);

                // Send the request to the server
                client.Send(BeardedManStudios.Forge.Networking.Frame.Text.CreateFromString(client.Time.Timestep, sendData.ToString(), true, Receivers.Server, MessageGroupIds.MASTER_SERVER_GET, true));
            }
            catch
            {
                // If anything fails, then this client needs to be disconnected
                client.Disconnect(true);
                client = null;
            }
        };

        // An event that is raised when the server responds with hosts
        client.textMessageReceived += (player, frame, sender) =>
        {
            try
            {
                // Get the list of hosts to iterate through from the frame payload
                JSONNode data = JSONNode.Parse(frame.ToString());
                if (data["hosts"] != null)
                {
                    // Create a C# object for the response from the master server
                    MasterServerResponse response = new MasterServerResponse(data["hosts"].AsArray);

                    if (response != null && response.serverResponse.Count > 0)
                    {
                        // Go through all of the available hosts and add them to the server browser
                        foreach (MasterServerResponse.Server server in response.serverResponse)
                        {
                            Debug.Log("Found server " + server.Name);

                            // Update UI

                            MainThreadManager.Run(() =>
                            {
                                AddServer(server.Name, server.Address, server.PlayerCount, server.Mode);
                            });
                        }
                    }
                }
            }
            finally
            {
                if (client != null)
                {
                    // If we succeed or fail the client needs to disconnect from the Master Server
                    client.Disconnect(true);
                    client = null;
                }
            }
        };

        client.Connect(masterServerHost, (ushort)masterServerPort);
    }