26 package jdk.incubator.http; |
26 package jdk.incubator.http; |
27 |
27 |
28 import jdk.incubator.http.internal.common.ByteBufferReference; |
28 import jdk.incubator.http.internal.common.ByteBufferReference; |
29 |
29 |
30 import java.io.IOException; |
30 import java.io.IOException; |
31 import java.nio.ByteBuffer; |
|
32 import java.util.function.Consumer; |
|
33 import java.util.function.Supplier; |
|
34 |
31 |
35 /** |
32 /** |
36 * Implemented by classes that offer an asynchronous interface. |
33 * Implemented by classes that offer an asynchronous interface. |
37 * |
34 * |
38 * PlainHttpConnection, AsyncSSLConnection AsyncSSLDelegate. |
35 * PlainHttpConnection, AsyncSSLConnection. |
39 * |
36 * |
40 * setAsyncCallbacks() is called to set the callback for reading |
37 * setAsyncCallbacks() is called to set the callback for reading |
41 * and error notification. Reads all happen on the selector thread, which |
38 * and error notification. Reads all happen on the selector thread, which |
42 * must not block. |
39 * must not block. |
43 * |
40 * |
49 * the selector thread |
46 * the selector thread |
50 */ |
47 */ |
51 interface AsyncConnection { |
48 interface AsyncConnection { |
52 |
49 |
53 /** |
50 /** |
54 * Enables asynchronous sending and receiving mode. The given async |
|
55 * receiver will receive all incoming data. asyncInput() will be called |
|
56 * to trigger reads. asyncOutput() will be called to drive writes. |
|
57 * |
|
58 * The errorReceiver callback must be called when any fatal exception |
|
59 * occurs. Connection is assumed to be closed afterwards. |
|
60 */ |
|
61 void setAsyncCallbacks(Consumer<ByteBufferReference> asyncReceiver, |
|
62 Consumer<Throwable> errorReceiver, |
|
63 Supplier<ByteBufferReference> readBufferSupplier); |
|
64 |
|
65 |
|
66 |
|
67 /** |
|
68 * Does whatever is required to start reading. Usually registers |
|
69 * an event with the selector thread. |
|
70 */ |
|
71 void startReading(); |
|
72 |
|
73 /** |
|
74 * Cancel asynchronous reading. Used to downgrade a HTTP/2 connection to HTTP/1 |
|
75 */ |
|
76 void stopAsyncReading(); |
|
77 |
|
78 /** |
|
79 * In async mode, this method puts buffers at the end of the send queue. |
51 * In async mode, this method puts buffers at the end of the send queue. |
80 * When in async mode, calling this method should later be followed by |
52 * When in async mode, calling this method should later be followed by |
81 * subsequent flushAsync invocation. |
53 * subsequent flushAsync invocation. |
82 * That allows multiple threads to put buffers into the queue while some other |
54 * That allows multiple threads to put buffers into the queue while some other |
83 * thread is writing. |
55 * thread is writing. |
84 */ |
56 */ |
85 void writeAsync(ByteBufferReference[] buffers) throws IOException; |
57 void writeAsync(ByteBufferReference[] buffers) throws IOException; |
86 |
|
87 /** |
|
88 * Re-enable asynchronous reads through the callback |
|
89 */ |
|
90 void enableCallback(); |
|
91 |
58 |
92 /** |
59 /** |
93 * In async mode, this method may put buffers at the beginning of send queue, |
60 * In async mode, this method may put buffers at the beginning of send queue, |
94 * breaking frames sequence and allowing to write these buffers before other |
61 * breaking frames sequence and allowing to write these buffers before other |
95 * buffers in the queue. |
62 * buffers in the queue. |