|
1 |
| -// This source code is dual-licensed under the Apache License, version |
2 |
| -// 2.0, and the Mozilla Public License, version 1.1. |
3 |
| -// |
4 |
| -// The APL v2.0: |
5 |
| -// |
6 |
| -//--------------------------------------------------------------------------- |
7 |
| -// Copyright (c) 2007-2016 Pivotal Software, Inc. |
8 |
| -// |
9 |
| -// Licensed under the Apache License, Version 2.0 (the "License"); |
10 |
| -// you may not use this file except in compliance with the License. |
11 |
| -// You may obtain a copy of the License at |
12 |
| -// |
13 |
| -// http://www.apache.org/licenses/LICENSE-2.0 |
14 |
| -// |
15 |
| -// Unless required by applicable law or agreed to in writing, software |
16 |
| -// distributed under the License is distributed on an "AS IS" BASIS, |
17 |
| -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 |
| -// See the License for the specific language governing permissions and |
19 |
| -// limitations under the License. |
20 |
| -//--------------------------------------------------------------------------- |
21 |
| -// |
22 |
| -// The MPL v1.1: |
23 |
| -// |
24 |
| -//--------------------------------------------------------------------------- |
25 |
| -// The contents of this file are subject to the Mozilla Public License |
26 |
| -// Version 1.1 (the "License"); you may not use this file except in |
27 |
| -// compliance with the License. You may obtain a copy of the License |
28 |
| -// at http://www.mozilla.org/MPL/ |
29 |
| -// |
30 |
| -// Software distributed under the License is distributed on an "AS IS" |
31 |
| -// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
32 |
| -// the License for the specific language governing rights and |
33 |
| -// limitations under the License. |
34 |
| -// |
35 |
| -// The Original Code is RabbitMQ. |
36 |
| -// |
37 |
| -// The Initial Developer of the Original Code is Pivotal Software, Inc. |
38 |
| -// Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved. |
39 |
| -//--------------------------------------------------------------------------- |
40 |
| - |
41 |
| -using System; |
42 |
| -using System.Collections.Generic; |
43 |
| -using System.IO; |
44 |
| -using System.Net; |
45 |
| -using RabbitMQ.Client.Framing.Impl; |
46 |
| -using RabbitMQ.Client.Framing; |
47 |
| -using RabbitMQ.Util; |
48 |
| - |
49 |
| -namespace RabbitMQ.Client.Impl |
50 |
| -{ |
51 |
| - public class Command |
52 |
| - { |
53 |
| - // EmptyFrameSize, 8 = 1 + 2 + 4 + 1 |
54 |
| - // - 1 byte of frame type |
55 |
| - // - 2 bytes of channel number |
56 |
| - // - 4 bytes of frame payload length |
57 |
| - // - 1 byte of payload trailer FrameEnd byte |
58 |
| - private const int EmptyFrameSize = 8; |
59 |
| - private readonly MemoryStream m_body; |
60 |
| - private static readonly byte[] m_emptyByteArray = new byte[0]; |
61 |
| - |
62 |
| - static Command() |
63 |
| - { |
64 |
| - CheckEmptyFrameSize(); |
65 |
| - } |
66 |
| - |
67 |
| - public Command() : this(null, null, null) |
68 |
| - { |
69 |
| - m_body= new MemoryStream(); |
70 |
| - } |
71 |
| - |
72 |
| - public Command(MethodBase method) : this(method, null, null) |
73 |
| - { |
74 |
| - m_body = new MemoryStream(); |
75 |
| - } |
76 |
| - |
77 |
| - public Command(MethodBase method, ContentHeaderBase header, byte[] body) |
78 |
| - { |
79 |
| - Method = method; |
80 |
| - Header = header; |
81 |
| - if (body != null) |
82 |
| - { |
83 |
| - m_body = new MemoryStream(body); |
84 |
| - } |
85 |
| - else |
86 |
| - { |
87 |
| - m_body = new MemoryStream(); |
88 |
| - } |
89 |
| - } |
90 |
| - |
91 |
| - public byte[] Body |
92 |
| - { |
93 |
| - get { return ConsolidateBody(); } |
94 |
| - } |
95 |
| - |
96 |
| - public ContentHeaderBase Header { get; set; } |
97 |
| - |
98 |
| - public MethodBase Method { get; set; } |
99 |
| - |
100 |
| - public static void CheckEmptyFrameSize() |
101 |
| - { |
102 |
| - var f = new EmptyWriteFrame(); |
103 |
| - var stream = new MemoryStream(); |
104 |
| - var writer = new NetworkBinaryWriter(stream); |
105 |
| - f.WriteTo(writer); |
106 |
| - long actualLength = stream.Length; |
107 |
| - |
108 |
| - if (EmptyFrameSize != actualLength) |
109 |
| - { |
110 |
| - string message = |
111 |
| - string.Format("EmptyFrameSize is incorrect - defined as {0} where the computed value is in fact {1}.", |
112 |
| - EmptyFrameSize, |
113 |
| - actualLength); |
114 |
| - throw new ProtocolViolationException(message); |
115 |
| - } |
116 |
| - } |
117 |
| - |
118 |
| - public void AppendBodyFragment(byte[] fragment) |
119 |
| - { |
120 |
| - if(fragment !=null) |
121 |
| - { |
122 |
| - m_body.Write(fragment, 0, fragment.Length); |
123 |
| - } |
124 |
| - } |
125 |
| - |
126 |
| - public byte[] ConsolidateBody() |
127 |
| - { |
128 |
| - return m_body.Length == 0 ? m_emptyByteArray : m_body.ToArray(); |
129 |
| - } |
130 |
| - |
131 |
| - public void Transmit(int channelNumber, Connection connection) |
132 |
| - { |
133 |
| - if(Method.HasContent) |
134 |
| - { |
135 |
| - TransmitAsFrameSet(channelNumber, connection); |
136 |
| - } |
137 |
| - else |
138 |
| - { |
139 |
| - TransmitAsSingleFrame(channelNumber, connection); |
140 |
| - } |
141 |
| - } |
142 |
| - |
143 |
| - public void TransmitAsSingleFrame(int channelNumber, Connection connection) |
144 |
| - { |
145 |
| - connection.WriteFrame(new MethodWriteFrame(channelNumber, Method)); |
146 |
| - } |
147 |
| - |
148 |
| - public void TransmitAsFrameSet(int channelNumber, Connection connection) |
149 |
| - { |
150 |
| - var frames = new List<WriteFrame>(); |
151 |
| - frames.Add(new MethodWriteFrame(channelNumber, Method)); |
152 |
| - if (Method.HasContent) |
153 |
| - { |
154 |
| - var body = ConsolidateBody(); // Cache, since the property is compiled. |
155 |
| - |
156 |
| - frames.Add(new HeaderWriteFrame(channelNumber, Header, body.Length)); |
157 |
| - var frameMax = (int) Math.Min(int.MaxValue, connection.FrameMax); |
158 |
| - var bodyPayloadMax = (frameMax == 0) ? body.Length : frameMax - EmptyFrameSize; |
159 |
| - for (int offset = 0; offset < body.Length; offset += bodyPayloadMax) |
160 |
| - { |
161 |
| - var remaining = body.Length - offset; |
162 |
| - var count = (remaining < bodyPayloadMax) ? remaining : bodyPayloadMax; |
163 |
| - frames.Add(new BodySegmentWriteFrame(channelNumber, body, offset, count)); |
164 |
| - } |
165 |
| - } |
166 |
| - |
167 |
| - connection.WriteFrameSet(frames); |
168 |
| - } |
169 |
| - } |
170 |
| -} |
| 1 | +// This source code is dual-licensed under the Apache License, version |
| 2 | +// 2.0, and the Mozilla Public License, version 1.1. |
| 3 | +// |
| 4 | +// The APL v2.0: |
| 5 | +// |
| 6 | +//--------------------------------------------------------------------------- |
| 7 | +// Copyright (c) 2007-2016 Pivotal Software, Inc. |
| 8 | +// |
| 9 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +// you may not use this file except in compliance with the License. |
| 11 | +// You may obtain a copy of the License at |
| 12 | +// |
| 13 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +// |
| 15 | +// Unless required by applicable law or agreed to in writing, software |
| 16 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +// See the License for the specific language governing permissions and |
| 19 | +// limitations under the License. |
| 20 | +//--------------------------------------------------------------------------- |
| 21 | +// |
| 22 | +// The MPL v1.1: |
| 23 | +// |
| 24 | +//--------------------------------------------------------------------------- |
| 25 | +// The contents of this file are subject to the Mozilla Public License |
| 26 | +// Version 1.1 (the "License"); you may not use this file except in |
| 27 | +// compliance with the License. You may obtain a copy of the License |
| 28 | +// at http://www.mozilla.org/MPL/ |
| 29 | +// |
| 30 | +// Software distributed under the License is distributed on an "AS IS" |
| 31 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 32 | +// the License for the specific language governing rights and |
| 33 | +// limitations under the License. |
| 34 | +// |
| 35 | +// The Original Code is RabbitMQ. |
| 36 | +// |
| 37 | +// The Initial Developer of the Original Code is Pivotal Software, Inc. |
| 38 | +// Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved. |
| 39 | +//--------------------------------------------------------------------------- |
| 40 | + |
| 41 | +using System; |
| 42 | +using System.Collections.Generic; |
| 43 | +using System.IO; |
| 44 | +using System.Net; |
| 45 | +using RabbitMQ.Client.Framing.Impl; |
| 46 | +using RabbitMQ.Client.Framing; |
| 47 | +using RabbitMQ.Util; |
| 48 | + |
| 49 | +namespace RabbitMQ.Client.Impl |
| 50 | +{ |
| 51 | + public class Command |
| 52 | + { |
| 53 | + // EmptyFrameSize, 8 = 1 + 2 + 4 + 1 |
| 54 | + // - 1 byte of frame type |
| 55 | + // - 2 bytes of channel number |
| 56 | + // - 4 bytes of frame payload length |
| 57 | + // - 1 byte of payload trailer FrameEnd byte |
| 58 | + private const int EmptyFrameSize = 8; |
| 59 | + private readonly MemoryStream m_body; |
| 60 | + private static readonly byte[] m_emptyByteArray = new byte[0]; |
| 61 | + |
| 62 | + static Command() |
| 63 | + { |
| 64 | + CheckEmptyFrameSize(); |
| 65 | + } |
| 66 | + |
| 67 | + public Command() : this(null, null, null) |
| 68 | + { |
| 69 | + m_body = new MemoryStream(); |
| 70 | + } |
| 71 | + |
| 72 | + public Command(MethodBase method) : this(method, null, null) |
| 73 | + { |
| 74 | + m_body = new MemoryStream(); |
| 75 | + } |
| 76 | + |
| 77 | + public Command(MethodBase method, ContentHeaderBase header, byte[] body) |
| 78 | + { |
| 79 | + Method = method; |
| 80 | + Header = header; |
| 81 | + if (body != null) |
| 82 | + { |
| 83 | + m_body = new MemoryStream(body); |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + m_body = new MemoryStream(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public byte[] Body |
| 92 | + { |
| 93 | + get { return ConsolidateBody(); } |
| 94 | + } |
| 95 | + |
| 96 | + public ContentHeaderBase Header { get; set; } |
| 97 | + |
| 98 | + public MethodBase Method { get; set; } |
| 99 | + |
| 100 | + public static void CheckEmptyFrameSize() |
| 101 | + { |
| 102 | + var f = new EmptyWriteFrame(); |
| 103 | + var stream = new MemoryStream(); |
| 104 | + var writer = new NetworkBinaryWriter(stream); |
| 105 | + f.WriteTo(writer); |
| 106 | + long actualLength = stream.Length; |
| 107 | + |
| 108 | + if (EmptyFrameSize != actualLength) |
| 109 | + { |
| 110 | + string message = |
| 111 | + string.Format("EmptyFrameSize is incorrect - defined as {0} where the computed value is in fact {1}.", |
| 112 | + EmptyFrameSize, |
| 113 | + actualLength); |
| 114 | + throw new ProtocolViolationException(message); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public void AppendBodyFragment(byte[] fragment) |
| 119 | + { |
| 120 | + if (fragment != null) |
| 121 | + { |
| 122 | + m_body.Write(fragment, 0, fragment.Length); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public byte[] ConsolidateBody() |
| 127 | + { |
| 128 | + return m_body.Length == 0 ? m_emptyByteArray : m_body.ToArray(); |
| 129 | + } |
| 130 | + |
| 131 | + public void Transmit(int channelNumber, Connection connection) |
| 132 | + { |
| 133 | + if (Method.HasContent) |
| 134 | + { |
| 135 | + TransmitAsFrameSet(channelNumber, connection); |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + TransmitAsSingleFrame(channelNumber, connection); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public void TransmitAsSingleFrame(int channelNumber, Connection connection) |
| 144 | + { |
| 145 | + connection.WriteFrame(new MethodWriteFrame(channelNumber, Method)); |
| 146 | + } |
| 147 | + |
| 148 | + public void TransmitAsFrameSet(int channelNumber, Connection connection) |
| 149 | + { |
| 150 | + var frames = new List<WriteFrame>(); |
| 151 | + frames.Add(new MethodWriteFrame(channelNumber, Method)); |
| 152 | + if (Method.HasContent) |
| 153 | + { |
| 154 | + var body = ConsolidateBody(); // Cache, since the property is compiled. |
| 155 | + |
| 156 | + frames.Add(new HeaderWriteFrame(channelNumber, Header, body.Length)); |
| 157 | + var frameMax = (int)Math.Min(int.MaxValue, connection.FrameMax); |
| 158 | + var bodyPayloadMax = (frameMax == 0) ? body.Length : frameMax - EmptyFrameSize; |
| 159 | + for (int offset = 0; offset < body.Length; offset += bodyPayloadMax) |
| 160 | + { |
| 161 | + var remaining = body.Length - offset; |
| 162 | + var count = (remaining < bodyPayloadMax) ? remaining : bodyPayloadMax; |
| 163 | + frames.Add(new BodySegmentWriteFrame(channelNumber, body, offset, count)); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + connection.WriteFrameSet(frames); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments