| | | 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.Collections.Generic; |
| | | 6 | | using System.IO; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF |
| | | 10 | | { |
| | | 11 | | internal class BufferedMessageBuffer : MessageBuffer |
| | | 12 | | { |
| | | 13 | | private IBufferedMessageData _messageData; |
| | | 14 | | private readonly KeyValuePair<string, object>[] _properties; |
| | | 15 | | private bool _closed; |
| | | 16 | | private readonly bool[] _understoodHeaders; |
| | | 17 | | private readonly bool _understoodHeadersModified; |
| | | 18 | | |
| | 0 | 19 | | public BufferedMessageBuffer(IBufferedMessageData messageData, |
| | 0 | 20 | | KeyValuePair<string, object>[] properties, bool[] understoodHeaders, bool understoodHeadersModified) |
| | | 21 | | { |
| | 0 | 22 | | _messageData = messageData; |
| | 0 | 23 | | _properties = properties; |
| | 0 | 24 | | _understoodHeaders = understoodHeaders; |
| | 0 | 25 | | _understoodHeadersModified = understoodHeadersModified; |
| | 0 | 26 | | messageData.Open(); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public override int BufferSize |
| | | 30 | | { |
| | | 31 | | get |
| | | 32 | | { |
| | 0 | 33 | | lock (ThisLock) |
| | | 34 | | { |
| | 0 | 35 | | if (_closed) |
| | | 36 | | { |
| | 0 | 37 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateBufferDisposedException()); |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | return _messageData.Buffer.Count; |
| | | 41 | | } |
| | 0 | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public override void WriteMessage(Stream stream) |
| | | 46 | | { |
| | 0 | 47 | | if (stream == null) |
| | | 48 | | { |
| | 0 | 49 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(stream)); |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | lock (ThisLock) |
| | | 53 | | { |
| | 0 | 54 | | if (_closed) |
| | | 55 | | { |
| | 0 | 56 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateBufferDisposedException()); |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | ArraySegment<byte> buffer = _messageData.Buffer; |
| | 0 | 60 | | stream.Write(buffer.Array, buffer.Offset, buffer.Count); |
| | 0 | 61 | | } |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public override string MessageContentType |
| | | 65 | | { |
| | | 66 | | get |
| | | 67 | | { |
| | 0 | 68 | | lock (ThisLock) |
| | | 69 | | { |
| | 0 | 70 | | if (_closed) |
| | | 71 | | { |
| | 0 | 72 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateBufferDisposedException()); |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | return _messageData.MessageEncoder.ContentType; |
| | | 76 | | } |
| | 0 | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | private object ThisLock { get; } = new object(); |
| | | 81 | | |
| | | 82 | | public override void Close() |
| | | 83 | | { |
| | 0 | 84 | | lock (ThisLock) |
| | | 85 | | { |
| | 0 | 86 | | if (!_closed) |
| | | 87 | | { |
| | 0 | 88 | | _closed = true; |
| | 0 | 89 | | _messageData.Close(); |
| | 0 | 90 | | _messageData = null; |
| | | 91 | | } |
| | 0 | 92 | | } |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | public override Message CreateMessage() |
| | | 96 | | { |
| | 0 | 97 | | lock (ThisLock) |
| | | 98 | | { |
| | 0 | 99 | | if (_closed) |
| | | 100 | | { |
| | 0 | 101 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateBufferDisposedException()); |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | RecycledMessageState recycledMessageState = _messageData.TakeMessageState(); |
| | 0 | 105 | | if (recycledMessageState == null) |
| | | 106 | | { |
| | 0 | 107 | | recycledMessageState = new RecycledMessageState(); |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | BufferedMessage bufferedMessage = new BufferedMessage(_messageData, recycledMessageState, _understoodHea |
| | 0 | 111 | | foreach (KeyValuePair<string, object> keypair in _properties) |
| | | 112 | | { |
| | 0 | 113 | | bufferedMessage.Properties[keypair.Key] = keypair.Value; |
| | | 114 | | } |
| | 0 | 115 | | _messageData.Open(); |
| | 0 | 116 | | return bufferedMessage; |
| | | 117 | | } |
| | 0 | 118 | | } |
| | | 119 | | |
| | | 120 | | private Exception CreateBufferDisposedException() |
| | | 121 | | { |
| | 0 | 122 | | return new ObjectDisposedException("", SR.MessageBufferIsClosed); |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | } |