Beispiel #1
0
        void ParseMessage(byte[] bytes)
        {
            // note: we don't use ReadString here because the list server
            //       doesn't know C#'s '7-bit-length + utf8' encoding for strings
            BinaryReader reader        = new BinaryReader(new MemoryStream(bytes, false), Encoding.UTF8);
            byte         ipBytesLength = reader.ReadByte();

            byte[] ipBytes = reader.ReadBytes(ipBytesLength);
            string ip      = new IPAddress(ipBytes).ToString();
            //ushort port = reader.ReadUInt16(); <- not all Transports use a port. assume default.
            ushort players     = reader.ReadUInt16();
            ushort capacity    = reader.ReadUInt16();
            ushort titleLength = reader.ReadUInt16();
            string title       = Encoding.UTF8.GetString(reader.ReadBytes(titleLength));
            //Debug.Log("PARSED: ip=" + ip + /*" port=" + port +*/ " players=" + players + " capacity= " + capacity + " title=" + title);

            // build key
            string key = ip /* + ":" + port*/;

            // find existing or create new one
            ServerStatus server;

            if (list.TryGetValue(key, out server))
            {
                // refresh
                server.title    = title;
                server.players  = players;
                server.capacity = capacity;
            }
            else
            {
                // create
                server = new ServerStatus(ip, /*port,*/ title, players, capacity);
            }

            // save
            list[key] = server;
        }
Beispiel #2
0
        void ParseMessage(byte[] bytes)
        {
            // use binary reader because our NetworkReader uses custom string reading with bools
            // => we don't use ReadString here because the listen server doesn't
            //    know C#'s '7-bit-length + utf8' encoding for strings
            BinaryReader reader   = new BinaryReader(new MemoryStream(bytes, false), Encoding.UTF8);
            ushort       ipLength = reader.ReadUInt16();
            string       ip       = new string(reader.ReadChars(ipLength));
            //ushort port = reader.ReadUInt16(); <- not all Transports use a port. assume default.
            ushort titleLength = reader.ReadUInt16();
            string title       = new string(reader.ReadChars(titleLength));
            ushort players     = reader.ReadUInt16();
            ushort capacity    = reader.ReadUInt16();
            //Debug.Log("PARSED: ip=" + ip + /*" port=" + port +*/ " title=" + title + " players=" + players + " capacity= " + capacity);

            // build key
            string key = ip /* + ":" + port*/;

            // find existing or create new one
            ServerStatus server;

            if (list.TryGetValue(key, out server))
            {
                // refresh
                server.title    = title;
                server.players  = players;
                server.capacity = capacity;
            }
            else
            {
                // create
                server = new ServerStatus(ip, /*port,*/ title, players, capacity);
            }

            // save
            list[key] = server;
        }
Beispiel #3
0
        void OnUI()
        {
            // only show while client not connected and server not started
            if (!NetworkManager.singleton.isNetworkActive || IsConnecting())
            {
                mainPanel.SetActive(true);

                // status text
                if (clientToListenConnection.Connecting)
                {
                    //statusText.color = Color.yellow;
                    statusText.text = "Connecting...";
                }
                else if (clientToListenConnection.Connected)
                {
                    //statusText.color = Color.green;
                    statusText.text = "Connected!";
                }
                else
                {
                    //statusText.color = Color.gray;
                    statusText.text = "Disconnected";
                }

                // instantiate/destroy enough slots
                BalancePrefabs(slotPrefab.gameObject, list.Count, content);

                // refresh all members
                for (int i = 0; i < list.Values.Count; ++i)
                {
                    UIServerStatusSlot slot   = content.GetChild(i).GetComponent <UIServerStatusSlot>();
                    ServerStatus       server = list.Values.ToList()[i];
                    slot.titleText.text          = server.title;
                    slot.playersText.text        = server.players + "/" + server.capacity;
                    slot.latencyText.text        = server.lastLatency != -1 ? server.lastLatency.ToString() : "...";
                    slot.addressText.text        = server.ip;
                    slot.joinButton.interactable = !IsConnecting();
                    slot.joinButton.gameObject.SetActive(server.players < server.capacity);
                    slot.joinButton.onClick.RemoveAllListeners();
                    slot.joinButton.onClick.AddListener(() =>
                    {
                        NetworkManager.singleton.networkAddress = server.ip;
                        NetworkManager.singleton.StartClient();
                    });
                }

                // server buttons
                serverAndPlayButton.interactable = !IsConnecting();
                serverAndPlayButton.onClick.RemoveAllListeners();
                serverAndPlayButton.onClick.AddListener(() =>
                {
                    NetworkManager.singleton.StartHost();
                });

                serverOnlyButton.interactable = !IsConnecting();
                serverOnlyButton.onClick.RemoveAllListeners();
                serverOnlyButton.onClick.AddListener(() =>
                {
                    NetworkManager.singleton.StartServer();
                });
            }
            else
            {
                mainPanel.SetActive(false);
            }

            // show connecting panel while connecting
            if (IsConnecting())
            {
                connectingPanel.SetActive(true);

                // . => .. => ... => ....
                connectingDots      = ((connectingDots + 1) % 4);
                connectingText.text = "Connecting" + new string('.', connectingDots);

                // cancel button
                connectingCancelButton.onClick.RemoveAllListeners();
                connectingCancelButton.onClick.AddListener(NetworkManager.singleton.StopClient);
            }
            else
            {
                connectingPanel.SetActive(false);
            }
        }