예제 #1
0
 public void Write(long value)
 {
     if (value >= Minus2ToThe4 && value < Plus2ToThe7)
     {
         _stream.WriteByte((byte)value);
     }
     else if (value >= Minus2ToThe7 && value < Minus2ToThe4)
     {
         _stream.WriteByte(Int8);
         _stream.Write(PackStreamBitConverter.GetBytes((byte)value));
     }
     else if (value >= Minus2ToThe15 && value < Plus2ToThe15)
     {
         _stream.WriteByte(PackStream.Int16);
         _stream.Write(PackStreamBitConverter.GetBytes((short)value));
     }
     else if (value >= Minus2ToThe31 && value < Plus2ToThe31)
     {
         _stream.WriteByte(PackStream.Int32);
         _stream.Write(PackStreamBitConverter.GetBytes((int)value));
     }
     else
     {
         _stream.WriteByte(PackStream.Int64);
         _stream.Write(PackStreamBitConverter.GetBytes(value));
     }
 }
예제 #2
0
        private async Task <bool> ConstructMessageAsync(Stream outputMessageStream)
        {
            bool dataRead = false;

            while (true)
            {
                var chunkHeader = await ReadDataOfSizeAsync(ChunkHeaderSize).ConfigureAwait(false);

                var chunkSize = PackStreamBitConverter.ToUInt16(chunkHeader);

                if (chunkSize == 0) //NOOP or end of message
                {
                    //We have been reading data so this is the end of a message zero chunk
                    //Or there is no data remaining after this NOOP
                    if (dataRead || ChunkBufferRemaining <= 0)
                    {
                        break;
                    }

                    //Its a NOOP so skip it
                    continue;
                }

                var rawChunkData = await ReadDataOfSizeAsync(chunkSize).ConfigureAwait(false);

                dataRead = true;
                outputMessageStream.Write(rawChunkData, 0, chunkSize);    //Put the raw chunk data into the outputstream
            }

            return(dataRead);    //Return if a message was constructed
        }
예제 #3
0
        private int ReadChunkSize()
        {
            Array.ConstrainedCopy(_buffer, _lastReadPosition, _chunkSizeBuffer, 0, _chunkSizeBuffer.Length);

            _lastReadPosition += _chunkSizeBuffer.Length;

            return(PackStreamBitConverter.ToUInt16(_chunkSizeBuffer));
        }
        public string ReadString()
        {
            var markerByte = NextByte();

            if (markerByte == PackStream.TinyString) // Note no mask, so we compare to 0x80.
            {
                return(string.Empty);
            }

            return(PackStreamBitConverter.ToString(ReadUtf8(markerByte)));
        }
예제 #5
0
 public void Write(string value)
 {
     if (value == null)
     {
         WriteNull();
     }
     else
     {
         var bytes = PackStreamBitConverter.GetBytes(value);
         WriteStringHeader(bytes.Length);
         _stream.Write(bytes);
     }
 }
예제 #6
0
 private void WriteBytesHeader(int size)
 {
     if (size <= byte.MaxValue)
     {
         _stream.WriteByte(Bytes8);
         _stream.Write(new byte[] { (byte)size });
     }
     else if (size <= short.MaxValue)
     {
         _stream.WriteByte(Bytes16);
         _stream.Write(PackStreamBitConverter.GetBytes((short)size));
     }
     else
     {
         _stream.WriteByte(Bytes32);
         _stream.Write(PackStreamBitConverter.GetBytes(size));
     }
 }
예제 #7
0
 private void WriteStringHeader(int size)
 {
     if (size < 0x10)
     {
         _stream.WriteByte((byte)(TinyString | size));
     }
     else if (size <= byte.MaxValue)
     {
         _stream.WriteByte(String8);
         _stream.Write(new byte[] { (byte)size });
     }
     else if (size <= short.MaxValue)
     {
         _stream.WriteByte(String16);
         _stream.Write(PackStreamBitConverter.GetBytes((short)size));
     }
     else
     {
         _stream.WriteByte(String32);
         _stream.Write(PackStreamBitConverter.GetBytes(size));
     }
 }
예제 #8
0
 public void WriteMapHeader(int size)
 {
     if (size < 0x10)
     {
         _stream.WriteByte((byte)(TinyMap | size));
         _stream.Write(new byte[0]);
     }
     else if (size <= byte.MaxValue)
     {
         _stream.WriteByte(Map8);
         _stream.Write(new byte[] { (byte)size });
     }
     else if (size <= short.MaxValue)
     {
         _stream.WriteByte(Map16);
         _stream.Write(PackStreamBitConverter.GetBytes((short)size));
     }
     else
     {
         _stream.WriteByte(Map32);
         _stream.Write(PackStreamBitConverter.GetBytes(size));
     }
 }
예제 #9
0
 internal void WriteListHeader(int size)
 {
     if (size < 0x10)
     {
         _stream.WriteByte((byte)(TinyList | size));
         _stream.Write(new byte[0]);
     }
     else if (size <= byte.MaxValue)
     {
         _stream.WriteByte(List8);
         _stream.Write(new byte[] { (byte)size });
     }
     else if (size <= short.MaxValue)
     {
         _stream.WriteByte(List16);
         _stream.Write(PackStreamBitConverter.GetBytes((short)size));
     }
     else
     {
         _stream.WriteByte(List32);
         _stream.Write(PackStreamBitConverter.GetBytes(size));
     }
 }
        public void CloseChunk()
        {
            // Fill size buffers with the actual length of the chunk.
            var count = _chunkStream.Position - _dataPos;

            if (count > 0)
            {
                var chunkSize =
                    PackStreamBitConverter.GetBytes((ushort)count);

                var previousPos = _chunkStream.Position;
                try
                {
                    _chunkStream.Position = _startPos;

                    _chunkStream.Write(chunkSize, 0, chunkSize.Length);
                }
                finally
                {
                    _chunkStream.Position = previousPos;
                }
            }
        }
예제 #11
0
 public void WriteStructHeader(int size, byte signature)
 {
     if (size < 0x10)
     {
         _stream.WriteByte((byte)(TinyStruct | size));
         _stream.Write(new byte[] { signature });
     }
     else if (size <= byte.MaxValue)
     {
         _stream.WriteByte(Struct8);
         _stream.Write(new byte[] { (byte)size, signature });
     }
     else if (size <= short.MaxValue)
     {
         _stream.WriteByte(Struct16);
         _stream.Write(PackStreamBitConverter.GetBytes((short)size));
         _stream.WriteByte(signature);
     }
     else
     {
         throw new ProtocolException(
                   $"Structures cannot have more than {short.MaxValue} fields");
     }
 }
        public double NextDouble()
        {
            _stream.Read(_longBuffer);

            return(PackStreamBitConverter.ToDouble(_longBuffer));
        }
        public long NextLong()
        {
            _stream.Read(_longBuffer);

            return(PackStreamBitConverter.ToInt64(_longBuffer));
        }
        public int NextInt()
        {
            _stream.Read(_intBuffer);

            return(PackStreamBitConverter.ToInt32(_intBuffer));
        }
        public short NextShort()
        {
            _stream.Read(_shortBuffer);

            return(PackStreamBitConverter.ToInt16(_shortBuffer));
        }
예제 #16
0
 public void Write(double value)
 {
     _stream.WriteByte(Float64);
     _stream.Write(PackStreamBitConverter.GetBytes(value));
 }
 private int ReadChunkSize()
 {
     DataStreamBuffer.WriteInto(_chunkSizeBuffer, 0, ChunkHeaderSize);
     return(PackStreamBitConverter.ToUInt16(_chunkSizeBuffer));
 }