| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Channels |
| | | 10 | | { |
| | | 11 | | internal class MaxMessageSizeStream : DelegatingStream |
| | | 12 | | { |
| | | 13 | | private readonly long _maxMessageSize; |
| | | 14 | | private long _totalBytesRead; |
| | | 15 | | private long _bytesWritten; |
| | | 16 | | |
| | | 17 | | public MaxMessageSizeStream(Stream stream, long maxMessageSize) |
| | 0 | 18 | | : base(stream) |
| | | 19 | | { |
| | 0 | 20 | | _maxMessageSize = maxMessageSize; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationTo |
| | | 24 | | { |
| | 0 | 25 | | count = PrepareRead(count); |
| | 0 | 26 | | int bytesRead = await base.ReadAsync(buffer, offset, count, cancellationToken); |
| | 0 | 27 | | return FinishRead(bytesRead); |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | | 31 | | { |
| | 0 | 32 | | PrepareWrite(count); |
| | 0 | 33 | | return base.WriteAsync(buffer, offset, count, cancellationToken); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 37 | | { |
| | 0 | 38 | | count = PrepareRead(count); |
| | 0 | 39 | | return FinishRead(base.Read(buffer, offset, count)); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public override int ReadByte() |
| | | 43 | | { |
| | 0 | 44 | | PrepareRead(1); |
| | 0 | 45 | | int i = base.ReadByte(); |
| | 0 | 46 | | if (i != -1) |
| | | 47 | | { |
| | 0 | 48 | | FinishRead(1); |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | return i; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public override void Write(byte[] buffer, int offset, int count) |
| | | 55 | | { |
| | 0 | 56 | | PrepareWrite(count); |
| | 0 | 57 | | base.Write(buffer, offset, count); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | public override void WriteByte(byte value) |
| | | 61 | | { |
| | 0 | 62 | | PrepareWrite(1); |
| | 0 | 63 | | base.WriteByte(value); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | public static Exception CreateMaxReceivedMessageSizeExceededException(long maxMessageSize) |
| | | 67 | | { |
| | 0 | 68 | | string message = SRCommon.Format(SRCommon.MaxReceivedMessageSizeExceeded, maxMessageSize); |
| | 0 | 69 | | Exception inner = new QuotaExceededException(message); |
| | | 70 | | |
| | 0 | 71 | | return new CommunicationException(message, inner); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | internal static Exception CreateMaxSentMessageSizeExceededException(long maxMessageSize) |
| | | 75 | | { |
| | 0 | 76 | | string message = SRCommon.Format(SRCommon.MaxSentMessageSizeExceeded, maxMessageSize); |
| | 0 | 77 | | Exception inner = new QuotaExceededException(message); |
| | | 78 | | |
| | 0 | 79 | | return new CommunicationException(message, inner); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | private int PrepareRead(int bytesToRead) |
| | | 83 | | { |
| | 0 | 84 | | if (_totalBytesRead >= _maxMessageSize) |
| | | 85 | | { |
| | 0 | 86 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateMaxReceivedMessageSizeExceededException( |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | long bytesRemaining = _maxMessageSize - _totalBytesRead; |
| | | 90 | | |
| | 0 | 91 | | if (bytesRemaining > int.MaxValue) |
| | | 92 | | { |
| | 0 | 93 | | return bytesToRead; |
| | | 94 | | } |
| | | 95 | | else |
| | | 96 | | { |
| | 0 | 97 | | return Math.Min(bytesToRead, (int)(_maxMessageSize - _totalBytesRead)); |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | private int FinishRead(int bytesRead) |
| | | 102 | | { |
| | 0 | 103 | | _totalBytesRead += bytesRead; |
| | 0 | 104 | | return bytesRead; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | private void PrepareWrite(int bytesToWrite) |
| | | 108 | | { |
| | 0 | 109 | | if (_bytesWritten + bytesToWrite > _maxMessageSize) |
| | | 110 | | { |
| | 0 | 111 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateMaxSentMessageSizeExceededException(_max |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | _bytesWritten += bytesToWrite; |
| | 0 | 115 | | } |
| | | 116 | | } |
| | | 117 | | } |