| | | 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.Xml; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Channels |
| | | 9 | | { |
| | | 10 | | internal abstract class BufferedMessageWriter |
| | | 11 | | { |
| | | 12 | | private int[] _sizeHistory; |
| | | 13 | | private int _sizeHistoryIndex; |
| | | 14 | | private const int sizeHistoryCount = 4; |
| | | 15 | | private const int expectedSizeVariance = 256; |
| | | 16 | | private readonly BufferManagerOutputStream _stream; |
| | | 17 | | |
| | 24 | 18 | | public BufferedMessageWriter() |
| | | 19 | | { |
| | 24 | 20 | | _stream = new BufferManagerOutputStream(SR.MaxSentMessageSizeExceeded); |
| | 24 | 21 | | InitMessagePredictor(); |
| | 24 | 22 | | } |
| | | 23 | | |
| | | 24 | | protected abstract XmlDictionaryWriter TakeXmlWriter(Stream stream); |
| | | 25 | | protected abstract void ReturnXmlWriter(XmlDictionaryWriter writer); |
| | | 26 | | |
| | | 27 | | public ArraySegment<byte> WriteMessage(Message message, BufferManager bufferManager, int initialOffset, int maxS |
| | | 28 | | { |
| | | 29 | | int effectiveMaxSize; |
| | | 30 | | |
| | | 31 | | // make sure that maxSize has room for initialOffset without overflowing, since |
| | | 32 | | // the effective buffer size is message size + initialOffset |
| | 24 | 33 | | if (maxSizeQuota <= int.MaxValue - initialOffset) |
| | | 34 | | { |
| | 24 | 35 | | effectiveMaxSize = maxSizeQuota + initialOffset; |
| | | 36 | | } |
| | | 37 | | else |
| | | 38 | | { |
| | 0 | 39 | | effectiveMaxSize = int.MaxValue; |
| | | 40 | | } |
| | | 41 | | |
| | 24 | 42 | | int predictedMessageSize = PredictMessageSize(); |
| | 24 | 43 | | if (predictedMessageSize > effectiveMaxSize) |
| | | 44 | | { |
| | 0 | 45 | | predictedMessageSize = effectiveMaxSize; |
| | | 46 | | } |
| | 24 | 47 | | else if (predictedMessageSize < initialOffset) |
| | | 48 | | { |
| | 0 | 49 | | predictedMessageSize = initialOffset; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | try |
| | | 53 | | { |
| | 24 | 54 | | _stream.Init(predictedMessageSize, maxSizeQuota, effectiveMaxSize, bufferManager); |
| | 24 | 55 | | _stream.Skip(initialOffset); |
| | | 56 | | |
| | 24 | 57 | | XmlDictionaryWriter writer = TakeXmlWriter(_stream); |
| | 24 | 58 | | OnWriteStartMessage(writer); |
| | 24 | 59 | | message.WriteMessage(writer); |
| | 24 | 60 | | OnWriteEndMessage(writer); |
| | 24 | 61 | | writer.Flush(); |
| | 24 | 62 | | ReturnXmlWriter(writer); |
| | 24 | 63 | | byte[] buffer = _stream.ToArray(out int size); |
| | 24 | 64 | | RecordActualMessageSize(size); |
| | 24 | 65 | | return new ArraySegment<byte>(buffer, initialOffset, size - initialOffset); |
| | | 66 | | } |
| | | 67 | | finally |
| | | 68 | | { |
| | 24 | 69 | | _stream.Clear(); |
| | 24 | 70 | | } |
| | 24 | 71 | | } |
| | | 72 | | |
| | | 73 | | protected virtual void OnWriteStartMessage(XmlDictionaryWriter writer) |
| | | 74 | | { |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | protected virtual void OnWriteEndMessage(XmlDictionaryWriter writer) |
| | | 78 | | { |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | private void InitMessagePredictor() |
| | | 82 | | { |
| | 24 | 83 | | _sizeHistory = new int[4]; |
| | 240 | 84 | | for (int i = 0; i < sizeHistoryCount; i++) |
| | | 85 | | { |
| | 96 | 86 | | _sizeHistory[i] = 256; |
| | | 87 | | } |
| | 24 | 88 | | } |
| | | 89 | | |
| | | 90 | | private int PredictMessageSize() |
| | | 91 | | { |
| | 24 | 92 | | int max = 0; |
| | 240 | 93 | | for (int i = 0; i < sizeHistoryCount; i++) |
| | | 94 | | { |
| | 96 | 95 | | if (_sizeHistory[i] > max) |
| | | 96 | | { |
| | 24 | 97 | | max = _sizeHistory[i]; |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | 24 | 101 | | return max + expectedSizeVariance; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | private void RecordActualMessageSize(int size) |
| | | 105 | | { |
| | 24 | 106 | | _sizeHistory[_sizeHistoryIndex] = size; |
| | 24 | 107 | | _sizeHistoryIndex = (_sizeHistoryIndex + 1) % sizeHistoryCount; |
| | 24 | 108 | | } |
| | | 109 | | } |
| | | 110 | | } |