// 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) });
        }
 // called by object replication controllers, who typically pass themselves in as the first argument,
 // and the type of the entity they manage, and want to receive events for
 // for now, only one controller can register for each type
 // note that for class hierarchies, only the base type needs to be registered
 public void RegisterReplicatedObjectController(IReplicatedObjectController controller, Type managedtype)
 {
     if (objectcontrollers.ContainsKey(managedtype) && objectcontrollers[managedtype] != controller)
     {
         throw new Exception("Error: a replicated type can only be registered by a single replicatedobjectcontroller.  " +
                             "Duplicated type: " + managedtype.ToString() + " by " + controller.ToString() + " conflicts with " + objectcontrollers[managedtype].ToString());
     }
     if (!objectcontrollers.ContainsKey(managedtype))
     {
         objectcontrollers.Add(managedtype, controller);
         controller.ObjectCreated  += new ObjectCreatedHandler(controller_ObjectCreated);
         controller.ObjectDeleted  += new ObjectDeletedHandler(controller_ObjectDeleted);
         controller.ObjectModified += new ObjectModifiedHandler(controller_ObjectModified);
     }
 }
        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 ObjectDeletedRpc(IPEndPoint connection, int reference, string typename)
        {
            Type type = Type.GetType(typename);
            IReplicatedObjectController replicatedobjectcontroller = GetControllerForType(type);

            if (replicatedobjectcontroller == null)
            {
                LogFile.WriteLine("Warning: no replicatedobjectcontroller found for type " + typename);
                return;
            }

            if (rpc.isserver)
            {
                LogFile.WriteLine("ObjectDeletedRpc, server " + reference);
                dirtyobjectcontroller.MarkDeleted(reference, typename);
            }
            else
            {
                LogFile.WriteLine("ObjectDeletedRpc, client " + reference);
                replicatedobjectcontroller.ReplicatedObjectDeleted(this, new ObjectDeletedArgs(DateTime.Now, reference, typename));
            }
        }
        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 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()));
                }
            }
        }
 // called by object replication controllers, who typically pass themselves in as the first argument,
 // and the type of the entity they manage, and want to receive events for
 // for now, only one controller can register for each type
 // note that for class hierarchies, only the base type needs to be registered
 public void RegisterReplicatedObjectController( IReplicatedObjectController controller, Type managedtype )
 {
     if( objectcontrollers.ContainsKey( managedtype ) && objectcontrollers[ managedtype ] != controller )
     {
         throw new Exception( "Error: a replicated type can only be registered by a single replicatedobjectcontroller.  " +
             "Duplicated type: " + managedtype.ToString() + " by " + controller.ToString() + " conflicts with " + objectcontrollers[ managedtype ].ToString() );
     }
     if( !objectcontrollers.ContainsKey( managedtype ) )
     {
         objectcontrollers.Add( managedtype, controller );
         controller.ObjectCreated += new ObjectCreatedHandler(controller_ObjectCreated);
         controller.ObjectDeleted += new ObjectDeletedHandler(controller_ObjectDeleted);
         controller.ObjectModified += new ObjectModifiedHandler(controller_ObjectModified);
     }
 }