Example #1
0
        private Message ReadSingle(Bitstream stream)
        {
            bool isCompressed = stream.ReadBool();

            if (isCompressed)
            {
                uint uncompressed_length = stream.ReadBits(26);
                uint length = stream.ReadBits(18);

                byte[] data = new byte[length];
                stream.Read(data, 0, (int)length);

                return(new Message {
                    IsCompressed = false,
                    Data = Snappy.Sharp.Snappy.Uncompress(data)
                });
            }
            else
            {
                uint length = stream.ReadBits(18);
                Preconditions.CheckArgument(length < Int32.MaxValue);

                byte[] data = new byte[length];
                stream.Read(data, 0, (int)length);

                return(new Message {
                    IsCompressed = false,
                    Data = data,
                });
            }
        }
Example #2
0
        private Message ReadSingle(Bitstream stream)
        {
            var isCompressed = stream.ReadBool();

            if (isCompressed)
            {
                var uncompressed_length = stream.ReadBits(26);
                var length = stream.ReadBits(18);

                var data = new byte[length];
                stream.Read(data, 0, (int)length);

                return(new Message
                {
                    IsCompressed = false,
                    Data = new SnappyDecompressor().Decompress(data, 0, data.Length)
                });
            }
            else
            {
                var length = stream.ReadBits(18);

                var data = new byte[length];
                stream.Read(data, 0, (int)length);

                return(new Message
                {
                    IsCompressed = false,
                    Data = data
                });
            }
        }
Example #3
0
        private void ReadChunkHeader(Bitstream stream)
        {
            header = new ChunkedHeader();

            header.IsFile = stream.ReadBool();
            if (header.IsFile)
            {
                var filenameLength = stream.ReadUInt32();
                var filename       = new byte[filenameLength + 1]; // semantically wrong. should be
                // 0x104
                stream.Read(filename, 0, (int)filenameLength);     // and then read to end of string
                filename[filenameLength] = 0;                      // whatever
                header.Filename          = Encoding.UTF8.GetString(filename);
                throw new NotImplementedException();
            }

            header.IsCompressed = stream.ReadBool();
            if (header.IsCompressed)
            {
                header.DecompressedLength = stream.ReadBits(26);
            }

            header.ByteLength = stream.ReadBits(26);
            header.ChunkCount =
                (header.ByteLength + DotaGameConnection.BYTES_PER_CHUNK - 1) /
                DotaGameConnection.BYTES_PER_CHUNK;

            Receiving     = true;
            dataIn        = new byte[header.ByteLength];
            dataReceived  = new bool[header.ChunkCount];
            countReceived = 0;
        }
Example #4
0
        private Nullable <Message> ReadChunk(Bitstream stream)
        {
            uint offset = stream.ReadBits(18);
            uint count  = stream.ReadBits(3);

            log.DebugFormat("chunk start = {0} end = {1}", offset, count);

            if (offset == 0)
            {
                Preconditions.CheckArgument(!Receiving);
                ReadChunkHeader(stream);
            }
            else
            {
                Preconditions.CheckArgument(Receiving);
            }

            uint byteOffset = offset * Connection.BYTES_PER_CHUNK;

            uint byteCount;

            if (offset + count < header.ChunkCount)
            {
                byteCount = count * Connection.BYTES_PER_CHUNK;
            }
            else
            {
                byteCount = header.ByteLength - byteOffset;
            }

            stream.Read(dataIn, (int)byteOffset, (int)byteCount);

            for (uint i = offset;
                 i < offset + count;
                 ++i)
            {
                if (!dataReceived[i])
                {
                    dataReceived[i] = true;
                    ++countReceived;
                }
            }

            if (countReceived == header.ChunkCount)
            {
                log.Debug("chunk complete");
                Receiving = false;
                return(new Message {
                    IsCompressed = header.IsCompressed,
                    DecompressedLength = header.DecompressedLength,

                    Data = dataIn,
                });
            }
            else
            {
                log.DebugFormat("chunk has {0}/{1}", countReceived, header.ChunkCount);
                return(null);
            }
        }
Example #5
0
        private void SendRawDatagram(Bitstream stream)
        {
            var bytes = new byte[stream.Length];

            stream.Position = 0;
            stream.Read(bytes, 0, (int)stream.Length);

            socket.Send(bytes);
        }
Example #6
0
        public string UnpackString(PropertyInfo info, Bitstream stream)
        {
            var length = stream.ReadBits(9);

            var buffer = new byte[length];

            stream.Read(buffer, 0, (int)length);

            return(new string((from byte b in buffer select(char) b).ToArray <char>()));
        }
Example #7
0
        public string UnpackString(PropertyInfo info, Bitstream stream)
        {
            uint length = stream.ReadBits(9);

            Preconditions.CheckArgument(length <= MAX_STRING_LENGTH);

            byte[] buffer = new byte[length];
            stream.Read(buffer, 0, (int)length);

            return(new String((from byte b in buffer select(char) b).ToArray <char>()));
        }
Example #8
0
        private void HandleMessage(Bitstream stream)
        {
            uint type   = stream.ReadVarUInt();
            uint length = stream.ReadVarUInt();

            byte[] bytes = new byte[length];
            stream.Read(bytes, 0, (int)length);

            receivedInBand.Enqueue(new Message {
                Type = type,
                Data = bytes,
            });
        }
Example #9
0
        private Message?ReadChunk(Bitstream stream)
        {
            var offset = stream.ReadBits(18);
            var count  = stream.ReadBits(3);

            if (offset == 0)
            {
                ReadChunkHeader(stream);
            }

            var byteOffset = offset * DotaGameConnection.BYTES_PER_CHUNK;

            uint byteCount;

            if (offset + count < header.ChunkCount)
            {
                byteCount = count * DotaGameConnection.BYTES_PER_CHUNK;
            }
            else
            {
                byteCount = header.ByteLength - byteOffset;
            }

            stream.Read(dataIn, (int)byteOffset, (int)byteCount);

            for (var i = offset;
                 i < offset + count;
                 ++i)
            {
                if (!dataReceived[i])
                {
                    dataReceived[i] = true;
                    ++countReceived;
                }
            }

            if (countReceived == header.ChunkCount)
            {
                Receiving = false;
                return(new Message
                {
                    IsCompressed = header.IsCompressed,
                    DecompressedLength = header.DecompressedLength,
                    Data = dataIn
                });
            }
            return(null);
        }
Example #10
0
        private void ReadChunkHeader(Bitstream stream)
        {
            header = new ChunkedHeader();

            header.IsFile = stream.ReadBool();
            if (header.IsFile)
            {
                uint   filenameLength = stream.ReadUInt32();
                byte[] filename       = new byte[filenameLength + 1]; // semantically wrong. should be
                                                                      // 0x104
                stream.Read(filename, 0, (int)filenameLength);        // and then read to end of string
                filename[filenameLength] = 0;                         // whatever
                header.Filename          = Encoding.UTF8.GetString(filename);
                log.ErrorFormat("read filename: \"{0}\" ({1})", filenameLength, header.Filename);
                throw new NotImplementedException();
            }

            header.IsCompressed = stream.ReadBool();
            if (header.IsCompressed)
            {
                header.DecompressedLength = stream.ReadBits(26);
                log.DebugFormat(
                    "chunkheader compressed: decompressed len {0}",
                    header.DecompressedLength);
            }

            header.ByteLength = stream.ReadBits(26);
            header.ChunkCount =
                (header.ByteLength + Connection.BYTES_PER_CHUNK - 1) /
                Connection.BYTES_PER_CHUNK;
            log.DebugFormat(
                "chunkheader len {0} expecting {1} chunks",
                header.ByteLength, header.ChunkCount);

            Receiving     = true;
            dataIn        = new byte[header.ByteLength];
            dataReceived  = new bool[header.ChunkCount];
            countReceived = 0;
        }
