Exemplo n.º 1
0
 /// <summary>
 /// Writes the specified value.
 /// </summary>
 /// <param name="value">The value.</param>
 private void Write(MgBinaryStreamArgumentPacket value)
 {
     m_stream.Write(BitConverter.GetBytes((UInt32)value.PacketHeader), 0, UInt32Len);
     m_stream.Write(BitConverter.GetBytes((UInt32)value.ArgumentType), 0, UInt32Len);
     m_stream.Write(BitConverter.GetBytes((UInt32)value.Version), 0, UInt32Len);
     m_stream.Write(BitConverter.GetBytes((UInt64)value.Length), 0, UInt64Len);
 }
        private MgBinaryStreamArgumentPacket ReadBinaryStreamArgumentPacket()
        {
            MgBinaryStreamArgumentPacket p = new MgBinaryStreamArgumentPacket();

            p.PacketHeader = (MgPacketHeader)BitConverter.ToUInt32(ReadStream(MgBinarySerializer.UInt32Len), 0);
            p.ArgumentType = (MgArgumentType)BitConverter.ToUInt32(ReadStream(MgBinarySerializer.UInt32Len), 0);
            p.Version      = BitConverter.ToUInt32(ReadStream(MgBinarySerializer.UInt32Len), 0);
            p.Length       = BitConverter.ToUInt64(ReadStream(MgBinarySerializer.UInt64Len), 0);
            return(p);
        }
        /// <summary>
        /// Reads the stream.
        /// </summary>
        /// <returns></returns>
        public Stream ReadStream()
        {
            MgBinaryStreamArgumentPacket p = ReadBinaryStreamArgumentPacket();

            if (p.ArgumentType != MgArgumentType.Stream)
            {
                throw new InvalidCastException(string.Format(Strings.ErrorBinarySerializerUnexpectedType, p.ArgumentType.ToString(), "Stream")); //NOXLATE
            }
            if (ReadBool())
            {
                return(null);
            }

            //TODO: If the stream is large, we use a lot of memory, perhaps the filebacked async stream could be used
            //Due to the annoying embedded buffer size markers, we cannot return the stream 'as is' Grrrrrr!

            int   r;
            ulong remain = p.Length;

            byte[] buf = new byte[1024 * 8];

            MemoryStream ms = new MemoryStream();

            do
            {
                int blocksize = ReadInt32();
                r = 1;

                while (r > 0 && blocksize > 0)
                {
                    r          = m_stream.Read(buf, 0, (int)Math.Min((ulong)remain, (ulong)buf.Length));
                    blocksize -= r;
                    remain    -= (ulong)r;
                    ms.Write(buf, 0, r);
                }

                if (blocksize != 0)
                {
                    throw new Exception(Strings.ErrorBinarySerializerPrematureEndOfStream);
                }
            } while (remain > 0);

            ms.Position = 0;
            return(ms);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Writes the specified value.
 /// </summary>
 /// <param name="value">The value.</param>
 private void Write(MgBinaryStreamArgumentPacket value)
 {
     m_stream.Write(BitConverter.GetBytes((UInt32)value.PacketHeader), 0, UInt32Len);
     m_stream.Write(BitConverter.GetBytes((UInt32)value.ArgumentType), 0, UInt32Len);
     m_stream.Write(BitConverter.GetBytes((UInt32)value.Version), 0, UInt32Len);
     m_stream.Write(BitConverter.GetBytes((UInt64)value.Length), 0, UInt64Len);
 }
Exemplo n.º 5
0
 private MgBinaryStreamArgumentPacket ReadBinaryStreamArgumentPacket()
 {
     MgBinaryStreamArgumentPacket p = new MgBinaryStreamArgumentPacket();
     p.PacketHeader = (MgPacketHeader)BitConverter.ToUInt32(ReadStream(MgBinarySerializer.UInt32Len), 0);
     p.ArgumentType = (MgArgumentType)BitConverter.ToUInt32(ReadStream(MgBinarySerializer.UInt32Len), 0);
     p.Version = BitConverter.ToUInt32(ReadStream(MgBinarySerializer.UInt32Len), 0);
     p.Length = BitConverter.ToUInt64(ReadStream(MgBinarySerializer.UInt64Len), 0);
     return p;
 }