예제 #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);
 }