Skip to content

Commit

Permalink
JS.emscripten: adds connect callbacks to the ports. (#602)
Browse files Browse the repository at this point in the history
This allows delaying the stack start until the asynchronous connections succeed.
  • Loading branch information
balazsracz authored Jan 29, 2022
1 parent 456945f commit ea2fd25
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/utils/JSHubPort.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@

#ifdef __EMSCRIPTEN__

#include <functional>

extern int JSHubPort_debug_port_num;
int JSHubPort_debug_port_num = 0;

/// Invokes a function pointer.
extern "C" void __attribute__((used)) invoke_fnp(std::function<void()> *fp)
{
if (fp && *fp)
{
(*fp)();
}
}

#endif // __EMSCRIPTEN__
13 changes: 11 additions & 2 deletions src/utils/JSSerialPort.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@
class JSSerialPort
{
public:
JSSerialPort(CanHubFlow *hflow, string device)
/// Constructor
/// @param hflow the CAN hub object in the local binary to add this port to.
/// @param device the serial device name (see list_ports output)
/// @param cb will be invoked when the connection succeeds
JSSerialPort(CanHubFlow *hflow, string device,
std::function<void()> cb = []() {})
: canHub_(hflow)
, connectCallback_(std::move(cb))
{
string script = "Module.serial_device = '" + device + "';\n";
emscripten_run_script(script.c_str());
Expand Down Expand Up @@ -96,9 +102,10 @@ public:
client_port.abandon();
});
c.on('data', function(data) { client_port.recv(data.toString()); });
_invoke_fnp($1);
});
},
(unsigned long)canHub_);
(unsigned long)canHub_, (unsigned long)&connectCallback_);
}

static void list_ports() {
Expand All @@ -119,6 +126,8 @@ public:

private:
CanHubFlow *canHub_;
/// This function will be invoked when the connection succeeds.
std::function<void()> connectCallback_;
};

#endif // __EMSCRIPTEN__
Expand Down
16 changes: 14 additions & 2 deletions src/utils/JSTcpClient.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,20 @@
class JSTcpClient
{
public:
JSTcpClient(CanHubFlow *hflow, string host, int port)
/// Constructor
/// @param hflow the CAN hub object in the local binary to add this port to.
/// @param host the IP address or name of the remote host
/// @param port the TCP port number to connect to
/// @param cb will be invoked when the connection succeeds
JSTcpClient(
CanHubFlow *hflow, string host, int port,
std::function<void()> cb = []() {})
: canHub_(hflow)
, connectCallback_(std::move(cb))
{
string script = "Module.remote_server = '" + host + "';\n";
emscripten_run_script(script.c_str());

EM_ASM_(
{
var net = require('net');
Expand All @@ -73,13 +82,16 @@ public:
client_port.abandon();
});
c.on('data', function(data) { client_port.recv(data); });
_invoke_fnp($2);
});
},
port, (unsigned long)canHub_);
port, (unsigned long)canHub_, (unsigned long)&connectCallback_);
}

private:
CanHubFlow *canHub_;
/// This function will be invoked when the connection succeeds.
std::function<void()> connectCallback_;
};

#endif // __EMSCRIPTEN__
Expand Down

0 comments on commit ea2fd25

Please sign in to comment.