示例#1
0
    public override void OnReceivedBroadcast(string aFromAddress, string aRawData)
    {
        BeerPongBroadcastData data = new BeerPongBroadcastData();

        data.FromString(aRawData);

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

        var server = new BeerPongDiscoveredServer(data);

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

        bool newData = false;

        if (!discoveredServers.ContainsKey(aFromAddress))
        {
            //New Server
            discoveredServers.Add(aFromAddress, server);
            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);
        }
    }
示例#2
0
 public void SendDiscoveredServerMessage(BeerPongDiscoveredServer aServer)
 {
     listener.OnDiscoveredServer(aServer);
 }
示例#3
0
 public virtual void OnDiscoveredServer(BeerPongDiscoveredServer aServer)
 {
     // Override
 }
示例#4
0
    public void OnDiscoveredServer(BeerPongDiscoveredServer aServer)
    {
        if (verboseLogging)
        {
            Debug.Log("#BeerPong# Discoverd" + aServer.rawData);
        }

        if (discoveryServer.isOpen)
        {
            Debug.Log("#BeerPong# 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 player
                    }
                    else if (BestHostingCandidate() == aServer.peerId)
                    {
                        shouldJoin = true;
                    }
                }
            }
        }

        if (shouldJoin)
        {
            if (verboseLogging)
            {
                Debug.Log("#BeerPong# Should join!");
            }

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

            if (client == null)
            {
                if (discoveryClient.autoJoin)
                {
                    JoinServer(aServer.ipAddress, networkPort);
                }
                else
                {
                    if (verboseLogging)
                    {
                        Debug.Log("#BeerPong# JOIN CANCELED: Auto join disabled.");
                    }
                }
            }
            else
            {
                if (verboseLogging)
                {
                    Debug.Log("#BeerPong# JOIN CANCELED: Already have client.");
                }
            }
        }
        else
        {
            if (verboseLogging)
            {
                Debug.Log("#BeerPong# Should NOT join.");
            }
        }
    }