Exemplo n.º 1
0
        /// <summary>
        /// Builds a packet from the incoming buffer + offset. If a packet can be built, it is returned else null.
        /// </summary>
        /// <param name="offset">Current offset in buffer.</param>
        /// <param name="buffer">Incoming buffer.</param>
        /// <returns>Returns either a BasePacket or null if not enough data.</returns>
        public static BasePacket CreatePacket(ref int offset, byte[] buffer, int bytesRead)
        {
            BasePacket newPacket = null;

            //Too small to even get length
            if (bytesRead <= offset)
                return(null); }
Exemplo n.º 2
0
        public static BasePacket CreatePacket(SubPacket subpacket, bool isAuthed, bool isCompressed)
        {
            //Create Header
            var header = new BasePacketHeader();

            byte[] data = null;

            header.isAuthenticated = isAuthed ? (byte)1 : (byte)0;
            header.isCompressed    = isCompressed ? (byte)1 : (byte)0;
            header.numSubpackets   = 1;
            header.packetSize      = BASEPACKET_SIZE;
            header.timestamp       = Utils.MilisUnixTimeStampUTC();

            //Get packet size
            header.packetSize += subpacket.header.subpacketSize;

            data = new byte[header.packetSize - 0x10];

            //Add Subpackets
            var subpacketData = subpacket.GetBytes();

            Array.Copy(subpacketData, 0, data, 0, subpacketData.Length);

            Debug.Assert(data != null);

            var packet = new BasePacket(header, data);

            return(packet);
        }
Exemplo n.º 3
0
        public static BasePacket CreatePacket(byte[] data, bool isAuthed, bool isCompressed)
        {
            Debug.Assert(data != null);

            //Create Header
            var header = new BasePacketHeader();

            header.isAuthenticated = isAuthed ? (byte)1 : (byte)0;
            header.isCompressed    = isCompressed ? (byte)1 : (byte)0;
            header.numSubpackets   = 1;
            header.packetSize      = BASEPACKET_SIZE;
            header.timestamp       = Utils.MilisUnixTimeStampUTC();

            //Get packet size
            header.packetSize += (ushort)data.Length;

            var packet = new BasePacket(header, data);

            return(packet);
        }
Exemplo n.º 4
0
        public static BasePacket CreatePacket(List <SubPacket> subpackets, bool isAuthed, bool isCompressed)
        {
            //Create Header
            var header = new BasePacketHeader();

            byte[] data = null;

            header.isAuthenticated = isAuthed ? (byte)1 : (byte)0;
            header.isCompressed    = isCompressed ? (byte)1 : (byte)0;
            header.numSubpackets   = (ushort)subpackets.Count;
            header.packetSize      = BASEPACKET_SIZE;
            header.timestamp       = Utils.MilisUnixTimeStampUTC();

            //Get packet size
            foreach (var subpacket in subpackets)
            {
                header.packetSize += subpacket.header.subpacketSize;
            }

            data = new byte[header.packetSize - 0x10];

            //Add Subpackets
            var offset = 0;

            foreach (var subpacket in subpackets)
            {
                var subpacketData = subpacket.GetBytes();
                Array.Copy(subpacketData, 0, data, offset, subpacketData.Length);
                offset += (ushort)subpacketData.Length;
            }

            Debug.Assert(data != null && offset == data.Length && header.packetSize == 0x10 + offset);

            var packet = new BasePacket(header, data);

            return(packet);
        }