Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS.emscripten: adds connect callbacks to the ports. #602

Merged
merged 1 commit into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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