Exemple #1
0
        public void CreateEvent(IClientSerializable entity, object[] extraData = null)
        {
            if (GameMain.Client == null || GameMain.Client.Character == null)
            {
                return;
            }

            if (!(entity is Entity))
            {
                DebugConsole.ThrowError("Can't create an entity event for " + entity + "!");
                return;
            }

            if (((Entity)entity).Removed)
            {
                DebugConsole.ThrowError("Can't create an entity event for " + entity + " - the entity has been removed.\n" + Environment.StackTrace);
                return;
            }
            if (((Entity)entity).IdFreed)
            {
                DebugConsole.ThrowError("Can't create an entity event for " + entity + " - the ID of the entity has been freed.\n" + Environment.StackTrace);
                return;
            }

            ID++;
            var newEvent = new ClientEntityEvent(entity, ID);

            newEvent.CharacterStateID = GameMain.Client.Character.LastNetworkUpdateID;
            if (extraData != null)
            {
                newEvent.SetData(extraData);
            }

            events.Add(newEvent);
        }
        /// <summary>
        /// Read the events from the message, ignoring ones we've already received
        /// </summary>
        public void Read(NetIncomingMessage msg, Client sender = null)
        {
            UInt16 firstEventID = msg.ReadUInt16();
            int    eventCount   = msg.ReadByte();

            for (int i = 0; i < eventCount; i++)
            {
                UInt16 thisEventID = (UInt16)(firstEventID + (UInt16)i);
                UInt16 entityID    = msg.ReadUInt16();

                if (entityID == 0)
                {
                    msg.ReadPadBits();
                    if (thisEventID == (UInt16)(sender.LastSentEntityEventID + 1))
                    {
                        sender.LastSentEntityEventID++;
                    }
                    continue;
                }

                byte msgLength = msg.ReadByte();

                IClientSerializable entity = Entity.FindEntityByID(entityID) as IClientSerializable;

                //skip the event if we've already received it or if the entity isn't found
                if (thisEventID != (UInt16)(sender.LastSentEntityEventID + 1) || entity == null)
                {
                    if (GameSettings.VerboseLogging)
                    {
                        if (thisEventID != (UInt16)(sender.LastSentEntityEventID + 1))
                        {
                            DebugConsole.NewMessage("received msg " + thisEventID, Microsoft.Xna.Framework.Color.Red);
                        }
                        else if (entity == null)
                        {
                            DebugConsole.NewMessage(
                                "received msg " + thisEventID + ", entity " + entityID + " not found",
                                Microsoft.Xna.Framework.Color.Red);
                        }
                    }
                    msg.Position += msgLength * 8;
                }
                else
                {
                    if (GameSettings.VerboseLogging)
                    {
                        DebugConsole.NewMessage("received msg " + thisEventID, Microsoft.Xna.Framework.Color.Green);
                    }

                    UInt16 characterStateID = msg.ReadUInt16();

                    NetBuffer buffer = new NetBuffer();
                    buffer.Write(msg.ReadBytes(msgLength - 2));
                    BufferEvent(new BufferedEvent(sender, sender.Character, characterStateID, entity, buffer));

                    sender.LastSentEntityEventID++;
                }
                msg.ReadPadBits();
            }
        }
            public BufferedEvent(Client sender, Character senderCharacter, UInt16 characterStateID, IClientSerializable targetEntity, ReadWriteMessage data)
            {
                this.Sender           = sender;
                this.Character        = senderCharacter;
                this.CharacterStateID = characterStateID;

                this.TargetEntity = targetEntity;

                this.Data = data;
            }
        public void CreateEvent(IClientSerializable entity, object[] extraData = null)
        {
            if (GameMain.Client == null || GameMain.Client.Character == null)
            {
                return;
            }

            if (!(entity is Entity))
            {
                DebugConsole.ThrowError("Can't create an entity event for " + entity + "!");
                return;
            }

            if (((Entity)entity).Removed)
            {
                DebugConsole.ThrowError("Can't create an entity event for " + entity + " - the entity has been removed.\n" + Environment.StackTrace.CleanupStackTrace());
                return;
            }
            if (((Entity)entity).IdFreed)
            {
                DebugConsole.ThrowError("Can't create an entity event for " + entity + " - the ID of the entity has been freed.\n" + Environment.StackTrace.CleanupStackTrace());
                return;
            }

            var newEvent = new ClientEntityEvent(entity, (UInt16)(ID + 1))
            {
                CharacterStateID = GameMain.Client.Character.LastNetworkUpdateID
            };

            if (extraData != null)
            {
                newEvent.SetData(extraData);
            }

            for (int i = events.Count - 1; i >= 0; i--)
            {
                //we already have an identical event that's waiting to be sent
                // -> no need to add a new one
                if (!events[i].Sent && events[i].IsDuplicate(newEvent))
                {
                    return;
                }
            }

            ID++;

            events.Add(newEvent);
        }
