public void Alloc(int requiredSize)
 {
     if (requiredSize > this.remainingSize)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(this.maxSize));
     }
     this.remainingSize -= requiredSize;
 }
예제 #2
0
 public static Exception CreateMaxReceivedMessageSizeExceededException(long maxMessageSize)
 {
     return(MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(maxMessageSize));
 }
        private Message DecodeMessage(FramingConnection connection, ref ReadOnlySequence <byte> buffer)
        {
            // TODO: plumb through from binding
            int maxBufferSize = TransportDefaults.MaxBufferSize;
            var decoder       = connection.ServerSessionDecoder;

            while (!connection.EOF && buffer.Length > 0)
            {
                int bytesRead = decoder.Decode(buffer);
                if (bytesRead > 0)
                {
                    if (!connection.EnvelopeBuffer.IsEmpty)
                    {
                        var remainingEnvelopeBuffer = connection.EnvelopeBuffer.Slice(connection.EnvelopeOffset, connection.EnvelopeSize - connection.EnvelopeOffset);
                        CopyBuffer(buffer, remainingEnvelopeBuffer, bytesRead);
                        connection.EnvelopeOffset += bytesRead;
                    }

                    buffer = buffer.Slice(bytesRead);
                }

                switch (decoder.CurrentState)
                {
                case ServerSessionDecoder.State.EnvelopeStart:
                    int envelopeSize = decoder.EnvelopeSize;
                    if (envelopeSize > maxBufferSize)
                    {
                        // TODO: Sending faults
                        //base.SendFault(FramingEncodingString.MaxMessageSizeExceededFault, timeout);

                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                  MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(maxBufferSize));
                    }
                    connection.EnvelopeBuffer = connection.BufferManager.TakeBuffer(envelopeSize);
                    connection.EnvelopeSize   = envelopeSize;
                    connection.EnvelopeOffset = 0;
                    break;

                case ServerSessionDecoder.State.EnvelopeEnd:
                    if (!connection.EnvelopeBuffer.IsEmpty)
                    {
                        Message message = null;

                        try
                        {
                            message = connection.MessageEncoder.ReadMessage(
                                new ArraySegment <byte>(connection.EnvelopeBuffer.ToArray(), 0, connection.EnvelopeSize),
                                connection.BufferManager,
                                connection.ServerSessionDecoder.ContentType);
                        }
                        catch (XmlException xmlException)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                      new ProtocolException(SR.MessageXmlProtocolError, xmlException));
                        }

                        connection.EnvelopeBuffer = null;
                        return(message);
                    }
                    break;

                case ServerSessionDecoder.State.End:
                    connection.EOF = true;
                    break;
                }
            }

            return(null);
        }