Esempio n. 1
0
        public void Append(Request request)
        {
            #region Preconditions

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (!stream.CanWrite)
            {
                throw new ArgumentException("Must be writable", nameof(stream));
            }

            #endregion

            using (var requestDataStream = new MemoryStream())
            {
                Serializer.Serialize(requestDataStream, request);

                requestDataStream.Position = 0;

                Varint.Encode((ulong)requestDataStream.Length, stream); // Write the length

                requestDataStream.WriteTo(stream);
            }
        }
Esempio n. 2
0
        public bool TryRead(out Request request)
        {
            if (IsEof)
            {
                request = null;

                return(false);
            }

            int dataSize = (int)Varint.Read(stream);

            var dataBuffer = new byte[dataSize];

            stream.Read(dataBuffer, 0, dataSize);

            using (var ms = new MemoryStream(dataBuffer))
            {
                request = Serializer.Deserialize <Request>(ms);

                return(true);
            }
        }