Ejemplo n.º 1
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposing || Data == null)
            {
                return;
            }

            Data.Dispose();
            Data = null;
        }
Ejemplo n.º 2
0
        public IWritablePacket Packetize(IWritablePacket packet)
        {
            packet.Write(_cooldowns.Count);
            foreach (var kv in _cooldowns)
            {
                packet.Write(kv.Key);
                packet.Write(kv.Value);
            }

            return(packet);
        }
Ejemplo n.º 3
0
        public IWritablePacket Packetize(IWritablePacket packet)
        {
            packet.Write(_cellInfo.Count);
            foreach (var item in _cellInfo)
            {
                packet.Write(item.Key);
                packet.Write(item.Value);
            }

            return(packet);
        }
Ejemplo n.º 4
0
        public IWritablePacket Packetize(IWritablePacket packet)
        {
            packet.Write(Attributes.Count);
            foreach (var attribute in Attributes)
            {
                packet.Write((byte)attribute.Key);
                packet.Write(attribute.Value);
            }

            packet.Write((ICollection <ProjectileFactory>)Projectiles);

            return(packet);
        }
Ejemplo n.º 5
0
        public static IWritablePacket WriteWithTypeInfo <T>(this IWritablePacket packet, ICollection <T> data)
            where T : class
        {
            if (data == null)
            {
                return(packet.Write(-1));
            }

            packet.Write(data.Count);
            foreach (var item in data)
            {
                packet.WriteWithTypeInfo(item);
            }
            return(packet);
        }
Ejemplo n.º 6
0
        public IWritablePacket Packetize(IWritablePacket packet)
        {
            packet.Write(_changedShape.Count);
            foreach (var entity in _changedShape)
            {
                packet.Write(entity);
            }

            packet.Write(_changedMass.Count);
            foreach (var entity in _changedMass)
            {
                packet.Write(entity);
            }

            return(packet);
        }
Ejemplo n.º 7
0
        public IWritablePacket Packetize(IWritablePacket packet)
        {
            foreach (var id in _squadIds)
            {
                packet.Write(id);
                var data = _squads[id];
                packet.WriteWithTypeInfo(data.Formation);
                packet.Write(data.Spacing);
                packet.Write(data.Members.Count);
                foreach (var member in data.Members)
                {
                    packet.Write(member);
                }
            }

            return(packet);
        }
Ejemplo n.º 8
0
        /// <summary>Send a packet to a remote host.</summary>
        /// <param name="packet">the data to send</param>
        /// <param name="endPoint">the remote end point to send it to.</param>
        public void Send(IWritablePacket packet, IPEndPoint endPoint)
        {
            if (packet == null)
            {
                throw new ArgumentNullException("packet");
            }
            if (packet.Length < 1)
            {
                throw new ArgumentException("packet");
            }
            if (endPoint == null)
            {
                throw new ArgumentNullException("endPoint");
            }

            HandleSend(MakeMessage(packet), endPoint);
        }
Ejemplo n.º 9
0
        public IWritablePacket Packetize(IWritablePacket packet)
        {
            packet.Write(_newCollisions.Count);
            foreach (var info in _newCollisions)
            {
                packet.Write(info.Key);
                packet.Write(info.Value);
            }

            packet.Write(_activeCollisions.Count);
            foreach (var info in _activeCollisions)
            {
                packet.Write(info.Key);
                packet.Write(info.Value);
            }

            return(packet);
        }
Ejemplo n.º 10
0
        /// <summary>Special purpose packetize method, only writing own data, not that of the base class. Used for saving.</summary>
        /// <param name="packet">The packet to write to.</param>
        /// <returns>The written to packet.</returns>
        public IWritablePacket PacketizeLocal(IWritablePacket packet)
        {
            packet.Write(_baseAttributes.Count);
            foreach (var attribute in _baseAttributes)
            {
                packet.Write(Enum.GetName(typeof(TAttribute), attribute.Key));
                packet.Write(attribute.Value);
            }
            packet.Write(_modifiedAttributes.Count);
            foreach (var attribute in _modifiedAttributes)
            {
                packet.Write(Enum.GetName(typeof(TAttribute), attribute.Key));
                packet.Write(attribute.Value[0]);
                packet.Write(attribute.Value[1]);
            }

            return(packet);
        }
Ejemplo n.º 11
0
        public IWritablePacket Packetize(IWritablePacket packet)
        {
            packet.Write(_currentBehaviors.Count);
            var behaviorTypes = _currentBehaviors.ToArray();

            // Stacks iterators work backwards (first is the last pushed element),
            // so we need to iterate backwards.
            for (var i = behaviorTypes.Length; i > 0; --i)
            {
                packet.Write((byte)behaviorTypes[i - 1]);
            }

            foreach (var behavior in _behaviors.Values)
            {
                packet.Write(behavior);
            }

            return(packet);
        }
