@homaiohq/napi-quickfix 0.1.0 → 0.3.0
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/CMakeLists.txt +5 -1
- package/README.md +344 -18
- package/cpp/acceptor_wrap.cpp +253 -19
- package/cpp/acceptor_wrap.h +60 -1
- package/cpp/addon.cpp +10 -6
- package/cpp/application_bridge.cpp +114 -30
- package/cpp/application_bridge.h +57 -4
- package/cpp/data_dictionary_wrap.cpp +89 -3
- package/cpp/data_dictionary_wrap.h +27 -5
- package/cpp/engine_common.h +108 -0
- package/cpp/engine_workers.h +99 -18
- package/cpp/field_map_ops.h +492 -0
- package/cpp/group_wrap.cpp +221 -0
- package/cpp/group_wrap.h +76 -0
- package/cpp/initiator_wrap.cpp +262 -25
- package/cpp/initiator_wrap.h +37 -2
- package/cpp/message_wrap.cpp +171 -78
- package/cpp/message_wrap.h +47 -5
- package/cpp/session_op_gate.h +201 -0
- package/cpp/session_static.cpp +139 -12
- package/cpp/session_wrap.cpp +438 -0
- package/cpp/session_wrap.h +144 -0
- package/dist/cjs/acceptor.d.ts +0 -1
- package/dist/cjs/acceptor.js +0 -1
- package/dist/cjs/application.d.ts +0 -1
- package/dist/cjs/application.js +0 -1
- package/dist/cjs/data-dictionary.d.ts +21 -3
- package/dist/cjs/data-dictionary.js +32 -4
- package/dist/cjs/engine.d.ts +42 -4
- package/dist/cjs/engine.js +50 -5
- package/dist/cjs/enums.d.ts +48 -14
- package/dist/cjs/enums.js +20 -25
- package/dist/cjs/field-value.d.ts +12 -0
- package/dist/cjs/field-value.js +40 -0
- package/dist/cjs/generated/fields.d.ts +0 -1
- package/dist/cjs/generated/fields.js +0 -1
- package/dist/cjs/generated/values.d.ts +8548 -0
- package/dist/cjs/generated/values.js +8580 -0
- package/dist/cjs/group.d.ts +126 -0
- package/dist/cjs/group.js +185 -0
- package/dist/cjs/index.d.ts +28 -5
- package/dist/cjs/index.js +30 -28
- package/dist/cjs/initiator.d.ts +0 -1
- package/dist/cjs/initiator.js +0 -1
- package/dist/cjs/message.d.ts +129 -5
- package/dist/cjs/message.js +146 -9
- package/dist/cjs/native.d.ts +179 -17
- package/dist/cjs/native.js +0 -1
- package/dist/cjs/session-id.d.ts +12 -1
- package/dist/cjs/session-id.js +21 -1
- package/dist/cjs/session-settings.d.ts +0 -1
- package/dist/cjs/session-settings.js +0 -1
- package/dist/cjs/session.d.ts +213 -0
- package/dist/cjs/session.js +337 -0
- package/dist/cjs/tls-tunnel.d.ts +143 -0
- package/dist/cjs/tls-tunnel.js +261 -0
- package/dist/esm/acceptor.d.ts +0 -1
- package/dist/esm/acceptor.js +0 -1
- package/dist/esm/application.d.ts +0 -1
- package/dist/esm/application.js +0 -1
- package/dist/esm/data-dictionary.d.ts +21 -3
- package/dist/esm/data-dictionary.js +32 -4
- package/dist/esm/engine.d.ts +42 -4
- package/dist/esm/engine.js +51 -6
- package/dist/esm/enums.d.ts +48 -14
- package/dist/esm/enums.js +14 -24
- package/dist/esm/field-value.d.ts +12 -0
- package/dist/esm/field-value.js +37 -0
- package/dist/esm/generated/fields.d.ts +0 -1
- package/dist/esm/generated/fields.js +0 -1
- package/dist/esm/generated/values.d.ts +8548 -0
- package/dist/esm/generated/values.js +8564 -0
- package/dist/esm/group.d.ts +126 -0
- package/dist/esm/group.js +181 -0
- package/dist/esm/index.d.ts +28 -5
- package/dist/esm/index.js +18 -26
- package/dist/esm/initiator.d.ts +0 -1
- package/dist/esm/initiator.js +0 -1
- package/dist/esm/message.d.ts +129 -5
- package/dist/esm/message.js +146 -9
- package/dist/esm/native.d.ts +179 -17
- package/dist/esm/native.js +0 -1
- package/dist/esm/session-id.d.ts +12 -1
- package/dist/esm/session-id.js +20 -1
- package/dist/esm/session-settings.d.ts +0 -1
- package/dist/esm/session-settings.js +0 -1
- package/dist/esm/session.d.ts +213 -0
- package/dist/esm/session.js +329 -0
- package/dist/esm/tls-tunnel.d.ts +143 -0
- package/dist/esm/tls-tunnel.js +256 -0
- package/package.json +13 -1
- package/prebuilds/darwin-arm64/node.napi.node +0 -0
- package/prebuilds/darwin-x64/node.napi.node +0 -0
- package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
- package/prebuilds/linux-x64/node.napi.musl.node +0 -0
- package/prebuilds/win32-x64/node.napi.node +0 -0
- package/cpp/enums.cpp +0 -66
- package/dist/cjs/acceptor.d.ts.map +0 -1
- package/dist/cjs/acceptor.js.map +0 -1
- package/dist/cjs/application.d.ts.map +0 -1
- package/dist/cjs/application.js.map +0 -1
- package/dist/cjs/data-dictionary.d.ts.map +0 -1
- package/dist/cjs/data-dictionary.js.map +0 -1
- package/dist/cjs/engine.d.ts.map +0 -1
- package/dist/cjs/engine.js.map +0 -1
- package/dist/cjs/enums.d.ts.map +0 -1
- package/dist/cjs/enums.js.map +0 -1
- package/dist/cjs/generated/fields.d.ts.map +0 -1
- package/dist/cjs/generated/fields.js.map +0 -1
- package/dist/cjs/index.d.ts.map +0 -1
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/initiator.d.ts.map +0 -1
- package/dist/cjs/initiator.js.map +0 -1
- package/dist/cjs/message.d.ts.map +0 -1
- package/dist/cjs/message.js.map +0 -1
- package/dist/cjs/native.d.ts.map +0 -1
- package/dist/cjs/native.js.map +0 -1
- package/dist/cjs/session-id.d.ts.map +0 -1
- package/dist/cjs/session-id.js.map +0 -1
- package/dist/cjs/session-settings.d.ts.map +0 -1
- package/dist/cjs/session-settings.js.map +0 -1
- package/dist/esm/acceptor.d.ts.map +0 -1
- package/dist/esm/acceptor.js.map +0 -1
- package/dist/esm/application.d.ts.map +0 -1
- package/dist/esm/application.js.map +0 -1
- package/dist/esm/data-dictionary.d.ts.map +0 -1
- package/dist/esm/data-dictionary.js.map +0 -1
- package/dist/esm/engine.d.ts.map +0 -1
- package/dist/esm/engine.js.map +0 -1
- package/dist/esm/enums.d.ts.map +0 -1
- package/dist/esm/enums.js.map +0 -1
- package/dist/esm/generated/fields.d.ts.map +0 -1
- package/dist/esm/generated/fields.js.map +0 -1
- package/dist/esm/index.d.ts.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/initiator.d.ts.map +0 -1
- package/dist/esm/initiator.js.map +0 -1
- package/dist/esm/message.d.ts.map +0 -1
- package/dist/esm/message.js.map +0 -1
- package/dist/esm/native.d.ts.map +0 -1
- package/dist/esm/native.js.map +0 -1
- package/dist/esm/session-id.d.ts.map +0 -1
- package/dist/esm/session-id.js.map +0 -1
- package/dist/esm/session-settings.d.ts.map +0 -1
- package/dist/esm/session-settings.js.map +0 -1
package/cpp/initiator_wrap.cpp
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#include "initiator_wrap.h"
|
|
2
2
|
|
|
3
|
+
#include <memory>
|
|
3
4
|
#include <string>
|
|
4
5
|
|
|
5
6
|
#include "quickfix/FileLog.h"
|
|
@@ -8,7 +9,10 @@
|
|
|
8
9
|
#include "engine_common.h"
|
|
9
10
|
#include "engine_workers.h"
|
|
10
11
|
#include "errors.h"
|
|
12
|
+
#include "session_id_wrap.h"
|
|
13
|
+
#include "session_op_gate.h"
|
|
11
14
|
#include "session_settings_wrap.h"
|
|
15
|
+
#include "session_wrap.h"
|
|
12
16
|
|
|
13
17
|
namespace napi_quickfix {
|
|
14
18
|
|
|
@@ -34,6 +38,8 @@ Napi::Object InitiatorWrap::Init(Napi::Env env, Napi::Object exports) {
|
|
|
34
38
|
InstanceMethod("start", &InitiatorWrap::Start),
|
|
35
39
|
InstanceMethod("stop", &InitiatorWrap::Stop),
|
|
36
40
|
InstanceMethod("isLoggedOn", &InitiatorWrap::IsLoggedOn),
|
|
41
|
+
InstanceMethod("getSessions", &InitiatorWrap::GetSessions),
|
|
42
|
+
InstanceMethod("getSession", &InitiatorWrap::GetSession),
|
|
37
43
|
InstanceMethod("ref", &InitiatorWrap::Ref),
|
|
38
44
|
InstanceMethod("unref", &InitiatorWrap::Unref),
|
|
39
45
|
});
|
|
@@ -77,16 +83,32 @@ InitiatorWrap::InitiatorWrap(const Napi::CallbackInfo& info)
|
|
|
77
83
|
storeFactory_ = std::make_unique<FIX::FileStoreFactory>(settings);
|
|
78
84
|
}
|
|
79
85
|
|
|
86
|
+
std::unique_ptr<FIX::LogFactory> logFactory;
|
|
80
87
|
if (log == "none") {
|
|
81
|
-
|
|
88
|
+
logFactory = std::make_unique<NullLogFactory>();
|
|
82
89
|
} else if (log == "file") {
|
|
83
|
-
|
|
90
|
+
logFactory = std::make_unique<FIX::FileLogFactory>(settings);
|
|
84
91
|
} else {
|
|
85
|
-
|
|
92
|
+
logFactory = std::make_unique<FIX::ScreenLogFactory>(settings);
|
|
86
93
|
}
|
|
94
|
+
logFactory_ = std::make_unique<TrackingLogFactory>(std::move(logFactory));
|
|
87
95
|
|
|
88
|
-
|
|
89
|
-
|
|
96
|
+
RejectDuplicateSessions(settings, "initiator");
|
|
97
|
+
try {
|
|
98
|
+
initiator_ = std::make_unique<FIX::SocketInitiator>(
|
|
99
|
+
*bridge_, *storeFactory_, settings, *logFactory_);
|
|
100
|
+
} catch (...) {
|
|
101
|
+
// A [SESSION] after the first valid one threw: QuickFIX has leaked the
|
|
102
|
+
// sessions it had already created (see DestroyOrphanedSessions) and the
|
|
103
|
+
// engine-level Log it requested before creating any of them (see
|
|
104
|
+
// TrackingLogFactory). Both would otherwise only have been reclaimed by
|
|
105
|
+
// the engine destructor that never runs.
|
|
106
|
+
DestroyOrphanedSessions(settings, "initiator");
|
|
107
|
+
logFactory_->DestroyOrphanedEngineLog();
|
|
108
|
+
throw;
|
|
109
|
+
}
|
|
110
|
+
sessionIDs_ = initiator_->getSessions();
|
|
111
|
+
SessionOpGate::Register(sessionIDs_, gate_);
|
|
90
112
|
} NQ_CATCH(env)
|
|
91
113
|
|
|
92
114
|
// Register a cleanup hook so teardown runs deterministically on the MAIN
|
|
@@ -114,6 +136,7 @@ InitiatorWrap::~InitiatorWrap() {
|
|
|
114
136
|
cleanupHook_.Remove(env_);
|
|
115
137
|
}
|
|
116
138
|
Teardown(true);
|
|
139
|
+
DestroyEngine();
|
|
117
140
|
}
|
|
118
141
|
|
|
119
142
|
void InitiatorWrap::Teardown(bool force) {
|
|
@@ -122,15 +145,33 @@ void InitiatorWrap::Teardown(bool force) {
|
|
|
122
145
|
// 1. Deactivate the bridge (set a flag, no TSFN op): any callback firing on
|
|
123
146
|
// a QuickFIX network thread during the upcoming stop() will pass through
|
|
124
147
|
// immediately without touching the TSFN or waiting on the (frozen) JS
|
|
125
|
-
// loop
|
|
126
|
-
//
|
|
127
|
-
//
|
|
128
|
-
//
|
|
129
|
-
|
|
148
|
+
// loop, and every BlockingCall already waiting is woken. This breaks
|
|
149
|
+
// the stop()->onLogout->BlockingCall deadlock.
|
|
150
|
+
// 2. Join the stop() worker if one is in flight: its operation may still
|
|
151
|
+
// be inside FIX::Initiator::stop() -- or waiting in gate->Freeze() for
|
|
152
|
+
// a sendToTarget() that step 1 has just unblocked -- on a libuv thread,
|
|
153
|
+
// and the destructor's DestroyEngine() that follows Teardown() must not
|
|
154
|
+
// delete the engine under it. (Node drains the thread pool before it
|
|
155
|
+
// runs cleanup hooks or finalizers, so this wait is expected to return
|
|
156
|
+
// at once; it makes the ordering a property of this code rather than
|
|
157
|
+
// of Node's shutdown sequence.) This is also why the early return is
|
|
158
|
+
// keyed on tornDown_, not stopped_: stopped_ is set for the whole
|
|
159
|
+
// duration of an in-flight stop().
|
|
160
|
+
// 3. stop(force): joins all QuickFIX network threads. A no-op if the
|
|
161
|
+
// worker's stop() completed (FIX::Initiator::isStopped()); the real
|
|
162
|
+
// stop if it threw part-way or no stop() was ever called. After it
|
|
163
|
+
// returns, no network thread is running, so none can race into the TSFN.
|
|
164
|
+
// 4. Abort the TSFN: safe now that no producer thread remains.
|
|
165
|
+
if (tornDown_) return;
|
|
166
|
+
tornDown_ = true;
|
|
130
167
|
stopped_ = true;
|
|
131
168
|
if (bridge_) {
|
|
132
169
|
bridge_->Deactivate();
|
|
133
170
|
}
|
|
171
|
+
if (stopSync_) {
|
|
172
|
+
stopSync_->Wait();
|
|
173
|
+
stopSync_.reset();
|
|
174
|
+
}
|
|
134
175
|
if (initiator_ && started_) {
|
|
135
176
|
try {
|
|
136
177
|
initiator_->stop(force);
|
|
@@ -142,6 +183,32 @@ void InitiatorWrap::Teardown(bool force) {
|
|
|
142
183
|
}
|
|
143
184
|
}
|
|
144
185
|
|
|
186
|
+
// Destroy the FIX engine (and with it every FIX::Session it owns) on the JS
|
|
187
|
+
// thread, with no session operation in flight on a libuv thread. Called from
|
|
188
|
+
// the destructor only; stop() uses DestroyStoppedEngine() below instead, as
|
|
189
|
+
// its worker has already Freeze()d the gate on the worker thread (see Stop()).
|
|
190
|
+
// The gate is per engine, so Freeze() here only waits for operations on THIS
|
|
191
|
+
// engine's sessions, and Teardown() has already deactivated its bridge and
|
|
192
|
+
// joined its network threads: none of those operations can be blocked on the
|
|
193
|
+
// JS thread (or on a session mutex), so Freeze() cannot deadlock. Operations
|
|
194
|
+
// on other engines -- which may well be blocked in a BlockingCall that needs
|
|
195
|
+
// this very thread -- are not waited for.
|
|
196
|
+
void InitiatorWrap::DestroyEngine() {
|
|
197
|
+
if (!initiator_) return;
|
|
198
|
+
gate_->Freeze();
|
|
199
|
+
SessionOpGate::Unregister(sessionIDs_, gate_.get());
|
|
200
|
+
initiator_.reset();
|
|
201
|
+
gate_->Unfreeze();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// The stop() path: the gate was frozen by the stop worker; destroy and lift
|
|
205
|
+
// the freeze. JS thread only.
|
|
206
|
+
void InitiatorWrap::DestroyStoppedEngine() {
|
|
207
|
+
SessionOpGate::Unregister(sessionIDs_, gate_.get());
|
|
208
|
+
initiator_.reset();
|
|
209
|
+
gate_->Unfreeze();
|
|
210
|
+
}
|
|
211
|
+
|
|
145
212
|
Napi::Value InitiatorWrap::Start(const Napi::CallbackInfo& info) {
|
|
146
213
|
Napi::Env env = info.Env();
|
|
147
214
|
auto deferred = Napi::Promise::Deferred::New(env);
|
|
@@ -167,11 +234,12 @@ Napi::Value InitiatorWrap::Start(const Napi::CallbackInfo& info) {
|
|
|
167
234
|
auto* worker = new EngineOpWorker(
|
|
168
235
|
env, info.This().As<Napi::Object>(),
|
|
169
236
|
[initiator]() { initiator->start(); },
|
|
170
|
-
[self](bool ok) {
|
|
237
|
+
[self](bool ok, const std::shared_ptr<EngineOpWorker::Settlement>& s) {
|
|
171
238
|
self->busy_ = false;
|
|
172
239
|
// If start failed, mark not-started so teardown won't try to stop an
|
|
173
240
|
// engine that never came up, and a later start() can retry.
|
|
174
241
|
if (!ok) self->started_ = false;
|
|
242
|
+
s->Settle();
|
|
175
243
|
});
|
|
176
244
|
Napi::Promise promise = worker->Promise();
|
|
177
245
|
worker->Queue();
|
|
@@ -185,6 +253,14 @@ Napi::Value InitiatorWrap::Stop(const Napi::CallbackInfo& info) {
|
|
|
185
253
|
force = info[0].ToBoolean().Value();
|
|
186
254
|
}
|
|
187
255
|
auto deferred = Napi::Promise::Deferred::New(env);
|
|
256
|
+
// A stop() is in flight (its worker is running, or a graceful stop is
|
|
257
|
+
// waiting for its queued 'logout' events to be dispatched before destroying
|
|
258
|
+
// the engine): join it. Resolving right away here -- `stopped_` is already
|
|
259
|
+
// set -- would break the README's promise that the sessions are gone once
|
|
260
|
+
// stop() settles. The first call's `force` applies to both.
|
|
261
|
+
if (!stopPromise_.IsEmpty()) {
|
|
262
|
+
return stopPromise_.Value();
|
|
263
|
+
}
|
|
188
264
|
if (stopped_) {
|
|
189
265
|
deferred.Resolve(env.Undefined());
|
|
190
266
|
return deferred.Promise();
|
|
@@ -195,8 +271,8 @@ Napi::Value InitiatorWrap::Stop(const Napi::CallbackInfo& info) {
|
|
|
195
271
|
.Value());
|
|
196
272
|
return deferred.Promise();
|
|
197
273
|
}
|
|
198
|
-
if (!initiator_
|
|
199
|
-
//
|
|
274
|
+
if (!initiator_) {
|
|
275
|
+
// Already torn down: just release the TSFN and resolve.
|
|
200
276
|
stopped_ = true;
|
|
201
277
|
if (bridge_) bridge_->Release();
|
|
202
278
|
deferred.Resolve(env.Undefined());
|
|
@@ -204,42 +280,203 @@ Napi::Value InitiatorWrap::Stop(const Napi::CallbackInfo& info) {
|
|
|
204
280
|
}
|
|
205
281
|
busy_ = true;
|
|
206
282
|
stopped_ = true;
|
|
283
|
+
// Lets Teardown() wait for the worker's operation instead of destroying the
|
|
284
|
+
// engine under it; cleared in onSettled once the operation has returned.
|
|
285
|
+
auto stopDone = std::make_shared<StopCompletion>();
|
|
286
|
+
stopSync_ = stopDone;
|
|
207
287
|
|
|
288
|
+
// A never-started engine takes the same async path (minus the stop() call):
|
|
289
|
+
// its FIX::Session objects are registered from construction, so a session
|
|
290
|
+
// operation may already be in flight against them and the destruction below
|
|
291
|
+
// must wait for it just the same.
|
|
208
292
|
FIX::SocketInitiator* initiator = initiator_.get();
|
|
209
293
|
ApplicationBridge* bridge = bridge_.get();
|
|
294
|
+
const bool started = started_;
|
|
295
|
+
std::shared_ptr<SessionOpGate> gate = gate_;
|
|
210
296
|
auto* self = this;
|
|
211
297
|
auto* worker = new EngineOpWorker(
|
|
212
298
|
env, info.This().As<Napi::Object>(),
|
|
213
|
-
//
|
|
299
|
+
// Runs on the worker thread. stop()'s onLogout/toAdmin callbacks fire on
|
|
214
300
|
// QuickFIX threads while the main loop is free to service the TSFN.
|
|
215
|
-
[initiator, force]() {
|
|
216
|
-
|
|
301
|
+
[initiator, gate, started, force, stopDone]() {
|
|
302
|
+
// Signal completion on every exit of this lambda, success or throw,
|
|
303
|
+
// and only after the gate freeze below has returned as well.
|
|
304
|
+
struct SignalOnExit {
|
|
305
|
+
std::shared_ptr<StopCompletion> done;
|
|
306
|
+
~SignalOnExit() { done->Signal(); }
|
|
307
|
+
} signalOnExit{stopDone};
|
|
308
|
+
try {
|
|
309
|
+
if (started) initiator->stop(force);
|
|
310
|
+
} catch (...) {
|
|
311
|
+
gate->Freeze();
|
|
312
|
+
throw;
|
|
313
|
+
}
|
|
314
|
+
// stop() has joined the network threads. Now block new session
|
|
315
|
+
// operations (SendToTargetWorker) on THIS engine's
|
|
316
|
+
// sessions and wait for the in-flight ones to finish: they hold raw
|
|
317
|
+
// FIX::Session pointers that the destruction below is about to
|
|
318
|
+
// invalidate. Waiting HERE, on the worker thread, keeps the JS loop
|
|
319
|
+
// free for any BlockingCall such an operation is still making (e.g.
|
|
320
|
+
// sendToTarget() -> toApp). Other engines' operations are unaffected.
|
|
321
|
+
gate->Freeze();
|
|
322
|
+
},
|
|
323
|
+
[self, bridge, env, force, gate](
|
|
324
|
+
bool ok, const std::shared_ptr<EngineOpWorker::Settlement>& s) {
|
|
217
325
|
self->busy_ = false;
|
|
218
|
-
//
|
|
219
|
-
//
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
326
|
+
self->stopSync_.reset(); // the worker's operation has returned
|
|
327
|
+
// Settles the promise and lets a later stop() start over (the
|
|
328
|
+
// in-flight stop is finished, successfully or not).
|
|
329
|
+
auto settle = [self, s]() {
|
|
330
|
+
self->stopPromise_.Reset();
|
|
331
|
+
s->Settle();
|
|
332
|
+
};
|
|
333
|
+
if (!ok) {
|
|
334
|
+
// stop() threw part-way: the network threads may still be running,
|
|
335
|
+
// so the engine is NOT safe to destroy. Keep it, let session
|
|
336
|
+
// operations through again and clear stopped_ so that a later
|
|
337
|
+
// stop() -- or the destructor's Teardown() -- retries. Mirrors
|
|
338
|
+
// Start() clearing started_ on failure. The bridge is released (or
|
|
339
|
+
// aborted) as on success, so a failed stop() does not keep the
|
|
340
|
+
// event loop alive: the TSFN holds a loop reference until it is
|
|
341
|
+
// released, and a script that catches the rejection and falls off
|
|
342
|
+
// the end would otherwise never exit. From here on the engine's
|
|
343
|
+
// callbacks pass through without reaching JS; the retried stop()
|
|
344
|
+
// then destroys the engine as soon as its worker settles, since
|
|
345
|
+
// RunAfterQueued() refuses a released bridge.
|
|
346
|
+
self->stopped_ = false;
|
|
347
|
+
self->gate_->Unfreeze();
|
|
348
|
+
if (bridge) {
|
|
349
|
+
if (force) {
|
|
350
|
+
bridge->Abort();
|
|
351
|
+
} else {
|
|
352
|
+
bridge->Release();
|
|
353
|
+
}
|
|
225
354
|
}
|
|
355
|
+
settle();
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
// stop() has completed: the network threads are joined and session
|
|
359
|
+
// operations are frozen. Destroy the engine on the JS thread rather
|
|
360
|
+
// than at GC: QuickFIX only deletes its FIX::Session objects -- and
|
|
361
|
+
// removes them from the process-wide registry that lookupSession() /
|
|
362
|
+
// sendToTarget() read -- in the engine destructor. Deferring that to GC
|
|
363
|
+
// would leave stale Session handles resolvable and make a new engine
|
|
364
|
+
// with the same SessionIDs fail with ConfigError "Duplicate Session"
|
|
365
|
+
// until the old wrap happened to be collected. A stopped engine cannot
|
|
366
|
+
// be restarted anyway (start() rejects once stopped_ is set).
|
|
367
|
+
if (force || bridge == nullptr) {
|
|
368
|
+
// Force: abort the TSFN, discarding any callback still queued (a
|
|
369
|
+
// 'logout' listener may therefore never run), and destroy right away.
|
|
370
|
+
if (bridge) bridge->Abort();
|
|
371
|
+
self->DestroyStoppedEngine();
|
|
372
|
+
settle();
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
// Graceful: the 'logout' events stop() fired are still queued in the
|
|
376
|
+
// bridge, and their listeners may read the session they name
|
|
377
|
+
// (getSession() / Session handles are documented to stay live until
|
|
378
|
+
// stop() settles). Nothing orders the TSFN's dispatch of those events
|
|
379
|
+
// before this AsyncWorker completion, so destroy the engine -- and
|
|
380
|
+
// settle the promise -- only once they have been dispatched: queue the
|
|
381
|
+
// destruction through the bridge's own FIFO behind them, then release
|
|
382
|
+
// the TSFN so it closes once drained. The owner reference held by the
|
|
383
|
+
// settlement keeps this wrap alive until then.
|
|
384
|
+
const bool queued =
|
|
385
|
+
bridge->RunAfterQueued(env, [self, s, settle, gate](Napi::Env e) {
|
|
386
|
+
if (e == nullptr) {
|
|
387
|
+
// Discarded: the environment is being torn down. The wrap's
|
|
388
|
+
// own teardown (cleanup hook, then destructor) destroys the
|
|
389
|
+
// engine; touch nothing but the settlement and the gate,
|
|
390
|
+
// which this closure owns (the gate by shared_ptr). In
|
|
391
|
+
// particular do not reach through `self`: what keeps the
|
|
392
|
+
// wrap alive until here is the TSFN's reference on the
|
|
393
|
+
// napi_env (the env -- and with it every ObjectWrap
|
|
394
|
+
// finalizer -- is deleted only after the TSFN has dropped
|
|
395
|
+
// its queue), a Node internal this code must not lean on.
|
|
396
|
+
// The promise reference needs no special care: the wrap
|
|
397
|
+
// destructor deletes it, and runs with the env valid.
|
|
398
|
+
//
|
|
399
|
+
// The freeze the stop worker took is still held: lift it,
|
|
400
|
+
// as DestroyStoppedEngine() would have. A SendToTargetWorker
|
|
401
|
+
// parked in OpScope since then is otherwise never woken, and
|
|
402
|
+
// the destructor's own DestroyEngine() -- which nests its
|
|
403
|
+
// freeze inside this one -- never brings the count to zero
|
|
404
|
+
// either; the libuv pool thread it sits on would be parked
|
|
405
|
+
// for good, and whoever drains the pool at exit (a Worker's
|
|
406
|
+
// terminate(), the process teardown) would hang on it. Once
|
|
407
|
+
// woken it runs against the engine, still intact at this
|
|
408
|
+
// point (the bridge is inactive, so a callback it makes
|
|
409
|
+
// passes through), or is refused by Acquire() if the engine
|
|
410
|
+
// has already been unregistered; the destructor's Freeze()
|
|
411
|
+
// waits for it either way.
|
|
412
|
+
s->Abandon();
|
|
413
|
+
gate->Unfreeze();
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
self->DestroyStoppedEngine();
|
|
417
|
+
settle();
|
|
418
|
+
});
|
|
419
|
+
bridge->Release();
|
|
420
|
+
if (!queued) {
|
|
421
|
+
self->DestroyStoppedEngine();
|
|
422
|
+
settle();
|
|
226
423
|
}
|
|
227
424
|
});
|
|
228
425
|
Napi::Promise promise = worker->Promise();
|
|
426
|
+
stopPromise_ = Napi::Persistent(static_cast<Napi::Object>(promise));
|
|
229
427
|
worker->Queue();
|
|
230
428
|
return promise;
|
|
231
429
|
}
|
|
232
430
|
|
|
431
|
+
// isLoggedOn(sessionID?): with no argument, true if ANY session of this engine
|
|
432
|
+
// is logged on (FIX::Initiator/Acceptor::isLoggedOn); with a SessionID, true
|
|
433
|
+
// only if THAT session belongs to this engine and is logged on. Always false
|
|
434
|
+
// before start() and once stop() has settled; while a graceful stop() is in
|
|
435
|
+
// progress it still reports the live state (the engine is only destroyed when
|
|
436
|
+
// the stop() promise settles).
|
|
233
437
|
Napi::Value InitiatorWrap::IsLoggedOn(const Napi::CallbackInfo& info) {
|
|
234
438
|
Napi::Env env = info.Env();
|
|
235
|
-
|
|
439
|
+
const bool hasId = info.Length() >= 1 && !info[0].IsUndefined();
|
|
440
|
+
// Validate the argument even when the engine is down, for a stable API.
|
|
441
|
+
SessionIDWrap* id =
|
|
442
|
+
hasId ? SessionIDWrap::UnwrapArg(env, info[0], "sessionID") : nullptr;
|
|
443
|
+
if (!initiator_ || !started_) {
|
|
236
444
|
return Napi::Boolean::New(env, false);
|
|
237
445
|
}
|
|
238
446
|
NQ_TRY(env) {
|
|
239
|
-
|
|
447
|
+
if (id == nullptr) {
|
|
448
|
+
return Napi::Boolean::New(env, initiator_->isLoggedOn());
|
|
449
|
+
}
|
|
450
|
+
FIX::Session* session = initiator_->getSession(id->SessionID());
|
|
451
|
+
return Napi::Boolean::New(env,
|
|
452
|
+
session != nullptr && session->isLoggedOn());
|
|
240
453
|
} NQ_CATCH(env)
|
|
241
454
|
}
|
|
242
455
|
|
|
456
|
+
// getSessions(): SessionID[] -- the sessions this engine was configured with.
|
|
457
|
+
Napi::Value InitiatorWrap::GetSessions(const Napi::CallbackInfo& info) {
|
|
458
|
+
Napi::Env env = info.Env();
|
|
459
|
+
Napi::Array out = Napi::Array::New(env, sessionIDs_.size());
|
|
460
|
+
uint32_t i = 0;
|
|
461
|
+
for (const FIX::SessionID& sid : sessionIDs_) {
|
|
462
|
+
out.Set(i++, SessionIDWrap::NewInstance(env, sid));
|
|
463
|
+
}
|
|
464
|
+
return out;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// getSession(sessionID): Session | undefined -- undefined if the id is not one
|
|
468
|
+
// of this engine's sessions or stop() has settled (its sessions are destroyed
|
|
469
|
+
// then, see Stop()). While stop() is still in progress the sessions are alive
|
|
470
|
+
// and a handle is returned, e.g. for a 'logout' listener reading final seq nums.
|
|
471
|
+
Napi::Value InitiatorWrap::GetSession(const Napi::CallbackInfo& info) {
|
|
472
|
+
Napi::Env env = info.Env();
|
|
473
|
+
SessionIDWrap* id = SessionIDWrap::UnwrapArg(env, info[0], "sessionID");
|
|
474
|
+
if (!initiator_ || !initiator_->has(id->SessionID())) {
|
|
475
|
+
return env.Undefined();
|
|
476
|
+
}
|
|
477
|
+
return SessionWrap::NewInstance(env, id->SessionID(), gate_);
|
|
478
|
+
}
|
|
479
|
+
|
|
243
480
|
Napi::Value InitiatorWrap::Ref(const Napi::CallbackInfo& info) {
|
|
244
481
|
Napi::Env env = info.Env();
|
|
245
482
|
if (bridge_) bridge_->Ref(env);
|
package/cpp/initiator_wrap.h
CHANGED
|
@@ -4,12 +4,17 @@
|
|
|
4
4
|
#include <napi.h>
|
|
5
5
|
|
|
6
6
|
#include <memory>
|
|
7
|
+
#include <set>
|
|
7
8
|
|
|
8
9
|
#include "quickfix/Log.h"
|
|
9
10
|
#include "quickfix/MessageStore.h"
|
|
11
|
+
#include "quickfix/SessionID.h"
|
|
10
12
|
#include "quickfix/SocketInitiator.h"
|
|
11
13
|
|
|
12
14
|
#include "application_bridge.h"
|
|
15
|
+
#include "engine_common.h"
|
|
16
|
+
#include "engine_workers.h"
|
|
17
|
+
#include "session_op_gate.h"
|
|
13
18
|
|
|
14
19
|
namespace napi_quickfix {
|
|
15
20
|
|
|
@@ -19,7 +24,8 @@ namespace napi_quickfix {
|
|
|
19
24
|
// store: 'file' | 'memory' (default 'file')
|
|
20
25
|
// log: 'screen' | 'file' | 'none' (default 'screen')
|
|
21
26
|
//
|
|
22
|
-
// Methods: start / stop(force?) / isLoggedOn /
|
|
27
|
+
// Methods: start / stop(force?) / isLoggedOn(sessionID?) / getSessions /
|
|
28
|
+
// getSession(sessionID) / ref / unref.
|
|
23
29
|
class InitiatorWrap : public Napi::ObjectWrap<InitiatorWrap> {
|
|
24
30
|
public:
|
|
25
31
|
static Napi::Object Init(Napi::Env env, Napi::Object exports);
|
|
@@ -32,22 +38,51 @@ class InitiatorWrap : public Napi::ObjectWrap<InitiatorWrap> {
|
|
|
32
38
|
Napi::Value Start(const Napi::CallbackInfo& info);
|
|
33
39
|
Napi::Value Stop(const Napi::CallbackInfo& info);
|
|
34
40
|
Napi::Value IsLoggedOn(const Napi::CallbackInfo& info);
|
|
41
|
+
Napi::Value GetSessions(const Napi::CallbackInfo& info);
|
|
42
|
+
Napi::Value GetSession(const Napi::CallbackInfo& info);
|
|
35
43
|
Napi::Value Ref(const Napi::CallbackInfo& info);
|
|
36
44
|
Napi::Value Unref(const Napi::CallbackInfo& info);
|
|
37
45
|
|
|
38
46
|
void Teardown(bool force);
|
|
47
|
+
// Destroy the FIX engine under gate_->Freeze(); destructor only.
|
|
48
|
+
void DestroyEngine();
|
|
49
|
+
// Destroy the FIX engine once stop() has frozen the gate; Stop() only.
|
|
50
|
+
void DestroyStoppedEngine();
|
|
39
51
|
|
|
40
52
|
// Declaration order matters: members destruct in REVERSE order, so the
|
|
41
53
|
// initiator (declared last) is destroyed FIRST, before the factories and
|
|
42
54
|
// bridge it references.
|
|
43
55
|
std::unique_ptr<ApplicationBridge> bridge_;
|
|
44
56
|
std::unique_ptr<FIX::MessageStoreFactory> storeFactory_;
|
|
45
|
-
|
|
57
|
+
// Wraps the real factory so a failed engine constructor can reclaim the
|
|
58
|
+
// engine-level Log QuickFIX would otherwise leak (see engine_common.h).
|
|
59
|
+
std::unique_ptr<TrackingLogFactory> logFactory_;
|
|
60
|
+
// Gate between this engine's destruction and the libuv-thread session
|
|
61
|
+
// operations on ITS sessions (see session_op_gate.h). Shared with the stop
|
|
62
|
+
// worker; registered for sessionIDs_ at construction, unregistered when the
|
|
63
|
+
// engine is destroyed.
|
|
64
|
+
std::shared_ptr<SessionOpGate> gate_ = std::make_shared<SessionOpGate>();
|
|
46
65
|
std::unique_ptr<FIX::SocketInitiator> initiator_;
|
|
47
66
|
|
|
67
|
+
// The SessionIDs this engine was configured with, copied at construction so
|
|
68
|
+
// getSessions() still answers after stop() has destroyed the engine.
|
|
69
|
+
std::set<FIX::SessionID> sessionIDs_;
|
|
70
|
+
|
|
48
71
|
bool started_ = false;
|
|
72
|
+
// Set by stop() when it launches its worker (so start() rejects from then
|
|
73
|
+
// on) and by Teardown(); cleared again if that stop() failed. It is NOT
|
|
74
|
+
// "the engine is stopped": a stop() may be in flight.
|
|
49
75
|
bool stopped_ = false;
|
|
76
|
+
bool tornDown_ = false; // Teardown() has run (it runs once)
|
|
50
77
|
bool busy_ = false; // guards against overlapping start/stop AsyncWorkers
|
|
78
|
+
// Signalled by the in-flight stop() worker once its operation has returned;
|
|
79
|
+
// non-null from Stop() launching the worker until that stop settles on the
|
|
80
|
+
// JS thread. Teardown() waits on it (see there).
|
|
81
|
+
std::shared_ptr<StopCompletion> stopSync_;
|
|
82
|
+
// The promise of the stop() in flight, from the call until it settles
|
|
83
|
+
// (which, for a graceful stop, is after its worker's OnOK). A stop() made
|
|
84
|
+
// meanwhile returns this same promise instead of resolving early.
|
|
85
|
+
Napi::ObjectReference stopPromise_;
|
|
51
86
|
|
|
52
87
|
static void CleanupEntry(InitiatorWrap* self);
|
|
53
88
|
|