author | dfuchs |
Mon, 23 Apr 2018 15:45:40 +0100 | |
branch | http-client-branch |
changeset 56474 | fe2bf7b369b8 |
parent 56463 | b583caf69b39 |
child 56481 | 247ed0848e48 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
49765 | 2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
48083 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
49765 | 26 |
package jdk.internal.net.http; |
48083 | 27 |
|
28 |
import java.io.IOException; |
|
29 |
import java.nio.ByteBuffer; |
|
30 |
import java.util.List; |
|
31 |
import java.util.Objects; |
|
32 |
import java.util.concurrent.Flow; |
|
33 |
import java.util.concurrent.atomic.AtomicLong; |
|
34 |
import java.util.concurrent.atomic.AtomicReference; |
|
35 |
import java.nio.channels.SelectableChannel; |
|
36 |
import java.nio.channels.SelectionKey; |
|
37 |
import java.nio.channels.SocketChannel; |
|
38 |
import java.util.ArrayList; |
|
39 |
import java.util.function.Consumer; |
|
40 |
import java.util.function.Supplier; |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
41 |
import jdk.internal.net.http.common.BufferSupplier; |
49765 | 42 |
import jdk.internal.net.http.common.Demand; |
43 |
import jdk.internal.net.http.common.FlowTube; |
|
44 |
import jdk.internal.net.http.common.Logger; |
|
45 |
import jdk.internal.net.http.common.SequentialScheduler; |
|
46 |
import jdk.internal.net.http.common.SequentialScheduler.DeferredCompleter; |
|
47 |
import jdk.internal.net.http.common.SequentialScheduler.RestartableTask; |
|
48 |
import jdk.internal.net.http.common.Utils; |
|
48083 | 49 |
|
50 |
/** |
|
51 |
* A SocketTube is a terminal tube plugged directly into the socket. |
|
52 |
* The read subscriber should call {@code subscribe} on the SocketTube before |
|
49765 | 53 |
* the SocketTube is subscribed to the write publisher. |
48083 | 54 |
*/ |
55 |
final class SocketTube implements FlowTube { |
|
56 |
||
49765 | 57 |
final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
48083 | 58 |
static final AtomicLong IDS = new AtomicLong(); |
59 |
||
60 |
private final HttpClientImpl client; |
|
61 |
private final SocketChannel channel; |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
62 |
private final SliceBufferSource sliceBuffersSource; |
48083 | 63 |
private final Object lock = new Object(); |
64 |
private final AtomicReference<Throwable> errorRef = new AtomicReference<>(); |
|
65 |
private final InternalReadPublisher readPublisher; |
|
66 |
private final InternalWriteSubscriber writeSubscriber; |
|
67 |
private final long id = IDS.incrementAndGet(); |
|
68 |
||
69 |
public SocketTube(HttpClientImpl client, SocketChannel channel, |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
70 |
Supplier<ByteBuffer> buffersFactory) { |
48083 | 71 |
this.client = client; |
72 |
this.channel = channel; |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
73 |
this.sliceBuffersSource = new SliceBufferSource(buffersFactory); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
74 |
|
48083 | 75 |
this.readPublisher = new InternalReadPublisher(); |
76 |
this.writeSubscriber = new InternalWriteSubscriber(); |
|
77 |
} |
|
78 |
||
79 |
/** |
|
80 |
* Returns {@code true} if this flow is finished. |
|
81 |
* This happens when this flow internal read subscription is completed, |
|
82 |
* either normally (EOF reading) or exceptionally (EOF writing, or |
|
83 |
* underlying socket closed, or some exception occurred while reading or |
|
84 |
* writing to the socket). |
|
85 |
* |
|
86 |
* @return {@code true} if this flow is finished. |
|
87 |
*/ |
|
88 |
public boolean isFinished() { |
|
89 |
InternalReadPublisher.InternalReadSubscription subscription = |
|
90 |
readPublisher.subscriptionImpl; |
|
91 |
return subscription != null && subscription.completed |
|
92 |
|| subscription == null && errorRef.get() != null; |
|
93 |
} |
|
94 |
||
95 |
// ===================================================================== // |
|
96 |
// Flow.Publisher // |
|
97 |
// ======================================================================// |
|
98 |
||
99 |
/** |
|
100 |
* {@inheritDoc } |
|
101 |
* @apiNote This method should be called first. In particular, the caller |
|
102 |
* must ensure that this method must be called by the read |
|
103 |
* subscriber before the write publisher can call {@code onSubscribe}. |
|
104 |
* Failure to adhere to this contract may result in assertion errors. |
|
105 |
*/ |
|
106 |
@Override |
|
107 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) { |
|
108 |
Objects.requireNonNull(s); |
|
109 |
assert s instanceof TubeSubscriber : "Expected TubeSubscriber, got:" + s; |
|
110 |
readPublisher.subscribe(s); |
|
111 |
} |
|
112 |
||
113 |
||
114 |
// ===================================================================== // |
|
115 |
// Flow.Subscriber // |
|
116 |
// ======================================================================// |
|
117 |
||
118 |
/** |
|
119 |
* {@inheritDoc } |
|
120 |
* @apiNote The caller must ensure that {@code subscribe} is called by |
|
121 |
* the read subscriber before {@code onSubscribe} is called by |
|
122 |
* the write publisher. |
|
123 |
* Failure to adhere to this contract may result in assertion errors. |
|
124 |
*/ |
|
125 |
@Override |
|
126 |
public void onSubscribe(Flow.Subscription subscription) { |
|
127 |
writeSubscriber.onSubscribe(subscription); |
|
128 |
} |
|
129 |
||
130 |
@Override |
|
131 |
public void onNext(List<ByteBuffer> item) { |
|
132 |
writeSubscriber.onNext(item); |
|
133 |
} |
|
134 |
||
135 |
@Override |
|
136 |
public void onError(Throwable throwable) { |
|
137 |
writeSubscriber.onError(throwable); |
|
138 |
} |
|
139 |
||
140 |
@Override |
|
141 |
public void onComplete() { |
|
142 |
writeSubscriber.onComplete(); |
|
143 |
} |
|
144 |
||
145 |
// ===================================================================== // |
|
146 |
// Events // |
|
147 |
// ======================================================================// |
|
148 |
||
49765 | 149 |
void signalClosed() { |
150 |
// Ensures that the subscriber will be terminated and that future |
|
151 |
// subscribers will be notified when the connection is closed. |
|
152 |
readPublisher.subscriptionImpl.signalError( |
|
153 |
new IOException("connection closed locally")); |
|
154 |
} |
|
155 |
||
48083 | 156 |
/** |
157 |
* A restartable task used to process tasks in sequence. |
|
158 |
*/ |
|
159 |
private static class SocketFlowTask implements RestartableTask { |
|
160 |
final Runnable task; |
|
161 |
private final Object monitor = new Object(); |
|
162 |
SocketFlowTask(Runnable task) { |
|
163 |
this.task = task; |
|
164 |
} |
|
165 |
@Override |
|
166 |
public final void run(DeferredCompleter taskCompleter) { |
|
167 |
try { |
|
168 |
// non contentious synchronized for visibility. |
|
169 |
synchronized(monitor) { |
|
170 |
task.run(); |
|
171 |
} |
|
172 |
} finally { |
|
173 |
taskCompleter.complete(); |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
||
49765 | 178 |
// This is best effort - there's no guarantee that the printed set of values |
179 |
// is consistent. It should only be considered as weakly accurate - in |
|
180 |
// particular in what concerns the events states, especially when displaying |
|
181 |
// a read event state from a write event callback and conversely. |
|
48083 | 182 |
void debugState(String when) { |
49765 | 183 |
if (debug.on()) { |
48083 | 184 |
StringBuilder state = new StringBuilder(); |
185 |
||
186 |
InternalReadPublisher.InternalReadSubscription sub = |
|
187 |
readPublisher.subscriptionImpl; |
|
188 |
InternalReadPublisher.ReadEvent readEvent = |
|
189 |
sub == null ? null : sub.readEvent; |
|
190 |
Demand rdemand = sub == null ? null : sub.demand; |
|
191 |
InternalWriteSubscriber.WriteEvent writeEvent = |
|
192 |
writeSubscriber.writeEvent; |
|
49765 | 193 |
Demand wdemand = writeSubscriber.writeDemand; |
48083 | 194 |
int rops = readEvent == null ? 0 : readEvent.interestOps(); |
195 |
long rd = rdemand == null ? 0 : rdemand.get(); |
|
196 |
int wops = writeEvent == null ? 0 : writeEvent.interestOps(); |
|
197 |
long wd = wdemand == null ? 0 : wdemand.get(); |
|
198 |
||
199 |
state.append(when).append(" Reading: [ops=") |
|
200 |
.append(rops).append(", demand=").append(rd) |
|
201 |
.append(", stopped=") |
|
202 |
.append((sub == null ? false : sub.readScheduler.isStopped())) |
|
203 |
.append("], Writing: [ops=").append(wops) |
|
204 |
.append(", demand=").append(wd) |
|
205 |
.append("]"); |
|
49765 | 206 |
debug.log(state.toString()); |
48083 | 207 |
} |
208 |
} |
|
209 |
||
210 |
/** |
|
49765 | 211 |
* A repeatable event that can be paused or resumed by changing its |
212 |
* interestOps. When the event is fired, it is first paused before being |
|
213 |
* signaled. It is the responsibility of the code triggered by |
|
214 |
* {@code signalEvent} to resume the event if required. |
|
48083 | 215 |
*/ |
216 |
private static abstract class SocketFlowEvent extends AsyncEvent { |
|
217 |
final SocketChannel channel; |
|
218 |
final int defaultInterest; |
|
219 |
volatile int interestOps; |
|
220 |
volatile boolean registered; |
|
221 |
SocketFlowEvent(int defaultInterest, SocketChannel channel) { |
|
222 |
super(AsyncEvent.REPEATING); |
|
223 |
this.defaultInterest = defaultInterest; |
|
224 |
this.channel = channel; |
|
225 |
} |
|
226 |
final boolean registered() {return registered;} |
|
227 |
final void resume() { |
|
228 |
interestOps = defaultInterest; |
|
229 |
registered = true; |
|
230 |
} |
|
231 |
final void pause() {interestOps = 0;} |
|
232 |
@Override |
|
233 |
public final SelectableChannel channel() {return channel;} |
|
234 |
@Override |
|
235 |
public final int interestOps() {return interestOps;} |
|
236 |
||
237 |
@Override |
|
238 |
public final void handle() { |
|
239 |
pause(); // pause, then signal |
|
240 |
signalEvent(); // won't be fired again until resumed. |
|
241 |
} |
|
242 |
@Override |
|
243 |
public final void abort(IOException error) { |
|
49765 | 244 |
debug().log(() -> "abort: " + error); |
48083 | 245 |
pause(); // pause, then signal |
246 |
signalError(error); // should not be resumed after abort (not checked) |
|
247 |
} |
|
248 |
||
249 |
protected abstract void signalEvent(); |
|
250 |
protected abstract void signalError(Throwable error); |
|
49765 | 251 |
abstract Logger debug(); |
48083 | 252 |
} |
253 |
||
254 |
// ===================================================================== // |
|
255 |
// Writing // |
|
256 |
// ======================================================================// |
|
257 |
||
49765 | 258 |
// This class makes the assumption that the publisher will call onNext |
259 |
// sequentially, and that onNext won't be called if the demand has not been |
|
260 |
// incremented by request(1). |
|
48083 | 261 |
// It has a 'queue of 1' meaning that it will call request(1) in |
262 |
// onSubscribe, and then only after its 'current' buffer list has been |
|
263 |
// fully written and current set to null; |
|
264 |
private final class InternalWriteSubscriber |
|
265 |
implements Flow.Subscriber<List<ByteBuffer>> { |
|
266 |
||
49765 | 267 |
volatile WriteSubscription subscription; |
48083 | 268 |
volatile List<ByteBuffer> current; |
269 |
volatile boolean completed; |
|
49765 | 270 |
final AsyncTriggerEvent startSubscription = |
271 |
new AsyncTriggerEvent(this::signalError, this::startSubscription); |
|
48083 | 272 |
final WriteEvent writeEvent = new WriteEvent(channel, this); |
49765 | 273 |
final Demand writeDemand = new Demand(); |
48083 | 274 |
|
275 |
@Override |
|
276 |
public void onSubscribe(Flow.Subscription subscription) { |
|
49765 | 277 |
WriteSubscription previous = this.subscription; |
278 |
if (debug.on()) debug.log("subscribed for writing"); |
|
279 |
try { |
|
280 |
boolean needEvent = current == null; |
|
281 |
if (needEvent) { |
|
282 |
if (previous != null && previous.upstreamSubscription != subscription) { |
|
283 |
previous.dropSubscription(); |
|
48083 | 284 |
} |
285 |
} |
|
49765 | 286 |
this.subscription = new WriteSubscription(subscription); |
287 |
if (needEvent) { |
|
288 |
if (debug.on()) |
|
289 |
debug.log("write: registering startSubscription event"); |
|
290 |
client.registerEvent(startSubscription); |
|
291 |
} |
|
292 |
} catch (Throwable t) { |
|
293 |
signalError(t); |
|
48083 | 294 |
} |
295 |
} |
|
296 |
||
297 |
@Override |
|
298 |
public void onNext(List<ByteBuffer> bufs) { |
|
49765 | 299 |
assert current == null : dbgString() // this is a queue of 1. |
300 |
+ "w.onNext current: " + current; |
|
301 |
assert subscription != null : dbgString() |
|
302 |
+ "w.onNext: subscription is null"; |
|
48083 | 303 |
current = bufs; |
304 |
tryFlushCurrent(client.isSelectorThread()); // may be in selector thread |
|
305 |
// For instance in HTTP/2, a received SETTINGS frame might trigger |
|
306 |
// the sending of a SETTINGS frame in turn which might cause |
|
307 |
// onNext to be called from within the same selector thread that the |
|
308 |
// original SETTINGS frames arrived on. If rs is the read-subscriber |
|
309 |
// and ws is the write-subscriber then the following can occur: |
|
310 |
// ReadEvent -> rs.onNext(bytes) -> process server SETTINGS -> write |
|
311 |
// client SETTINGS -> ws.onNext(bytes) -> tryFlushCurrent |
|
312 |
debugState("leaving w.onNext"); |
|
313 |
} |
|
314 |
||
49765 | 315 |
// Don't use a SequentialScheduler here: rely on onNext() being invoked |
316 |
// sequentially, and not being invoked if there is no demand, request(1). |
|
317 |
// onNext is usually called from within a user / executor thread. |
|
318 |
// Initial writing will be performed in that thread. If for some reason, |
|
319 |
// not all the data can be written, a writeEvent will be registered, and |
|
320 |
// writing will resume in the the selector manager thread when the |
|
321 |
// writeEvent is fired. |
|
322 |
// |
|
323 |
// If this method is invoked in the selector manager thread (because of |
|
324 |
// a writeEvent), then the executor will be used to invoke request(1), |
|
325 |
// ensuring that onNext() won't be invoked from within the selector |
|
326 |
// thread. If not in the selector manager thread, then request(1) is |
|
327 |
// invoked directly. |
|
48083 | 328 |
void tryFlushCurrent(boolean inSelectorThread) { |
329 |
List<ByteBuffer> bufs = current; |
|
330 |
if (bufs == null) return; |
|
331 |
try { |
|
332 |
assert inSelectorThread == client.isSelectorThread() : |
|
333 |
"should " + (inSelectorThread ? "" : "not ") |
|
334 |
+ " be in the selector thread"; |
|
335 |
long remaining = Utils.remaining(bufs); |
|
49765 | 336 |
if (debug.on()) debug.log("trying to write: %d", remaining); |
48083 | 337 |
long written = writeAvailable(bufs); |
49765 | 338 |
if (debug.on()) debug.log("wrote: %d", written); |
339 |
assert written >= 0 : "negative number of bytes written:" + written; |
|
48083 | 340 |
assert written <= remaining; |
341 |
if (remaining - written == 0) { |
|
342 |
current = null; |
|
49765 | 343 |
if (writeDemand.tryDecrement()) { |
344 |
Runnable requestMore = this::requestMore; |
|
345 |
if (inSelectorThread) { |
|
346 |
assert client.isSelectorThread(); |
|
347 |
client.theExecutor().execute(requestMore); |
|
348 |
} else { |
|
349 |
assert !client.isSelectorThread(); |
|
350 |
requestMore.run(); |
|
351 |
} |
|
48083 | 352 |
} |
353 |
} else { |
|
354 |
resumeWriteEvent(inSelectorThread); |
|
355 |
} |
|
356 |
} catch (Throwable t) { |
|
357 |
signalError(t); |
|
358 |
subscription.cancel(); |
|
359 |
} |
|
360 |
} |
|
361 |
||
49765 | 362 |
// Kick off the initial request:1 that will start the writing side. |
363 |
// Invoked in the selector manager thread. |
|
364 |
void startSubscription() { |
|
48083 | 365 |
try { |
49765 | 366 |
if (debug.on()) debug.log("write: starting subscription"); |
367 |
assert client.isSelectorThread(); |
|
368 |
// make sure read registrations are handled before; |
|
369 |
readPublisher.subscriptionImpl.handlePending(); |
|
370 |
if (debug.on()) debug.log("write: offloading requestMore"); |
|
371 |
// start writing; |
|
372 |
client.theExecutor().execute(this::requestMore); |
|
373 |
} catch(Throwable t) { |
|
48083 | 374 |
signalError(t); |
375 |
} |
|
376 |
} |
|
377 |
||
49765 | 378 |
void requestMore() { |
379 |
WriteSubscription subscription = this.subscription; |
|
380 |
subscription.requestMore(); |
|
381 |
} |
|
382 |
||
48083 | 383 |
@Override |
384 |
public void onError(Throwable throwable) { |
|
385 |
signalError(throwable); |
|
386 |
} |
|
387 |
||
388 |
@Override |
|
389 |
public void onComplete() { |
|
390 |
completed = true; |
|
391 |
// no need to pause the write event here: the write event will |
|
392 |
// be paused if there is nothing more to write. |
|
393 |
List<ByteBuffer> bufs = current; |
|
394 |
long remaining = bufs == null ? 0 : Utils.remaining(bufs); |
|
49765 | 395 |
if (debug.on()) |
396 |
debug.log( "write completed, %d yet to send", remaining); |
|
48083 | 397 |
debugState("InternalWriteSubscriber::onComplete"); |
398 |
} |
|
399 |
||
400 |
void resumeWriteEvent(boolean inSelectorThread) { |
|
49765 | 401 |
if (debug.on()) debug.log("scheduling write event"); |
48083 | 402 |
resumeEvent(writeEvent, this::signalError); |
403 |
} |
|
404 |
||
405 |
void signalWritable() { |
|
49765 | 406 |
if (debug.on()) debug.log("channel is writable"); |
48083 | 407 |
tryFlushCurrent(true); |
408 |
} |
|
409 |
||
410 |
void signalError(Throwable error) { |
|
49765 | 411 |
debug.log(() -> "write error: " + error); |
48083 | 412 |
completed = true; |
413 |
readPublisher.signalError(error); |
|
414 |
} |
|
415 |
||
416 |
// A repeatable WriteEvent which is paused after firing and can |
|
417 |
// be resumed if required - see SocketFlowEvent; |
|
418 |
final class WriteEvent extends SocketFlowEvent { |
|
419 |
final InternalWriteSubscriber sub; |
|
420 |
WriteEvent(SocketChannel channel, InternalWriteSubscriber sub) { |
|
421 |
super(SelectionKey.OP_WRITE, channel); |
|
422 |
this.sub = sub; |
|
423 |
} |
|
424 |
@Override |
|
425 |
protected final void signalEvent() { |
|
426 |
try { |
|
427 |
client.eventUpdated(this); |
|
428 |
sub.signalWritable(); |
|
429 |
} catch(Throwable t) { |
|
430 |
sub.signalError(t); |
|
431 |
} |
|
432 |
} |
|
433 |
||
434 |
@Override |
|
435 |
protected void signalError(Throwable error) { |
|
436 |
sub.signalError(error); |
|
437 |
} |
|
438 |
||
439 |
@Override |
|
49765 | 440 |
Logger debug() { return debug; } |
441 |
} |
|
442 |
||
443 |
final class WriteSubscription implements Flow.Subscription { |
|
444 |
final Flow.Subscription upstreamSubscription; |
|
445 |
volatile boolean cancelled; |
|
446 |
WriteSubscription(Flow.Subscription subscription) { |
|
447 |
this.upstreamSubscription = subscription; |
|
448 |
} |
|
449 |
||
450 |
@Override |
|
451 |
public void request(long n) { |
|
452 |
if (cancelled) return; |
|
453 |
upstreamSubscription.request(n); |
|
454 |
} |
|
455 |
||
456 |
@Override |
|
457 |
public void cancel() { |
|
458 |
dropSubscription(); |
|
459 |
upstreamSubscription.cancel(); |
|
460 |
} |
|
461 |
||
462 |
void dropSubscription() { |
|
463 |
synchronized (InternalWriteSubscriber.this) { |
|
464 |
cancelled = true; |
|
465 |
if (debug.on()) debug.log("write: resetting demand to 0"); |
|
466 |
writeDemand.reset(); |
|
467 |
} |
|
48083 | 468 |
} |
469 |
||
49765 | 470 |
void requestMore() { |
471 |
try { |
|
472 |
if (completed || cancelled) return; |
|
473 |
boolean requestMore; |
|
474 |
long d; |
|
475 |
// don't fiddle with demand after cancel. |
|
476 |
// see dropSubscription. |
|
477 |
synchronized (InternalWriteSubscriber.this) { |
|
478 |
if (cancelled) return; |
|
479 |
d = writeDemand.get(); |
|
480 |
requestMore = writeDemand.increaseIfFulfilled(); |
|
481 |
} |
|
482 |
if (requestMore) { |
|
483 |
if (debug.on()) debug.log("write: requesting more..."); |
|
484 |
upstreamSubscription.request(1); |
|
485 |
} else { |
|
486 |
if (debug.on()) |
|
487 |
debug.log("write: no need to request more: %d", d); |
|
488 |
} |
|
489 |
} catch (Throwable t) { |
|
490 |
if (debug.on()) |
|
491 |
debug.log("write: error while requesting more: " + t); |
|
492 |
cancelled = true; |
|
493 |
signalError(t); |
|
494 |
subscription.cancel(); |
|
495 |
} finally { |
|
496 |
debugState("leaving requestMore: "); |
|
497 |
} |
|
498 |
} |
|
48083 | 499 |
} |
500 |
} |
|
501 |
||
502 |
// ===================================================================== // |
|
503 |
// Reading // |
|
504 |
// ===================================================================== // |
|
505 |
||
506 |
// The InternalReadPublisher uses a SequentialScheduler to ensure that |
|
507 |
// onNext/onError/onComplete are called sequentially on the caller's |
|
508 |
// subscriber. |
|
509 |
// However, it relies on the fact that the only time where |
|
510 |
// runOrSchedule() is called from a user/executor thread is in signalError, |
|
511 |
// right after the errorRef has been set. |
|
512 |
// Because the sequential scheduler's task always checks for errors first, |
|
513 |
// and always terminate the scheduler on error, then it is safe to assume |
|
514 |
// that if it reaches the point where it reads from the channel, then |
|
515 |
// it is running in the SelectorManager thread. This is because all |
|
516 |
// other invocation of runOrSchedule() are triggered from within a |
|
517 |
// ReadEvent. |
|
518 |
// |
|
519 |
// When pausing/resuming the event, some shortcuts can then be taken |
|
520 |
// when we know we're running in the selector manager thread |
|
521 |
// (in that case there's no need to call client.eventUpdated(readEvent); |
|
522 |
// |
|
523 |
private final class InternalReadPublisher |
|
524 |
implements Flow.Publisher<List<ByteBuffer>> { |
|
525 |
private final InternalReadSubscription subscriptionImpl |
|
526 |
= new InternalReadSubscription(); |
|
527 |
AtomicReference<ReadSubscription> pendingSubscription = new AtomicReference<>(); |
|
528 |
private volatile ReadSubscription subscription; |
|
529 |
||
530 |
@Override |
|
531 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) { |
|
532 |
Objects.requireNonNull(s); |
|
533 |
||
534 |
TubeSubscriber sub = FlowTube.asTubeSubscriber(s); |
|
535 |
ReadSubscription target = new ReadSubscription(subscriptionImpl, sub); |
|
536 |
ReadSubscription previous = pendingSubscription.getAndSet(target); |
|
537 |
||
538 |
if (previous != null && previous != target) { |
|
49765 | 539 |
if (debug.on()) |
540 |
debug.log("read publisher: dropping pending subscriber: " |
|
541 |
+ previous.subscriber); |
|
48083 | 542 |
previous.errorRef.compareAndSet(null, errorRef.get()); |
543 |
previous.signalOnSubscribe(); |
|
544 |
if (subscriptionImpl.completed) { |
|
545 |
previous.signalCompletion(); |
|
546 |
} else { |
|
547 |
previous.subscriber.dropSubscription(); |
|
548 |
} |
|
549 |
} |
|
550 |
||
49765 | 551 |
if (debug.on()) debug.log("read publisher got subscriber"); |
48083 | 552 |
subscriptionImpl.signalSubscribe(); |
553 |
debugState("leaving read.subscribe: "); |
|
554 |
} |
|
555 |
||
556 |
void signalError(Throwable error) { |
|
49765 | 557 |
if (debug.on()) debug.log("error signalled " + error); |
48083 | 558 |
if (!errorRef.compareAndSet(null, error)) { |
559 |
return; |
|
560 |
} |
|
561 |
subscriptionImpl.handleError(); |
|
562 |
} |
|
563 |
||
564 |
final class ReadSubscription implements Flow.Subscription { |
|
565 |
final InternalReadSubscription impl; |
|
566 |
final TubeSubscriber subscriber; |
|
567 |
final AtomicReference<Throwable> errorRef = new AtomicReference<>(); |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
568 |
final BufferSource bufferSource; |
48083 | 569 |
volatile boolean subscribed; |
570 |
volatile boolean cancelled; |
|
571 |
volatile boolean completed; |
|
572 |
||
573 |
public ReadSubscription(InternalReadSubscription impl, |
|
574 |
TubeSubscriber subscriber) { |
|
575 |
this.impl = impl; |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
576 |
this.bufferSource = subscriber.supportsRecycling() |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
577 |
? new SSLDirectBufferSource(client) |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
578 |
: SocketTube.this.sliceBuffersSource; |
48083 | 579 |
this.subscriber = subscriber; |
580 |
} |
|
581 |
||
582 |
@Override |
|
583 |
public void cancel() { |
|
584 |
cancelled = true; |
|
585 |
} |
|
586 |
||
587 |
@Override |
|
588 |
public void request(long n) { |
|
589 |
if (!cancelled) { |
|
590 |
impl.request(n); |
|
591 |
} else { |
|
49765 | 592 |
if (debug.on()) |
593 |
debug.log("subscription cancelled, ignoring request %d", n); |
|
48083 | 594 |
} |
595 |
} |
|
596 |
||
597 |
void signalCompletion() { |
|
598 |
assert subscribed || cancelled; |
|
599 |
if (completed || cancelled) return; |
|
600 |
synchronized (this) { |
|
601 |
if (completed) return; |
|
602 |
completed = true; |
|
603 |
} |
|
604 |
Throwable error = errorRef.get(); |
|
605 |
if (error != null) { |
|
49765 | 606 |
if (debug.on()) |
607 |
debug.log("forwarding error to subscriber: " + error); |
|
48083 | 608 |
subscriber.onError(error); |
609 |
} else { |
|
49765 | 610 |
if (debug.on()) debug.log("completing subscriber"); |
48083 | 611 |
subscriber.onComplete(); |
612 |
} |
|
613 |
} |
|
614 |
||
615 |
void signalOnSubscribe() { |
|
616 |
if (subscribed || cancelled) return; |
|
617 |
synchronized (this) { |
|
618 |
if (subscribed || cancelled) return; |
|
619 |
subscribed = true; |
|
620 |
} |
|
621 |
subscriber.onSubscribe(this); |
|
49765 | 622 |
if (debug.on()) debug.log("onSubscribe called"); |
48083 | 623 |
if (errorRef.get() != null) { |
624 |
signalCompletion(); |
|
625 |
} |
|
626 |
} |
|
627 |
} |
|
628 |
||
629 |
final class InternalReadSubscription implements Flow.Subscription { |
|
630 |
||
631 |
private final Demand demand = new Demand(); |
|
632 |
final SequentialScheduler readScheduler; |
|
633 |
private volatile boolean completed; |
|
634 |
private final ReadEvent readEvent; |
|
635 |
private final AsyncEvent subscribeEvent; |
|
636 |
||
637 |
InternalReadSubscription() { |
|
638 |
readScheduler = new SequentialScheduler(new SocketFlowTask(this::read)); |
|
639 |
subscribeEvent = new AsyncTriggerEvent(this::signalError, |
|
640 |
this::handleSubscribeEvent); |
|
641 |
readEvent = new ReadEvent(channel, this); |
|
642 |
} |
|
643 |
||
644 |
/* |
|
645 |
* This method must be invoked before any other method of this class. |
|
646 |
*/ |
|
647 |
final void signalSubscribe() { |
|
648 |
if (readScheduler.isStopped() || completed) { |
|
649 |
// if already completed or stopped we can handle any |
|
650 |
// pending connection directly from here. |
|
49765 | 651 |
if (debug.on()) |
652 |
debug.log("handling pending subscription while completed"); |
|
48083 | 653 |
handlePending(); |
654 |
} else { |
|
655 |
try { |
|
49765 | 656 |
if (debug.on()) debug.log("registering subscribe event"); |
48083 | 657 |
client.registerEvent(subscribeEvent); |
658 |
} catch (Throwable t) { |
|
659 |
signalError(t); |
|
660 |
handlePending(); |
|
661 |
} |
|
662 |
} |
|
663 |
} |
|
664 |
||
665 |
final void handleSubscribeEvent() { |
|
666 |
assert client.isSelectorThread(); |
|
49765 | 667 |
debug.log("subscribe event raised"); |
48083 | 668 |
readScheduler.runOrSchedule(); |
669 |
if (readScheduler.isStopped() || completed) { |
|
670 |
// if already completed or stopped we can handle any |
|
671 |
// pending connection directly from here. |
|
49765 | 672 |
if (debug.on()) |
673 |
debug.log("handling pending subscription when completed"); |
|
48083 | 674 |
handlePending(); |
675 |
} |
|
676 |
} |
|
677 |
||
678 |
||
679 |
/* |
|
680 |
* Although this method is thread-safe, the Reactive-Streams spec seems |
|
681 |
* to not require it to be as such. It's a responsibility of the |
|
682 |
* subscriber to signal demand in a thread-safe manner. |
|
683 |
* |
|
49765 | 684 |
* See Reactive Streams specification, rules 2.7 and 3.4. |
48083 | 685 |
*/ |
686 |
@Override |
|
687 |
public final void request(long n) { |
|
688 |
if (n > 0L) { |
|
689 |
boolean wasFulfilled = demand.increase(n); |
|
690 |
if (wasFulfilled) { |
|
49765 | 691 |
if (debug.on()) debug.log("got some demand for reading"); |
48083 | 692 |
resumeReadEvent(); |
693 |
// if demand has been changed from fulfilled |
|
694 |
// to unfulfilled register read event; |
|
695 |
} |
|
696 |
} else { |
|
697 |
signalError(new IllegalArgumentException("non-positive request")); |
|
698 |
} |
|
699 |
debugState("leaving request("+n+"): "); |
|
700 |
} |
|
701 |
||
702 |
@Override |
|
703 |
public final void cancel() { |
|
704 |
pauseReadEvent(); |
|
705 |
readScheduler.stop(); |
|
706 |
} |
|
707 |
||
708 |
private void resumeReadEvent() { |
|
49765 | 709 |
if (debug.on()) debug.log("resuming read event"); |
48083 | 710 |
resumeEvent(readEvent, this::signalError); |
711 |
} |
|
712 |
||
713 |
private void pauseReadEvent() { |
|
49765 | 714 |
if (debug.on()) debug.log("pausing read event"); |
48083 | 715 |
pauseEvent(readEvent, this::signalError); |
716 |
} |
|
717 |
||
718 |
||
719 |
final void handleError() { |
|
720 |
assert errorRef.get() != null; |
|
721 |
readScheduler.runOrSchedule(); |
|
722 |
} |
|
723 |
||
724 |
final void signalError(Throwable error) { |
|
725 |
if (!errorRef.compareAndSet(null, error)) { |
|
726 |
return; |
|
727 |
} |
|
49765 | 728 |
if (debug.on()) debug.log("got read error: " + error); |
48083 | 729 |
readScheduler.runOrSchedule(); |
730 |
} |
|
731 |
||
732 |
final void signalReadable() { |
|
733 |
readScheduler.runOrSchedule(); |
|
734 |
} |
|
735 |
||
736 |
/** The body of the task that runs in SequentialScheduler. */ |
|
737 |
final void read() { |
|
738 |
// It is important to only call pauseReadEvent() when stopping |
|
739 |
// the scheduler. The event is automatically paused before |
|
740 |
// firing, and trying to pause it again could cause a race |
|
741 |
// condition between this loop, which calls tryDecrementDemand(), |
|
742 |
// and the thread that calls request(n), which will try to resume |
|
743 |
// reading. |
|
744 |
try { |
|
745 |
while(!readScheduler.isStopped()) { |
|
746 |
if (completed) return; |
|
747 |
||
748 |
// make sure we have a subscriber |
|
749 |
if (handlePending()) { |
|
49765 | 750 |
if (debug.on()) |
751 |
debug.log("pending subscriber subscribed"); |
|
48083 | 752 |
return; |
753 |
} |
|
754 |
||
755 |
// If an error was signaled, we might not be in the |
|
756 |
// the selector thread, and that is OK, because we |
|
757 |
// will just call onError and return. |
|
758 |
ReadSubscription current = subscription; |
|
49765 | 759 |
Throwable error = errorRef.get(); |
760 |
if (current == null) { |
|
761 |
assert error != null; |
|
762 |
if (debug.on()) |
|
763 |
debug.log("error raised before subscriber subscribed: %s", |
|
764 |
(Object)error); |
|
765 |
return; |
|
766 |
} |
|
48083 | 767 |
TubeSubscriber subscriber = current.subscriber; |
768 |
if (error != null) { |
|
769 |
completed = true; |
|
770 |
// safe to pause here because we're finished anyway. |
|
771 |
pauseReadEvent(); |
|
49765 | 772 |
if (debug.on()) |
773 |
debug.log("Sending error " + error |
|
774 |
+ " to subscriber " + subscriber); |
|
48083 | 775 |
current.errorRef.compareAndSet(null, error); |
776 |
current.signalCompletion(); |
|
777 |
readScheduler.stop(); |
|
778 |
debugState("leaving read() loop with error: "); |
|
779 |
return; |
|
780 |
} |
|
781 |
||
782 |
// If we reach here then we must be in the selector thread. |
|
783 |
assert client.isSelectorThread(); |
|
784 |
if (demand.tryDecrement()) { |
|
785 |
// we have demand. |
|
786 |
try { |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
787 |
List<ByteBuffer> bytes = readAvailable(subscription.bufferSource); |
48083 | 788 |
if (bytes == EOF) { |
789 |
if (!completed) { |
|
49765 | 790 |
if (debug.on()) debug.log("got read EOF"); |
48083 | 791 |
completed = true; |
792 |
// safe to pause here because we're finished |
|
793 |
// anyway. |
|
794 |
pauseReadEvent(); |
|
795 |
current.signalCompletion(); |
|
796 |
readScheduler.stop(); |
|
797 |
} |
|
798 |
debugState("leaving read() loop after EOF: "); |
|
799 |
return; |
|
800 |
} else if (Utils.remaining(bytes) > 0) { |
|
801 |
// the subscriber is responsible for offloading |
|
802 |
// to another thread if needed. |
|
49765 | 803 |
if (debug.on()) |
804 |
debug.log("read bytes: " + Utils.remaining(bytes)); |
|
48083 | 805 |
assert !current.completed; |
806 |
subscriber.onNext(bytes); |
|
807 |
// we could continue looping until the demand |
|
808 |
// reaches 0. However, that would risk starving |
|
809 |
// other connections (bound to other socket |
|
810 |
// channels) - as other selected keys activated |
|
811 |
// by the selector manager thread might be |
|
812 |
// waiting for this event to terminate. |
|
813 |
// So resume the read event and return now... |
|
814 |
resumeReadEvent(); |
|
815 |
debugState("leaving read() loop after onNext: "); |
|
816 |
return; |
|
817 |
} else { |
|
818 |
// nothing available! |
|
49765 | 819 |
if (debug.on()) debug.log("no more bytes available"); |
48083 | 820 |
// re-increment the demand and resume the read |
821 |
// event. This ensures that this loop is |
|
822 |
// executed again when the socket becomes |
|
823 |
// readable again. |
|
824 |
demand.increase(1); |
|
825 |
resumeReadEvent(); |
|
826 |
debugState("leaving read() loop with no bytes"); |
|
827 |
return; |
|
828 |
} |
|
829 |
} catch (Throwable x) { |
|
830 |
signalError(x); |
|
831 |
continue; |
|
832 |
} |
|
833 |
} else { |
|
49765 | 834 |
if (debug.on()) debug.log("no more demand for reading"); |
48083 | 835 |
// the event is paused just after firing, so it should |
836 |
// still be paused here, unless the demand was just |
|
837 |
// incremented from 0 to n, in which case, the |
|
838 |
// event will be resumed, causing this loop to be |
|
839 |
// invoked again when the socket becomes readable: |
|
840 |
// This is what we want. |
|
841 |
// Trying to pause the event here would actually |
|
842 |
// introduce a race condition between this loop and |
|
843 |
// request(n). |
|
844 |
debugState("leaving read() loop with no demand"); |
|
845 |
break; |
|
846 |
} |
|
847 |
} |
|
848 |
} catch (Throwable t) { |
|
49765 | 849 |
if (debug.on()) debug.log("Unexpected exception in read loop", t); |
48083 | 850 |
signalError(t); |
851 |
} finally { |
|
852 |
handlePending(); |
|
853 |
} |
|
854 |
} |
|
855 |
||
856 |
boolean handlePending() { |
|
857 |
ReadSubscription pending = pendingSubscription.getAndSet(null); |
|
858 |
if (pending == null) return false; |
|
49765 | 859 |
if (debug.on()) |
860 |
debug.log("handling pending subscription for %s", |
|
861 |
pending.subscriber); |
|
48083 | 862 |
ReadSubscription current = subscription; |
863 |
if (current != null && current != pending && !completed) { |
|
864 |
current.subscriber.dropSubscription(); |
|
865 |
} |
|
49765 | 866 |
if (debug.on()) debug.log("read demand reset to 0"); |
48083 | 867 |
subscriptionImpl.demand.reset(); // subscriber will increase demand if it needs to. |
868 |
pending.errorRef.compareAndSet(null, errorRef.get()); |
|
869 |
if (!readScheduler.isStopped()) { |
|
870 |
subscription = pending; |
|
871 |
} else { |
|
49765 | 872 |
if (debug.on()) debug.log("socket tube is already stopped"); |
48083 | 873 |
} |
49765 | 874 |
if (debug.on()) debug.log("calling onSubscribe"); |
48083 | 875 |
pending.signalOnSubscribe(); |
876 |
if (completed) { |
|
877 |
pending.errorRef.compareAndSet(null, errorRef.get()); |
|
878 |
pending.signalCompletion(); |
|
879 |
} |
|
880 |
return true; |
|
881 |
} |
|
882 |
} |
|
883 |
||
884 |
||
885 |
// A repeatable ReadEvent which is paused after firing and can |
|
886 |
// be resumed if required - see SocketFlowEvent; |
|
887 |
final class ReadEvent extends SocketFlowEvent { |
|
888 |
final InternalReadSubscription sub; |
|
889 |
ReadEvent(SocketChannel channel, InternalReadSubscription sub) { |
|
890 |
super(SelectionKey.OP_READ, channel); |
|
891 |
this.sub = sub; |
|
892 |
} |
|
893 |
@Override |
|
894 |
protected final void signalEvent() { |
|
895 |
try { |
|
896 |
client.eventUpdated(this); |
|
897 |
sub.signalReadable(); |
|
898 |
} catch(Throwable t) { |
|
899 |
sub.signalError(t); |
|
900 |
} |
|
901 |
} |
|
902 |
||
903 |
@Override |
|
904 |
protected final void signalError(Throwable error) { |
|
905 |
sub.signalError(error); |
|
906 |
} |
|
907 |
||
908 |
@Override |
|
49765 | 909 |
Logger debug() { return debug; } |
48083 | 910 |
} |
911 |
} |
|
912 |
||
913 |
// ===================================================================== // |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
914 |
// Buffer Management // |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
915 |
// ===================================================================== // |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
916 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
917 |
// This interface is used by readAvailable(BufferSource); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
918 |
public interface BufferSource { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
919 |
/** |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
920 |
* Returns a buffer to read data from the socket. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
921 |
* Different implementation can have different strategies, as to |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
922 |
* which kind of buffer to return, or whether to return the same |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
923 |
* buffer. The only constraints are that |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
924 |
* a. the buffer returned must not be null |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
925 |
* b. the buffer position indicates where to start reading |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
926 |
* c. the buffer limit indicates where to stop reading. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
927 |
* d. the buffer is 'free' - that is - it is not used |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
928 |
* or retained by anybody else |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
929 |
* @return A buffer to read data from the socket. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
930 |
*/ |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
931 |
ByteBuffer getBuffer(); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
932 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
933 |
/** |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
934 |
* Append the data read into the buffer to the list of buffer to |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
935 |
* be sent downstream to the subscriber. May return a new |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
936 |
* list, or append to the given list. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
937 |
* |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
938 |
* Different implementation can have different strategies, but |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
939 |
* must obviously be consistent with the implementation of the |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
940 |
* getBuffer() method. For instance, an implementation could |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
941 |
* decide to add the buffer to the list and return a new buffer |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
942 |
* next time getBuffer() is called, or could decide to add a buffer |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
943 |
* slice to the list and return the same buffer (if remaining |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
944 |
* space is available) next time getBuffer() is called. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
945 |
* |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
946 |
* @param list The list before adding the data. Can be null. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
947 |
* @param buffer The buffer containing the data to add to the list. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
948 |
* @param start The start position at which data were read. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
949 |
* The current buffer position indicates the end. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
950 |
* @return A possibly new list where a buffer containing the |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
951 |
* data read from the socket has been added. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
952 |
*/ |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
953 |
List<ByteBuffer> append(List <ByteBuffer> list, ByteBuffer buffer, int start); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
954 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
955 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
956 |
// An implementation of BufferSource used for unencrypted data. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
957 |
// This buffer source uses heap buffers and avoids wasting memory |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
958 |
// by forwarding read only buffer slices downstream. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
959 |
// Buffers allocated through this source are simply GC'ed when |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
960 |
// they are no longer referenced. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
961 |
static final class SliceBufferSource implements BufferSource { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
962 |
private final Supplier<ByteBuffer> factory; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
963 |
private volatile ByteBuffer current; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
964 |
public SliceBufferSource() { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
965 |
this(Utils::getBuffer); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
966 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
967 |
public SliceBufferSource(Supplier<ByteBuffer> factory) { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
968 |
this.factory = Objects.requireNonNull(factory); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
969 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
970 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
971 |
// reuse the same buffer if some space remains available. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
972 |
// otherwise, returns a new heap buffer. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
973 |
@Override |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
974 |
public final ByteBuffer getBuffer() { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
975 |
ByteBuffer buf = current; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
976 |
buf = (buf == null || !buf.hasRemaining()) |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
977 |
? (current = factory.get()) : buf; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
978 |
assert buf.hasRemaining(); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
979 |
return buf; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
980 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
981 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
982 |
// Adds a read only slice to the list, potentially returning a |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
983 |
// new list with with that slice at the end. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
984 |
@Override |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
985 |
public final List<ByteBuffer> append(List <ByteBuffer> list, ByteBuffer buf, int start) { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
986 |
// creates a slice to add to the list |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
987 |
int limit = buf.limit(); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
988 |
buf.limit(buf.position()); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
989 |
buf.position(start); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
990 |
ByteBuffer slice = buf.slice(); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
991 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
992 |
// restore buffer state to what it was before creating the slice |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
993 |
buf.position(buf.limit()); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
994 |
buf.limit(limit); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
995 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
996 |
// add the buffer to the list |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
997 |
return SocketTube.listOf(list, slice.asReadOnlyBuffer()); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
998 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
999 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1000 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1001 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1002 |
// An implementation of BufferSource used for encrypted data. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1003 |
// This buffer source use direct byte buffers that will be |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1004 |
// recycled by the SocketTube subscriber. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1005 |
// |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1006 |
static final class SSLDirectBufferSource implements BufferSource { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1007 |
private final Supplier<ByteBuffer> factory; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1008 |
private final HttpClientImpl client; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1009 |
private volatile ByteBuffer current; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1010 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1011 |
public SSLDirectBufferSource(HttpClientImpl client) { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1012 |
this.client = Objects.requireNonNull(client); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1013 |
this.factory = Objects.requireNonNull(client.getSSLBufferSupplier()); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1014 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1015 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1016 |
// Obtain a 'free' byte buffer from the pool, or return |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1017 |
// the same buffer if nothing was read at the previous cycle. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1018 |
// The subscriber will be responsible for recycling this |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1019 |
// buffer into the pool (see SSLFlowDelegate.Reader) |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1020 |
@Override |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1021 |
public final ByteBuffer getBuffer() { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1022 |
assert client.isSelectorThread(); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1023 |
ByteBuffer buf = current; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1024 |
if (buf == null) { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1025 |
buf = current = factory.get(); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1026 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1027 |
assert buf.hasRemaining(); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1028 |
assert buf.position() == 0; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1029 |
return buf; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1030 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1031 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1032 |
// Adds the buffer to the list. The buffer will be later returned to the |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1033 |
// pool by the subscriber (see SSLFlowDelegate.Reader). |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1034 |
// The next buffer returned by getBuffer() will be obtained from the |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1035 |
// pool. It might be the same buffer or another one. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1036 |
// Because socket tube can read up to MAX_BUFFERS = 3 buffers, and because |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1037 |
// recycling will happen in the flow before onNext returns, then the |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1038 |
// pool can not grow larger than MAX_BUFFERS = 3 buffers, even though |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1039 |
// it's shared by all SSL connections opened on that client. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1040 |
@Override |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1041 |
public final List<ByteBuffer> append(List <ByteBuffer> list, ByteBuffer buf, int start) { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1042 |
assert client.isSelectorThread(); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1043 |
assert buf.isDirect(); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1044 |
assert start == 0; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1045 |
assert current == buf; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1046 |
current = null; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1047 |
buf.limit(buf.position()); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1048 |
buf.position(start); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1049 |
// add the buffer to the list |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1050 |
return SocketTube.listOf(list, buf); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1051 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1052 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1053 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1054 |
// ===================================================================== // |
48083 | 1055 |
// Socket Channel Read/Write // |
1056 |
// ===================================================================== // |
|
1057 |
static final int MAX_BUFFERS = 3; |
|
1058 |
static final List<ByteBuffer> EOF = List.of(); |
|
49765 | 1059 |
static final List<ByteBuffer> NOTHING = List.of(Utils.EMPTY_BYTEBUFFER); |
48083 | 1060 |
|
49765 | 1061 |
// readAvailable() will read bytes into the 'current' ByteBuffer until |
1062 |
// the ByteBuffer is full, or 0 or -1 (EOF) is returned by read(). |
|
1063 |
// When that happens, a slice of the data that has been read so far |
|
1064 |
// is inserted into the returned buffer list, and if the current buffer |
|
1065 |
// has remaining space, that space will be used to read more data when |
|
1066 |
// the channel becomes readable again. |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1067 |
private List<ByteBuffer> readAvailable(BufferSource buffersSource) throws IOException { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1068 |
ByteBuffer buf = buffersSource.getBuffer(); |
48083 | 1069 |
assert buf.hasRemaining(); |
1070 |
||
1071 |
int read; |
|
1072 |
int pos = buf.position(); |
|
1073 |
List<ByteBuffer> list = null; |
|
1074 |
while (buf.hasRemaining()) { |
|
49765 | 1075 |
try { |
1076 |
while ((read = channel.read(buf)) > 0) { |
|
1077 |
if (!buf.hasRemaining()) |
|
1078 |
break; |
|
1079 |
} |
|
1080 |
} catch (IOException x) { |
|
1081 |
if (buf.position() == pos && list == null) { |
|
1082 |
// no bytes have been read, just throw... |
|
1083 |
throw x; |
|
1084 |
} else { |
|
1085 |
// some bytes have been read, return them and fail next time |
|
1086 |
errorRef.compareAndSet(null, x); |
|
1087 |
read = 0; // ensures outer loop will exit |
|
1088 |
} |
|
48083 | 1089 |
} |
1090 |
||
1091 |
// nothing read; |
|
1092 |
if (buf.position() == pos) { |
|
49765 | 1093 |
// An empty list signals the end of data, and should only be |
1094 |
// returned if read == -1. If some data has already been read, |
|
1095 |
// then it must be returned. -1 will be returned next time |
|
1096 |
// the caller attempts to read something. |
|
1097 |
if (list == null) { |
|
1098 |
// nothing read - list was null - return EOF or NOTHING |
|
1099 |
list = read == -1 ? EOF : NOTHING; |
|
48083 | 1100 |
} |
49765 | 1101 |
break; |
48083 | 1102 |
} |
49765 | 1103 |
|
1104 |
// check whether this buffer has still some free space available. |
|
1105 |
// if so, we will keep it for the next round. |
|
1106 |
final boolean hasRemaining = buf.hasRemaining(); |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1107 |
list = buffersSource.append(list, buf, pos); |
49765 | 1108 |
|
1109 |
if (read <= 0 || list.size() == MAX_BUFFERS) { |
|
1110 |
break; |
|
48083 | 1111 |
} |
49765 | 1112 |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1113 |
buf = buffersSource.getBuffer(); |
48083 | 1114 |
pos = buf.position(); |
1115 |
assert buf.hasRemaining(); |
|
1116 |
} |
|
1117 |
return list; |
|
1118 |
} |
|
1119 |
||
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
1120 |
private static <T> List<T> listOf(List<T> list, T item) { |
49765 | 1121 |
int size = list == null ? 0 : list.size(); |
1122 |
switch (size) { |
|
1123 |
case 0: return List.of(item); |
|
1124 |
case 1: return List.of(list.get(0), item); |
|
1125 |
case 2: return List.of(list.get(0), list.get(1), item); |
|
1126 |
default: // slow path if MAX_BUFFERS > 3 |
|
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
1127 |
List<T> res = list instanceof ArrayList ? list : new ArrayList<>(list); |
49765 | 1128 |
res.add(item); |
1129 |
return res; |
|
1130 |
} |
|
1131 |
} |
|
1132 |
||
48083 | 1133 |
private long writeAvailable(List<ByteBuffer> bytes) throws IOException { |
1134 |
ByteBuffer[] srcs = bytes.toArray(Utils.EMPTY_BB_ARRAY); |
|
1135 |
final long remaining = Utils.remaining(srcs); |
|
1136 |
long written = 0; |
|
1137 |
while (remaining > written) { |
|
49765 | 1138 |
try { |
1139 |
long w = channel.write(srcs); |
|
1140 |
assert w >= 0 : "negative number of bytes written:" + w; |
|
1141 |
if (w == 0) { |
|
1142 |
break; |
|
1143 |
} |
|
1144 |
written += w; |
|
1145 |
} catch (IOException x) { |
|
1146 |
if (written == 0) { |
|
1147 |
// no bytes were written just throw |
|
1148 |
throw x; |
|
1149 |
} else { |
|
1150 |
// return how many bytes were written, will fail next time |
|
1151 |
break; |
|
1152 |
} |
|
1153 |
} |
|
48083 | 1154 |
} |
1155 |
return written; |
|
1156 |
} |
|
1157 |
||
1158 |
private void resumeEvent(SocketFlowEvent event, |
|
1159 |
Consumer<Throwable> errorSignaler) { |
|
1160 |
boolean registrationRequired; |
|
1161 |
synchronized(lock) { |
|
1162 |
registrationRequired = !event.registered(); |
|
1163 |
event.resume(); |
|
1164 |
} |
|
1165 |
try { |
|
1166 |
if (registrationRequired) { |
|
1167 |
client.registerEvent(event); |
|
1168 |
} else { |
|
1169 |
client.eventUpdated(event); |
|
1170 |
} |
|
1171 |
} catch(Throwable t) { |
|
1172 |
errorSignaler.accept(t); |
|
1173 |
} |
|
1174 |
} |
|
1175 |
||
1176 |
private void pauseEvent(SocketFlowEvent event, |
|
1177 |
Consumer<Throwable> errorSignaler) { |
|
1178 |
synchronized(lock) { |
|
1179 |
event.pause(); |
|
1180 |
} |
|
1181 |
try { |
|
1182 |
client.eventUpdated(event); |
|
1183 |
} catch(Throwable t) { |
|
1184 |
errorSignaler.accept(t); |
|
1185 |
} |
|
1186 |
} |
|
1187 |
||
1188 |
@Override |
|
1189 |
public void connectFlows(TubePublisher writePublisher, |
|
1190 |
TubeSubscriber readSubscriber) { |
|
49765 | 1191 |
if (debug.on()) debug.log("connecting flows"); |
48083 | 1192 |
this.subscribe(readSubscriber); |
1193 |
writePublisher.subscribe(this); |
|
1194 |
} |
|
1195 |
||
1196 |
||
1197 |
@Override |
|
1198 |
public String toString() { |
|
1199 |
return dbgString(); |
|
1200 |
} |
|
1201 |
||
1202 |
final String dbgString() { |
|
1203 |
return "SocketTube("+id+")"; |
|
1204 |
} |
|
1205 |
} |