Example #11
0
        private void ReadChunkHeader(Bitstream stream) {
            header = new ChunkedHeader();

            header.IsFile = stream.ReadBool();
            if (header.IsFile) {
                uint filenameLength = stream.ReadUInt32();
                byte[] filename = new byte[filenameLength + 1]; // semantically wrong. should be
                                                                // 0x104
                stream.Read(filename, 0, (int)filenameLength); // and then read to end of string
                filename[filenameLength] = 0; // whatever 
                header.Filename = Encoding.UTF8.GetString(filename);
                log.ErrorFormat("read filename: \"{0}\" ({1})", filenameLength, header.Filename);
                throw new NotImplementedException();
            }

            header.IsCompressed = stream.ReadBool();
            if (header.IsCompressed) {
                header.DecompressedLength = stream.ReadBits(26);
                log.DebugFormat(
                    "chunkheader compressed: decompressed len {0}", 
                    header.DecompressedLength);
            }

            header.ByteLength = stream.ReadBits(26);
            header.ChunkCount = 
                (header.ByteLength + Connection.BYTES_PER_CHUNK - 1) /
                Connection.BYTES_PER_CHUNK;
            log.DebugFormat(
                "chunkheader len {0} expecting {1} chunks", 
                header.ByteLength, header.ChunkCount);

            Receiving = true;
            dataIn = new byte[header.ByteLength];
            dataReceived = new bool[header.ChunkCount];
            countReceived = 0;
        }
Example #12
0
        public string UnpackString(PropertyInfo info, Bitstream stream) {
            uint length = stream.ReadBits(9);

            Preconditions.CheckArgument(length <= MAX_STRING_LENGTH);

            byte[] buffer = new byte[length];
            stream.Read(buffer, 0, (int) length);

            return new String((from byte b in buffer select (char) b).ToArray<char>());
        }
Example #13
0
        private Nullable<Message> ReadChunk(Bitstream stream) {
            uint offset = stream.ReadBits(18);
            uint count = stream.ReadBits(3);

            log.DebugFormat("chunk start = {0} end = {1}", offset, count);

            if (offset == 0) {
                Preconditions.CheckArgument(!Receiving);
                ReadChunkHeader(stream);
            } else {
                Preconditions.CheckArgument(Receiving);
            }

            uint byteOffset = offset * Connection.BYTES_PER_CHUNK;

            uint byteCount;
            if (offset + count < header.ChunkCount) {
                byteCount = count * Connection.BYTES_PER_CHUNK;
            } else {
                byteCount = header.ByteLength - byteOffset;
            }

            stream.Read(dataIn, (int)byteOffset, (int)byteCount);

            for (uint i = offset;
                    i < offset + count;
                    ++i) {
                if (!dataReceived[i]) {
                    dataReceived[i] = true;
                    ++countReceived;
                }
            }

            if (countReceived == header.ChunkCount) {
                log.Debug("chunk complete");
                Receiving = false;
                return new Message {
                    IsCompressed = header.IsCompressed,
                    DecompressedLength = header.DecompressedLength,

                    Data = dataIn,
                };
            } else {
                log.DebugFormat("chunk has {0}/{1}", countReceived, header.ChunkCount);
                return null;
            }
        }
Example #14
0
        private Message ReadSingle(Bitstream stream) {
            bool isCompressed = stream.ReadBool();
            
            if (isCompressed) {
                uint uncompressed_length = stream.ReadBits(26);
                uint length = stream.ReadBits(18);

                byte[] data = new byte[length];
                stream.Read(data, 0, (int) length);

                return new Message {
                    IsCompressed = false,
                    Data = Snappy.Sharp.Snappy.Uncompress(data)
                };
            } else {
                uint length = stream.ReadBits(18);
                Preconditions.CheckArgument(length < Int32.MaxValue);

                byte[] data = new byte[length];
                stream.Read(data, 0, (int)length);

                return new Message {
                    IsCompressed = false,
                    Data = data,
                };
            }
        }