コード例 #1
0
 private void CheckPeer()
 {
     if (_myPeer.isInvalid())
     {
         _myPeer = HumbleNetAPI.myPeer;
         if (_myPeer.isValid())
         {
             peerIdText.text = string.Format("Peer ID: {0}", _myPeer);
         }
     }
 }
コード例 #2
0
    bool Run()
    {
        HumbleNet.P2P.Wait(100);

        if (myPeer.isInvalid())
        {
            myPeer = HumbleNet.P2P.getMyPeerId();
            if (myPeer.isValid())
            {
                Console.WriteLine("My Peer is {0}", myPeer);
                connected = true;
            }
        }

        if (connected)
        {
            List <HumbleNet.PeerId> toRemove = null;

            bool done = false;
            while (!done)
            {
                byte[] data;

                HumbleNet.PeerId otherPeer;
                if (HumbleNet.P2P.RecvFrom(out data, out otherPeer, CHANNEL) > 0)
                {
                    processMessage(otherPeer, data);
                }
                else if (otherPeer.isValid())
                {
                    if (toRemove == null)
                    {
                        toRemove = new List <HumbleNet.PeerId>();
                    }
                    toRemove.Insert(0, otherPeer);
                }
                else
                {
                    done = true;
                }
            }

            DateTime now = DateTime.UtcNow;

            var span = now.Subtract(last);
            if (span.TotalSeconds > 1)
            {
                foreach (var peer in pendingPeers)
                {
                    if (!sendHello(peer))
                    {
                        if (toRemove == null)
                        {
                            toRemove = new List <HumbleNet.PeerId>();
                        }
                        toRemove.Insert(0, peer);
                    }
                }
                last = now;
            }

            // disconnect lost peers
            if (toRemove != null)
            {
                foreach (var peer in toRemove)
                {
                    disconnectPeer(peer);
                }
            }
        }
        return(_running);
    }