示例#1
0
        public IEnumerator SetServer(NetConnection server, NetZone zone)
        {
            zone.Assigned = true;

            if (server != Socket.Self)
            {
                var setServerRequest = Socket.Request.Send <string>("AssignZone", server, zone);

                yield return(setServerRequest.WaitUntilDone);

                if (!setServerRequest.IsSuccessful)
                {
                    zone.Assigned = false;
                    yield break;
                }

                zone.PublicEndpoint = setServerRequest.Result;
            }
            else
            {
                NetZoneServer zoneServ = GetComponent <NetZoneServer>();
                zoneServ.AssignZoneSelf(zone);
            }

            zone.Server         = server;
            zone.ServerEndpoint = zone.Server.Endpoint;
            if (string.IsNullOrEmpty(zone.PublicEndpoint))
            {
                zone.PublicEndpoint = zone.Server.Endpoint.ToString();
            }
            ZoneAssigned(zone);
            NetLog.Info("Server assigned to zone. Endpoint: " + server.Endpoint);
            zone.Available = true;
        }
示例#2
0
        public IEnumerator SetServer(NetConnection server, NetZone zone)
        {
            zone.Assigned = true;

            if (server != Socket.Self) {
                var setServerRequest = Socket.Request.Send<string>("AssignZone", server, zone);

                yield return setServerRequest.WaitUntilDone;

                if (!setServerRequest.IsSuccessful) {
                    zone.Assigned = false;
                    yield break;
                }

                zone.PublicEndpoint = setServerRequest.Result;
            } else {
                NetZoneServer zoneServ = GetComponent<NetZoneServer>();
                zoneServ.AssignZoneSelf(zone);
            }

            zone.Server = server;
            zone.ServerEndpoint = zone.Server.Endpoint;
            if (string.IsNullOrEmpty(zone.PublicEndpoint)) zone.PublicEndpoint = zone.Server.Endpoint.ToString();
            ZoneAssigned(zone);
            NetLog.Info("Server assigned to zone. Endpoint: " + server.Endpoint);
            zone.Available = true;
        }
示例#3
0
 internal void ZoneAssigned(NetZone zone)
 {
     if (unassignedPeers.ContainsKey(zone.Server))
     {
         unassignedPeers.Remove(zone.Server);
     }
     for (int i = 0; i < zones.Count; i++)
     {
         NetZone peerZone = zones[i];
         if (zone == peerZone || peerZone.Server == null)
         {
             continue;
         }
         if (peerZone.Server == Socket.Self)
         {
             GetComponent <NetZoneServer>().AddPeerSelf(zone);
         }
         if (zone.Server != Socket.Self)
         {
             Socket.Send("AddPeer", zone.Server, peerZone);
         }
         if (peerZone.Server != Socket.Self)
         {
             Socket.Send("AddPeer", peerZone.Server, zone);
         }
     }
 }
示例#4
0
        /// <summary> Attempts to find a Zone for the provided position. </summary>
        public bool TryGetZone(Vector3 position, out NetZone zone)
        {
            NetZone closestZone     = null;
            float   closestDistance = float.MaxValue;

            foreach (NetZone z in zones)
            {
                if (!z.Assigned)
                {
                    continue;
                }

                float distance = z.Distance(position);
                if (distance > z.HandoverMaxDistance)
                {
                    continue;
                }
                if (distance > closestDistance)
                {
                    continue;
                }

                closestZone     = z;
                closestDistance = distance;
            }

            if (closestZone == null)
            {
                zone = null;
                return(false);
            }

            zone = closestZone;
            return(true);
        }
示例#5
0
 internal void AssignZoneSelf(NetZone zone)
 {
     if (ViewManager.GenerateViewId == null) ViewManager.GenerateViewId += AllocateViewId;
     NetLog.Info("Assigned to zone.");
     Debug.Log("Zone Position:" + zone.Position);
     self = zone;
     if (OnAssignment != null) OnAssignment();
 }
示例#6
0
 private void RemovePeer(NetZone zone, NetConnection connection)
 {
     if (!connection.IsPeer && !connection.IsServer)
     {
         return;
     }
     RemovePeerSelf(zone);
 }
