Exemplo n.º 1
0
        public static async Task <Frame> ReadAsync(Stream stream, CancellationToken ct, Logger logger)
        {
            try
            {
                var frameletCount = await ReadUInt16Async(stream, ct);

                if (frameletCount == 0)
                {
                    throw new EpoxyProtocolErrorException("Zero framelets");
                }

                var frame = new Frame(frameletCount, logger);

                while (frameletCount > 0)
                {
                    if (ct.IsCancellationRequested)
                    {
                        return(null);
                    }

                    var frameletType = await ReadUInt16Async(stream, ct);

                    if (!Framelet.IsKnownType(frameletType))
                    {
                        throw new EpoxyProtocolErrorException("Unknown framelet type: " + frameletType);
                    }

                    var frameletLength = await ReadUInt32Async(stream, ct);

                    if (frameletLength > int.MaxValue)
                    {
                        throw new EpoxyProtocolErrorException("Framelet too big: " + frameletLength);
                    }

                    byte[] frameletContents = await ReadBufferAsync(stream, unchecked ((int)frameletLength), ct);

                    frame.Add(new Framelet((FrameletType)frameletType, new ArraySegment <byte>(frameletContents)));

                    --frameletCount;
                }

                return(frame);
            }
            catch (Exception ex) when(ex is OperationCanceledException || ex is EndOfStreamException || ex is ObjectDisposedException)
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        public void Add(Framelet framelet)
        {
            if (framelets.Count == UInt16.MaxValue)
            {
                var message = logger.Site().ErrorAndReturnFormatted("Exceeded maximum allowed count of framelets.");
                throw new InvalidOperationException(message);
            }

            try
            {
                // add space for framelet type, length of message, and actual message content
                TotalSize = checked (TotalSize + sizeof(UInt16) + sizeof(UInt32) + framelet.Contents.Count);
                framelets.Add(framelet);
            }
            catch (OverflowException oex)
            {
                var message = logger.Site().ErrorAndReturnFormatted("Exceeded maximum size of frame.");
                throw new InvalidOperationException(message, oex);
            }
        }