public void Alloc(int requiredSize) { if (requiredSize > this.remainingSize) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(this.maxSize)); } this.remainingSize -= requiredSize; }
public async Task <(Message, Stream)> ReceiveAsync(FramingConnection connection, TimeSpan timeout) { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); ReadOnlySequence <byte> buffer = ReadOnlySequence <byte> .Empty; for (; ;) { var readResult = await connection.Input.ReadAsync(); await Task.Yield(); if (readResult.IsCompleted || readResult.Buffer.Length == 0) { if (!readResult.IsCompleted) { connection.Input.AdvanceTo(readResult.Buffer.Start); } //EnsureDecoderAtEof(connection); connection.EOF = true; } if (connection.EOF) { return(null, null); } buffer = readResult.Buffer; bool atEnvelopeStart = DecodeBytes(connection, ref buffer); connection.Input.AdvanceTo(buffer.Start); if (atEnvelopeStart) { break; } if (connection.EOF) { return(null, null); } } // we're ready to read a message Stream connectionStream = new SingletonInputConnectionStream(connection, connection.ServiceDispatcher.Binding); Stream inputStream = new MaxMessageSizeStream(connectionStream, connection.MaxReceivedMessageSize); //using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity(true) : null) //{ // if (DiagnosticUtility.ShouldUseActivity) // { // ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityProcessingMessage, TraceUtility.RetrieveMessageNumber()), ActivityType.ProcessMessage); // } Message message = null; try { message = await connection.MessageEncoderFactory.Encoder.ReadMessageAsync( inputStream, connection.MaxBufferSize, connection.FramingDecoder.ContentType); } catch (XmlException xmlException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ProtocolException(SR.MessageXmlProtocolError, xmlException)); } //if (DiagnosticUtility.ShouldUseActivity) //{ // TraceUtility.TransferFromTransport(message); //} PrepareMessage(connection, message); return(message, inputStream); //} }
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); }