示例#7
0
        internal void AddPeerSelf(NetZone zone) {
            if (peerLookup.ContainsKey(zone.ServerEndpoint)) peerLookup.Remove(zone.ServerEndpoint);
            peerLookup.Add(zone.ServerEndpoint, zone);

            if (peers.Contains(zone)) peers.Remove(zone);
            peers.Add(zone);

            if (Socket.EndpointConnected(zone.ServerEndpoint)) PeerConnected(Socket.EndpointToConnection(zone.ServerEndpoint));
        }
示例#8
0
 private string AssignZone(NetZone zone, NetConnection connection)
 {
     if (!connection.IsPeer && !connection.IsServer)
     {
         return("");
     }
     AssignZoneSelf(zone);
     return(Socket.Address);
 }
示例#9
0
        /// <summary> Creates a Zone at the specified position. </summary>
        public NetZone CreateZone(Vector3 position)
        {
            if (!CanAllocateIdBlock()) {
                throw new Exception("Cannot Create Zone: Unable to allocate ViewID block, limit has been reached.");
            }

            var newZone = new NetZone(position, GetViewIdMin(), GetViewIdMax());
            zones.Add(newZone);
            AssignServers();
            return newZone;
        }
示例#10
0
 private void AddPeer(NetZone zone, NetConnection connection)
 {
     if (!connection.IsPeer && !connection.IsServer)
     {
         return;
     }
     AddPeerSelf(zone);
     if (!Socket.EndpointConnected(zone.ServerEndpoint))
     {
         Socket.ConnectToPeer(zone.ServerEndpoint);
     }
 }
示例#11
0
 internal void AssignZoneSelf(NetZone zone)
 {
     if (ViewManager.GenerateViewId == null)
     {
         ViewManager.GenerateViewId += AllocateViewId;
     }
     NetLog.Info("Assigned to zone.");
     self = zone;
     if (OnAssignment != null)
     {
         OnAssignment();
     }
 }
示例#12
0
 private void StartHandoff(NetView view, NetZone peer)
 {
     view.SendCreatorData(peer.Server);
     view.Server = peer.Server;
     foreach (NetConnection connection in view.Controllers)
     {
         if (connection.View == view)
         {
             connection.InternalScope = view.Scope;
         }
     }
     ViewManager.DestroyView(view);
 }
示例#13
0
        /// <summary> Creates a Zone at the specified position. </summary>
        public NetZone CreateZone(Vector3 position)
        {
            if (!CanAllocateIdBlock())
            {
                throw new Exception("Cannot Create Zone: Unable to allocate ViewID block, limit has been reached.");
            }

            var newZone = new NetZone(position, GetViewIdMin(), GetViewIdMax());

            zones.Add(newZone);
            AssignServers();
            return(newZone);
        }
示例#14
0
        private void PeerConnected(NetConnection connection)
        {
            if (!peerLookup.ContainsKey(connection.Endpoint))
            {
                return;
            }
            NetZone peer = peerLookup[connection.Endpoint];

            NetLog.Info("Connection is Zone Peer: " + peer);
            peer.Server = connection;
            connection.InternalScope              = new NetScope();
            connection.InternalScope.Position     = peer.Position;
            connection.InternalScope.OutScopeDist = (int)(peer.HandoverMaxDistance * 1.25);
            connection.InternalScope.InScopeDist  = peer.HandoverMaxDistance;
        }
示例#15
0
        private void IncrementalHandoffCheck()
        {
            int pos = incHandoffFrame * handoffBatchSize;

            for (int i = pos; i < pos + handoffBatchSize; i++)
            {
                if (i >= ViewManager.Views.Count)
                {
                    break;
                }

                NetView view = ViewManager.Views[i];
                if (view.Server != Socket.Self)
                {
                    continue;
                }

                float dist = Vector3.Distance(view.Scope.Position, self.Position);
                if (dist < self.HandoverDistance)
                {
                    continue;
                }

                for (int j = 0; j < peers.Count; j++)
                {
                    NetZone peer     = peers[j];
                    float   peerDist = Vector3.Distance(view.Scope.Position, peer.Position);
                    if (peerDist > peer.HandoverMinDistance && (dist < self.HandoverMaxDistance || dist < peerDist))
                    {
                        continue;
                    }
                    StartHandoff(view, peer);
                    break;
                }
            }

            incHandoffFrame++;
            if (incHandoffFrame != (Application.targetFrameRate * 2))
            {
                return;
            }

            incHandoffFrame  = 0;
            handoffBatchSize = (ViewManager.Views.Count / (Application.targetFrameRate * 2)) + 1;
        }
