Skip to content

Commit a2df10a

Browse files
committed
adsb: Fix beast, raw websockets deleting state improperly
1 parent 3f1aee8 commit a2df10a

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

phy_adsb.cc

+28-13
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,18 @@ kis_adsb_phy::kis_adsb_phy(int in_phyid) :
266266
try {
267267
ss >> device_json;
268268

269-
auto adsb_content = hex_to_bytes(device_json["adsb_raw_msg"]);
269+
std::string adsb_content;
270+
271+
auto hex_j = device_json["adsb"];
272+
273+
if (hex_j.is_null() || !hex_j.is_string()) {
274+
hex_j = device_json["adsb_raw_msg"];
275+
adsb_content = hex_to_bytes(hex_j);
276+
} else {
277+
const auto stradsb = hex_j.get<std::string>();
278+
adsb_content = hex_to_bytes(stradsb.substr(1, stradsb.size() - 2));
279+
}
280+
270281

271282
if (adsb_content.size() != 7 && adsb_content.size() != 14) {
272283
_MSG_DEBUG("unexpected content length {}", adsb_content.size());
@@ -301,11 +312,9 @@ kis_adsb_phy::kis_adsb_phy(int in_phyid) :
301312
delete[] buf;
302313

303314
} catch (std::exception& e) {
304-
delete uptr;
305315
return 0;
306316
}
307317

308-
delete uptr;
309318
return 1;
310319
}, uptr, CHAINPOS_LOGGING, 1000);
311320

@@ -318,6 +327,7 @@ kis_adsb_phy::kis_adsb_phy(int in_phyid) :
318327
}
319328

320329
packetchain->remove_handler(beast_handler_id, CHAINPOS_LOGGING);
330+
delete uptr;
321331
}));
322332

323333
httpd->register_websocket_route("/phy/ADSB/raw", {httpd->RO_ROLE, "ADSB"}, {"ws"},
@@ -327,7 +337,7 @@ kis_adsb_phy::kis_adsb_phy(int in_phyid) :
327337
auto ws =
328338
std::make_shared<kis_net_web_websocket_endpoint>(con,
329339
[](std::shared_ptr<kis_net_web_websocket_endpoint> ws,
330-
boost::beast::flat_buffer& buf, bool text) {
340+
boost::beast::flat_buffer& buf, bool text) mutable {
331341
// Do nothing on input
332342
});
333343

@@ -341,7 +351,7 @@ kis_adsb_phy::kis_adsb_phy(int in_phyid) :
341351
uptr->ws = ws;
342352
uptr->adsb = this;
343353

344-
auto beast_handler_id =
354+
auto raw_handler_id =
345355
packetchain->register_handler(
346356
[](void *auxdata, const std::shared_ptr<kis_packet>& in_pack) -> int {
347357

@@ -364,16 +374,20 @@ kis_adsb_phy::kis_adsb_phy(int in_phyid) :
364374
try {
365375
ss >> device_json;
366376

377+
auto hex_j = device_json["adsb"];
378+
379+
if (hex_j.is_null() || !hex_j.is_string()) {
380+
hex_j = device_json["adsb_raw_msg"];
381+
}
382+
367383
auto adsb_content =
368-
fmt::format("*{};\n", device_json["adsb_raw_msg"].get<std::string>());
384+
fmt::format("{}\n", hex_j.get<std::string>());
369385

370386
uptr->ws->write(adsb_content);
371387
} catch (std::exception& e) {
372-
delete uptr;
373388
return 0;
374389
}
375390

376-
delete uptr;
377391
return 1;
378392
}, uptr, CHAINPOS_LOGGING, 1000);
379393

@@ -384,8 +398,9 @@ kis_adsb_phy::kis_adsb_phy(int in_phyid) :
384398
} catch (const std::exception& e) {
385399
;
386400
}
387-
388-
packetchain->remove_handler(beast_handler_id, CHAINPOS_LOGGING);
401+
402+
packetchain->remove_handler(raw_handler_id, CHAINPOS_LOGGING);
403+
delete(uptr);
389404
}));
390405

391406
httpd->register_websocket_route("/datasource/by-uuid/:uuid/adsb_raw", {httpd->RO_ROLE, "ADSB"}, {"ws"},
@@ -417,7 +432,7 @@ kis_adsb_phy::kis_adsb_phy(int in_phyid) :
417432
uptr->adsb = this;
418433
uptr->srcuuid = srcuuid;
419434

420-
auto beast_handler_id =
435+
auto raw_handler_id =
421436
packetchain->register_handler(
422437
[](void *auxdata, const std::shared_ptr<kis_packet>& in_pack) -> int {
423438

@@ -449,7 +464,7 @@ kis_adsb_phy::kis_adsb_phy(int in_phyid) :
449464
ss >> device_json;
450465

451466
auto adsb_content =
452-
fmt::format("*{};\n", device_json["adsb_raw_msg"].get<std::string>());
467+
fmt::format("*{};\n", device_json["adsb"].get<std::string>());
453468

454469
uptr->ws->write(adsb_content);
455470
} catch (std::exception& e) {
@@ -469,7 +484,7 @@ kis_adsb_phy::kis_adsb_phy(int in_phyid) :
469484
;
470485
}
471486

472-
packetchain->remove_handler(beast_handler_id, CHAINPOS_LOGGING);
487+
packetchain->remove_handler(raw_handler_id, CHAINPOS_LOGGING);
473488
}));
474489

475490
}

0 commit comments

Comments
 (0)