Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/engine/impl/FrameWriter.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21 package org.apache.qpid.proton.engine.impl;
22
23 import java.nio.ByteBuffer;
24
25 import org.apache.qpid.proton.amqp.Binary;
26 import org.apache.qpid.proton.amqp.security.SaslFrameBody;
27 import org.apache.qpid.proton.amqp.transport.EmptyFrame;
28 import org.apache.qpid.proton.amqp.transport.FrameBody;
29 import org.apache.qpid.proton.codec.EncoderImpl;
30 import org.apache.qpid.proton.codec.ReadableBuffer;
31 import org.apache.qpid.proton.framing.TransportFrame;
32
33 /**
34 * Writes Frames to an internal buffer for later processing by the transport.
35 */
36 class FrameWriter {
37
38 static final int DEFAULT_FRAME_BUFFER_FULL_MARK = 64 * 1024;
39 static final int FRAME_HEADER_SIZE = 8;
40
41 static final byte AMQP_FRAME_TYPE = 0;
42 static final byte SASL_FRAME_TYPE = 1;
43
44 private final TransportImpl transport;
45 private final EncoderImpl encoder;
46 private final FrameWriterBuffer frameBuffer = new FrameWriterBuffer();
47
48 // Configuration of this Frame Writer
49 private int maxFrameSize;
50 private final byte frameType;
51 private int frameBufferMaxBytes = DEFAULT_FRAME_BUFFER_FULL_MARK;
52
53 // State of current write operation, reset on start of each new write
54 private int frameStart;
55
56 // Frame Writer metrics
57 private long framesOutput;
58
59 FrameWriter(EncoderImpl encoder, int maxFrameSize, byte frameType, TransportImpl transport) {
60 this.encoder = encoder;
61 this.maxFrameSize = maxFrameSize;
62 this.frameType = frameType;
63 this.transport = transport;
64
65 encoder.setByteBuffer(frameBuffer);
66 }
67
68 boolean isFull() {
69 return frameBuffer.position() > frameBufferMaxBytes;
70 }
71
72 int readBytes(ByteBuffer dst) {
73 return frameBuffer.transferTo(dst);
74 }
75
76 long getFramesOutput() {
77 return framesOutput;
78 }
79
80 void setMaxFrameSize(int maxFrameSize) {
81 this.maxFrameSize = maxFrameSize;
82 }
83
84 void setFrameWriterMaxBytes(int maxBytes) {
85 this.frameBufferMaxBytes = maxBytes;
86 }
87
88 int getFrameWriterMaxBytes() {
89 return frameBufferMaxBytes;
90 }
91
92 void writeHeader(byte[] header) {
93 frameBuffer.put(header, 0, header.length);
94 }
95
96 void writeFrame(Object frameBody) {
97 writeFrame(0, frameBody, null, null);
98 }
99
100 void writeFrame(int channel, Object frameBody, ReadableBuffer payload, Runnable onPayloadTooLarge) {
101 frameStart = frameBuffer.position();
102 try {
103 final int performativeSize = writePerformative(frameBody, payload, onPayloadTooLarge);
104 final int capacity = maxFrameSize > 0 ? maxFrameSize - performativeSize : Integer.MAX_VALUE;
105 final int payloadSize = Math.min(payload == null ? 0 : payload.remaining(), capacity);
106
107 if (transport.isFrameTracingEnabled()) {
108 logFrame(channel, frameBody, payload, payloadSize);
109 }
110
111 if (payloadSize > 0) {
112 int oldLimit = payload.limit();
113 payload.limit(payload.position() + payloadSize);
114 frameBuffer.put(payload);
115 payload.limit(oldLimit);
116 }
117
118 endFrame(channel);
119
120 framesOutput++;
121 } catch (Exception e) {
122 frameBuffer.position(frameStart);
123 throw e;
124 }
125 }
126
127 private int writePerformative(Object frameBody, ReadableBuffer payload, Runnable onPayloadTooLarge) {
128 frameBuffer.position(frameStart + FRAME_HEADER_SIZE);
129
130 if (frameBody != null) {
131 encoder.writeObject(frameBody);
132 }
133
134 int performativeSize = frameBuffer.position() - frameStart;
135
136 if (onPayloadTooLarge != null && maxFrameSize > 0 && payload != null && (payload.remaining() + performativeSize) > maxFrameSize) {
137 // Next iteration will re-encode the frame body again with updates from the <payload-to-large>
138 // handler and then we can move onto the body portion.
139 onPayloadTooLarge.run();
140 performativeSize = writePerformative(frameBody, payload, null);
141 }
142
143 return performativeSize;
144 }
145
146 private void endFrame(int channel) {
147 int frameSize = frameBuffer.position() - frameStart;
148 int originalPosition = frameBuffer.position();
149
150 frameBuffer.position(frameStart);
151 frameBuffer.putInt(frameSize);
152 frameBuffer.put((byte) 2);
153 frameBuffer.put(frameType);
154 frameBuffer.putShort((short) channel);
155 frameBuffer.position(originalPosition);
156 }
157
158 private void logFrame(int channel, Object frameBody, ReadableBuffer payload, int payloadSize) {
159 ProtocolTracer tracer = transport.getProtocolTracer();
160 if (frameType == AMQP_FRAME_TYPE) {
161 ReadableBuffer originalPayload = null;
162 if (payload != null) {
163 originalPayload = payload.slice();
164 originalPayload.limit(payloadSize);
165 }
166
167 Binary payloadBin = Binary.create(originalPayload);
168 FrameBody body = null;
169 if (frameBody == null) {
170 body = EmptyFrame.INSTANCE;
171 } else {
172 body = (FrameBody) frameBody;
173 }
174
175 TransportFrame frame = new TransportFrame(channel, body, payloadBin);
176
177 transport.log(TransportImpl.OUTGOING, frame);
178
179 if (tracer != null) {
180 tracer.sentFrame(frame);
181 }
182 } else {
183 SaslFrameBody body = (SaslFrameBody) frameBody;
184 transport.log(TransportImpl.OUTGOING, body);
185 if (tracer != null) {
186 tracer.sentSaslBody(body);
187 }
188 }
189 }
190 }