public override void OnReceivedBroadcast(string aFromAddress, string aRawData)
        {
            BroadcastData data = new BroadcastData();

            data.FromString(aRawData);

            // DEBUG LOG
            broadcastLogCounter += 1;
            receivedBroadcastLog.Enqueue(broadcastLogTokens[broadcastLogCounter % broadcastLogTokens.Length] + " " + aRawData);
            if (receivedBroadcastLog.Count > maxLogLines)
            {
                receivedBroadcastLog.Dequeue();
            }

            var server = new DiscoveredServer(data);

            server.rawData   = aRawData;
            server.ipAddress = aFromAddress;
            server.timestamp = Time.time;

            bool newData = false;

            if (!discoveredServers.ContainsKey(aFromAddress))
            {
                // New server
                discoveredServers.Add(aFromAddress, server);
                Debug.Log(server.ipAddress);
                newData = true;
            }
            else
            {
                if (discoveredServers[aFromAddress].rawData != aRawData)
                {
                    // Old server with new info
                    discoveredServers[aFromAddress] = server;
                    newData = true;
                }
                else
                {
                    // Just update the timestamp
                    discoveredServers[aFromAddress].timestamp = Time.time;
                    newData = false;
                }
            }

            networkManager.OnReceivedBroadcast(aFromAddress, aRawData);

            if (newData)
            {
                networkManager.OnDiscoveredServer(server);
            }
        }
Exemple #2
0
        public void OnDiscoveredServer(DiscoveredServer aServer)
        {
            if (verboseLogging)
            {
                Debug.Log("#CaptainsMess# Discovered " + aServer.rawData);
            }

            if (discoveryServer.isOpen)
            {
                Debug.Log("#CaptainsMess# Already hosting a server, ignoring " + aServer.rawData);
                return;
            }

            SendDiscoveredServerMessage(aServer);

            bool shouldJoin = false;
            bool isMe       = (aServer.peerId == peerId);

            if (!isMe)
            {
                if (aServer.isOpen && aServer.numPlayers < maxPlayers)
                {
                    if (aServer.privateTeamKey == discoveryServer.privateTeamKey)
                    {
                        if (aServer.numPlayers > 0)
                        {
                            shouldJoin = true; // Pick the first server that already has players
                        }
                        else if (BestHostingCandidate() == aServer.peerId)
                        {
                            shouldJoin = true;
                        }
                    }
                }
            }
            Debug.Log("Discovered server: " + discoveryClient.discoveredServers.Count());
            Debug.Log("This client should joint : " + shouldJoin);
            if (shouldJoin)
            {
                if (verboseLogging)
                {
                    Debug.Log("#CaptainsMess# Should join!");
                }

                // We found something! Cancel hosting...
                CancelInvoke(maybeStartHostingFunction);

                if (client == null)
                {
                    if (discoveryClient.autoJoin)
                    {
                        Debug.Log("joining " + aServer.Name);
                        JoinServer(aServer.ipAddress, networkPort);
                    }
                    else
                    {
                        if (verboseLogging)
                        {
                            Debug.Log("#CaptainsMess# JOIN CANCELED: Auto join disabled.");
                        }
                    }
                }
                else
                {
                    if (verboseLogging)
                    {
                        Debug.Log("#CaptainsMess# JOIN CANCELED: Already have client.");
                    }
                }
            }
            else
            {
                if (verboseLogging)
                {
                    Debug.Log("#CaptainsMess# Should NOT join.");
                }
            }
        }
Exemple #3
0
 public void SendDiscoveredServerMessage(DiscoveredServer aServer)
 {
     handler.OnDiscoveredServer(aServer);
 }
 public virtual void OnDiscoveredServer(DiscoveredServer aServer)
 {
     // Override
 }