Ejemplo n.º 1
0
 internal MessageMerger(int maxSize, ulong flushDelay)
 {
     _buffer        = new byte[maxSize];
     _buffer[0]     = HeaderPacker.Pack((byte)MessageType.Merge, false);
     _position      = 1;
     _lastFlushTime = DateTime.Now;
     _flushDelay    = flushDelay;
 }
Ejemplo n.º 2
0
        // DONT MAKE STATIC FOR THREAD SAFETY.
        internal List <ArraySegment <byte> > Unpack(ArraySegment <byte> payload)
        {
            lock (_lock)
            {
                // TODO: VarInt
                if (payload.Count < 3)
                {
                    // Payload is too small
                    return(null);
                }

                // Clear the segments list
                _unpackSegments.Clear();

                // The offset for walking the buffer
                int packetOffset = 0;

                while (true)
                {
                    if (payload.Count < packetOffset + 2)
                    {
                        // No more data to be read
                        return(_unpackSegments);
                    }

                    // TODO: VarInt
                    // Read the size
                    ushort size = (ushort)(payload.Array[payload.Offset + packetOffset] | (ushort)(payload.Array[payload.Offset + packetOffset + 1] << 8));

                    if (size < 1)
                    {
                        // The size is too small. Doesnt fit the header
                        return(_unpackSegments);
                    }

                    // Make sure the size can even fit
                    if (payload.Count < (packetOffset + 2 + size))
                    {
                        // Payload is too small to fit the claimed size. Exit
                        return(_unpackSegments);
                    }

                    // Read the header
                    HeaderPacker.Unpack(payload.Array[payload.Offset + packetOffset + 2], out byte type, out bool fragment);

                    // Prevent merging a merge
                    if (type != (byte)MessageType.Merge)
                    {
                        // Add the new segment
                        _unpackSegments.Add(new ArraySegment <byte>(payload.Array, payload.Offset + packetOffset + 2, size));
                    }

                    // Increment the packetOffset
                    packetOffset += 2 + size;
                }
            }
        }
Ejemplo n.º 3
0
 internal MessageMerger(int maxSize, int startSize)
 {
     _buffer    = new byte[maxSize];
     _buffer[0] = HeaderPacker.Pack(MessageType.Merge);
     _position  = 1;
     _size      = startSize;
     _startSize = startSize;
     _maxSize   = maxSize;
 }
Ejemplo n.º 4
0
 internal MessageMerger(int maxSize, int startSize, int flushDelay)
 {
     _buffer        = new byte[maxSize];
     _buffer[0]     = HeaderPacker.Pack(MessageType.Merge);
     _position      = 1;
     _lastFlushTime = NetTime.Now;
     _flushDelay    = flushDelay;
     _size          = startSize;
     _startSize     = startSize;
     _maxSize       = maxSize;
 }
Ejemplo n.º 5
0
        internal static void Unpack(ArraySegment <byte> payload, List <ArraySegment <byte> > list)
        {
            if (list == null)
            {
                list = new List <ArraySegment <byte> >();
            }
            else if (list.Count > 0)
            {
                // Clear the segments list
                list.Clear();
            }

            // TODO: VarInt
            if (payload.Count < 3)
            {
                // Payload is too small
                return;
            }

            // The offset for walking the buffer
            int packetOffset = 0;

            while (true)
            {
                if (payload.Count < packetOffset + 2)
                {
                    // No more data to be read
                    return;
                }

                // TODO: VarInt
                // Read the size
                ushort size = (ushort)(payload.Array[payload.Offset + packetOffset] | (ushort)(payload.Array[payload.Offset + packetOffset + 1] << 8));

                if (size < 1)
                {
                    // The size is too small. Doesnt fit the header
                    return;
                }

                // Make sure the size can even fit
                if (payload.Count < (packetOffset + 2 + size))
                {
                    // Payload is too small to fit the claimed size. Exit
                    return;
                }

                // Read the header
                HeaderPacker.Unpack(payload.Array[payload.Offset + packetOffset + 2], out MessageType type);

                // Prevent merging a merge
                if (type != MessageType.Merge)
                {
                    // Add the new segment
                    list.Add(new ArraySegment <byte>(payload.Array, payload.Offset + packetOffset + 2, size));
                }

                // Increment the packetOffset
                packetOffset += 2 + size;
            }
        }