// 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 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()));
                }
            }
        }