示例#16
0
        internal void AddPeerSelf(NetZone zone)
        {
            if (peerLookup.ContainsKey(zone.ServerEndpoint))
            {
                peerLookup.Remove(zone.ServerEndpoint);
            }
            peerLookup.Add(zone.ServerEndpoint, zone);

            if (peers.Contains(zone))
            {
                peers.Remove(zone);
            }
            peers.Add(zone);

            if (Socket.EndpointConnected(zone.ServerEndpoint))
            {
                PeerConnected(Socket.EndpointToConnection(zone.ServerEndpoint));
            }
        }
示例#17
0
        internal void RemovePeerSelf(NetZone zone)
        {
            NetLog.Info("Removing zone");

            if (peerLookup.ContainsKey(zone.ServerEndpoint))
            {
                peerLookup.Remove(zone.ServerEndpoint);
            }
            else if (peerLookup.ContainsValue(zone))
            {
                NetLog.Warning("RemovePeer: Zone endpoint mismatch.");
            }

            if (peers.Contains(zone))
            {
                peers.Remove(zone);
            }
            else
            {
                NetLog.Info("RemovePeer: Zone not in peer list.");
            }
        }
示例#18
0
        /// <summary> Attempts to find a Zone for the provided position. </summary>
        public bool TryGetZone(Vector3 position, out NetZone zone) {

            NetZone closestZone = null;
            float closestDistance = float.MaxValue;

            foreach (NetZone z in zones) {
                if (!z.Assigned) continue;

                float distance = z.Distance(position);
                if (distance > z.HandoverMaxDistance) continue;
                if (distance > closestDistance) continue;

                closestZone = z;
                closestDistance = distance;
            }

            if (closestZone == null) {
                zone = null;
                return false;
            }

            zone = closestZone;
            return true;
        }
示例#19
0
 internal void ZoneAssigned(NetZone zone)
 {
     if (unassignedPeers.ContainsKey(zone.Server)) unassignedPeers.Remove(zone.Server);
     for (int i = 0; i < zones.Count; i++) {
         NetZone peerZone = zones[i];
         if (zone == peerZone || peerZone.Server == null) continue;
         if (peerZone.Server == Socket.Self) GetComponent<NetZoneServer>().AddPeerSelf(zone);
         if (zone.Server != Socket.Self) Socket.Send("AddPeer", zone.Server, peerZone);
         if (peerZone.Server != Socket.Self) Socket.Send("AddPeer", peerZone.Server, zone);
     }
 }
示例#20
0
        internal void RemovePeerSelf(NetZone zone) {
            NetLog.Info("Removing zone");

            if (peerLookup.ContainsKey(zone.ServerEndpoint)) peerLookup.Remove(zone.ServerEndpoint);
            else if (peerLookup.ContainsValue(zone)) NetLog.Warning("RemovePeer: Zone endpoint mismatch.");

            if (peers.Contains(zone)) peers.Remove(zone);
            else NetLog.Info("RemovePeer: Zone not in peer list.");
        }
示例#21
0
 private void StartHandoff(NetView view, NetZone peer) {
     view.SendCreatorData(peer.Server);
     view.Server = peer.Server;
     foreach (NetConnection connection in view.Controllers) {
         if (connection.View == view) connection.InternalScope = view.Scope;
     }
     ViewManager.DestroyView(view);
 }
示例#22
0
 private string AssignZone(NetZone zone, NetConnection connection) {
     if (!connection.IsPeer && !connection.IsServer) return "";
     AssignZoneSelf(zone);
     return Socket.Address;
 }
示例#23
0
 private void AddPeer(NetZone zone, NetConnection connection) {
     if (!connection.IsPeer && !connection.IsServer) return;
     AddPeerSelf(zone);
     if (!Socket.EndpointConnected(zone.ServerEndpoint)) Socket.ConnectToPeer(zone.ServerEndpoint);
 }
示例#24
0
 private void RemovePeer(NetZone zone, NetConnection connection) {
     if (!connection.IsPeer && !connection.IsServer) return;
     RemovePeerSelf(zone);
 }