/// <summary> /// this is when a session is removed from the system, we need to find which zone's they were interested in and remove them from those zones /// </summary> /// <param name="sessionIdToClose"></param> public void ProcessDisconnectSession(Guid sessionIdToClose) { //find the distributed objects owned by the session List <DistributedObjectId> distributedObjectsOwnedBySession = mServerObjectRepository.GetDistributedObjectIdsOwnedBySessionId(sessionIdToClose); //find the zones this session was interested in... List <ZoneId> zonesToDisconnectSessionFrom = mSessionManager.GetZoneIdsFromInterestedSessionId(sessionIdToClose); foreach (ZoneId zoneId in zonesToDisconnectSessionFrom) { //...find everyone else in that zone... List <Guid> connectedSessionsInZone = mSessionManager.GetSessionIdsInterestedInZoneId(zoneId); //we remove the session to close from this list so we don't broadcast a message to a diconnected session connectedSessionsInZone.Remove(sessionIdToClose); //...broadcast an object delete for all the objects belonging to the disconnected session id... foreach (DistributedObjectId distributedObjectId in distributedObjectsOwnedBySession) { IServerDistributedObject distributedObjectToDelete = (IServerDistributedObject)mServerObjectRepository.GetObject(distributedObjectId); this.SendDestroyObjectMessage(distributedObjectToDelete, connectedSessionsInZone); } } //take care of the backend cleanup mSessionManager.RemoveSession(sessionIdToClose); mServerObjectRepository.RemoveSessionId(sessionIdToClose); }
private void SendDestroyObjectMessage(IServerDistributedObject obj, Guid sessionId) { List <Guid> sessionIds = new List <Guid>(); sessionIds.Add(sessionId); SendDestroyObjectMessage(obj, sessionIds); }
private void SendDestroyObjectMessage(IServerDistributedObject obj, List <Guid> sessionIds) { Message deleteObjectMessage = new Message(); deleteObjectMessage.DeleteObjectMessage(false, obj.DistributedObjectId, obj.Data); mSendMessageToReflectorCallback(deleteObjectMessage, sessionIds); }
// Returns the zone that this object is currently in. public ZoneId GetZone(IServerDistributedObject obj) { ZoneId zoneId = null; mDistributedObjectIdToObjectZones.TryGetValue(obj.DistributedObjectId, out zoneId); return(zoneId); }
private void SendCreateObjectMessage(IServerDistributedObject obj, List <Guid> sessionIds) { Message createMessage = new Message(); createMessage.CreateObjectMessage(false, false, obj.DistributedObjectId, obj.Data); mSendMessageToReflectorCallback(createMessage, sessionIds); }
public void RemoveObject(IServerDistributedObject obj) { base.RemoveObject(obj); DistributedObjectId doId = obj.DistributedObjectId; // Remove from object zones dict ZoneId zone = null; mDistributedObjectIdToObjectZones.TryGetValue(doId, out zone); mDistributedObjectIdToObjectZones.Remove(doId); // Remove from zone dict List <IServerDistributedObject> objList = null; if (zone != null && mZoneDict.TryGetValue(zone, out objList)) { objList.Remove(obj); } //remove from mSessionIdsToDistributedObjectIds foreach (KeyValuePair <Guid, List <DistributedObjectId> > sessionIdToDistributedObjectIds in mSessionIdsToDistributedObjectIds) { sessionIdToDistributedObjectIds.Value.Remove(doId); } }
public void SendDistributedObjectStateUpdate(IServerDistributedObject distributedObject) { Message distributedObjectStateUpdateMessage = null; if (distributedObject.DistributedObjectStateUpdate(out distributedObjectStateUpdateMessage)) { BroadcastDistributedObjectUpdateMessageToZone(distributedObjectStateUpdateMessage, distributedObject.DistributedObjectId); } }
// Add this DistributedObject to the repository. public void AddObjectToSessionId(Guid sessionId, IServerDistributedObject obj) { if (!mSessionIdsToDistributedObjectIds.ContainsKey(sessionId)) { mSessionIdsToDistributedObjectIds[sessionId] = new List <DistributedObjectId>(); } DistributedObjectId doId = obj.DistributedObjectId; mSessionIdsToDistributedObjectIds[sessionId].Add(doId); // Store in the dictionary of objects mObjectIds[doId] = obj; }
//broadcasts the message to all session ids listening to that zone public ZoneId RemoveObjectFromZone(IServerDistributedObject obj) { ZoneId zoneId = mServerObjectRepository.GetZone(obj); if (zoneId == null) { mLogger.Warn("object " + obj.DistributedObjectId + " was already in null zone"); return(null); } // Sessions interested in the zone this object is being deleted from List <Guid> sessionsInterestedInZone = mSessionManager.GetSessionIdsInterestedInZoneId(zoneId); SendDestroyObjectMessage(obj, sessionsInterestedInZone); mServerObjectRepository.SetObjectZone(obj, null); return(zoneId); }
// This object is changing zones, so record the new zone public void SetObjectZone(IServerDistributedObject obj, ZoneId zone) { // Record this object as moving into this new zone. // Cleanup any references to the old zone it was in. DistributedObjectId doId = obj.DistributedObjectId; // First grab the old zone ZoneId oldZone = null; mDistributedObjectIdToObjectZones.TryGetValue(doId, out oldZone); // Now store the new zone mDistributedObjectIdToObjectZones[doId] = zone; // If it had a zone before, remove it from the ZoneDict's list if (oldZone != null) { // Remove obj from the old zone list List <IServerDistributedObject> objList = null; if (mZoneDict.TryGetValue(oldZone, out objList)) { objList.Remove(obj); // We don't remove an empty list here because the assumption // is that it will be used again in the near future... } } // Now add this object to the ZoneDict's list in the new zone if (zone != null) { // Create an object list for this zone if it is not there already... List <IServerDistributedObject> objList = null; if (!mZoneDict.TryGetValue(zone, out objList)) { // New zone being used, create a new list objList = new List <IServerDistributedObject>(); mZoneDict.Add(zone, objList); } objList.Add(obj); } }
public void ReceiveRequest(Message receivedMessage, Guid senderId) { switch (receivedMessage.MessageType) { case MessageType.Update: if (receivedMessage.DistributedObjectId == null) { StateServerAssert.Assert(new System.Exception("receivedMessage.DistributedObjectId is null")); } else { IServerDistributedObject serverDistributedObject = (IServerDistributedObject)this.GetObject(receivedMessage.DistributedObjectId); if (serverDistributedObject == null) { Console.WriteLine("WARNING: ReceiveMessage for object not on server: %i", receivedMessage.DistributedObjectId); } else { serverDistributedObject.ProcessMessage(receivedMessage); } } break; } }
public void ProcessZoneChange(IServerDistributedObject obj, ZoneId zoneId) { if (zoneId == null) { mLogger.Error("ZoneId is null!!"); return; } //Console.WriteLine("ProcessZoneChange objId = " + obj.DistributedObjectId + " newZoneId = " + zoneId); ZoneId oldZoneId = mServerObjectRepository.GetZone(obj); //Console.WriteLine("old zone = " + oldZoneId); if (zoneId == oldZoneId) { mLogger.Warn("Object wants to change to zone it is already in."); return; } // Sessions interested in the zone this object came from List <Guid> sessionsInterestedInOldZone = new List <Guid>(); if (oldZoneId != null) { sessionsInterestedInOldZone = mSessionManager.GetSessionIdsInterestedInZoneId(oldZoneId); } // Sessions interested in the zone this object is going to List <Guid> sessionsInterestedInNewZone = mSessionManager.GetSessionIdsInterestedInZoneId(zoneId); // Record the new zone on this object in the repository mServerObjectRepository.SetObjectZone(obj, zoneId); // Send object create messages to all sessions interested in the new zone // but were not interested in the old zone List <Guid> sessionsNeedingCreates = new List <Guid>(); List <Guid> sessionsNeedingDeletes = new List <Guid>(); List <Guid> sessionsNeedingZoneChanges = new List <Guid>(); foreach (Guid sessionId in sessionsInterestedInNewZone) { //if no one is interested in the old zone, then we just need to send create messages if (sessionsInterestedInOldZone == null || !sessionsInterestedInOldZone.Contains(sessionId)) { sessionsNeedingCreates.Add(sessionId); } else { sessionsNeedingZoneChanges.Add(sessionId); } } SendCreateObjectMessage(obj, sessionsNeedingCreates); mServerObjectRepository.SendDistributedObjectStateUpdate(obj); foreach (Guid sessionId in sessionsInterestedInOldZone) { //if theres a client who was interested in the old zone but is not interested in the zone this object is being moved to, we need to delete it for that client if (!sessionsInterestedInNewZone.Contains(sessionId)) { sessionsNeedingDeletes.Add(sessionId); } } SendDestroyObjectMessage(obj, sessionsNeedingDeletes); }
public void RemoveObjectFromServer(IServerDistributedObject obj) { RemoveObjectFromZone(obj); mServerObjectRepository.RemoveObject(obj); }