@deepseek-ai/libreoffice-kit 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -10
- package/README.zh.md +18 -10
- package/frame-forwarding/README.md +4 -2
- package/frame-forwarding/README.zh.md +4 -2
- package/frame-forwarding/include/dsh_frame_forwarding.h +1 -1
- package/frame-forwarding/prebuilds/darwin-arm64/frame-forwarding.node +0 -0
- package/frame-forwarding/prebuilds/darwin-arm64/libdsh_frame_forwarding_sdk.a +0 -0
- package/frame-forwarding/prebuilds/darwin-arm64/manifest.json +11 -11
- package/frame-forwarding/prebuilds/darwin-x64/frame-forwarding.node +0 -0
- package/frame-forwarding/prebuilds/darwin-x64/libdsh_frame_forwarding_sdk.a +0 -0
- package/frame-forwarding/prebuilds/darwin-x64/manifest.json +11 -11
- package/frame-forwarding/prebuilds/win32-arm64/dsh_frame_forwarding_sdk.lib +0 -0
- package/frame-forwarding/prebuilds/win32-arm64/frame-forwarding.node +0 -0
- package/frame-forwarding/prebuilds/win32-arm64/manifest.json +11 -11
- package/frame-forwarding/prebuilds/win32-x64/dsh_frame_forwarding_sdk.lib +0 -0
- package/frame-forwarding/prebuilds/win32-x64/frame-forwarding.node +0 -0
- package/frame-forwarding/prebuilds/win32-x64/manifest.json +11 -11
- package/frame-forwarding/receipt.json +7 -7
- package/frame-forwarding/scripts/metadata.mjs +2 -2
- package/frame-forwarding/sdk.json +2 -2
- package/frame-forwarding/src/receiver.cc +33 -8
- package/frame-forwarding/src/transport.cc +259 -73
- package/frame-forwarding/src/transport.h +15 -0
- package/frame-forwarding/verify.mjs +1 -1
- package/lib/cli.js +69 -58
- package/lib/editor.js +66 -23
- package/lib/index.js +69 -58
- package/lib/types/engine.d.ts +3 -4
- package/lib/types/native.d.ts +8 -19
- package/lib/types/runtime.d.ts +4 -1
- package/package.json +7 -6
|
@@ -57,6 +57,13 @@ struct Event {
|
|
|
57
57
|
std::weak_ptr<Connection> source;
|
|
58
58
|
bool delivered = false, source_keyed_mutex = false;
|
|
59
59
|
std::chrono::steady_clock::time_point deadline;
|
|
60
|
+
// A queued event can outlive both its connection and receiver, including during environment teardown.
|
|
61
|
+
std::shared_ptr<std::atomic<unsigned>> pending;
|
|
62
|
+
bool reserve(const std::shared_ptr<std::atomic<unsigned>>& count, unsigned limit) {
|
|
63
|
+
// dispatch_mutex serializes reservations; only destruction can change the count concurrently, downwards.
|
|
64
|
+
if (count->load() >= limit) return false;
|
|
65
|
+
++*count; pending = count; return true;
|
|
66
|
+
}
|
|
60
67
|
~Event();
|
|
61
68
|
};
|
|
62
69
|
struct GrantWake {
|
|
@@ -199,6 +206,7 @@ struct FrameLedger {
|
|
|
199
206
|
Event::~Event() {
|
|
200
207
|
// A frame discarded before its callback has no JavaScript owner to return its credit.
|
|
201
208
|
if (ledger && !delivered) ledger->release(delivery);
|
|
209
|
+
if (pending) --*pending;
|
|
202
210
|
}
|
|
203
211
|
struct GpuJob {
|
|
204
212
|
std::unique_ptr<Event> event;
|
|
@@ -237,6 +245,7 @@ struct Connection : std::enable_shared_from_this<Connection> {
|
|
|
237
245
|
bool wait_failed = false;
|
|
238
246
|
#endif
|
|
239
247
|
std::atomic<bool> stopped{false};
|
|
248
|
+
std::shared_ptr<std::atomic<unsigned>> pending_events = std::make_shared<std::atomic<unsigned>>(0);
|
|
240
249
|
std::shared_ptr<FrameLedger> ledger = std::make_shared<FrameLedger>();
|
|
241
250
|
std::thread thread, writer, copier;
|
|
242
251
|
std::mutex jobs_mutex;
|
|
@@ -806,18 +815,30 @@ struct Receiver {
|
|
|
806
815
|
napi_threadsafe_function callback = nullptr;
|
|
807
816
|
std::thread worker;
|
|
808
817
|
bool closing = false;
|
|
809
|
-
|
|
818
|
+
std::shared_ptr<std::atomic<unsigned>> pending_rejection = std::make_shared<std::atomic<unsigned>>(0);
|
|
819
|
+
// Messages and JS frames share 256 slots per connection; refusals share one slot per receiver. The three
|
|
820
|
+
// connection transitions and the GPU worker's drain-blocked warning each occur at most once per connection;
|
|
821
|
+
// the listener fails once. These lifecycle notices share the FIFO, not the quota. A tx holder making successive
|
|
822
|
+
// short connections can accumulate lifecycle notices while JS stalls: 256 is not a receiver-wide bound.
|
|
823
|
+
bool emit(Event* raw, Connection* producer = nullptr) {
|
|
824
|
+
std::unique_ptr<Event> event(raw);
|
|
810
825
|
{
|
|
811
|
-
std::
|
|
812
|
-
if (dispatcher_closed || worker_released)
|
|
813
|
-
|
|
814
|
-
|
|
826
|
+
std::unique_lock<std::mutex> lock(dispatch_mutex);
|
|
827
|
+
if (dispatcher_closed || worker_released) return false;
|
|
828
|
+
if (producer && !event->reserve(producer->pending_events, 256)) {
|
|
829
|
+
lock.unlock(); // Stopping interrupts I/O; it must not hold the dispatcher lock.
|
|
830
|
+
producer->fail_connection("Frame-forwarding producer exceeded the pending event limit (256)");
|
|
831
|
+
return false;
|
|
832
|
+
}
|
|
833
|
+
if (event->type == "rejected" && !event->reserve(pending_rejection, 1)) return true;
|
|
834
|
+
const auto result = napi_call_threadsafe_function(callback, event.get(), napi_tsfn_nonblocking);
|
|
835
|
+
if (result == napi_ok) { event.release(); return true; }
|
|
815
836
|
// napi_closing relinquishes the native thread's TSFN reference.
|
|
816
837
|
if (result != napi_closing) napi_release_threadsafe_function(callback, napi_tsfn_release);
|
|
817
838
|
worker_released = true; dispatcher_closed = true;
|
|
818
839
|
}
|
|
819
840
|
// Callers may hold state_mutex; the listener performs the stop this requests.
|
|
820
|
-
|
|
841
|
+
stopping = true; drain->notify(); return false;
|
|
821
842
|
}
|
|
822
843
|
void close_dispatcher() {
|
|
823
844
|
std::lock_guard<std::mutex> lock(dispatch_mutex);
|
|
@@ -982,7 +1003,11 @@ Connection::Connection(Receiver& receiver, uint64_t connection, uint32_t produce
|
|
|
982
1003
|
}
|
|
983
1004
|
#endif
|
|
984
1005
|
}
|
|
985
|
-
bool Connection::emit(Event* event) {
|
|
1006
|
+
bool Connection::emit(Event* event) {
|
|
1007
|
+
event->connection = id;
|
|
1008
|
+
const bool producer_event = event->type == "message" || event->type == "frame";
|
|
1009
|
+
return owner.emit(event, producer_event ? this : nullptr);
|
|
1010
|
+
}
|
|
986
1011
|
void Connection::report_blocked(const std::string& text) {
|
|
987
1012
|
auto event = new Event; event->type = "drain-blocked"; event->text = text; emit(event);
|
|
988
1013
|
}
|
|
@@ -1158,7 +1183,7 @@ napi_value create(napi_env env, napi_callback_info info) {
|
|
|
1158
1183
|
// The TX credential: producers present its secret; it names the receiver process, never a producer.
|
|
1159
1184
|
auto tx = std::string("1|") + std::to_string(value->endpoint.pid) + "|" + value->endpoint.token + "|" + value->endpoint.name;
|
|
1160
1185
|
auto name = string(env, "DSH frame-forwarding receiver");
|
|
1161
|
-
if (napi_create_threadsafe_function(env, args[2], nullptr, name,
|
|
1186
|
+
if (napi_create_threadsafe_function(env, args[2], nullptr, name, 0, 2, nullptr, nullptr, nullptr, dispatch, &value->callback) != napi_ok)
|
|
1162
1187
|
return fail(env, "Cannot create frame-forwarding event dispatcher");
|
|
1163
1188
|
done_holder = new std::shared_ptr<Receiver>(value);
|
|
1164
1189
|
auto done_name = string(env, "DSH frame-forwarding receiver completion");
|
|
@@ -27,12 +27,17 @@ Endpoint parse_endpoint(const std::string& value) {
|
|
|
27
27
|
auto a = value.find('|'), b = value.find('|', a + 1), c = value.find('|', b + 1);
|
|
28
28
|
if (a == std::string::npos || b == std::string::npos || c == std::string::npos || value.substr(0, a) != "1")
|
|
29
29
|
throw std::runtime_error("Unsupported frame-forwarding endpoint");
|
|
30
|
-
auto pid_text = value.substr(a + 1, b - a - 1);
|
|
30
|
+
const auto pid_text = value.substr(a + 1, b - a - 1);
|
|
31
|
+
const char* invalid = "Invalid frame-forwarding endpoint";
|
|
32
|
+
if (pid_text.empty() || !std::all_of(pid_text.begin(), pid_text.end(), [](char c) { return c >= '0' && c <= '9'; }))
|
|
33
|
+
throw std::runtime_error(invalid);
|
|
31
34
|
size_t used = 0;
|
|
32
|
-
|
|
35
|
+
unsigned long long pid;
|
|
36
|
+
try { pid = std::stoull(pid_text, &used); }
|
|
37
|
+
catch (const std::exception&) { throw std::runtime_error(invalid); }
|
|
38
|
+
if (used != pid_text.size() || !pid || pid > UINT32_MAX) throw std::runtime_error(invalid);
|
|
33
39
|
Endpoint result{static_cast<uint32_t>(pid), value.substr(b + 1, c - b - 1), value.substr(c + 1)};
|
|
34
|
-
if (
|
|
35
|
-
throw std::runtime_error("Invalid frame-forwarding endpoint");
|
|
40
|
+
if (result.token.size() != 64 || result.name.empty() || result.name.size() > 200) throw std::runtime_error(invalid);
|
|
36
41
|
return result;
|
|
37
42
|
}
|
|
38
43
|
uint64_t parse_connection(const char (&token)[65]) {
|
|
@@ -59,50 +64,153 @@ bool Channel::send(const Packet& packet, uintptr_t resource) {
|
|
|
59
64
|
std::memcpy(stamped.token, connection_text_, sizeof(stamped.token));
|
|
60
65
|
return send_packet(stamped, resource);
|
|
61
66
|
}
|
|
67
|
+
namespace {
|
|
68
|
+
/** Bytes of the marker, version, and kind that every protocol version's packet begins with. */
|
|
69
|
+
constexpr size_t wire_prefix = 3 * sizeof(uint32_t);
|
|
70
|
+
/** Write the header of `packet` and return its text size; the text follows the header on the wire. */
|
|
71
|
+
uint32_t encode_header(const Packet& packet, WireHeader& wire) {
|
|
72
|
+
wire = WireHeader{};
|
|
73
|
+
wire.marker = packet.marker; wire.version = packet.version; wire.kind = packet.kind;
|
|
74
|
+
wire.width = packet.width; wire.height = packet.height; wire.stride = packet.stride; wire.max_frames = packet.max_frames;
|
|
75
|
+
wire.frame_id = packet.frame_id; wire.allocation_id = packet.allocation_id; wire.max_bytes = packet.max_bytes; wire.resource = packet.resource;
|
|
76
|
+
std::memcpy(wire.token, packet.token, sizeof(wire.token));
|
|
77
|
+
// An unterminated text is sent whole, so the receiver sees it unterminated and refuses the packet.
|
|
78
|
+
const auto* nul = static_cast<const char*>(std::memchr(packet.text, 0, sizeof(packet.text)));
|
|
79
|
+
wire.text_size = nul ? static_cast<uint32_t>(nul - packet.text) : DSH_FRAME_FORWARDING_TEXT_CAPACITY;
|
|
80
|
+
return wire.text_size;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Decode one received message of `size` bytes, a header followed by its text, which Mach pads to 4 bytes.
|
|
84
|
+
* @returns False for a malformed message. A hello of another protocol version yields only its marker, version, and
|
|
85
|
+
* kind, so the receiver refuses it by version whatever its layout.
|
|
86
|
+
*/
|
|
87
|
+
bool decode_packet(const char* data, size_t size, bool padded, Packet& out) {
|
|
88
|
+
if (size < wire_prefix) return false;
|
|
89
|
+
uint32_t prefix[3];
|
|
90
|
+
std::memcpy(prefix, data, sizeof(prefix));
|
|
91
|
+
if (prefix[0] == magic && prefix[1] != DSH_FRAME_FORWARDING_PROTOCOL_VERSION && prefix[2] == hello) {
|
|
92
|
+
out.marker = prefix[0]; out.version = prefix[1]; out.kind = prefix[2];
|
|
93
|
+
out.width = out.height = out.stride = out.max_frames = 0;
|
|
94
|
+
out.frame_id = out.allocation_id = out.max_bytes = out.resource = 0;
|
|
95
|
+
std::memset(out.token, 0, sizeof(out.token));
|
|
96
|
+
out.text[0] = '\0';
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
if (size < sizeof(WireHeader)) return false;
|
|
100
|
+
WireHeader wire;
|
|
101
|
+
std::memcpy(&wire, data, sizeof(wire));
|
|
102
|
+
if (wire.text_size > DSH_FRAME_FORWARDING_TEXT_CAPACITY) return false;
|
|
103
|
+
const size_t text = padded ? (wire.text_size + 3u) & ~3u : wire.text_size;
|
|
104
|
+
if (size != sizeof(WireHeader) + text) return false;
|
|
105
|
+
out.marker = wire.marker; out.version = wire.version; out.kind = wire.kind;
|
|
106
|
+
out.width = wire.width; out.height = wire.height; out.stride = wire.stride; out.max_frames = wire.max_frames;
|
|
107
|
+
out.frame_id = wire.frame_id; out.allocation_id = wire.allocation_id; out.max_bytes = wire.max_bytes; out.resource = wire.resource;
|
|
108
|
+
std::memcpy(out.token, wire.token, sizeof(out.token));
|
|
109
|
+
std::memcpy(out.text, data + sizeof(WireHeader), wire.text_size);
|
|
110
|
+
if (wire.text_size < DSH_FRAME_FORWARDING_TEXT_CAPACITY) out.text[wire.text_size] = '\0';
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
} // namespace
|
|
62
114
|
#if defined(__APPLE__)
|
|
63
115
|
struct MachMessage {
|
|
64
116
|
mach_msg_header_t header;
|
|
65
117
|
mach_msg_body_t body;
|
|
66
118
|
mach_msg_port_descriptor_t port;
|
|
67
|
-
|
|
119
|
+
WireHeader wire;
|
|
120
|
+
/** WireHeader::text_size bytes, then zeros up to a multiple of 4; the kernel writes the trailer after them. */
|
|
121
|
+
char text[DSH_FRAME_FORWARDING_TEXT_CAPACITY];
|
|
68
122
|
mach_msg_audit_trailer_t trailer;
|
|
69
123
|
};
|
|
124
|
+
static_assert(offsetof(MachMessage, text) % 4 == 0, "Mach message sizes are multiples of 4 bytes");
|
|
125
|
+
static_assert(offsetof(MachMessage, trailer) >= sizeof(mach_no_senders_notification_t) - sizeof(mach_msg_format_0_trailer_t),
|
|
126
|
+
"no-senders notification fits before the audit trailer");
|
|
70
127
|
/**
|
|
71
|
-
* Receive one protocol message
|
|
72
|
-
*
|
|
128
|
+
* Receive one protocol message and decode it into `packet`: 1 data, 0 timeout, -1 failure, -2 a message of another
|
|
129
|
+
* layout, one larger than `msg`, or one carrying a port right the protocol never sends (destroyed), -3 a kernel
|
|
130
|
+
* no-senders notice on a watched connection port.
|
|
131
|
+
*
|
|
132
|
+
* Only a send right may arrive in the single port descriptor. Our descriptors carry the connection port and the
|
|
133
|
+
* IOSurface as send rights, and no message sends a receive or send-once right, or a reply right in its header; such a
|
|
134
|
+
* right would outlive its message, because the success path only deallocates a send right.
|
|
135
|
+
*
|
|
136
|
+
* When `watch_no_senders` is true, only a genuine kernel `MACH_NOTIFY_NO_SENDERS` notification (non-complex, no reply
|
|
137
|
+
* port, delivered via the notification's send-once right, and carrying the kernel audit token pid 0) returns -3. Every
|
|
138
|
+
* other unexpected or rejected message is destroyed with `mach_msg_destroy` before returning -2, so a forged
|
|
139
|
+
* notification carrying a reply right or descriptor cannot leave any right in the process.
|
|
73
140
|
*/
|
|
74
|
-
int receive_mach(mach_port_t port, uint32_t timeout, MachMessage& msg, uint32_t& pid) {
|
|
75
|
-
|
|
141
|
+
int receive_mach(mach_port_t port, bool watch_no_senders, uint32_t timeout, MachMessage& msg, uint32_t& pid, Packet& packet) {
|
|
142
|
+
std::memset(&msg.header, 0, sizeof(msg.header));
|
|
143
|
+
const auto result = mach_msg(&msg.header, MACH_RCV_MSG | MACH_RCV_TIMEOUT |
|
|
76
144
|
MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0) | MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT),
|
|
77
145
|
0, sizeof(msg), port, timeout, MACH_PORT_NULL);
|
|
78
146
|
if (result == MACH_RCV_TIMED_OUT) return 0;
|
|
147
|
+
// Without MACH_RCV_LARGE the kernel has already destroyed the oversized message; the port keeps working.
|
|
148
|
+
if (result == MACH_RCV_TOO_LARGE) return -2;
|
|
79
149
|
if (result != KERN_SUCCESS) return -1;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
150
|
+
const size_t size = msg.header.msgh_size;
|
|
151
|
+
const size_t trailer_offset = (size + 3u) & ~size_t{3};
|
|
152
|
+
if (size < sizeof(mach_msg_header_t) || trailer_offset > offsetof(MachMessage, trailer)) {
|
|
153
|
+
mach_msg_destroy(&msg.header); return -2;
|
|
154
|
+
}
|
|
155
|
+
const auto* trailer = reinterpret_cast<const mach_msg_audit_trailer_t*>(
|
|
156
|
+
reinterpret_cast<const char*>(&msg) + trailer_offset);
|
|
157
|
+
if (trailer->msgh_trailer_size < sizeof(mach_msg_audit_trailer_t)) {
|
|
83
158
|
mach_msg_destroy(&msg.header); return -2;
|
|
84
159
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
160
|
+
const pid_t sender_pid = audit_token_to_pid(trailer->msgh_audit);
|
|
161
|
+
// Neither protocol packets nor kernel notifications may carry a reply right in the header.
|
|
162
|
+
if (msg.header.msgh_remote_port != MACH_PORT_NULL || MACH_MSGH_BITS_REMOTE(msg.header.msgh_bits) != 0) {
|
|
163
|
+
mach_msg_destroy(&msg.header); return -2;
|
|
164
|
+
}
|
|
165
|
+
if (watch_no_senders && msg.header.msgh_id == MACH_NOTIFY_NO_SENDERS) {
|
|
166
|
+
// Only the kernel holds the notification send-once right and audit pid 0; a peer with a send right cannot forge it.
|
|
167
|
+
const bool valid_notice = !(msg.header.msgh_bits & MACH_MSGH_BITS_COMPLEX)
|
|
168
|
+
&& MACH_MSGH_BITS_LOCAL(msg.header.msgh_bits) == MACH_MSG_TYPE_PORT_SEND_ONCE
|
|
169
|
+
&& sender_pid == 0
|
|
170
|
+
&& size >= sizeof(mach_no_senders_notification_t) - sizeof(mach_msg_format_0_trailer_t);
|
|
171
|
+
mach_msg_destroy(&msg.header);
|
|
172
|
+
return valid_notice ? -3 : -2;
|
|
173
|
+
}
|
|
174
|
+
if (sender_pid <= 0 || size < offsetof(MachMessage, wire) || size > offsetof(MachMessage, trailer)
|
|
175
|
+
|| !(msg.header.msgh_bits & MACH_MSGH_BITS_COMPLEX)
|
|
176
|
+
|| msg.body.msgh_descriptor_count != 1 || msg.port.type != MACH_MSG_PORT_DESCRIPTOR
|
|
177
|
+
|| (msg.port.name && msg.port.disposition != MACH_MSG_TYPE_PORT_SEND)
|
|
178
|
+
|| !decode_packet(reinterpret_cast<const char*>(&msg.wire), size - offsetof(MachMessage, wire), true, packet)) {
|
|
179
|
+
mach_msg_destroy(&msg.header); return -2;
|
|
180
|
+
}
|
|
181
|
+
pid = static_cast<uint32_t>(sender_pid);
|
|
88
182
|
return 1;
|
|
89
183
|
}
|
|
90
|
-
mach_port_t
|
|
184
|
+
mach_port_t allocate_receive_port() {
|
|
91
185
|
mach_port_t port = MACH_PORT_NULL;
|
|
92
|
-
if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port) != KERN_SUCCESS)
|
|
186
|
+
if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port) != KERN_SUCCESS)
|
|
187
|
+
throw std::runtime_error("Cannot allocate frame-forwarding Mach port");
|
|
188
|
+
return port;
|
|
189
|
+
}
|
|
190
|
+
mach_port_t allocate_listener_port() {
|
|
191
|
+
mach_port_t port = allocate_receive_port();
|
|
93
192
|
if (mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS) {
|
|
94
|
-
|
|
193
|
+
mach_port_destroy(mach_task_self(), port);
|
|
194
|
+
throw std::runtime_error("Cannot allocate frame-forwarding Mach port");
|
|
95
195
|
}
|
|
96
196
|
return port;
|
|
97
197
|
}
|
|
98
198
|
void destroy_port(mach_port_t port) {
|
|
99
199
|
if (!port) return;
|
|
100
|
-
|
|
101
|
-
|
|
200
|
+
mach_port_destroy(mach_task_self(), port);
|
|
201
|
+
}
|
|
202
|
+
/** Drop any send rights a timed-out `MAKE_SEND` hand-off left on our own receive port. */
|
|
203
|
+
void drop_local_sends(mach_port_t port) {
|
|
204
|
+
mach_port_urefs_t refs = 0;
|
|
205
|
+
if (mach_port_get_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, &refs) == KERN_SUCCESS && refs > 0)
|
|
206
|
+
mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, -static_cast<mach_port_delta_t>(refs));
|
|
102
207
|
}
|
|
103
208
|
/**
|
|
104
|
-
* One producer connection. The producer's hello carries its
|
|
105
|
-
*
|
|
209
|
+
* One producer connection. The producer's hello carries the single send right to its receive port; the receiver
|
|
210
|
+
* answers on it with ready, carrying the single send right to its connection port. Each side watches its own receive
|
|
211
|
+
* port with `MACH_NOTIFY_NO_SENDERS`, so peer exit or disconnect wakes the reader without creating dead-name
|
|
212
|
+
* notification references on `remote_`; a receive that times out also reads `remote_`'s type, which covers a candidate
|
|
213
|
+
* that armed no notice and a peer that vanished before its notice was requested.
|
|
106
214
|
*/
|
|
107
215
|
class MachChannel final : public Channel {
|
|
108
216
|
mach_port_t local_ = MACH_PORT_NULL, remote_ = MACH_PORT_NULL;
|
|
@@ -111,35 +219,33 @@ class MachChannel final : public Channel {
|
|
|
111
219
|
uint32_t source_pid_;
|
|
112
220
|
std::atomic<bool> stopped_{false};
|
|
113
221
|
std::atomic<uint32_t> peer_{0};
|
|
222
|
+
/** True once `local_`'s single send right reached the peer and `MACH_NOTIFY_NO_SENDERS` is armed on `local_`. */
|
|
223
|
+
std::atomic<bool> senders_watched_{false};
|
|
114
224
|
std::mutex writes_;
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
auto result = mach_port_request_notification(mach_task_self(), remote_, MACH_NOTIFY_DEAD_NAME, 0, local_, MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous);
|
|
118
|
-
if (previous) mach_port_deallocate(mach_task_self(), previous);
|
|
119
|
-
if (result != KERN_SUCCESS) stopped_ = true;
|
|
120
|
-
}
|
|
121
|
-
/** Cancel the dead-name request so releasing the right posts no port-deleted notice. */
|
|
122
|
-
void unwatch_peer() {
|
|
225
|
+
/** Request `MACH_NOTIFY_NO_SENDERS` once the peer owns the only send right to `local_` (`mscount` >= 1). */
|
|
226
|
+
void watch_no_senders() {
|
|
123
227
|
mach_port_t previous = MACH_PORT_NULL;
|
|
124
|
-
|
|
228
|
+
const auto result = mach_port_request_notification(
|
|
229
|
+
mach_task_self(), local_, MACH_NOTIFY_NO_SENDERS, 1, local_, MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous);
|
|
125
230
|
if (previous) mach_port_deallocate(mach_task_self(), previous);
|
|
231
|
+
if (result == KERN_SUCCESS) senders_watched_.store(true, std::memory_order_release);
|
|
232
|
+
else stopped_ = true;
|
|
126
233
|
}
|
|
127
234
|
public:
|
|
128
235
|
/** Producer side: discover the listener; ready later replaces it with the connection port. */
|
|
129
236
|
explicit MachChannel(const Endpoint& endpoint) : server_(false), source_pid_(endpoint.pid) {
|
|
130
|
-
local_ =
|
|
237
|
+
local_ = allocate_receive_port();
|
|
131
238
|
if (bootstrap_look_up(bootstrap_port, const_cast<char*>(endpoint.name.c_str()), &remote_) != KERN_SUCCESS) {
|
|
132
239
|
destroy_port(local_); throw std::runtime_error("Cannot discover frame-forwarding Mach endpoint");
|
|
133
240
|
}
|
|
134
|
-
watch_peer();
|
|
135
241
|
}
|
|
136
242
|
/** Receiver side: adopt the candidate's reply port and open a connection port for it. */
|
|
137
243
|
MachChannel(mach_port_t reply, uint32_t pid) : remote_(reply), server_(true), source_pid_(pid) {
|
|
138
|
-
try { local_ =
|
|
139
|
-
peer_ = pid;
|
|
244
|
+
try { local_ = allocate_receive_port(); } catch (...) { mach_port_deallocate(mach_task_self(), reply); throw; }
|
|
245
|
+
peer_ = pid;
|
|
140
246
|
}
|
|
141
247
|
~MachChannel() override {
|
|
142
|
-
if (remote_)
|
|
248
|
+
if (remote_) mach_port_deallocate(mach_task_self(), remote_);
|
|
143
249
|
destroy_port(local_);
|
|
144
250
|
}
|
|
145
251
|
uint32_t peer_pid() const override { return peer_; }
|
|
@@ -148,33 +254,61 @@ class MachChannel final : public Channel {
|
|
|
148
254
|
bool send_packet(const Packet& packet, uintptr_t resource) override {
|
|
149
255
|
std::lock_guard<std::mutex> lock(writes_);
|
|
150
256
|
if (stopped_ || !remote_) return false;
|
|
151
|
-
MachMessage msg
|
|
257
|
+
MachMessage msg;
|
|
258
|
+
std::memset(&msg.header, 0, sizeof(msg.header));
|
|
259
|
+
std::memset(&msg.body, 0, sizeof(msg.body));
|
|
260
|
+
std::memset(&msg.port, 0, sizeof(msg.port));
|
|
152
261
|
msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
|
|
153
|
-
msg.header.msgh_size = offsetof(MachMessage, trailer);
|
|
154
262
|
msg.header.msgh_remote_port = remote_;
|
|
155
263
|
msg.header.msgh_id = magic;
|
|
156
264
|
msg.body.msgh_descriptor_count = 1;
|
|
157
265
|
msg.port.type = MACH_MSG_PORT_DESCRIPTOR;
|
|
158
266
|
msg.port.disposition = MACH_MSG_TYPE_COPY_SEND;
|
|
159
267
|
mach_port_t surface_port = MACH_PORT_NULL;
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
268
|
+
const bool hands_local = (!server_ && packet.kind == hello) || (server_ && packet.kind == ready);
|
|
269
|
+
if (hands_local) {
|
|
270
|
+
msg.port.name = local_;
|
|
271
|
+
msg.port.disposition = MACH_MSG_TYPE_MAKE_SEND;
|
|
272
|
+
} else if (resource) {
|
|
273
|
+
msg.port.name = surface_port = IOSurfaceCreateMachPort(reinterpret_cast<IOSurfaceRef>(resource));
|
|
274
|
+
}
|
|
275
|
+
const auto text_size = encode_header(packet, msg.wire);
|
|
276
|
+
const uint32_t aligned_text = (text_size + 3u) & ~3u;
|
|
277
|
+
std::memcpy(msg.text, packet.text, text_size);
|
|
278
|
+
std::memset(msg.text + text_size, 0, aligned_text - text_size);
|
|
279
|
+
msg.header.msgh_size = static_cast<mach_msg_size_t>(offsetof(MachMessage, text) + aligned_text);
|
|
280
|
+
const auto result = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, msg.header.msgh_size, 0, MACH_PORT_NULL, 1000, MACH_PORT_NULL);
|
|
281
|
+
if ((result & ~MACH_MSG_MASK) == MACH_SEND_TIMED_OUT) {
|
|
282
|
+
// A timed-out send returns the copied or created rights in `msg`, separately owned from our own ports.
|
|
283
|
+
mach_msg_destroy(&msg.header);
|
|
284
|
+
if (hands_local) drop_local_sends(local_);
|
|
285
|
+
}
|
|
164
286
|
if (surface_port) mach_port_deallocate(mach_task_self(), surface_port);
|
|
165
|
-
|
|
287
|
+
if (result == KERN_SUCCESS && hands_local) watch_no_senders();
|
|
288
|
+
return result == KERN_SUCCESS && !stopped_;
|
|
166
289
|
}
|
|
167
290
|
int receive_packet(Received& out, uint32_t timeout) override {
|
|
168
291
|
if (stopped_) return -1;
|
|
169
|
-
MachMessage msg
|
|
170
|
-
const auto result = receive_mach(local_, timeout, msg, pid);
|
|
171
|
-
if (result == 0
|
|
292
|
+
MachMessage msg; uint32_t pid = 0;
|
|
293
|
+
const auto result = receive_mach(local_, senders_watched_.load(std::memory_order_acquire), timeout, msg, pid, out.packet);
|
|
294
|
+
if (result == 0) {
|
|
295
|
+
// A peer that vanished between the hand-off and the no-senders request leaves a send count of zero, which the
|
|
296
|
+
// kernel answers with no notice ever; a timed-out receive is where its dead port then shows up. Queued packets
|
|
297
|
+
// are read first, so a goodbye that arrived before the peer exited still ends the connection without a reason.
|
|
298
|
+
mach_port_type_t type = 0;
|
|
299
|
+
if (remote_ && (mach_port_type(mach_task_self(), remote_, &type) != KERN_SUCCESS || (type & MACH_PORT_TYPE_DEAD_NAME))) {
|
|
300
|
+
stopped_ = true;
|
|
301
|
+
return -1;
|
|
302
|
+
}
|
|
303
|
+
return stopped_ ? -1 : 0;
|
|
304
|
+
}
|
|
305
|
+
if (result == -2) return stopped_ ? -1 : 0;
|
|
172
306
|
if (result < 0) return -1;
|
|
173
|
-
out.pid = pid;
|
|
307
|
+
out.pid = pid;
|
|
174
308
|
if (pid != source_pid_) { mach_msg_destroy(&msg.header); return -1; }
|
|
175
309
|
if (!server_ && out.packet.kind == ready && msg.port.name) {
|
|
176
310
|
// The connection port replaces the listener for every later packet.
|
|
177
|
-
|
|
311
|
+
mach_port_deallocate(mach_task_self(), remote_); remote_ = msg.port.name; msg.port.name = MACH_PORT_NULL;
|
|
178
312
|
} else if (out.packet.kind == frame && msg.port.name) {
|
|
179
313
|
out.resource = reinterpret_cast<uintptr_t>(IOSurfaceLookupFromMachPort(msg.port.name));
|
|
180
314
|
}
|
|
@@ -188,7 +322,7 @@ class MachListener final : public Listener {
|
|
|
188
322
|
std::atomic<bool> stopped_{false};
|
|
189
323
|
public:
|
|
190
324
|
explicit MachListener(const Endpoint& endpoint) : name_(endpoint.name) {
|
|
191
|
-
port_ =
|
|
325
|
+
port_ = allocate_listener_port();
|
|
192
326
|
if (bootstrap_register(bootstrap_port, const_cast<char*>(name_.c_str()), port_) != KERN_SUCCESS) {
|
|
193
327
|
destroy_port(port_); throw std::runtime_error("Cannot register frame-forwarding Mach endpoint");
|
|
194
328
|
}
|
|
@@ -200,13 +334,14 @@ class MachListener final : public Listener {
|
|
|
200
334
|
void interrupt() override { stopped_ = true; }
|
|
201
335
|
int accept(Candidate& candidate, uint32_t timeout) override {
|
|
202
336
|
if (stopped_) return -1;
|
|
203
|
-
MachMessage msg
|
|
204
|
-
|
|
337
|
+
MachMessage msg; uint32_t pid = 0;
|
|
338
|
+
candidate.hello = Received{};
|
|
339
|
+
const auto result = receive_mach(port_, false, timeout, msg, pid, candidate.hello.packet);
|
|
205
340
|
if (result == 0 || result <= -2) return stopped_ ? -1 : 0;
|
|
206
341
|
if (result < 0) return -1;
|
|
207
|
-
if (
|
|
342
|
+
if (candidate.hello.packet.kind != hello || !msg.port.name || msg.port.disposition != MACH_MSG_TYPE_PORT_SEND) { mach_msg_destroy(&msg.header); return 0; }
|
|
208
343
|
const auto reply = msg.port.name;
|
|
209
|
-
candidate.hello
|
|
344
|
+
candidate.hello.pid = pid;
|
|
210
345
|
candidate.channel = std::make_unique<MachChannel>(reply, pid);
|
|
211
346
|
return 1;
|
|
212
347
|
}
|
|
@@ -336,41 +471,73 @@ int wait_pipe(HANDLE pipe, OVERLAPPED& operation, HANDLE stop, HANDLE deadline,
|
|
|
336
471
|
}
|
|
337
472
|
return GetOverlappedResult(pipe, &operation, &count, FALSE) ? 1 : -1;
|
|
338
473
|
}
|
|
474
|
+
/** One pipe message: a header, then its text; only sizeof(WireHeader) + header.text_size bytes are sent. */
|
|
475
|
+
struct WireMessage {
|
|
476
|
+
WireHeader header;
|
|
477
|
+
char text[DSH_FRAME_FORWARDING_TEXT_CAPACITY];
|
|
478
|
+
};
|
|
479
|
+
static_assert(sizeof(WireMessage) == sizeof(WireHeader) + DSH_FRAME_FORWARDING_TEXT_CAPACITY, "WireMessage layout");
|
|
339
480
|
/**
|
|
340
481
|
* One connected pipe instance: the producer's client handle, or one instance the listener accepted. A producer sends
|
|
341
482
|
* its own handle values, and the receiving instance duplicates each one from the producer's process.
|
|
342
483
|
*/
|
|
343
484
|
class PipeChannel final : public Channel {
|
|
344
485
|
HANDLE pipe_ = INVALID_HANDLE_VALUE, stop_ = nullptr, deadline_ = nullptr;
|
|
486
|
+
/** Manual-reset completion events of the single reader and of writes, which writes_ serializes. */
|
|
487
|
+
HANDLE read_event_ = nullptr, write_event_ = nullptr;
|
|
345
488
|
/** The producer process a receiving instance duplicates handles from; null on the producer side. */
|
|
346
489
|
HANDLE producer_ = nullptr;
|
|
347
490
|
std::atomic<uint32_t> peer_{0};
|
|
348
491
|
std::mutex writes_;
|
|
492
|
+
/** Message buffers of the single reader and of writes; each stays untouched until its overlapped I/O completes. */
|
|
493
|
+
WireMessage read_buffer_, write_buffer_;
|
|
349
494
|
void open_events() {
|
|
350
495
|
stop_ = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
|
351
496
|
deadline_ = create_deadline_timer();
|
|
352
|
-
|
|
497
|
+
read_event_ = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
|
498
|
+
write_event_ = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
|
499
|
+
if (!stop_ || !deadline_ || !read_event_ || !write_event_) throw std::runtime_error("Cannot create frame-forwarding I/O wait events");
|
|
353
500
|
}
|
|
354
501
|
void close_handles() {
|
|
355
502
|
if (pipe_ != INVALID_HANDLE_VALUE) CloseHandle(pipe_);
|
|
356
503
|
if (stop_) CloseHandle(stop_);
|
|
357
504
|
if (deadline_) CloseHandle(deadline_);
|
|
505
|
+
if (read_event_) CloseHandle(read_event_);
|
|
506
|
+
if (write_event_) CloseHandle(write_event_);
|
|
358
507
|
if (producer_) CloseHandle(producer_);
|
|
359
|
-
pipe_ = INVALID_HANDLE_VALUE; stop_ = nullptr; deadline_ = nullptr; producer_ = nullptr;
|
|
508
|
+
pipe_ = INVALID_HANDLE_VALUE; stop_ = nullptr; deadline_ = nullptr; read_event_ = nullptr; write_event_ = nullptr; producer_ = nullptr;
|
|
360
509
|
}
|
|
361
510
|
public:
|
|
362
511
|
/** Producer side: connect to the receiver's pipe and check its process identity. */
|
|
363
512
|
PipeChannel(const Endpoint& endpoint, const std::function<bool()>& cancelled) {
|
|
364
513
|
try {
|
|
365
514
|
open_events();
|
|
366
|
-
|
|
515
|
+
using Clock = std::chrono::steady_clock;
|
|
516
|
+
const auto deadline = Clock::now() + std::chrono::seconds(10);
|
|
517
|
+
Clock::time_point missing_until{};
|
|
518
|
+
for (;;) {
|
|
519
|
+
if (cancelled && cancelled()) throw std::runtime_error("Frame-forwarding connection cancelled or timed out");
|
|
520
|
+
const auto left = std::chrono::ceil<std::chrono::milliseconds>(deadline - Clock::now()).count();
|
|
521
|
+
if (!cancelled && left <= 0) break;
|
|
522
|
+
if (WaitNamedPipeA(endpoint.name.c_str(), cancelled ? 100 : static_cast<DWORD>(left))) {
|
|
523
|
+
pipe_ = CreateFileA(endpoint.name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
|
|
524
|
+
if (pipe_ != INVALID_HANDLE_VALUE) break;
|
|
525
|
+
}
|
|
367
526
|
const auto error = GetLastError();
|
|
527
|
+
// A successful wait does not reserve an instance: another client can open it first.
|
|
528
|
+
if (error == ERROR_PIPE_BUSY || error == ERROR_SEM_TIMEOUT) { missing_until = {}; continue; }
|
|
529
|
+
if (error != ERROR_FILE_NOT_FOUND) break;
|
|
530
|
+
// Between closing the last candidate and opening its replacement, both calls can see no pipe.
|
|
531
|
+
// Allow half a second of continuous absence, not a full connection timeout for a withdrawn endpoint.
|
|
532
|
+
const auto now = Clock::now();
|
|
533
|
+
if (missing_until == Clock::time_point{}) missing_until = now + std::chrono::milliseconds(500);
|
|
534
|
+
if (now >= missing_until) break;
|
|
535
|
+
Sleep(10); // WaitNamedPipe returns immediately when the name is absent.
|
|
536
|
+
}
|
|
537
|
+
if (pipe_ == INVALID_HANDLE_VALUE) {
|
|
368
538
|
if (cancelled && cancelled()) throw std::runtime_error("Frame-forwarding connection cancelled or timed out");
|
|
369
|
-
|
|
370
|
-
throw std::runtime_error("Frame-forwarding receiver unavailable");
|
|
539
|
+
throw std::runtime_error("Frame-forwarding receiver unavailable");
|
|
371
540
|
}
|
|
372
|
-
pipe_ = CreateFileA(endpoint.name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
|
|
373
|
-
if (pipe_ == INVALID_HANDLE_VALUE) throw std::runtime_error("Frame-forwarding receiver unavailable");
|
|
374
541
|
DWORD mode = PIPE_READMODE_MESSAGE; SetNamedPipeHandleState(pipe_, &mode, nullptr, nullptr);
|
|
375
542
|
ULONG pid = 0;
|
|
376
543
|
if (!GetNamedPipeServerProcessId(pipe_, &pid) || pid != endpoint.pid) throw std::runtime_error("Frame-forwarding receiver identity mismatch");
|
|
@@ -387,24 +554,27 @@ class PipeChannel final : public Channel {
|
|
|
387
554
|
void interrupt() override { if (stop_) SetEvent(stop_); if (pipe_ != INVALID_HANDLE_VALUE) CancelIoEx(pipe_, nullptr); }
|
|
388
555
|
protected:
|
|
389
556
|
/** The sender keeps a sent handle open until the receiver releases its frame, so the receiver can duplicate it. */
|
|
390
|
-
bool send_packet(const Packet&
|
|
557
|
+
bool send_packet(const Packet& packet, uintptr_t resource) override {
|
|
391
558
|
std::lock_guard<std::mutex> lock(writes_);
|
|
392
559
|
if (WaitForSingleObject(stop_, 0) == WAIT_OBJECT_0) return false;
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
560
|
+
const auto text_size = encode_header(packet, write_buffer_.header);
|
|
561
|
+
write_buffer_.header.resource = static_cast<uint64_t>(resource);
|
|
562
|
+
std::memcpy(write_buffer_.text, packet.text, text_size);
|
|
563
|
+
const DWORD size = static_cast<DWORD>(sizeof(WireHeader) + text_size);
|
|
564
|
+
ResetEvent(write_event_);
|
|
565
|
+
OVERLAPPED operation{}; operation.hEvent = write_event_; DWORD count = 0;
|
|
566
|
+
bool ok = WriteFile(pipe_, &write_buffer_, size, &count, &operation) != FALSE;
|
|
396
567
|
if (!ok && GetLastError() == ERROR_IO_PENDING) ok = wait_pipe(pipe_, operation, stop_, deadline_, INFINITE, count) == 1;
|
|
397
|
-
|
|
398
|
-
return ok && count == sizeof(packet);
|
|
568
|
+
return ok && count == size;
|
|
399
569
|
}
|
|
400
570
|
int receive_packet(Received& out, uint32_t timeout) override {
|
|
401
571
|
if (WaitForSingleObject(stop_, 0) == WAIT_OBJECT_0) return -1;
|
|
402
|
-
|
|
403
|
-
|
|
572
|
+
ResetEvent(read_event_);
|
|
573
|
+
OVERLAPPED operation{}; operation.hEvent = read_event_; DWORD count = 0;
|
|
574
|
+
int result = ReadFile(pipe_, &read_buffer_, sizeof(read_buffer_), &count, &operation) != FALSE ? 1 : -1;
|
|
404
575
|
if (result < 0 && GetLastError() == ERROR_IO_PENDING) result = wait_pipe(pipe_, operation, stop_, deadline_, timeout, count);
|
|
405
|
-
CloseHandle(operation.hEvent);
|
|
406
576
|
if (result == 0) return WaitForSingleObject(stop_, 0) == WAIT_OBJECT_0 ? -1 : 0;
|
|
407
|
-
if (result < 0 || count
|
|
577
|
+
if (result < 0 || !decode_packet(reinterpret_cast<const char*>(&read_buffer_), count, false, out.packet)) return -1;
|
|
408
578
|
out.pid = peer_; out.resource = 0;
|
|
409
579
|
// Only a receiving instance takes handles, and only from the producer's own handle table.
|
|
410
580
|
if (producer_ && out.packet.resource) {
|
|
@@ -445,19 +615,27 @@ class PipeListener final : public Listener {
|
|
|
445
615
|
OVERLAPPED connect_{};
|
|
446
616
|
bool first_ = true, connecting_ = false;
|
|
447
617
|
static constexpr DWORD instances = 4;
|
|
618
|
+
/** Longest hello wait between checks for a stop request. */
|
|
619
|
+
static constexpr long long handshake_slice_ms = 50;
|
|
448
620
|
void open_instance() {
|
|
449
621
|
PSECURITY_DESCRIPTOR descriptor = nullptr;
|
|
450
622
|
if (!ConvertStringSecurityDescriptorToSecurityDescriptorA(sddl_.c_str(), SDDL_REVISION_1, &descriptor, nullptr)) throw std::runtime_error("Cannot restrict frame-forwarding pipe");
|
|
451
623
|
SECURITY_ATTRIBUTES security{sizeof(security), descriptor, FALSE};
|
|
452
624
|
const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | (first_ ? FILE_FLAG_FIRST_PIPE_INSTANCE : 0);
|
|
453
625
|
instance_ = CreateNamedPipeA(name_.c_str(), open_mode, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
|
|
454
|
-
instances, sizeof(
|
|
626
|
+
instances, sizeof(WireMessage) * 4, sizeof(WireMessage) * 4, 0, &security);
|
|
455
627
|
LocalFree(descriptor);
|
|
456
628
|
if (instance_ == INVALID_HANDLE_VALUE) throw std::runtime_error("Cannot open private frame-forwarding pipe");
|
|
457
629
|
first_ = false;
|
|
458
630
|
}
|
|
459
631
|
void close_handles() {
|
|
460
|
-
if (instance_ != INVALID_HANDLE_VALUE) {
|
|
632
|
+
if (instance_ != INVALID_HANDLE_VALUE) {
|
|
633
|
+
CancelIoEx(instance_, nullptr);
|
|
634
|
+
// A pending connect writes connect_ when it completes, so the cancelled operation finishes before its release.
|
|
635
|
+
DWORD count = 0;
|
|
636
|
+
if (connecting_) { GetOverlappedResult(instance_, &connect_, &count, TRUE); connecting_ = false; }
|
|
637
|
+
CloseHandle(instance_); instance_ = INVALID_HANDLE_VALUE;
|
|
638
|
+
}
|
|
461
639
|
if (connect_.hEvent) { CloseHandle(connect_.hEvent); connect_.hEvent = nullptr; }
|
|
462
640
|
if (stop_) { CloseHandle(stop_); stop_ = nullptr; }
|
|
463
641
|
if (deadline_) { CloseHandle(deadline_); deadline_ = nullptr; }
|
|
@@ -502,7 +680,15 @@ class PipeListener final : public Listener {
|
|
|
502
680
|
if (!producer) { CloseHandle(pipe); return 0; }
|
|
503
681
|
auto channel = std::make_unique<PipeChannel>(pipe, static_cast<uint32_t>(pid), producer);
|
|
504
682
|
Received hello;
|
|
505
|
-
|
|
683
|
+
// interrupt() no longer reaches the instance this channel now owns, so the hello wait checks for a stop between slices.
|
|
684
|
+
const auto handshake_end = std::chrono::steady_clock::now() + std::chrono::milliseconds(handshake_ms_);
|
|
685
|
+
int received = 0;
|
|
686
|
+
do {
|
|
687
|
+
if (WaitForSingleObject(stop_, 0) == WAIT_OBJECT_0) return -1;
|
|
688
|
+
const auto left = std::chrono::duration_cast<std::chrono::milliseconds>(handshake_end - std::chrono::steady_clock::now()).count();
|
|
689
|
+
received = channel->receive(hello, static_cast<uint32_t>(std::clamp<long long>(left, 0, handshake_slice_ms)));
|
|
690
|
+
} while (received == 0 && std::chrono::steady_clock::now() < handshake_end);
|
|
691
|
+
if (received != 1 || hello.resource) { release_resource(hello.resource); return 0; }
|
|
506
692
|
candidate.hello = hello; candidate.channel = std::move(channel);
|
|
507
693
|
return 1;
|
|
508
694
|
}
|
|
@@ -48,6 +48,21 @@ struct Packet {
|
|
|
48
48
|
char token[65] = {};
|
|
49
49
|
char text[DSH_FRAME_FORWARDING_TEXT_CAPACITY] = {};
|
|
50
50
|
};
|
|
51
|
+
/**
|
|
52
|
+
* Wire form of a Packet: this header, then text_size bytes of text. text_size counts the text before its terminating
|
|
53
|
+
* NUL, or is DSH_FRAME_FORWARDING_TEXT_CAPACITY for a text without one, which is sent whole and stays unterminated so
|
|
54
|
+
* the receiver refuses it. Mach messages pad the text with zeros to a multiple of 4 bytes. Protocol 1 sent the whole
|
|
55
|
+
* Packet; only the marker, version, and kind of its hello are read, so that hello is refused as another version.
|
|
56
|
+
*/
|
|
57
|
+
struct WireHeader {
|
|
58
|
+
uint32_t marker = magic, version = DSH_FRAME_FORWARDING_PROTOCOL_VERSION, kind = 0;
|
|
59
|
+
uint32_t width = 0, height = 0, stride = 0, max_frames = 0;
|
|
60
|
+
uint32_t text_size = 0;
|
|
61
|
+
uint64_t frame_id = 0, allocation_id = 0, max_bytes = 0, resource = 0;
|
|
62
|
+
char token[65] = {};
|
|
63
|
+
char reserved[7] = {};
|
|
64
|
+
};
|
|
65
|
+
static_assert(sizeof(WireHeader) == 136, "WireHeader layout");
|
|
51
66
|
struct Received { Packet packet; uint32_t pid = 0; uintptr_t resource = 0; };
|
|
52
67
|
/**
|
|
53
68
|
* One physical producer connection. receive returns 1 data, 0 timeout, -1 closed/error. After bind_connection, every
|
|
@@ -60,7 +60,7 @@ export async function verifyFrameForwardingPackage(directory, target) {
|
|
|
60
60
|
const root = join(directory, 'prebuilds', name);
|
|
61
61
|
const manifest = JSON.parse(await readFile(join(root, 'manifest.json'), 'utf8'));
|
|
62
62
|
const sdk = JSON.parse(await readFile(join(directory, 'sdk.json'), 'utf8'));
|
|
63
|
-
if (typeof sdk.version !== 'string' || !sdk.version || sdk.protocolVersion !==
|
|
63
|
+
if (typeof sdk.version !== 'string' || !sdk.version || sdk.protocolVersion !== 2
|
|
64
64
|
|| !sameCapabilities(sdk.capabilities) || !sameCapabilities(manifest.capabilities)
|
|
65
65
|
|| JSON.stringify(sdk.platforms) !== JSON.stringify(targets)
|
|
66
66
|
|| JSON.stringify(sdk.buildSources) !== JSON.stringify(buildSources)
|