@deepseek-ai/libreoffice-kit 0.2.0 → 0.2.3-rc.3
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/NOTICE +1 -1
- package/README.md +5 -5
- package/README.zh.md +5 -5
- package/frame-forwarding/CMakeLists.txt +3 -4
- package/frame-forwarding/README.md +21 -17
- package/frame-forwarding/README.zh.md +22 -18
- package/frame-forwarding/include/dsh_frame_forwarding.h +42 -9
- package/frame-forwarding/index.d.ts +58 -54
- package/frame-forwarding/index.mjs +14 -17
- 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 +23 -21
- 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 +23 -21
- 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 +23 -21
- 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 +23 -21
- package/frame-forwarding/receipt.json +16 -14
- package/frame-forwarding/scripts/build-webgpu.mjs +1 -0
- package/frame-forwarding/scripts/build.mjs +3 -3
- package/frame-forwarding/scripts/metadata.mjs +12 -8
- package/frame-forwarding/scripts/source-identity.mjs +3 -3
- package/frame-forwarding/sdk.json +7 -5
- package/frame-forwarding/{include/dsh_webgpu.h → src/graphite-interop-internal.hxx} +22 -22
- package/frame-forwarding/src/{webgpu.cc → graphite-interop.cc} +34 -24
- package/frame-forwarding/src/node-producer.cc +24 -154
- package/frame-forwarding/src/producer.cc +120 -30
- package/frame-forwarding/src/receiver.cc +422 -212
- package/frame-forwarding/src/relay-transport.inc +38 -10
- package/frame-forwarding/src/transport.cc +328 -155
- package/frame-forwarding/src/transport.h +73 -9
- package/frame-forwarding/verify.d.mts +1 -1
- package/frame-forwarding/verify.mjs +19 -17
- package/lib/cli.js +6 -6
- package/lib/editor.js +15 -10
- package/lib/index.js +6 -6
- package/lib/types/editor.d.ts +17 -4
- package/lib/types/engine.d.ts +1 -1
- package/lib/types/fonts.d.ts +1 -1
- package/lib/worker.js +3 -2
- package/package.json +8 -10
- package/frame-forwarding/include/dsh_frame_forwarding_engine.h +0 -66
- package/frame-forwarding/include/dsh_frame_forwarding_node.h +0 -27
|
@@ -1,10 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frame-forwarding receiver for a Node host process. One receiver is one logical channel: a stable listener and the
|
|
3
|
+
* TX credential producers present. It serves one authenticated producer connection at a time; each connection owns
|
|
4
|
+
* its frames, allocations, credits, send queue and GPU relay state. After a connection ends, its frames drain before
|
|
5
|
+
* the next producer holding the credential receives ready, and credit never crosses connections. Host-local texture
|
|
6
|
+
* handles reach JavaScript, which hands them on and releases each delivery. Node-API only.
|
|
7
|
+
*/
|
|
1
8
|
#include "transport.h"
|
|
2
9
|
#include <node_api.h>
|
|
3
|
-
#include <uv.h>
|
|
4
10
|
#include <condition_variable>
|
|
11
|
+
#include <cstdio>
|
|
5
12
|
#include <cstring>
|
|
6
13
|
#include <map>
|
|
7
14
|
#include <deque>
|
|
15
|
+
#include <algorithm>
|
|
8
16
|
#include <thread>
|
|
9
17
|
#include <cmath>
|
|
10
18
|
#if defined(__APPLE__)
|
|
@@ -14,8 +22,8 @@
|
|
|
14
22
|
#include <windows.h>
|
|
15
23
|
#include <bcrypt.h>
|
|
16
24
|
#endif
|
|
17
|
-
using namespace dsh_frame_forwarding;
|
|
18
25
|
void register_node_producer(napi_env env, napi_value exports);
|
|
26
|
+
using namespace dsh_frame_forwarding;
|
|
19
27
|
namespace {
|
|
20
28
|
enum class PresentationPath { iosurface, shared_texture, gpu_copy, cpu_relay };
|
|
21
29
|
PresentationPath resolve_presentation_path(const std::string& producer, const std::string& display, bool keyed_mutex = false) {
|
|
@@ -40,10 +48,13 @@ uint64_t source_byte_budget(PresentationPath path, uint32_t frames, uint64_t byt
|
|
|
40
48
|
return path == PresentationPath::cpu_relay ? bytes / (2 * frames + 2) * frames : bytes / allocation_bytes_factor(path);
|
|
41
49
|
}
|
|
42
50
|
struct FrameLedger;
|
|
51
|
+
struct Connection;
|
|
43
52
|
struct Event {
|
|
44
53
|
std::string type, text, adapter, presentation_adapter;
|
|
45
|
-
Packet packet; uintptr_t resource = 0; uint32_t pid = 0; uint64_t delivery = 0;
|
|
54
|
+
Packet packet; uintptr_t resource = 0; uint32_t pid = 0; uint64_t delivery = 0, connection = 0;
|
|
46
55
|
std::shared_ptr<FrameLedger> ledger;
|
|
56
|
+
/** The connection a frame came from; a frame that cannot be delivered ends it. */
|
|
57
|
+
std::weak_ptr<Connection> source;
|
|
47
58
|
bool delivered = false, source_keyed_mutex = false;
|
|
48
59
|
std::chrono::steady_clock::time_point deadline;
|
|
49
60
|
~Event();
|
|
@@ -115,6 +126,12 @@ template<class Texture> struct RelayStorage {
|
|
|
115
126
|
using RelayTarget = RelayStorage<RelayTexture>;
|
|
116
127
|
using RelayStaging = RelayStorage<RelayReadback>;
|
|
117
128
|
#endif
|
|
129
|
+
/** Wakes a disconnected connection waiting for its drain whenever one of its frames is released. */
|
|
130
|
+
struct DrainSignal {
|
|
131
|
+
std::mutex mutex;
|
|
132
|
+
std::condition_variable changed;
|
|
133
|
+
void notify() { std::lock_guard<std::mutex> lock(mutex); changed.notify_all(); }
|
|
134
|
+
};
|
|
118
135
|
struct HeldFrame {
|
|
119
136
|
uintptr_t resource; uint64_t bytes, allocation, source_id;
|
|
120
137
|
#if defined(_WIN32)
|
|
@@ -128,9 +145,12 @@ struct FrameLedger {
|
|
|
128
145
|
std::map<uint64_t, HeldFrame> frames;
|
|
129
146
|
std::map<uint64_t, std::shared_ptr<Reservation>> allocations;
|
|
130
147
|
std::shared_ptr<Budget> budget;
|
|
148
|
+
std::shared_ptr<DrainSignal> drain;
|
|
131
149
|
uint64_t bytes = 0;
|
|
132
150
|
std::atomic<bool> connected{false};
|
|
133
151
|
std::atomic<bool> accepting_frames{true};
|
|
152
|
+
/** No frame of this connection remains: every delivery was released and every undispatched frame discarded. */
|
|
153
|
+
bool empty() { std::lock_guard<std::mutex> lock(mutex); return frames.empty(); }
|
|
134
154
|
~FrameLedger() {
|
|
135
155
|
for (auto& frame : frames) if (frame.second.resource) release_resource(frame.second.resource);
|
|
136
156
|
frames.clear();
|
|
@@ -163,6 +183,7 @@ struct FrameLedger {
|
|
|
163
183
|
if (!released) return false;
|
|
164
184
|
if (resource) release_resource(resource);
|
|
165
185
|
if (!connected) retire_idle();
|
|
186
|
+
if (drain) drain->notify();
|
|
166
187
|
return true;
|
|
167
188
|
}
|
|
168
189
|
uint64_t release_source(uint64_t delivery) {
|
|
@@ -179,7 +200,6 @@ Event::~Event() {
|
|
|
179
200
|
// A frame discarded before its callback has no JavaScript owner to return its credit.
|
|
180
201
|
if (ledger && !delivered) ledger->release(delivery);
|
|
181
202
|
}
|
|
182
|
-
struct Dispatcher { std::atomic<bool> failed{false}; };
|
|
183
203
|
struct GpuJob {
|
|
184
204
|
std::unique_ptr<Event> event;
|
|
185
205
|
uint64_t allocation = 0;
|
|
@@ -191,21 +211,18 @@ struct GpuJob {
|
|
|
191
211
|
#endif
|
|
192
212
|
bool ready = false;
|
|
193
213
|
};
|
|
194
|
-
struct
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
std::unique_ptr<CloseWork> close_work;
|
|
214
|
+
struct Receiver;
|
|
215
|
+
/**
|
|
216
|
+
* One producer connection of a channel and every resource it owns. Its thread reads the producer, then drains: the
|
|
217
|
+
* connection stays current until each of its frames is released, so its credit and GPU state never reach a successor.
|
|
218
|
+
*/
|
|
219
|
+
struct Connection : std::enable_shared_from_this<Connection> {
|
|
220
|
+
Receiver& owner;
|
|
221
|
+
const uint64_t id;
|
|
222
|
+
const uint32_t pid;
|
|
204
223
|
std::unique_ptr<Channel> channel;
|
|
205
|
-
Endpoint endpoint;
|
|
206
224
|
uint32_t max_frames;
|
|
207
225
|
uint64_t max_bytes;
|
|
208
|
-
uint64_t next_delivery = 1;
|
|
209
226
|
PresentationPath presentation_path = PresentationPath::iosurface;
|
|
210
227
|
uint32_t readback_timeout = 0, allocation_factor = allocation_bytes_factor(presentation_path);
|
|
211
228
|
std::string display_adapter, producer_adapter;
|
|
@@ -219,20 +236,19 @@ struct Receiver {
|
|
|
219
236
|
HANDLE jobs_event = nullptr, retry_timer = nullptr;
|
|
220
237
|
bool wait_failed = false;
|
|
221
238
|
#endif
|
|
222
|
-
std::atomic<uint32_t> expected_pid{0};
|
|
223
239
|
std::atomic<bool> stopped{false};
|
|
224
|
-
std::condition_variable bound;
|
|
225
240
|
std::shared_ptr<FrameLedger> ledger = std::make_shared<FrameLedger>();
|
|
226
|
-
|
|
227
|
-
std::shared_ptr<Dispatcher> dispatcher = std::make_shared<Dispatcher>();
|
|
228
|
-
std::thread worker, writer, copier;
|
|
241
|
+
std::thread thread, writer, copier;
|
|
229
242
|
std::mutex jobs_mutex;
|
|
230
243
|
std::condition_variable jobs_ready;
|
|
231
244
|
std::deque<GpuJob> jobs;
|
|
232
245
|
bool input_done = false;
|
|
233
246
|
std::shared_ptr<GrantWake> output = std::make_shared<GrantWake>();
|
|
234
|
-
|
|
235
|
-
|
|
247
|
+
/** Why the connection ended; empty after the producer's goodbye. */
|
|
248
|
+
std::mutex failure_mutex;
|
|
249
|
+
std::string failure;
|
|
250
|
+
Connection(Receiver& receiver, uint64_t connection, uint32_t producer, std::unique_ptr<Channel> link);
|
|
251
|
+
~Connection() {
|
|
236
252
|
#if defined(_WIN32)
|
|
237
253
|
if (jobs_event) CloseHandle(jobs_event);
|
|
238
254
|
if (retry_timer) CloseHandle(retry_timer);
|
|
@@ -251,9 +267,10 @@ struct Receiver {
|
|
|
251
267
|
ledger->accepting_frames = false;
|
|
252
268
|
stopped = true;
|
|
253
269
|
output->closed = true;
|
|
270
|
+
// Any thread may stop a connection; run() releases the channel under the same lock.
|
|
271
|
+
if (channel) channel->interrupt();
|
|
254
272
|
}
|
|
255
|
-
|
|
256
|
-
if (channel) channel->interrupt();
|
|
273
|
+
output->ready.notify_all(); notify_jobs();
|
|
257
274
|
}
|
|
258
275
|
bool enqueue(const Packet& packet) {
|
|
259
276
|
std::lock_guard<std::mutex> lock(output->mutex);
|
|
@@ -274,25 +291,14 @@ struct Receiver {
|
|
|
274
291
|
if (have_packet && !stopped && !channel->send(packet)) { stop(); break; }
|
|
275
292
|
}
|
|
276
293
|
}
|
|
277
|
-
bool emit(Event* event)
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
if (result == napi_ok) return true;
|
|
283
|
-
// napi_closing relinquishes the native thread's TSFN reference.
|
|
284
|
-
if (result != napi_closing) napi_release_threadsafe_function(callback, napi_tsfn_release);
|
|
285
|
-
worker_released = true; dispatcher_closed = true;
|
|
286
|
-
}
|
|
287
|
-
delete event; stop(); return false;
|
|
288
|
-
}
|
|
289
|
-
void close_dispatcher() {
|
|
290
|
-
std::lock_guard<std::mutex> lock(dispatch_mutex);
|
|
291
|
-
dispatcher_closed = true;
|
|
292
|
-
if (!worker_released) { worker_released = true; napi_release_threadsafe_function(callback, napi_tsfn_release); }
|
|
293
|
-
if (!main_released) { main_released = true; napi_release_threadsafe_function(callback, napi_tsfn_abort); }
|
|
294
|
+
bool emit(Event* event);
|
|
295
|
+
/** End this connection with a reason; the first reason is kept. */
|
|
296
|
+
void fail_connection(const std::string& text) {
|
|
297
|
+
{ std::lock_guard<std::mutex> lock(failure_mutex); if (failure.empty()) failure = text; }
|
|
298
|
+
stop();
|
|
294
299
|
}
|
|
295
|
-
|
|
300
|
+
/** GPU completion is unknown: resources and budget stay retained and the drain waits, so report it without timing out. */
|
|
301
|
+
void report_blocked(const std::string& text);
|
|
296
302
|
void grants() {
|
|
297
303
|
std::lock_guard<std::mutex> lock(ledger->mutex);
|
|
298
304
|
for (auto& allocation : ledger->allocations) {
|
|
@@ -339,21 +345,19 @@ struct Receiver {
|
|
|
339
345
|
auto& event = *job.event;
|
|
340
346
|
std::string diagnostic, adapter;
|
|
341
347
|
if (stopped) return;
|
|
342
|
-
if (std::chrono::steady_clock::now() >= event.deadline) throw std::runtime_error("
|
|
348
|
+
if (std::chrono::steady_clock::now() >= event.deadline) throw std::runtime_error("Frame-forwarding frame expired in GPU queue");
|
|
343
349
|
const auto& packet = event.packet;
|
|
344
|
-
if (!resource_dimensions(event.resource, packet.width, packet.height, packet.stride, diagnostic, adapter, &event.source_keyed_mutex)) throw std::runtime_error(diagnostic);
|
|
345
|
-
if (!producer_adapter.empty() && producer_adapter != adapter) throw std::runtime_error("
|
|
350
|
+
if (!resource_dimensions(event.resource, packet.width, packet.height, packet.stride, event.deadline, diagnostic, adapter, &event.source_keyed_mutex)) throw std::runtime_error(diagnostic);
|
|
351
|
+
if (!producer_adapter.empty() && producer_adapter != adapter) throw std::runtime_error("Frame-forwarding texture adapter differs from producer handshake");
|
|
346
352
|
event.adapter = adapter;
|
|
347
353
|
#if defined(_WIN32)
|
|
348
354
|
switch (presentation_path) {
|
|
349
355
|
case PresentationPath::shared_texture:
|
|
350
356
|
if (!event.source_keyed_mutex) throw std::runtime_error("Producer declared keyed mutex support but supplied a legacy texture");
|
|
351
|
-
if (!resource_ready(event.resource, event.deadline, diagnostic)) throw std::runtime_error(diagnostic);
|
|
352
357
|
job.ready = true;
|
|
353
358
|
break;
|
|
354
359
|
case PresentationPath::gpu_copy: {
|
|
355
360
|
if (event.source_keyed_mutex) {
|
|
356
|
-
if (!resource_ready(event.resource, event.deadline, diagnostic)) throw std::runtime_error(diagnostic);
|
|
357
361
|
job.ready = true;
|
|
358
362
|
break;
|
|
359
363
|
}
|
|
@@ -377,13 +381,13 @@ struct Receiver {
|
|
|
377
381
|
}
|
|
378
382
|
void deliver_frame(std::unique_ptr<Event> event) {
|
|
379
383
|
if (stopped) return;
|
|
380
|
-
if (std::chrono::steady_clock::now() >= event->deadline) throw std::runtime_error("
|
|
384
|
+
if (std::chrono::steady_clock::now() >= event->deadline) throw std::runtime_error("Frame-forwarding frame expired before delivery");
|
|
381
385
|
emit(event.release());
|
|
382
386
|
}
|
|
383
387
|
void process_frame(std::unique_ptr<Event> event) {
|
|
384
388
|
GpuJob job; job.event = std::move(event);
|
|
385
389
|
try { prepare_frame(job); deliver_frame(std::move(job.event)); }
|
|
386
|
-
catch (const std::exception& exception) {
|
|
390
|
+
catch (const std::exception& exception) { fail_connection(exception.what()); }
|
|
387
391
|
}
|
|
388
392
|
#if defined(_WIN32)
|
|
389
393
|
bool gpu_idle(std::string& diagnostic) {
|
|
@@ -410,12 +414,12 @@ struct Receiver {
|
|
|
410
414
|
if (wait_ns != INT64_MAX) {
|
|
411
415
|
LARGE_INTEGER due; due.QuadPart = -std::max<int64_t>(1, wait_ns / 100);
|
|
412
416
|
if (!SetWaitableTimer(retry_timer, &due, 0, nullptr, nullptr, FALSE)) {
|
|
413
|
-
wait_failed = true;
|
|
417
|
+
wait_failed = true; fail_connection("Frame-forwarding GPU retry timer failed; retaining unfinished GPU resources"); return;
|
|
414
418
|
}
|
|
415
419
|
waits.push_back(retry_timer);
|
|
416
420
|
}
|
|
417
421
|
if (WaitForMultipleObjects(static_cast<DWORD>(waits.size()), waits.data(), FALSE, INFINITE) == WAIT_FAILED) {
|
|
418
|
-
wait_failed = true;
|
|
422
|
+
wait_failed = true; fail_connection("Frame-forwarding GPU worker wait failed; retaining unfinished GPU resources");
|
|
419
423
|
}
|
|
420
424
|
}
|
|
421
425
|
bool advance(GpuJob& job) {
|
|
@@ -471,15 +475,16 @@ struct Receiver {
|
|
|
471
475
|
if (wait_ns != INT64_MAX) {
|
|
472
476
|
LARGE_INTEGER due; due.QuadPart = -std::max<int64_t>(1, wait_ns / 100);
|
|
473
477
|
if (!SetWaitableTimer(retry_timer, &due, 0, nullptr, nullptr, FALSE)) {
|
|
474
|
-
wait_failed = true;
|
|
478
|
+
wait_failed = true; fail_connection("Frame-forwarding GPU retry timer failed; retaining unfinished GPU resources"); return;
|
|
475
479
|
}
|
|
476
480
|
waits.push_back(retry_timer);
|
|
477
481
|
}
|
|
478
482
|
if (WaitForMultipleObjects(static_cast<DWORD>(waits.size()), waits.data(), FALSE, INFINITE) == WAIT_FAILED) {
|
|
479
|
-
wait_failed = true;
|
|
483
|
+
wait_failed = true; fail_connection("Frame-forwarding GPU worker wait failed; retaining unfinished GPU resources");
|
|
480
484
|
}
|
|
481
485
|
}
|
|
482
486
|
void relay_loop() {
|
|
487
|
+
std::map<uint64_t, std::shared_ptr<RelaySource>> sources;
|
|
483
488
|
std::unique_ptr<GpuJob> reading, uploading;
|
|
484
489
|
std::deque<GpuJob> pending;
|
|
485
490
|
bool retiring = false, stalled_reported = false;
|
|
@@ -490,11 +495,11 @@ struct Receiver {
|
|
|
490
495
|
std::string diagnostic;
|
|
491
496
|
if (!relay_idle(diagnostic)) {
|
|
492
497
|
if (!stalled_reported && std::chrono::steady_clock::now() >= cleanup_deadline) {
|
|
493
|
-
stalled_reported = true;
|
|
498
|
+
stalled_reported = true; report_blocked("Frame-forwarding GPU cleanup is stalled; resources and budget remain retained");
|
|
494
499
|
}
|
|
495
500
|
wait_relay(reading.get(), uploading.get(), pending, true); continue;
|
|
496
501
|
}
|
|
497
|
-
reading.reset(); uploading.reset(); pending.clear();
|
|
502
|
+
reading.reset(); uploading.reset(); pending.clear(); sources.clear();
|
|
498
503
|
std::deque<GpuJob> remaining;
|
|
499
504
|
{ std::lock_guard<std::mutex> lock(jobs_mutex); remaining.swap(jobs); }
|
|
500
505
|
for (auto& job : remaining) if (job.retirement) ledger->budget->release(job.retirement);
|
|
@@ -506,19 +511,24 @@ struct Receiver {
|
|
|
506
511
|
deliver_frame(std::move(uploading->event)); uploading.reset();
|
|
507
512
|
}
|
|
508
513
|
if (reading) {
|
|
509
|
-
if (std::chrono::steady_clock::now() >= reading->event->deadline) throw std::runtime_error("
|
|
514
|
+
if (std::chrono::steady_clock::now() >= reading->event->deadline) throw std::runtime_error("Frame-forwarding source readback timed out");
|
|
510
515
|
const auto complete = relay_readback_ready(reading->staging->texture, false, diagnostic);
|
|
511
516
|
if (complete < 0) throw std::runtime_error(diagnostic);
|
|
512
517
|
if (complete > 0) {
|
|
513
518
|
Packet packet; packet.kind = dsh_frame_forwarding::release; packet.frame_id = ledger->release_source(reading->event->delivery);
|
|
514
|
-
if (!enqueue(packet)) throw std::runtime_error("Cannot return
|
|
519
|
+
if (!enqueue(packet)) throw std::runtime_error("Cannot return frame-forwarding source credit");
|
|
515
520
|
if (latest) pending.clear();
|
|
521
|
+
// A staged replaceable frame supersedes older staged replaceable frames; ordered frames stay queued.
|
|
522
|
+
else if (reading->event->packet.max_frames & frame_replaceable) {
|
|
523
|
+
pending.erase(std::remove_if(pending.begin(), pending.end(),
|
|
524
|
+
[](const GpuJob& job) { return (job.event->packet.max_frames & frame_replaceable) != 0; }), pending.end());
|
|
525
|
+
}
|
|
516
526
|
pending.push_back(std::move(*reading)); reading.reset();
|
|
517
527
|
}
|
|
518
528
|
}
|
|
519
529
|
if (!uploading && !pending.empty()) {
|
|
520
530
|
auto& job = pending.front();
|
|
521
|
-
if (std::chrono::steady_clock::now() >= job.event->deadline) throw std::runtime_error("
|
|
531
|
+
if (std::chrono::steady_clock::now() >= job.event->deadline) throw std::runtime_error("Frame-forwarding frame expired before upload");
|
|
522
532
|
auto target = available_slot(relay_targets);
|
|
523
533
|
if (target) {
|
|
524
534
|
const auto& packet = job.event->packet;
|
|
@@ -543,19 +553,22 @@ struct Receiver {
|
|
|
543
553
|
}
|
|
544
554
|
}
|
|
545
555
|
if (found) {
|
|
546
|
-
if (job.retirement)
|
|
556
|
+
if (job.retirement) {
|
|
557
|
+
sources.erase(job.allocation);
|
|
558
|
+
ledger->budget->release(job.retirement);
|
|
559
|
+
}
|
|
547
560
|
else {
|
|
548
561
|
auto& event = *job.event; const auto& packet = event.packet;
|
|
549
|
-
if (std::chrono::steady_clock::now() >= event.deadline) throw std::runtime_error("
|
|
550
|
-
if (!resource_dimensions(event.resource, packet.width, packet.height, packet.stride, diagnostic, event.adapter, &event.source_keyed_mutex)) throw std::runtime_error(diagnostic);
|
|
551
|
-
if (producer_adapter != event.adapter) throw std::runtime_error("Native texture adapter differs from producer handshake");
|
|
562
|
+
if (std::chrono::steady_clock::now() >= event.deadline) throw std::runtime_error("Frame-forwarding frame expired in source queue");
|
|
552
563
|
job.staging = staging;
|
|
553
|
-
if (!relay_readback_begin(event.resource, packet.width, packet.height, staging->texture,
|
|
564
|
+
if (!relay_readback_begin(event.resource, packet.width, packet.height, packet.stride, staging->texture, sources[packet.allocation_id], diagnostic,
|
|
565
|
+
event.adapter, &event.source_keyed_mutex, event.deadline)) throw std::runtime_error(diagnostic);
|
|
566
|
+
if (producer_adapter != event.adapter) throw std::runtime_error("Frame-forwarding texture adapter differs from producer handshake");
|
|
554
567
|
reading = std::make_unique<GpuJob>(std::move(job));
|
|
555
568
|
}
|
|
556
569
|
continue;
|
|
557
570
|
}
|
|
558
|
-
} catch (const std::exception& exception) {
|
|
571
|
+
} catch (const std::exception& exception) { fail_connection(exception.what()); continue; }
|
|
559
572
|
wait_relay(reading.get(), uploading.get(), pending, false);
|
|
560
573
|
}
|
|
561
574
|
{ std::lock_guard<std::mutex> lock(ledger->mutex); relay_staging.clear(); relay_targets.clear(); }
|
|
@@ -583,7 +596,7 @@ struct Receiver {
|
|
|
583
596
|
if (!gpu_idle(diagnostic)) {
|
|
584
597
|
if (!stalled_reported && std::chrono::steady_clock::now() >= cleanup_deadline) {
|
|
585
598
|
stalled_reported = true;
|
|
586
|
-
|
|
599
|
+
report_blocked("Frame-forwarding GPU cleanup is stalled; resources and budget remain retained");
|
|
587
600
|
}
|
|
588
601
|
wait_gpu(active, true); continue;
|
|
589
602
|
}
|
|
@@ -599,7 +612,7 @@ struct Receiver {
|
|
|
599
612
|
advance(active[index]);
|
|
600
613
|
}
|
|
601
614
|
deliver_ready(active);
|
|
602
|
-
} catch (const std::exception& exception) {
|
|
615
|
+
} catch (const std::exception& exception) { fail_connection(exception.what()); continue; }
|
|
603
616
|
}
|
|
604
617
|
#endif
|
|
605
618
|
GpuJob job; bool found = false, finished;
|
|
@@ -625,7 +638,7 @@ struct Receiver {
|
|
|
625
638
|
else
|
|
626
639
|
#endif
|
|
627
640
|
deliver_frame(std::move(job.event));
|
|
628
|
-
} catch (const std::exception& exception) {
|
|
641
|
+
} catch (const std::exception& exception) { fail_connection(exception.what()); }
|
|
629
642
|
}
|
|
630
643
|
continue;
|
|
631
644
|
}
|
|
@@ -642,38 +655,29 @@ struct Receiver {
|
|
|
642
655
|
relay_textures.clear(); relay_device.reset();
|
|
643
656
|
#endif
|
|
644
657
|
}
|
|
658
|
+
uint64_t next_delivery();
|
|
645
659
|
void read_loop() {
|
|
646
|
-
|
|
647
|
-
while (!stopped && !dispatcher->failed) {
|
|
660
|
+
while (!stopped) {
|
|
648
661
|
Received received;
|
|
649
662
|
int result = channel->receive(received, 100);
|
|
650
663
|
if (result == 0) continue;
|
|
651
|
-
if (result < 0)
|
|
664
|
+
if (result < 0) {
|
|
665
|
+
if (channel->foreign_packet()) fail_connection("Frame-forwarding producer sent a packet for another connection");
|
|
666
|
+
else if (channel->invalid_resource()) fail_connection("Frame-forwarding producer sent a handle it does not own");
|
|
667
|
+
else if (!stopped) fail_connection("Frame-forwarding producer connection lost");
|
|
668
|
+
break;
|
|
669
|
+
}
|
|
652
670
|
const auto received_at = std::chrono::steady_clock::now();
|
|
653
671
|
auto& packet = received.packet;
|
|
654
|
-
if (packet.marker != magic ||
|
|
655
|
-
release_resource(received.resource);
|
|
672
|
+
if (packet.marker != magic || !std::memchr(packet.text, 0, sizeof(packet.text))) {
|
|
673
|
+
release_resource(received.resource); fail_connection("Frame-forwarding producer sent an invalid packet"); break;
|
|
656
674
|
}
|
|
657
|
-
if (
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
std::unique_lock<std::mutex> lock(ledger->mutex);
|
|
663
|
-
while (!stopped && !expected_pid) bound.wait_for(lock, std::chrono::milliseconds(100));
|
|
664
|
-
}
|
|
665
|
-
if (stopped) break;
|
|
666
|
-
if (received.pid != expected_pid) { fail("Native render producer identity mismatch"); break; }
|
|
667
|
-
producer_adapter = packet.text;
|
|
668
|
-
presentation_path = resolve_presentation_path(producer_adapter, display_adapter, (packet.max_frames & hello_keyed_mutex) != 0);
|
|
669
|
-
allocation_factor = allocation_bytes_factor(presentation_path);
|
|
670
|
-
Packet response; response.kind = ready; response.max_frames = max_frames; response.max_bytes = source_byte_budget(presentation_path, max_frames, max_bytes);
|
|
671
|
-
if (!channel->send(response)) break;
|
|
672
|
-
authenticated = true; ledger->connected = true;
|
|
673
|
-
auto event = new Event; event->type = "connected"; event->pid = received.pid; emit(event);
|
|
674
|
-
continue;
|
|
675
|
+
if (packet.version != DSH_FRAME_FORWARDING_PROTOCOL_VERSION) {
|
|
676
|
+
release_resource(received.resource);
|
|
677
|
+
fail_connection("Frame-forwarding protocol mismatch: producer version " + std::to_string(packet.version)
|
|
678
|
+
+ ", receiver version " + std::to_string(DSH_FRAME_FORWARDING_PROTOCOL_VERSION));
|
|
679
|
+
break;
|
|
675
680
|
}
|
|
676
|
-
if (received.pid != expected_pid) { release_resource(received.resource); fail("Native render sender identity changed"); break; }
|
|
677
681
|
if (packet.kind == allocate && !received.resource) {
|
|
678
682
|
bool valid;
|
|
679
683
|
{
|
|
@@ -690,7 +694,7 @@ struct Receiver {
|
|
|
690
694
|
#endif
|
|
691
695
|
if (valid) ledger->allocations.emplace(packet.allocation_id, ledger->budget->request(packet.allocation_id, packet.max_bytes * allocation_factor, output));
|
|
692
696
|
}
|
|
693
|
-
if (!valid) {
|
|
697
|
+
if (!valid) { fail_connection("Invalid frame-forwarding allocation request"); break; }
|
|
694
698
|
} else if (packet.kind == deallocate && !received.resource) {
|
|
695
699
|
bool used = false;
|
|
696
700
|
std::shared_ptr<Reservation> reservation;
|
|
@@ -702,13 +706,13 @@ struct Receiver {
|
|
|
702
706
|
reservation = allocation->second; ledger->allocations.erase(allocation);
|
|
703
707
|
}
|
|
704
708
|
}
|
|
705
|
-
if (used) {
|
|
709
|
+
if (used) { fail_connection("Producer released an in-use frame-forwarding allocation"); break; }
|
|
706
710
|
if (reservation) {
|
|
707
711
|
GpuJob job{{}, packet.allocation_id, std::move(reservation)};
|
|
708
712
|
if (!post(std::move(job))) {
|
|
709
713
|
// Close retires this reservation after the worker has drained its GPU resources.
|
|
710
714
|
{ std::lock_guard<std::mutex> lock(ledger->mutex); ledger->allocations.emplace(packet.allocation_id, std::move(job.retirement)); }
|
|
711
|
-
|
|
715
|
+
fail_connection("Frame-forwarding GPU job queue rejected allocation retirement"); break;
|
|
712
716
|
}
|
|
713
717
|
}
|
|
714
718
|
if (presentation_path == PresentationPath::iosurface) clear_validation_device();
|
|
@@ -721,7 +725,7 @@ struct Receiver {
|
|
|
721
725
|
#else
|
|
722
726
|
packet.stride != packet.width * 4) {
|
|
723
727
|
#endif
|
|
724
|
-
release_resource(received.resource);
|
|
728
|
+
release_resource(received.resource); fail_connection("Invalid frame-forwarding frame fields"); break;
|
|
725
729
|
}
|
|
726
730
|
#if defined(__APPLE__)
|
|
727
731
|
const auto size = resource_bytes(received.resource);
|
|
@@ -729,7 +733,7 @@ struct Receiver {
|
|
|
729
733
|
const auto size = static_cast<uint64_t>(packet.stride) * packet.height;
|
|
730
734
|
#endif
|
|
731
735
|
bool accepted;
|
|
732
|
-
const auto delivery = next_delivery
|
|
736
|
+
const auto delivery = next_delivery();
|
|
733
737
|
{
|
|
734
738
|
std::lock_guard<std::mutex> lock(ledger->mutex);
|
|
735
739
|
auto allocation = ledger->allocations.find(packet.allocation_id);
|
|
@@ -739,46 +743,283 @@ struct Receiver {
|
|
|
739
743
|
for (auto& frame : ledger->frames) if (frame.second.resource && (frame.second.allocation == packet.allocation_id || frame.second.source_id == packet.frame_id)) accepted = false;
|
|
740
744
|
if (accepted) { ledger->frames.emplace(delivery, HeldFrame{received.resource, size, packet.allocation_id, packet.frame_id}); ledger->bytes += size; }
|
|
741
745
|
}
|
|
742
|
-
if (!accepted) { release_resource(received.resource);
|
|
746
|
+
if (!accepted) { release_resource(received.resource); fail_connection("Frame-forwarding frame budget or identifier violated"); break; }
|
|
743
747
|
auto event = std::make_unique<Event>(); event->type = "frame"; event->packet = packet; event->resource = received.resource; event->delivery = delivery;
|
|
744
|
-
event->ledger = ledger; event->deadline = received_at + std::chrono::milliseconds(readback_timeout);
|
|
748
|
+
event->ledger = ledger; event->source = weak_from_this(); event->deadline = received_at + std::chrono::milliseconds(readback_timeout);
|
|
745
749
|
if (presentation_path == PresentationPath::iosurface) process_frame(std::move(event));
|
|
746
750
|
else {
|
|
747
751
|
GpuJob job; job.event = std::move(event);
|
|
748
|
-
if (!post(std::move(job))) {
|
|
752
|
+
if (!post(std::move(job))) { fail_connection("Frame-forwarding GPU job queue rejected frame"); break; }
|
|
749
753
|
}
|
|
750
754
|
} else if (packet.kind == message && !received.resource) {
|
|
751
755
|
auto event = new Event; event->type = "message"; event->text = packet.text; if (!emit(event)) break;
|
|
752
756
|
} else if (packet.kind == goodbye && !received.resource) break;
|
|
753
|
-
else { release_resource(received.resource);
|
|
757
|
+
else { release_resource(received.resource); fail_connection("Unexpected frame-forwarding message"); break; }
|
|
754
758
|
}
|
|
755
759
|
}
|
|
756
|
-
void run()
|
|
760
|
+
void run();
|
|
761
|
+
};
|
|
762
|
+
struct CloseWork { napi_deferred deferred = nullptr; napi_ref keep = nullptr; };
|
|
763
|
+
/** Constant-time comparison of a presented TX secret. */
|
|
764
|
+
bool same_secret(const char (&presented)[65], const std::string& expected) {
|
|
765
|
+
if (!std::memchr(presented, 0, sizeof(presented)) || std::strlen(presented) != expected.size()) return false;
|
|
766
|
+
unsigned char difference = 0;
|
|
767
|
+
for (size_t index = 0; index < expected.size(); ++index) difference |= static_cast<unsigned char>(presented[index] ^ expected[index]);
|
|
768
|
+
return difference == 0;
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* One logical channel: Idle, Active(N) and Draining(N) while it accepts producers, then Closed. While connection N is
|
|
772
|
+
* active, other candidates are refused as busy; while it drains, one authenticated successor waits and others are
|
|
773
|
+
* refused. The successor receives ready once N's frames are released.
|
|
774
|
+
*/
|
|
775
|
+
struct Receiver {
|
|
776
|
+
napi_env env = nullptr;
|
|
777
|
+
napi_async_cleanup_hook_handle cleanup = nullptr;
|
|
778
|
+
// Signals worker completion to the JavaScript thread; its finalizer frees the owning holder.
|
|
779
|
+
napi_threadsafe_function done = nullptr;
|
|
780
|
+
std::atomic<bool> ended{false};
|
|
781
|
+
bool environment_closing = false, closing_handle = false;
|
|
782
|
+
bool main_released = false, worker_released = false, dispatcher_closed = false;
|
|
783
|
+
std::mutex dispatch_mutex;
|
|
784
|
+
std::unique_ptr<CloseWork> close_work;
|
|
785
|
+
std::unique_ptr<Listener> listener;
|
|
786
|
+
Endpoint endpoint;
|
|
787
|
+
uint32_t max_frames = 0;
|
|
788
|
+
uint64_t max_bytes = 0;
|
|
789
|
+
uint32_t readback_timeout = 0;
|
|
790
|
+
std::string display_adapter;
|
|
791
|
+
bool latest = false;
|
|
792
|
+
std::shared_ptr<Budget> budget;
|
|
793
|
+
std::shared_ptr<DrainSignal> drain = std::make_shared<DrainSignal>();
|
|
794
|
+
std::atomic<uint64_t> deliveries{1};
|
|
795
|
+
std::mutex state_mutex;
|
|
796
|
+
uint64_t next_connection = 1;
|
|
797
|
+
/** The active or draining connection. */
|
|
798
|
+
std::shared_ptr<Connection> current;
|
|
799
|
+
/** An authenticated successor waiting for the current connection to drain. */
|
|
800
|
+
std::unique_ptr<Candidate> waiting;
|
|
801
|
+
/** Ended connections whose threads have not been joined, or whose frames are still held after close. */
|
|
802
|
+
std::vector<std::shared_ptr<Connection>> retired;
|
|
803
|
+
std::atomic<bool> stopped{false};
|
|
804
|
+
/** A failed dispatch requests a stop without taking state_mutex. */
|
|
805
|
+
std::atomic<bool> stopping{false};
|
|
806
|
+
napi_threadsafe_function callback = nullptr;
|
|
807
|
+
std::thread worker;
|
|
808
|
+
bool closing = false;
|
|
809
|
+
bool emit(Event* event) {
|
|
810
|
+
{
|
|
811
|
+
std::lock_guard<std::mutex> lock(dispatch_mutex);
|
|
812
|
+
if (dispatcher_closed || worker_released) { delete event; return false; }
|
|
813
|
+
const auto result = napi_call_threadsafe_function(callback, event, napi_tsfn_nonblocking);
|
|
814
|
+
if (result == napi_ok) return true;
|
|
815
|
+
// napi_closing relinquishes the native thread's TSFN reference.
|
|
816
|
+
if (result != napi_closing) napi_release_threadsafe_function(callback, napi_tsfn_release);
|
|
817
|
+
worker_released = true; dispatcher_closed = true;
|
|
818
|
+
}
|
|
819
|
+
// Callers may hold state_mutex; the listener performs the stop this requests.
|
|
820
|
+
delete event; stopping = true; drain->notify(); return false;
|
|
821
|
+
}
|
|
822
|
+
void close_dispatcher() {
|
|
823
|
+
std::lock_guard<std::mutex> lock(dispatch_mutex);
|
|
824
|
+
dispatcher_closed = true;
|
|
825
|
+
if (!worker_released) { worker_released = true; napi_release_threadsafe_function(callback, napi_tsfn_release); }
|
|
826
|
+
if (!main_released) { main_released = true; napi_release_threadsafe_function(callback, napi_tsfn_abort); }
|
|
827
|
+
}
|
|
828
|
+
/** Close the channel: the listener stops, a waiting successor is refused and the current connection ends. */
|
|
829
|
+
void stop() {
|
|
830
|
+
std::shared_ptr<Connection> active;
|
|
831
|
+
std::unique_ptr<Candidate> successor;
|
|
832
|
+
{
|
|
833
|
+
std::lock_guard<std::mutex> lock(state_mutex);
|
|
834
|
+
if (stopped.exchange(true)) return;
|
|
835
|
+
active = current; successor = std::move(waiting);
|
|
836
|
+
if (listener) listener->interrupt();
|
|
837
|
+
}
|
|
838
|
+
if (successor) refuse(*successor, reject_closed);
|
|
839
|
+
if (active) active->stop();
|
|
840
|
+
drain->notify();
|
|
841
|
+
}
|
|
842
|
+
void fail_channel(const std::string& text) {
|
|
843
|
+
auto event = new Event; event->type = "failed"; event->text = text; emit(event); stop();
|
|
844
|
+
}
|
|
845
|
+
void refuse(Candidate& candidate, const std::string& reason) {
|
|
846
|
+
Packet refusal; refusal.kind = goodbye; std::snprintf(refusal.text, sizeof(refusal.text), "%s", reason.c_str());
|
|
847
|
+
candidate.channel->send(refusal);
|
|
848
|
+
candidate.channel.reset();
|
|
849
|
+
auto event = new Event; event->type = "rejected"; event->text = reason; emit(event);
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* @returns The refusal reason, or empty for a producer holding the credential that supports transferable channels and
|
|
853
|
+
* names only handles of its own.
|
|
854
|
+
*/
|
|
855
|
+
std::string authenticate(const Candidate& candidate) const {
|
|
856
|
+
const auto& packet = candidate.hello.packet;
|
|
857
|
+
if (packet.marker != magic || packet.kind != hello || !std::memchr(packet.text, 0, sizeof(packet.text))) return reject_credential;
|
|
858
|
+
if (packet.version != DSH_FRAME_FORWARDING_PROTOCOL_VERSION)
|
|
859
|
+
return std::string(reject_protocol) + ": producer version " + std::to_string(packet.version)
|
|
860
|
+
+ ", receiver version " + std::to_string(DSH_FRAME_FORWARDING_PROTOCOL_VERSION);
|
|
861
|
+
if (!same_secret(packet.token, endpoint.token)) return reject_credential;
|
|
862
|
+
if (!(packet.max_frames & hello_transferable)) return reject_capability;
|
|
863
|
+
if (!(packet.max_frames & hello_producer_local_handles)) return reject_local_handles;
|
|
864
|
+
return {};
|
|
865
|
+
}
|
|
866
|
+
/** Promote an authenticated candidate to the current connection; the caller holds state_mutex. */
|
|
867
|
+
void start(std::unique_ptr<Candidate> candidate) {
|
|
868
|
+
std::shared_ptr<Connection> connection;
|
|
757
869
|
try {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
870
|
+
connection = std::make_shared<Connection>(*this, next_connection++, candidate->hello.pid, std::move(candidate->channel));
|
|
871
|
+
} catch (const std::exception& exception) {
|
|
872
|
+
auto event = new Event; event->type = "rejected"; event->text = exception.what(); emit(event); return;
|
|
873
|
+
}
|
|
874
|
+
const auto& hello_packet = candidate->hello.packet;
|
|
875
|
+
connection->producer_adapter = hello_packet.text;
|
|
876
|
+
connection->presentation_path = resolve_presentation_path(connection->producer_adapter, display_adapter, (hello_packet.max_frames & hello_keyed_mutex) != 0);
|
|
877
|
+
connection->allocation_factor = allocation_bytes_factor(connection->presentation_path);
|
|
878
|
+
connection->channel->bind_connection(connection->id);
|
|
879
|
+
Packet response; response.kind = ready; response.max_frames = max_frames;
|
|
880
|
+
response.max_bytes = source_byte_budget(connection->presentation_path, max_frames, max_bytes);
|
|
881
|
+
response.stride = hello_packet.max_frames & hello_capabilities;
|
|
882
|
+
// A candidate that vanished while waiting takes no connection.
|
|
883
|
+
if (!connection->channel->send(response)) return;
|
|
884
|
+
connection->ledger->connected = true;
|
|
885
|
+
current = connection;
|
|
886
|
+
auto event = new Event; event->type = "connected"; event->pid = connection->pid; event->connection = connection->id; emit(event);
|
|
887
|
+
connection->thread = std::thread([raw = connection.get()] { raw->run(); });
|
|
888
|
+
}
|
|
889
|
+
void admit(std::unique_ptr<Candidate> candidate) {
|
|
890
|
+
if (const auto reason = authenticate(*candidate); !reason.empty()) { refuse(*candidate, reason); return; }
|
|
891
|
+
std::lock_guard<std::mutex> lock(state_mutex);
|
|
892
|
+
if (stopped) refuse(*candidate, reject_closed);
|
|
893
|
+
else if (!current) start(std::move(candidate));
|
|
894
|
+
else if (!current->ledger->connected && !waiting) waiting = std::move(candidate);
|
|
895
|
+
else refuse(*candidate, reject_busy);
|
|
896
|
+
}
|
|
897
|
+
/** A waiting successor that disconnected, or spoke before ready, gives up its place; the caller holds state_mutex. */
|
|
898
|
+
void probe_waiting() {
|
|
899
|
+
if (!waiting) return;
|
|
900
|
+
Received early;
|
|
901
|
+
const auto result = waiting->channel->receive(early, 0);
|
|
902
|
+
if (result == 0) return;
|
|
903
|
+
if (result == 1) { release_resource(early.resource); refuse(*waiting, reject_credential); }
|
|
904
|
+
waiting.reset();
|
|
905
|
+
}
|
|
906
|
+
void join_retired() {
|
|
907
|
+
std::vector<std::shared_ptr<Connection>> ended_connections;
|
|
908
|
+
{ std::lock_guard<std::mutex> lock(state_mutex); ended_connections = retired; }
|
|
909
|
+
for (auto& connection : ended_connections) if (connection->thread.joinable()) connection->thread.join();
|
|
910
|
+
std::lock_guard<std::mutex> lock(state_mutex);
|
|
911
|
+
retired.erase(std::remove_if(retired.begin(), retired.end(), [](const std::shared_ptr<Connection>& connection) {
|
|
912
|
+
return !connection->thread.joinable() && connection->ledger->empty();
|
|
913
|
+
}), retired.end());
|
|
914
|
+
}
|
|
915
|
+
void listen_loop() {
|
|
916
|
+
try {
|
|
917
|
+
while (!stopped && !stopping) {
|
|
918
|
+
join_retired();
|
|
919
|
+
{ std::lock_guard<std::mutex> lock(state_mutex); probe_waiting(); }
|
|
920
|
+
auto candidate = std::make_unique<Candidate>();
|
|
921
|
+
const auto result = listener->accept(*candidate, 100);
|
|
922
|
+
if (result < 0) { if (!stopped && !stopping) fail_channel("Frame-forwarding channel listener failed"); break; }
|
|
923
|
+
if (result == 1) admit(std::move(candidate));
|
|
924
|
+
}
|
|
925
|
+
} catch (const std::exception& exception) { fail_channel(exception.what()); }
|
|
772
926
|
stop();
|
|
927
|
+
std::shared_ptr<Connection> active;
|
|
928
|
+
{
|
|
929
|
+
std::lock_guard<std::mutex> lock(state_mutex);
|
|
930
|
+
// Dropping the listener withdraws the endpoint, so the TX credential no longer connects.
|
|
931
|
+
listener.reset();
|
|
932
|
+
active = current;
|
|
933
|
+
}
|
|
934
|
+
if (active && active->thread.joinable()) active->thread.join();
|
|
935
|
+
join_retired();
|
|
773
936
|
{
|
|
774
937
|
std::lock_guard<std::mutex> lock(dispatch_mutex);
|
|
775
938
|
if (!worker_released) { worker_released = true; napi_release_threadsafe_function(callback, napi_tsfn_release); }
|
|
776
939
|
}
|
|
777
|
-
ended = true;
|
|
940
|
+
ended = true;
|
|
941
|
+
// A closing environment finishes through its async cleanup hook instead.
|
|
942
|
+
napi_call_threadsafe_function(done, nullptr, napi_tsfn_nonblocking);
|
|
943
|
+
}
|
|
944
|
+
/** Release one delivery of whichever connection owns it; credit returns only to that connection while it is connected. */
|
|
945
|
+
bool release(uint64_t delivery) {
|
|
946
|
+
std::vector<std::shared_ptr<Connection>> owners;
|
|
947
|
+
{
|
|
948
|
+
std::lock_guard<std::mutex> lock(state_mutex);
|
|
949
|
+
if (current) owners.push_back(current);
|
|
950
|
+
owners.insert(owners.end(), retired.begin(), retired.end());
|
|
951
|
+
}
|
|
952
|
+
for (auto& connection : owners) {
|
|
953
|
+
uint64_t source_id = 0;
|
|
954
|
+
if (!connection->ledger->release(delivery, true, &source_id)) continue;
|
|
955
|
+
connection->notify_jobs();
|
|
956
|
+
if (source_id) {
|
|
957
|
+
Packet packet; packet.kind = dsh_frame_forwarding::release; packet.frame_id = source_id;
|
|
958
|
+
if (connection->ledger->connected && !connection->enqueue(packet)) connection->stop();
|
|
959
|
+
}
|
|
960
|
+
return true;
|
|
961
|
+
}
|
|
962
|
+
return false;
|
|
963
|
+
}
|
|
964
|
+
/** Queue control JSON for one connection; false when that connection is not the current, connected one. */
|
|
965
|
+
bool send(uint64_t connection_id, const Packet& packet) {
|
|
966
|
+
std::lock_guard<std::mutex> lock(state_mutex);
|
|
967
|
+
return current && current->id == connection_id && current->ledger->connected && current->enqueue(packet);
|
|
778
968
|
}
|
|
779
969
|
};
|
|
780
|
-
|
|
781
|
-
|
|
970
|
+
Connection::Connection(Receiver& receiver, uint64_t connection, uint32_t producer, std::unique_ptr<Channel> link)
|
|
971
|
+
: owner(receiver), id(connection), pid(producer), channel(std::move(link)), max_frames(receiver.max_frames),
|
|
972
|
+
max_bytes(receiver.max_bytes), readback_timeout(receiver.readback_timeout), display_adapter(receiver.display_adapter),
|
|
973
|
+
latest(receiver.latest) {
|
|
974
|
+
ledger->budget = receiver.budget; ledger->drain = receiver.drain;
|
|
975
|
+
#if defined(_WIN32)
|
|
976
|
+
jobs_event = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
|
977
|
+
retry_timer = reinterpret_cast<HANDLE>(create_gpu_retry_timer());
|
|
978
|
+
if (!jobs_event || !retry_timer) {
|
|
979
|
+
if (jobs_event) CloseHandle(jobs_event);
|
|
980
|
+
if (retry_timer) CloseHandle(retry_timer);
|
|
981
|
+
throw std::runtime_error("Cannot create frame-forwarding GPU worker wake event or retry timer");
|
|
982
|
+
}
|
|
983
|
+
#endif
|
|
984
|
+
}
|
|
985
|
+
bool Connection::emit(Event* event) { event->connection = id; return owner.emit(event); }
|
|
986
|
+
void Connection::report_blocked(const std::string& text) {
|
|
987
|
+
auto event = new Event; event->type = "drain-blocked"; event->text = text; emit(event);
|
|
988
|
+
}
|
|
989
|
+
uint64_t Connection::next_delivery() { return owner.deliveries.fetch_add(1); }
|
|
990
|
+
void Connection::run() {
|
|
991
|
+
try {
|
|
992
|
+
writer = std::thread([this] { write_loop(); });
|
|
993
|
+
copier = std::thread([this] { copy_loop(); });
|
|
994
|
+
read_loop();
|
|
995
|
+
} catch (const std::exception& exception) { fail_connection(exception.what()); }
|
|
996
|
+
stop();
|
|
997
|
+
{ std::lock_guard<std::mutex> lock(jobs_mutex); input_done = true; }
|
|
998
|
+
notify_jobs();
|
|
999
|
+
if (copier.joinable()) copier.join();
|
|
1000
|
+
if (writer.joinable()) writer.join();
|
|
1001
|
+
clear_validation_device();
|
|
1002
|
+
ledger->connected = false;
|
|
1003
|
+
// Releasing the channel closes the producer's connection port or pipe instance.
|
|
1004
|
+
{ std::lock_guard<std::mutex> lock(output->mutex); channel.reset(); }
|
|
1005
|
+
ledger->retire_idle();
|
|
1006
|
+
{
|
|
1007
|
+
auto event = new Event; event->type = "disconnected";
|
|
1008
|
+
{ std::lock_guard<std::mutex> lock(failure_mutex); event->text = failure; }
|
|
1009
|
+
emit(event);
|
|
1010
|
+
}
|
|
1011
|
+
{
|
|
1012
|
+
std::unique_lock<std::mutex> lock(owner.drain->mutex);
|
|
1013
|
+
owner.drain->changed.wait(lock, [&] { return owner.stopped.load() || owner.stopping.load() || ledger->empty(); });
|
|
1014
|
+
}
|
|
1015
|
+
const bool drained = ledger->empty();
|
|
1016
|
+
if (drained) { auto event = new Event; event->type = "drained"; emit(event); }
|
|
1017
|
+
std::lock_guard<std::mutex> lock(owner.state_mutex);
|
|
1018
|
+
if (owner.current.get() == this) { owner.retired.push_back(owner.current); owner.current.reset(); }
|
|
1019
|
+
if (drained && !owner.stopped && owner.waiting) owner.start(std::move(owner.waiting));
|
|
1020
|
+
}
|
|
1021
|
+
void receiver_done_finalized(napi_env, void* data, void*) {
|
|
1022
|
+
auto holder = static_cast<std::shared_ptr<Receiver>*>(data);
|
|
782
1023
|
if ((*holder)->cleanup) napi_remove_async_cleanup_hook((*holder)->cleanup);
|
|
783
1024
|
delete holder;
|
|
784
1025
|
}
|
|
@@ -800,33 +1041,37 @@ void receiver_finish_on_main(Receiver* self) {
|
|
|
800
1041
|
self->closing_handle = true;
|
|
801
1042
|
// The reader joins both I/O helpers and drains submitted GPU work before setting ended.
|
|
802
1043
|
if (self->worker.joinable()) self->worker.join();
|
|
803
|
-
self->channel.reset();
|
|
804
1044
|
settle_close(self);
|
|
805
1045
|
{
|
|
806
1046
|
std::lock_guard<std::mutex> lock(self->dispatch_mutex);
|
|
807
1047
|
if (!self->main_released) { self->main_released = true; napi_release_threadsafe_function(self->callback, napi_tsfn_release); }
|
|
808
1048
|
}
|
|
809
|
-
|
|
1049
|
+
napi_release_threadsafe_function(self->done, napi_tsfn_release);
|
|
810
1050
|
}
|
|
811
|
-
void receiver_abandon_setup(Receiver* self,
|
|
1051
|
+
void receiver_abandon_setup(Receiver* self, std::shared_ptr<Receiver>* holder) {
|
|
812
1052
|
self->stop(); self->ended = true; self->environment_closing = true; self->closing_handle = true;
|
|
813
1053
|
if (self->callback) self->close_dispatcher();
|
|
814
|
-
if (
|
|
815
|
-
else
|
|
1054
|
+
if (self->done) napi_release_threadsafe_function(self->done, napi_tsfn_abort);
|
|
1055
|
+
else delete holder;
|
|
816
1056
|
}
|
|
817
1057
|
napi_value string(napi_env env, const std::string& value) { napi_value result; napi_create_string_utf8(env, value.data(), value.size(), &result); return result; }
|
|
818
1058
|
napi_value number(napi_env env, double value) { napi_value result; napi_create_double(env, value, &result); return result; }
|
|
819
1059
|
void property(napi_env env, napi_value object, const char* name, napi_value value) { napi_set_named_property(env, object, name, value); }
|
|
820
|
-
|
|
1060
|
+
/** End the connection of a frame that cannot be delivered; the frame's ledger entry is released with the event. */
|
|
1061
|
+
void undeliverable(const Event& event, const char* text) {
|
|
1062
|
+
if (auto connection = event.source.lock()) connection->fail_connection(text);
|
|
1063
|
+
}
|
|
1064
|
+
void dispatch(napi_env env, napi_value callback, void*, void* data) {
|
|
821
1065
|
std::unique_ptr<Event> event(static_cast<Event*>(data));
|
|
822
1066
|
if (!env || !callback) return;
|
|
823
|
-
|
|
824
|
-
if (event->type == "frame" &&
|
|
1067
|
+
// A frame of a stopped connection is released undelivered; one that outlived its deadline ends its connection.
|
|
1068
|
+
if (event->type == "frame" && !event->ledger->accepting_frames) return;
|
|
825
1069
|
if (event->type == "frame" && std::chrono::steady_clock::now() >= event->deadline) {
|
|
826
|
-
|
|
1070
|
+
undeliverable(*event, "Frame-forwarding frame expired in the JavaScript delivery queue"); return;
|
|
827
1071
|
}
|
|
828
1072
|
napi_value object; napi_create_object(env, &object);
|
|
829
1073
|
property(env, object, "type", string(env, event->type));
|
|
1074
|
+
if (event->connection) property(env, object, "connectionId", number(env, static_cast<double>(event->connection)));
|
|
830
1075
|
if (event->type == "frame") {
|
|
831
1076
|
property(env, object, "frameId", number(env, static_cast<double>(event->packet.frame_id)));
|
|
832
1077
|
property(env, object, "deliveryId", number(env, static_cast<double>(event->delivery)));
|
|
@@ -838,36 +1083,26 @@ void dispatch(napi_env env, napi_value callback, void* context, void* data) {
|
|
|
838
1083
|
property(env, object, "sourceKeyedMutex", source_keyed_mutex);
|
|
839
1084
|
napi_value buffer;
|
|
840
1085
|
if (napi_create_buffer_copy(env, sizeof(event->resource), &event->resource, nullptr, &buffer) != napi_ok) {
|
|
841
|
-
dispatcher.failed = true;
|
|
842
1086
|
bool pending = false;
|
|
843
1087
|
napi_is_exception_pending(env, &pending);
|
|
844
1088
|
if (pending) { napi_value exception; napi_get_and_clear_last_exception(env, &exception); }
|
|
845
|
-
|
|
846
|
-
property(env, object, "type", string(env, "error"));
|
|
847
|
-
property(env, object, "text", string(env, "Cannot allocate native render handle buffer"));
|
|
848
|
-
} else {
|
|
849
|
-
property(env, object, "handle", buffer);
|
|
850
|
-
napi_value presentation;
|
|
851
|
-
if (event->presentation_adapter.empty()) napi_get_null(env, &presentation);
|
|
852
|
-
else presentation = string(env, event->presentation_adapter);
|
|
853
|
-
property(env, object, "presentationAdapterId", presentation);
|
|
1089
|
+
undeliverable(*event, "Cannot allocate frame-forwarding handle buffer"); return;
|
|
854
1090
|
}
|
|
1091
|
+
property(env, object, "handle", buffer);
|
|
1092
|
+
napi_value presentation;
|
|
1093
|
+
if (event->presentation_adapter.empty()) napi_get_null(env, &presentation);
|
|
1094
|
+
else presentation = string(env, event->presentation_adapter);
|
|
1095
|
+
property(env, object, "presentationAdapterId", presentation);
|
|
855
1096
|
property(env, object, "adapterId", string(env, event->adapter));
|
|
856
|
-
} else if (event->type == "connected") property(env, object, "pid", number(env, event->pid));
|
|
857
|
-
else if (!event->text.empty()) property(env, object, "text", string(env, event->text));
|
|
858
|
-
if (event->type == "frame" && !dispatcher.failed) {
|
|
859
1097
|
std::lock_guard<std::mutex> lock(event->ledger->mutex);
|
|
860
1098
|
if (!event->ledger->accepting_frames) return;
|
|
861
1099
|
if (std::chrono::steady_clock::now() >= event->deadline) {
|
|
862
|
-
|
|
863
|
-
napi_create_object(env, &object);
|
|
864
|
-
property(env, object, "type", string(env, "error"));
|
|
865
|
-
property(env, object, "text", string(env, "Native frame expired during JavaScript delivery"));
|
|
866
|
-
} else {
|
|
867
|
-
event->ledger->frames.at(event->delivery).delivered = true;
|
|
868
|
-
event->delivered = true;
|
|
1100
|
+
undeliverable(*event, "Frame-forwarding frame expired during JavaScript delivery"); return;
|
|
869
1101
|
}
|
|
870
|
-
|
|
1102
|
+
event->ledger->frames.at(event->delivery).delivered = true;
|
|
1103
|
+
event->delivered = true;
|
|
1104
|
+
} else if (event->type == "connected") property(env, object, "pid", number(env, event->pid));
|
|
1105
|
+
else if (!event->text.empty() || event->type == "message") property(env, object, "text", string(env, event->text));
|
|
871
1106
|
napi_value global, result; napi_get_global(env, &global); napi_call_function(env, global, callback, 1, &object, &result);
|
|
872
1107
|
}
|
|
873
1108
|
napi_value fail(napi_env env, const char* text) { napi_throw_error(env, nullptr, text); return nullptr; }
|
|
@@ -900,54 +1135,45 @@ napi_value create(napi_env env, napi_callback_info info) {
|
|
|
900
1135
|
napi_get_value_string_utf8(env, args[4], adapter, sizeof(adapter), &adapter_size) != napi_ok || adapter_size >= sizeof(adapter) - 1 ||
|
|
901
1136
|
napi_typeof(env, args[2], &callback_type) != napi_ok || callback_type != napi_function) return fail(env, "Invalid native receiver options");
|
|
902
1137
|
#if defined(_WIN32)
|
|
903
|
-
if (!adapter_size) return fail(env, "
|
|
1138
|
+
if (!adapter_size) return fail(env, "Frame forwarding unavailable: Windows requires a display adapter");
|
|
904
1139
|
#endif
|
|
905
1140
|
std::shared_ptr<Receiver> value;
|
|
906
|
-
|
|
1141
|
+
// Owned by the completion function once it exists; freed by its finalizer.
|
|
1142
|
+
std::shared_ptr<Receiver>* done_holder = nullptr;
|
|
907
1143
|
try {
|
|
908
1144
|
value = std::make_shared<Receiver>(); value->env = env;
|
|
909
|
-
#if defined(_WIN32)
|
|
910
|
-
value->jobs_event = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
|
911
|
-
value->retry_timer = reinterpret_cast<HANDLE>(create_gpu_retry_timer());
|
|
912
|
-
if (!value->jobs_event || !value->retry_timer) return fail(env, "Cannot create GPU worker wake event or retry timer");
|
|
913
|
-
#endif
|
|
914
1145
|
value->max_frames = static_cast<uint32_t>(max_frames); value->max_bytes = max_bytes;
|
|
915
1146
|
value->display_adapter = adapter; value->readback_timeout = static_cast<uint32_t>(timeout); value->latest = latest;
|
|
916
1147
|
void* budget_pointer = nullptr;
|
|
917
|
-
if (napi_get_value_external(env, args[3], &budget_pointer) != napi_ok || !budget_pointer) return fail(env, "Invalid
|
|
918
|
-
value->
|
|
919
|
-
if (max_bytes > value->
|
|
1148
|
+
if (napi_get_value_external(env, args[3], &budget_pointer) != napi_ok || !budget_pointer) return fail(env, "Invalid frame-forwarding budget");
|
|
1149
|
+
value->budget = *static_cast<std::shared_ptr<Budget>*>(budget_pointer);
|
|
1150
|
+
if (max_bytes > value->budget->limit) return fail(env, "Channel budget exceeds shared frame-forwarding budget");
|
|
920
1151
|
value->endpoint.pid = process_id(); value->endpoint.token = token();
|
|
921
1152
|
#if defined(__APPLE__)
|
|
922
1153
|
value->endpoint.name = "ai.deepseek.render." + token();
|
|
923
1154
|
#else
|
|
924
1155
|
value->endpoint.name = "\\\\.\\pipe\\dsh-render-" + token();
|
|
925
1156
|
#endif
|
|
926
|
-
value->
|
|
927
|
-
|
|
928
|
-
auto
|
|
929
|
-
auto
|
|
930
|
-
if (napi_create_threadsafe_function(env, args[2], nullptr, name, 256, 2,
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
receiver_abandon_setup(value.get(), false); return fail(env, "Cannot find native receiver event loop");
|
|
938
|
-
}
|
|
939
|
-
value->done.data = new std::shared_ptr<Receiver>(value);
|
|
940
|
-
if (uv_async_init(loop, &value->done, [](uv_async_t* handle) {
|
|
941
|
-
receiver_finish_on_main(static_cast<std::shared_ptr<Receiver>*>(handle->data)->get());
|
|
942
|
-
}) != 0) {
|
|
943
|
-
receiver_abandon_setup(value.get(), false); return fail(env, "Cannot create native receiver completion handle");
|
|
1157
|
+
value->listener = listen(value->endpoint, value->readback_timeout);
|
|
1158
|
+
// The TX credential: producers present its secret; it names the receiver process, never a producer.
|
|
1159
|
+
auto tx = std::string("1|") + std::to_string(value->endpoint.pid) + "|" + value->endpoint.token + "|" + value->endpoint.name;
|
|
1160
|
+
auto name = string(env, "DSH frame-forwarding receiver");
|
|
1161
|
+
if (napi_create_threadsafe_function(env, args[2], nullptr, name, 256, 2, nullptr, nullptr, nullptr, dispatch, &value->callback) != napi_ok)
|
|
1162
|
+
return fail(env, "Cannot create frame-forwarding event dispatcher");
|
|
1163
|
+
done_holder = new std::shared_ptr<Receiver>(value);
|
|
1164
|
+
auto done_name = string(env, "DSH frame-forwarding receiver completion");
|
|
1165
|
+
if (napi_create_threadsafe_function(env, nullptr, nullptr, done_name, 0, 1, done_holder, receiver_done_finalized, value.get(),
|
|
1166
|
+
[](napi_env, napi_value, void* context, void*) { receiver_finish_on_main(static_cast<Receiver*>(context)); }, &value->done) != napi_ok) {
|
|
1167
|
+
receiver_abandon_setup(value.get(), done_holder); return fail(env, "Cannot create native receiver completion handle");
|
|
944
1168
|
}
|
|
945
|
-
handle_initialized = true;
|
|
946
1169
|
if (napi_add_async_cleanup_hook(env, [](napi_async_cleanup_hook_handle, void* data) {
|
|
947
1170
|
auto self = static_cast<Receiver*>(data); self->environment_closing = true;
|
|
948
|
-
self->stop(); self->close_dispatcher();
|
|
1171
|
+
self->stop(); self->close_dispatcher();
|
|
1172
|
+
// Node closes the completion function during teardown, so wait for the worker here instead of relying on its signal.
|
|
1173
|
+
if (self->worker.joinable()) self->worker.join();
|
|
1174
|
+
receiver_finish_on_main(self);
|
|
949
1175
|
}, value.get(), &value->cleanup) != napi_ok) {
|
|
950
|
-
receiver_abandon_setup(value.get(),
|
|
1176
|
+
receiver_abandon_setup(value.get(), done_holder); return fail(env, "Cannot register native receiver cleanup");
|
|
951
1177
|
}
|
|
952
1178
|
napi_value external, result;
|
|
953
1179
|
auto raw = value.get();
|
|
@@ -956,35 +1182,26 @@ napi_value create(napi_env env, napi_callback_info info) {
|
|
|
956
1182
|
std::unique_ptr<std::shared_ptr<Receiver>> holder(static_cast<std::shared_ptr<Receiver>*>(data));
|
|
957
1183
|
(*holder)->stop(); (*holder)->close_dispatcher();
|
|
958
1184
|
}, nullptr, &external) != napi_ok) {
|
|
959
|
-
receiver_abandon_setup(raw,
|
|
1185
|
+
receiver_abandon_setup(raw, done_holder); return fail(env, "Cannot create native receiver owner");
|
|
960
1186
|
}
|
|
961
1187
|
holder.release();
|
|
962
|
-
napi_create_object(env, &result); property(env, result, "native", external); property(env, result, "
|
|
963
|
-
raw->worker = std::thread([raw] { raw->
|
|
1188
|
+
napi_create_object(env, &result); property(env, result, "native", external); property(env, result, "tx", string(env, tx));
|
|
1189
|
+
raw->worker = std::thread([raw] { raw->listen_loop(); });
|
|
964
1190
|
return result;
|
|
965
1191
|
} catch (const std::exception& exception) {
|
|
966
|
-
if (value && value->callback) receiver_abandon_setup(value.get(),
|
|
1192
|
+
if (value && value->callback) receiver_abandon_setup(value.get(), done_holder);
|
|
967
1193
|
return fail(env, exception.what());
|
|
968
1194
|
}
|
|
969
1195
|
}
|
|
970
|
-
napi_value bind(napi_env env, napi_callback_info info) {
|
|
971
|
-
napi_value args[2]; size_t count = 2; napi_get_cb_info(env, info, &count, args, nullptr, nullptr);
|
|
972
|
-
if (count != 2) return fail(env, "Expected receiver and producer PID");
|
|
973
|
-
auto value = receiver(env, args[0]); uint64_t pid;
|
|
974
|
-
if (!value) return nullptr;
|
|
975
|
-
if (!uint_value(env, args[1], pid) || pid > UINT32_MAX || value->stopped || value->expected_pid) return fail(env, "Producer can only be bound once to a live receiver");
|
|
976
|
-
{ std::lock_guard<std::mutex> lock(value->ledger->mutex); value->expected_pid = static_cast<uint32_t>(pid); }
|
|
977
|
-
value->bound.notify_all();
|
|
978
|
-
napi_value result; napi_get_undefined(env, &result); return result;
|
|
979
|
-
}
|
|
980
1196
|
napi_value send(napi_env env, napi_callback_info info) {
|
|
981
|
-
napi_value args[
|
|
982
|
-
if (count !=
|
|
983
|
-
auto value = receiver(env, args[0]); if (!value) return nullptr;
|
|
1197
|
+
napi_value args[3]; size_t count = 3; napi_get_cb_info(env, info, &count, args, nullptr, nullptr);
|
|
1198
|
+
if (count != 3) return fail(env, "Expected receiver, connection ID and control JSON");
|
|
1199
|
+
auto value = receiver(env, args[0]); uint64_t connection; if (!value) return nullptr;
|
|
1200
|
+
if (!uint_value(env, args[1], connection)) return fail(env, "Invalid frame-forwarding connection ID");
|
|
984
1201
|
Packet packet; packet.kind = message; size_t length;
|
|
985
|
-
if (napi_get_value_string_utf8(env, args[
|
|
986
|
-
napi_get_value_string_utf8(env, args[
|
|
987
|
-
if (!value->
|
|
1202
|
+
if (napi_get_value_string_utf8(env, args[2], nullptr, 0, &length) != napi_ok || length >= sizeof(packet.text)) return fail(env, "Control message exceeds frame-forwarding limit");
|
|
1203
|
+
napi_get_value_string_utf8(env, args[2], packet.text, sizeof(packet.text), &length);
|
|
1204
|
+
if (!value->send(connection, packet)) return fail(env, "Frame-forwarding connection is not active");
|
|
988
1205
|
napi_value result; napi_get_undefined(env, &result); return result;
|
|
989
1206
|
}
|
|
990
1207
|
napi_value release(napi_env env, napi_callback_info info) {
|
|
@@ -992,14 +1209,7 @@ napi_value release(napi_env env, napi_callback_info info) {
|
|
|
992
1209
|
if (count != 2) return fail(env, "Expected receiver and delivery ID");
|
|
993
1210
|
auto value = receiver(env, args[0]); uint64_t id; if (!value) return nullptr;
|
|
994
1211
|
if (!uint_value(env, args[1], id)) return fail(env, "Invalid delivery ID");
|
|
995
|
-
|
|
996
|
-
const bool released = value->ledger->release(id, true, &source_id);
|
|
997
|
-
if (released) value->notify_jobs();
|
|
998
|
-
if (source_id) {
|
|
999
|
-
Packet packet; packet.kind = dsh_frame_forwarding::release; packet.frame_id = source_id;
|
|
1000
|
-
if (value->ledger->connected && !value->enqueue(packet)) value->stop();
|
|
1001
|
-
}
|
|
1002
|
-
napi_value result; napi_get_boolean(env, released, &result); return result;
|
|
1212
|
+
napi_value result; napi_get_boolean(env, value->release(id), &result); return result;
|
|
1003
1213
|
}
|
|
1004
1214
|
napi_value close(napi_env env, napi_callback_info info) {
|
|
1005
1215
|
napi_value args[1]; size_t count = 1; napi_get_cb_info(env, info, &count, args, nullptr, nullptr);
|
|
@@ -1018,7 +1228,7 @@ napi_value close(napi_env env, napi_callback_info info) {
|
|
|
1018
1228
|
napi_value create_budget(napi_env env, napi_callback_info info) {
|
|
1019
1229
|
napi_value args[1]; size_t count = 1; napi_get_cb_info(env, info, &count, args, nullptr, nullptr);
|
|
1020
1230
|
uint64_t bytes;
|
|
1021
|
-
if (count != 1 || !uint_value(env, args[0], bytes)) return fail(env, "Invalid shared
|
|
1231
|
+
if (count != 1 || !uint_value(env, args[0], bytes)) return fail(env, "Invalid shared frame-forwarding byte budget");
|
|
1022
1232
|
auto budget = new std::shared_ptr<Budget>(std::make_shared<Budget>(bytes));
|
|
1023
1233
|
napi_value result;
|
|
1024
1234
|
napi_create_external(env, budget, [](napi_env, void* data, void*) { delete static_cast<std::shared_ptr<Budget>*>(data); }, nullptr, &result);
|
|
@@ -1034,17 +1244,17 @@ napi_value get_adapter(napi_env env, napi_callback_info info) {
|
|
|
1034
1244
|
return string(env, resource_adapter(handle));
|
|
1035
1245
|
}
|
|
1036
1246
|
napi_value initialize(napi_env env, napi_value exports) {
|
|
1037
|
-
register_node_producer(env, exports);
|
|
1038
1247
|
const napi_property_descriptor methods[] = {
|
|
1039
1248
|
{"createBudget", nullptr, create_budget, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1040
1249
|
{"getAdapter", nullptr, get_adapter, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1041
1250
|
{"create", nullptr, create, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1042
|
-
{"bind", nullptr, bind, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1043
1251
|
{"send", nullptr, send, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1044
1252
|
{"release", nullptr, release, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1045
1253
|
{"close", nullptr, close, nullptr, nullptr, nullptr, napi_default, nullptr}
|
|
1046
1254
|
};
|
|
1047
|
-
napi_define_properties(env, exports, sizeof(methods) / sizeof(methods[0]), methods);
|
|
1255
|
+
napi_define_properties(env, exports, sizeof(methods) / sizeof(methods[0]), methods);
|
|
1256
|
+
register_node_producer(env, exports);
|
|
1257
|
+
return exports;
|
|
1048
1258
|
}
|
|
1049
1259
|
}
|
|
1050
1260
|
NAPI_MODULE(NODE_GYP_MODULE_NAME, initialize)
|