Esempio n. 1
0
 // Reads a single FIX messages and returns it. It shall contain
 // all fields, including header and trailer. No validation is
 // performed.
 //
 // This method is NOT thread-safe.
 //
 // Throws MessageTooLargeException if incoming message exceeds
 // MaxMessageSize. After that every call to ReadMessage() will throw
 // MessageTooLargeException.
 //
 // Throws EmptyStreamException if nothing can be read from the
 // underlying stream.
 public async Task<ArraySegment<byte>> ReadMessage(Stream strm)
 {
     Assert.True(strm != null);
     var trailerMatcher = new MessageTrailerMatcher();
     int messageEnd = trailerMatcher.FindTrailer(_buf, _startPos, _endPos);
     // Keep reading from the underlying stream until we find trailer.
     while (messageEnd == 0)
     {
         EnsureBufferSpace();
         Assert.True(_endPos < _buf.Length, "_endPos = {0}, _buf.Length = {1}", _endPos, _buf.Length);
         int read = await strm.ReadAsync(_buf, _endPos, _buf.Length - _endPos);
         if (read <= 0)
         {
             var partial = new ArraySegment<byte>(_buf, _startPos, _endPos - _startPos);
             throw new EmptyStreamException("Read so far: " + partial.AsAscii());
         }
         messageEnd = trailerMatcher.FindTrailer(_buf, _endPos, _endPos + read);
         _endPos += read;
     }
     var res = new ArraySegment<byte>(_buf, _startPos, messageEnd - _startPos);
     _startPos = messageEnd;
     if (_log.IsDebugEnabled) _log.Debug("IN: {0}", res.AsAscii());
     else _log.Info("IN: {0}", Truncate(res.AsAscii(), 1024));
     return res;
 }
Esempio n. 2
0
        // Reads a single FIX messages and returns it. It shall contain
        // all fields, including header and trailer. No validation is
        // performed.
        //
        // This method is NOT thread-safe.
        //
        // Throws MessageTooLargeException if incoming message exceeds
        // MaxMessageSize. After that every call to ReadMessage() will throw
        // MessageTooLargeException.
        //
        // Throws EmptyStreamException if nothing can be read from the
        // underlying stream.
        public async Task <ArraySegment <byte> > ReadMessage(Stream strm)
        {
            Assert.True(strm != null);
            var trailerMatcher = new MessageTrailerMatcher();
            int messageEnd     = trailerMatcher.FindTrailer(_buf, _startPos, _endPos);

            // Keep reading from the underlying stream until we find trailer.
            while (messageEnd == 0)
            {
                EnsureBufferSpace();
                Assert.True(_endPos < _buf.Length, "_endPos = {0}, _buf.Length = {1}", _endPos, _buf.Length);
                int read = await strm.ReadAsync(_buf, _endPos, _buf.Length - _endPos);

                if (read <= 0)
                {
                    var partial = new ArraySegment <byte>(_buf, _startPos, _endPos - _startPos);
                    throw new EmptyStreamException("Read so far: " + partial.AsAscii());
                }
                messageEnd = trailerMatcher.FindTrailer(_buf, _endPos, _endPos + read);
                _endPos   += read;
            }
            var res = new ArraySegment <byte>(_buf, _startPos, messageEnd - _startPos);

            _startPos = messageEnd;
            if (_log.IsDebugEnabled)
            {
                _log.Debug("IN: {0}", res.AsAscii());
            }
            else
            {
                _log.Info("IN: {0}", Truncate(res.AsAscii(), 1024));
            }
            return(res);
        }