Ejemplo n.º 12
0
        public IWritablePacket Packetize(IWritablePacket packet)
        {
            packet.Write(Capacity);
            packet.Write(Count);

            // Write actual item ids with their positions.
            for (var i = 0; i < _items.Count; i++)
            {
                var itemEntry = _items[i];
                if (itemEntry <= 0)
                {
                    continue;
                }

                packet.Write(i);
                packet.Write(itemEntry);
            }

            return(packet);
        }
Ejemplo n.º 13
0
        /// <summary>
        ///     Create a new message, compressing it if it makes the message smaller, and encrypting it. Finally, we put the
        ///     header in front of it.
        /// </summary>
        /// <param name="packet">the packet to make a message of.</param>
        /// <returns>the message data.</returns>
        private byte[] MakeMessage(IWritablePacket packet)
        {
            // Get the actual data in raw format.
            var data   = packet.GetBuffer();
            var length = packet.Length;

            // If packets are large, try compressing them, see if it helps.
            // Only start after a certain size. General overhead for gzip
            // seems to be around 130byte, so make sure we're well beyond that.
            var flag = false;

            if (length > 200)
            {
                var compressed = SimpleCompression.Compress(data, length);
                if (compressed.Length < length)
                {
                    // OK, worth it, it's smaller than before.
                    flag   = true;
                    data   = compressed;
                    length = compressed.Length;
                }
            }

            // Encrypt the message.
            data = Crypto.Encrypt(data, 0, length);

            if ((length & CompressedMask) > 0)
            {
                throw new ArgumentException("Packet too big.", "packet");
            }

            // Build the final message: header, then length + compressed bit, then data.
            var result = new byte[_header.Length + sizeof(uint) + length];

            _header.CopyTo(result, 0);
            BitConverter.GetBytes((uint)length | (flag ? CompressedMask : 0u)).CopyTo(result, _header.Length);
            data.CopyTo(result, _header.Length + sizeof(uint));
            return(result);
        }
Ejemplo n.º 14
0
        public static IWritablePacket WriteWithTypeInfo <T>([NotNull] this IWritablePacket packet, T data)
            where T : class
        {
            // Check whether we have something.
            if (data != null)
            {
                // Check its underlying type.
                var type = data.GetType();
                if (!IsPacketizable(type))
                {
                    return(packet);
                }

                // Flag that we have something.
                packet.Write(true);

                // Make sure we have a parameterless public constructor for deserialization.
                System.Diagnostics.Debug.Assert(type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null) != null);

                // Store the type, which also tells us the value us not null.
                packet.Write(type);

                // Packetize all fields, then give the object a chance to do manual
                // serialization, e.g. of collections and such.
                try
                {
                    GetPacketizer(type)(packet, data);
                }
                catch (Exception ex)
                {
                    throw new PacketException("Failed serializing " + type.Name, ex);
                }
                return(packet);
            }

            // Flag that we have nothing.
            return(packet.Write(false));
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Writes the specified packet to the underlying stream. It can then be read
        /// byte a <c>PacketStream</c> on the other end of the stream.
        /// </summary>
        /// <param name="packet">The packet to write.</param>
        /// <returns>The number of bytesa actually written. This can differ
        /// from the length of the specified packet due to transforms from
        /// wrapper streams (encryption, compression, ...)</returns>
        /// <exception cref="System.IO.IOException">If the underlying stream fails.</exception>
        public int Write(IWritablePacket packet)
        {
            using (var data = new Packet())
            {
                // If packets are large, try compressing them, see if it helps.
                // Only start after a certain size. General overhead for gzip
                // seems to be around 130byte, so make sure we're well beyond that.
                byte[] compressed;
                if (packet.Length > 200 && (compressed = SimpleCompression.Compress(packet.GetBuffer(), packet.Length)).Length < packet.Length)
                {
                    // OK, worth it, it's smaller than before.
                    data.Write(true);
                    data.Write(compressed);
                }
                else
                {
                    data.Write(false);
                    data.Write(packet.GetBuffer(), 0, packet.Length);
                }

                return(_stream.Write(data));
            }
        }
Ejemplo n.º 16
0
        public IWritablePacket Packetize(IWritablePacket packet)
        {
            if (_packetizer == null)
            {
                throw new InvalidOperationException("No serializer specified.");
            }

            packet.Write(_cells.Count);
            foreach (var entry in _cells)
            {
                packet.Write(entry.Key);
                packet.Write(entry.Value);
            }

            packet.Write(_entryBounds.Count);
            foreach (var entry in _entryBounds)
            {
                _packetizer(packet, entry.Key);
                packet.Write(entry.Value);
            }

            return(packet);
        }
