public void MarkDirty(IHasReference targetobject, Type[] dirtytypes) { foreach (DirtyObjectQueueSingleClient remoteclientdirtyqueue in remoteclientdirtyqueues.Values) { remoteclientdirtyqueue.MarkDirty(targetobject, dirtytypes); } }
public void Tick() { //LogFile.WriteLine("dirtyobjectqueuesingleclient.Tick()"); Queue <IHasReference> queue = new Queue <IHasReference>(); foreach (int reference in deleted.Keys) { parent.netreplicationcontroller.ReplicateDeletionToSingleClient( connection, reference, deleted[reference]); } deleted.Clear(); foreach (IHasReference entity in DirtyAttributesByEntity.Keys) { // Entity entity = (Entity)dirtyentityobject; // can add logic here to decide what objects are priority queue.Enqueue(entity); } for (int i = 0; i < Math.Min(1, queue.Count); i++) { IHasReference entity = queue.Dequeue(); { // entity.SendUpdate( connection, DirtyEntries[ entity ] ); parent.netreplicationcontroller.ReplicateSingleEntityToSingleClient( connection, entity, DirtyAttributesByEntity[entity].ToArray()); DirtyAttributesByEntity.Remove(entity); } } }
IReplicatedObjectController GetControllerForObject(IHasReference entity) { foreach (Type replicatedtype in objectcontrollers.Keys) { if (replicatedtype.IsInstanceOfType(entity)) { return(objectcontrollers[replicatedtype]); } } return(null); }
// incoming rpc calls // =================== public void ObjectCreatedRpcClientToServer(IPEndPoint connection, int remoteclientreference, string typename, int attributebitmap, byte[] entitydata) { LogFile.WriteLine("ObjectCreatedRpcClientToServer " + typename); //LogFile.WriteLine(Encoding.UTF8.GetString(entitydata)); // note to self: should probably make a whitelist of allowed typenames, maybe via primitive class registration Type newtype = Type.GetType(typename); IHasReference newobject = (IHasReference)Activator.CreateInstance(newtype); IReplicatedObjectController replicatedobjectcontroller = GetControllerForObject(newobject); List <Type> AttributeTypeList = new ReplicateAttributeHelper().BitmapToAttributeTypeArray(attributebitmap); int nextposition = 0; new BinaryPacker().UnpackIntoObjectUsingSpecifiedAttributes(entitydata, ref nextposition, newobject, new Type[] { typeof(Replicate) }); LogFile.WriteLine("server received replicated object: " + newobject.GetType()); int newobjectreference = 0; if (replicatedobjectcontroller != null && replicatedobjectcontroller is IReferenceGenerator) { newobjectreference = (replicatedobjectcontroller as IReferenceGenerator).GenerateReference(); } else { newobjectreference = nextreference; nextreference++; } LogFile.WriteLine("New object reference: " + newobjectreference); if (newobject is IHasReference) { (newobject as IHasReference).Reference = newobjectreference; } if (replicatedobjectcontroller != null) { replicatedobjectcontroller.ReplicatedObjectCreated(this, new ObjectCreatedArgs(DateTime.Now, (IHasReference)newobject)); // note to self: untested cast } new OSMP.NetworkInterfaces.ObjectReplicationServerToClient_ClientProxy(rpc, connection ).ObjectCreatedServerToCreatorClient( remoteclientreference, newobjectreference); dirtyobjectcontroller.MarkDirty(newobject, new Type[] { typeof(Replicate) }); }
public void ObjectCreatedRpcServerToCreatorClient(IPEndPoint connection, int clientreference, int globalreference) { LogFile.WriteLine("ObjectCreatedRpcServerToCreatorClient " + clientreference + " -> " + globalreference); IHasReference thisobject = this.tempreferences[clientreference]; //if (thisobject is IHasReference) //{ IReplicatedObjectController replicatedobjectcontroller = GetControllerForObject(thisobject); if (replicatedobjectcontroller == null) { LogFile.WriteLine("Warning, no replicatedobjectcontroller for type " + thisobject.GetType()); return; } replicatedobjectcontroller.AssignGlobalReference(thisobject, globalreference); //(thisobject as IHasReference).Reference = globalreference; //} tempreferences.Remove(clientreference); }
public void MarkDirty(IHasReference targetentity, Type[] dirtytypes) { if (!DirtyAttributesByEntity.ContainsKey(targetentity)) { DirtyAttributesByEntity.Add(targetentity, new List <Type>()); LogFile.WriteLine("dirtyobjectqueuesingleclient " + connection + " marking object " + targetentity + " dirty"); } foreach (Type type in dirtytypes) { if (!DirtyAttributesByEntity[targetentity].Contains(type)) { DirtyAttributesByEntity[targetentity].Add(type); } } //else //{ // DirtyEntities[ targetobject ] = ( (int)DirtyEntities[ targetobject ] ) | bitmask; //} }
// on server, called from dirtyobjectqueuesingleclient public void ReplicateSingleEntityToSingleClient(IPEndPoint connection, IHasReference entity, Type[] dirtyattributes) { LogFile.WriteLine("ReplicateSingleEntityToSingleClient " + entity.GetType() + " to " + connection); int bitmap = new ReplicateAttributeHelper().TypeArrayToBitmap(dirtyattributes); byte[] entitydata = new byte[4096]; int nextposition = 0; new BinaryPacker().PackObjectUsingSpecifiedAttributes(entitydata, ref nextposition, entity, dirtyattributes); byte[] entitydatatotransmit = new byte[nextposition]; Buffer.BlockCopy(entitydata, 0, entitydatatotransmit, 0, nextposition); //LogFile.WriteLine(Encoding.UTF8.GetString(entitydatatotransmit)); new NetworkInterfaces.ObjectReplicationServerToClient_ClientProxy( rpc, connection).ObjectModified( entity.Reference, entity.GetType().ToString(), bitmap, entitydatatotransmit); }
void IReplicatedObjectController.AssignGlobalReference(IHasReference entitytoassign, int globalreference) { // first check we dont already have an entity with same reference, but not same object // if we do it is duplicate, so delete foreach (Entity entity in entities) { if (entity.iReference == globalreference && entity != entitytoassign) { LogFile.WriteLine("removed duplicate " + globalreference); entities.Remove(entity); entitybyreference.Remove(globalreference); break; } } if (entitybyreference.ContainsKey(entitytoassign.Reference)) { entitybyreference.Remove(entitytoassign.Reference); } entitytoassign.Reference = globalreference; entitybyreference.Add(entitytoassign.Reference, entitytoassign as Entity); }
public void ObjectCreatedRpcServerToClient(IPEndPoint connection, int reference, string typename, int attributebitmap, byte[] entitydata) { LogFile.WriteLine("ObjectCreatedRpcServerToClient " + typename); Type newtype = Type.GetType(typename); IHasReference newobject = (IHasReference)Activator.CreateInstance(newtype); // note to self: insecure IReplicatedObjectController replicatedobjectcontroller = null; foreach (Type replicatedtype in objectcontrollers.Keys) { if (replicatedtype.IsInstanceOfType(newobject)) { replicatedobjectcontroller = objectcontrollers[replicatedtype]; } } if (replicatedobjectcontroller == null) { LogFile.WriteLine("Error: no controller for received type " + typename); return; } List <Type> AttributeTypeList = new ReplicateAttributeHelper().BitmapToAttributeTypeArray(attributebitmap); int nextposition = 0; new BinaryPacker().UnpackIntoObjectUsingSpecifiedAttributes(entitydata, ref nextposition, newobject, new Type[] { typeof(Replicate) }); if (!replicatedobjectcontroller.HasEntityForReference(newobject.Reference)) { LogFile.WriteLine("client received replicated object: " + newobject); replicatedobjectcontroller.ReplicatedObjectCreated(this, new ObjectCreatedArgs(DateTime.Now, newobject)); // note to self: untested cast } }
public ObjectModifiedArgs(DateTime timestamp, IHasReference TargetObject, Type[] modificationtypeattributes) { TimeStamp = timestamp; this.TargetObject = TargetObject; this.modificationtypeattributes = modificationtypeattributes; }
public void ObjectModifiedRpc(IPEndPoint connection, int reference, string typename, int attributebitmap, byte[] entity) { if (rpc.isserver) { Type modifiedobjecttype = Type.GetType(typename); IReplicatedObjectController replicatedobjectcontroller = GetControllerForType(modifiedobjecttype); if (replicatedobjectcontroller == null) { LogFile.WriteLine("ObjectModifiedRpc. Error: no controller for received type " + typename); return; } List <Type> AttributeTypeList = new ReplicateAttributeHelper().BitmapToAttributeTypeArray(attributebitmap); IHasReference thisobject = replicatedobjectcontroller.GetEntity(reference); if (thisobject == null) { LogFile.WriteLine("ObjectModifiedRpc received for unknown object " + reference); return; } int nextposition = 0; new BinaryPacker().UnpackIntoObjectUsingSpecifiedAttributes(entity, ref nextposition, thisobject, new Type[] { typeof(Replicate) }); LogFile.WriteLine("server received replicated modified object: " + thisobject); replicatedobjectcontroller.ReplicatedObjectModified(this, new ObjectModifiedArgs(DateTime.Now, thisobject, AttributeTypeList.ToArray())); dirtyobjectcontroller.MarkDirty(thisobject, AttributeTypeList.ToArray()); } else { LogFile.WriteLine("ObjectModifiedRpcServerToClient " + typename); Type modifiedobjecttype = Type.GetType(typename); IReplicatedObjectController replicatedobjectcontroller = GetControllerForType(modifiedobjecttype); if (replicatedobjectcontroller == null) { LogFile.WriteLine("ObjectModifiedRpc. Error: no controller for received type " + typename); return; } List <Type> AttributeTypeList = new ReplicateAttributeHelper().BitmapToAttributeTypeArray(attributebitmap); IHasReference thisobject = replicatedobjectcontroller.GetEntity(reference); bool objectisnew = false; if (thisobject == null) { LogFile.WriteLine("Creating new object"); thisobject = Activator.CreateInstance(modifiedobjecttype) as IHasReference; objectisnew = true; } int nextposition = 0; new BinaryPacker().UnpackIntoObjectUsingSpecifiedAttributes(entity, ref nextposition, thisobject, new Type[] { typeof(Replicate) }); LogFile.WriteLine("client received replicated modified object: " + thisobject); if (objectisnew) { replicatedobjectcontroller.ReplicatedObjectCreated(this, new ObjectCreatedArgs(DateTime.Now, thisobject)); } else { replicatedobjectcontroller.ReplicatedObjectModified(this, new ObjectModifiedArgs(DateTime.Now, thisobject, AttributeTypeList.ToArray())); } } }
public ObjectCreatedArgs(DateTime timestamp, IHasReference TargetObject) { TimeStamp = timestamp; this.TargetObject = TargetObject; }
public ObjectModifiedArgs(DateTime timestamp, IHasReference TargetObject, Type[]modificationtypeattributes) { TimeStamp = timestamp; this.TargetObject = TargetObject; this.modificationtypeattributes = modificationtypeattributes; }
public void MarkDirty(IHasReference targetentity, Type[] dirtytypes) { if (!DirtyAttributesByEntity.ContainsKey(targetentity)) { DirtyAttributesByEntity.Add(targetentity, new List<Type>() ); LogFile.WriteLine( "dirtyobjectqueuesingleclient " + connection + " marking object " + targetentity + " dirty" ); } foreach( Type type in dirtytypes ) { if( !DirtyAttributesByEntity[ targetentity ].Contains( type ) ) { DirtyAttributesByEntity[ targetentity ].Add( type ); } } //else //{ // DirtyEntities[ targetobject ] = ( (int)DirtyEntities[ targetobject ] ) | bitmask; //} }
IReplicatedObjectController GetControllerForObject(IHasReference entity) { foreach (Type replicatedtype in objectcontrollers.Keys) { if (replicatedtype.IsInstanceOfType(entity)) { return objectcontrollers[replicatedtype]; } } return null; }
void IReplicatedObjectController.AssignGlobalReference(IHasReference entitytoassign, int globalreference) { // first check we dont already have an entity with same reference, but not same object // if we do it is duplicate, so delete foreach (Entity entity in entities) { if (entity.iReference == globalreference && entity != entitytoassign ) { LogFile.WriteLine("removed duplicate " + globalreference); entities.Remove(entity); entitybyreference.Remove(globalreference); break; } } if (entitybyreference.ContainsKey(entitytoassign.Reference)) { entitybyreference.Remove(entitytoassign.Reference); } entitytoassign.Reference = globalreference; entitybyreference.Add(entitytoassign.Reference, entitytoassign as Entity); }