Exemple #5
0
 private void TrySendNetworkUpdate(ISerializableEntity entity, SerializableProperty property)
 {
     if (GameMain.Server != null)
     {
         IServerSerializable serverSerializable = entity as IServerSerializable;
         if (serverSerializable != null)
         {
             GameMain.Server.CreateEntityEvent(serverSerializable, new object[] { NetEntityEvent.Type.ChangeProperty, property });
         }
     }
     else if (GameMain.Client != null)
     {
         IClientSerializable clientSerializable = entity as IClientSerializable;
         if (clientSerializable != null)
         {
             GameMain.Client.CreateEntityEvent(clientSerializable, new object[] { NetEntityEvent.Type.ChangeProperty, property });
         }
     }
 }
        public void CreateEvent(IClientSerializable entity, object[] extraData = null)
        {
            if (GameMain.Client == null || GameMain.Client.Character == null)
            {
                return;
            }

            if (!(entity is Entity))
            {
                DebugConsole.ThrowError("Can't create an entity event for " + entity + "!");
                return;
            }

            ID++;
            var newEvent = new ClientEntityEvent(entity, ID);

            newEvent.CharacterStateID = GameMain.Client.Character.LastNetworkUpdateID;
            if (extraData != null)
            {
                newEvent.SetData(extraData);
            }

            events.Add(newEvent);
        }
        /// <summary>
        /// Read the events from the message, ignoring ones we've already received
        /// </summary>
        public void Read(IReadMessage msg, Client sender = null)
        {
            UInt16 firstEventID = msg.ReadUInt16();
            int    eventCount   = msg.ReadByte();

            for (int i = 0; i < eventCount; i++)
            {
                UInt16 thisEventID = (UInt16)(firstEventID + (UInt16)i);
                UInt16 entityID    = msg.ReadUInt16();

                if (entityID == Entity.NullEntityID)
                {
                    msg.ReadPadBits();
                    if (thisEventID == (UInt16)(sender.LastSentEntityEventID + 1))
                    {
                        sender.LastSentEntityEventID++;
                    }
                    continue;
                }

                int msgLength = (int)msg.ReadVariableUInt32();

                IClientSerializable entity = Entity.FindEntityByID(entityID) as IClientSerializable;

                //skip the event if we've already received it
                if (thisEventID != (UInt16)(sender.LastSentEntityEventID + 1))
                {
                    if (GameSettings.VerboseLogging)
                    {
                        DebugConsole.NewMessage("Received msg " + thisEventID + ", expecting " + sender.LastSentEntityEventID, Color.Red);
                    }
                    msg.BitPosition += msgLength * 8;
                }
                else if (entity == null)
                {
                    //entity not found -> consider the even read and skip over it
                    //(can happen, for example, when a client uses a medical item repeatedly
                    //and creates an event for it before receiving the event about it being removed)
                    if (GameSettings.VerboseLogging)
                    {
                        DebugConsole.NewMessage(
                            "Received msg " + thisEventID + ", entity " + entityID + " not found",
                            Microsoft.Xna.Framework.Color.Orange);
                    }
                    sender.LastSentEntityEventID++;
                    msg.BitPosition += msgLength * 8;
                }
                else
                {
                    if (GameSettings.VerboseLogging)
                    {
                        DebugConsole.NewMessage("Received msg " + thisEventID, Microsoft.Xna.Framework.Color.Green);
                    }

                    UInt16 characterStateID = msg.ReadUInt16();

                    ReadWriteMessage buffer = new ReadWriteMessage();
                    byte[]           temp   = msg.ReadBytes(msgLength - 2);
                    buffer.Write(temp, 0, msgLength - 2);
                    buffer.BitPosition = 0;
                    BufferEvent(new BufferedEvent(sender, sender.Character, characterStateID, entity, buffer));

                    sender.LastSentEntityEventID++;
                }
                msg.ReadPadBits();
            }
        }
 public ClientEntityEvent(IClientSerializable entity, UInt16 id)
     : base(entity, id)
 {
     serializable = entity;
 }