Ejemplo n.º 17
0
        public IWritablePacket Packetize(IWritablePacket packet)
        {
            packet.Write(IndexCount);
            for (var index = 0; index < _nextIndexId; ++index)
            {
                // Skip non-existing and empty trees.
                if (_trees[index] == null || _trees[index].Count == 0)
                {
                    System.Diagnostics.Debug.Assert(_changed[index] == null || _changed[index].Count == 0);
                    continue;
                }
                packet.Write(index);

                packet.Write(_trees[index]);

                packet.Write(_changed[index].Count);
                foreach (var component in _changed[index])
                {
                    packet.Write(component);
                }
            }

            return(packet);
        }
Ejemplo n.º 18
0
 /// <summary>Writes the specified rectangle value.</summary>
 /// <param name="packet">The packet to write to.</param>
 /// <param name="data">The value to write.</param>
 /// <returns>This packet, for call chaining.</returns>
 public static IWritablePacket Write(this IWritablePacket packet, FarRectangle data)
 {
     return(packet.Write(data.X).Write(data.Y).Write(data.Width).Write(data.Height));
 }
Ejemplo n.º 19
0
 /// <summary>Writes the specified rectangle value.</summary>
 /// <param name="packet">The packet to write to.</param>
 /// <param name="data">The value to write.</param>
 /// <returns>This packet, for call chaining.</returns>
 public static IWritablePacket Write(this IWritablePacket packet, FarPosition data)
 {
     return(packet.Write(data.X).Write(data.Y));
 }
Ejemplo n.º 20
0
 /// <summary>Writes the specified far value.</summary>
 /// <param name="packet">The packet to write to.</param>
 /// <param name="data">The value to write.</param>
 /// <returns>This packet, for call chaining.</returns>
 public static IWritablePacket Write(this IWritablePacket packet, FarValue data)
 {
     return(packet.Write(data.Segment).Write(data.Offset));
 }
Ejemplo n.º 21
0
 /// <summary>Writes the specified vector value.</summary>
 /// <param name="packet">The packet.</param>
 /// <param name="data">The value to write.</param>
 /// <returns>This packet, for call chaining.</returns>
 public static IWritablePacket Write(this IWritablePacket packet, Color data)
 {
     return(packet.Write(data.PackedValue));
 }
Ejemplo n.º 22
0
 /// <summary>Writes the specified vector value.</summary>
 /// <param name="packet">The packet.</param>
 /// <param name="data">The value to write.</param>
 /// <returns>This packet, for call chaining.</returns>
 public static IWritablePacket Write(this IWritablePacket packet, Vector3 data)
 {
     return(packet.Write(data.X).Write(data.Y).Write(data.Z));
 }
Ejemplo n.º 23
0
 /// <summary>Prepends all normal command messages with the corresponding flag.</summary>
 /// <param name="command">the command to send.</param>
 /// <param name="packet">the final packet to send.</param>
 /// <returns>the given packet, after writing.</returns>
 protected override IWritablePacket WrapDataForSend(FrameCommand command, IWritablePacket packet)
 {
     return(base.WrapDataForSend(command, packet.Write((byte)TssControllerMessage.Command)));
 }
Ejemplo n.º 24
0
 public static IWritablePacket Write(this IWritablePacket packet, string data)
 {
     return(data == null?packet.Write((byte[])null) : packet.Write(Encoding.UTF8.GetBytes(data)));
 }
Ejemplo n.º 25
0
 public static IWritablePacket Write(this IWritablePacket packet, byte[] data)
 {
     return(data == null?packet.Write(-1) : packet.Write(data, 0, data.Length));
 }
Ejemplo n.º 26
0
 public IWritablePacket Packetize(IWritablePacket packet)
 {
     return(packet.Write((ICollection <PositionedEffect>)Effects));
 }
Ejemplo n.º 27
0
 public static IWritablePacket Write(this IWritablePacket packet, Type data)
 {
     return(data == null?packet.Write((string)null) : packet.Write(data.AssemblyQualifiedName));
 }
Ejemplo n.º 28
0
 /// <summary>
 /// Prepends all normal command messages with the corresponding flag.
 /// </summary>
 /// <param name="command">the command to send.</param>
 /// <param name="packet">the final packet to send.</param>
 /// <returns>the given packet, after writing.</returns>
 protected override IWritablePacket WrapDataForSend(FrameCommand command, IWritablePacket packet)
 {
     // Wrap it up like a TSS client would.
     packet.Write((byte)AbstractTssController <IClientSession> .TssControllerMessage.Command);
     return(base.WrapDataForSend(command, packet));
 }
Ejemplo n.º 29
0
 /// <summary>Sends a data message with the the specified packet as its data to the specified player.</summary>
 /// <param name="player">The play to whom to send the packet.</param>
 /// <param name="packet">The data to send.</param>
 public void SendTo(Player player, IWritablePacket packet)
 {
     SendTo(player, SessionMessage.Data, packet);
 }
Ejemplo n.º 30
0
 public IWritablePacket Packetize(IWritablePacket packet)
 {
     return(packet.Write(GetBuffer(), 0, Length));
 }