comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/kj/compat/readiness-io.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 69:33d812a61356
1 // Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #pragma once
23
24 #include <kj/async-io.h>
25
26 KJ_BEGIN_HEADER
27
28 namespace kj {
29
30 class ReadyInputStreamWrapper {
31 // Provides readiness-based Async I/O as a wrapper around KJ's standard completion-based API, for
32 // compatibility with libraries that use readiness-based abstractions (e.g. OpenSSL).
33 //
34 // Unfortunately this requires buffering, so is not very efficient.
35
36 public:
37 ReadyInputStreamWrapper(AsyncInputStream& input);
38 ~ReadyInputStreamWrapper() noexcept(false);
39 KJ_DISALLOW_COPY_AND_MOVE(ReadyInputStreamWrapper);
40
41 kj::Maybe<size_t> read(kj::ArrayPtr<byte> dst);
42 // Reads bytes into `dst`, returning the number of bytes read. Returns zero only at EOF. Returns
43 // nullptr if not ready.
44
45 kj::Promise<void> whenReady();
46 // Returns a promise that resolves when read() will return non-null.
47
48 bool isAtEnd() { return eof; }
49 // Returns true if read() would return zero.
50
51 private:
52 AsyncInputStream& input;
53 kj::ForkedPromise<void> pumpTask = nullptr;
54 bool isPumping = false;
55 bool eof = false;
56
57 kj::ArrayPtr<const byte> content = nullptr; // Points to currently-valid part of `buffer`.
58 byte buffer[8192];
59 };
60
61 class ReadyOutputStreamWrapper {
62 // Provides readiness-based Async I/O as a wrapper around KJ's standard completion-based API, for
63 // compatibility with libraries that use readiness-based abstractions (e.g. OpenSSL).
64 //
65 // Unfortunately this requires buffering, so is not very efficient.
66
67 public:
68 ReadyOutputStreamWrapper(AsyncOutputStream& output);
69 ~ReadyOutputStreamWrapper() noexcept(false);
70 KJ_DISALLOW_COPY_AND_MOVE(ReadyOutputStreamWrapper);
71
72 kj::Maybe<size_t> write(kj::ArrayPtr<const byte> src);
73 // Writes bytes from `src`, returning the number of bytes written. Never returns zero for
74 // a non-empty `src`. Returns nullptr if not ready.
75
76 kj::Promise<void> whenReady();
77 // Returns a promise that resolves when write() will return non-null.
78
79 class Cork;
80 // An object that, when destructed, will uncork its parent stream.
81
82 Cork cork();
83 // After calling, data won't be pumped until either the internal buffer fills up or the returned
84 // object is destructed. Use this if you know multiple small write() calls will be happening in
85 // the near future and want to flush them all at once.
86 // Once the returned object is destructed, behavior goes back to normal. The returned object
87 // must be destructed before the ReadyOutputStreamWrapper.
88 // TODO(perf): This is an ugly hack to avoid sending lots of tiny packets when using TLS, which
89 // has to work around OpenSSL's readiness-based I/O layer. We could certainly do better here.
90
91 private:
92 AsyncOutputStream& output;
93 ArrayPtr<const byte> segments[2];
94 kj::ForkedPromise<void> pumpTask = nullptr;
95 bool isPumping = false;
96 bool corked = false;
97
98 uint start = 0; // index of first byte
99 uint filled = 0; // number of bytes currently in buffer
100
101 byte buffer[8192];
102
103 void uncork();
104
105 kj::Promise<void> pump();
106 // Asynchronously push the buffer out to the underlying stream.
107 };
108
109 class ReadyOutputStreamWrapper::Cork {
110 // An object that, when destructed, will uncork its parent stream.
111 public:
112 ~Cork() {
113 KJ_IF_MAYBE(p, parent) {
114 p->uncork();
115 }
116 }
117 Cork(Cork&& other) : parent(kj::mv(other.parent)) {
118 other.parent = nullptr;
119 }
120 KJ_DISALLOW_COPY(Cork);
121
122 private:
123 Cork(ReadyOutputStreamWrapper& parent) : parent(parent) {}
124
125 kj::Maybe<ReadyOutputStreamWrapper&> parent;
126 friend class ReadyOutputStreamWrapper;
127 };
128
129 } // namespace kj
130
131 KJ_END_HEADER