35#ifndef _UTILS_JSHUBPORT_HXX_
36#define _UTILS_JSHUBPORT_HXX_
40#include <emscripten.h>
41#include <emscripten/bind.h>
42#include <emscripten/val.h>
50 virtual void on_open() {}
53 virtual void on_close() {}
56 virtual void on_error(
string error) {}
58 static void call_on_error(
unsigned long p,
string error)
60 ((JSHubFeedback *)p)->on_error(error);
67 JSHubPort(
unsigned long parent, emscripten::val send_fn,
unsigned long feedback = 0)
68 : parent_(reinterpret_cast<
CanHubFlow *>(parent))
69 , feedback_(reinterpret_cast<JSHubFeedback *>(feedback))
71 , gcHub_(parent_->service())
73 GCAdapterBase::CreateGridConnectAdapter(&gcHub_, parent_, false))
75 extern int JSHubPort_debug_port_num;
76 myPortNum_ = JSHubPort_debug_port_num++;
77 HASSERT(sendFn_.typeof().as<std::string>() ==
"function");
78 gcHub_.register_port(
this);
95 feedback_->on_close();
99 void fb_error(
string e)
103 feedback_->on_error(e);
110 if (gcAdapter_.get() ==
nullptr && !gcHub_.is_waiting())
120 gcHub_.unregister_port(
this);
121 if (gcAdapter_->shutdown())
133 gcHub_.register_port(
this);
135 &gcHub_, parent_,
false));
141 unsigned priority = UINT_MAX)
OVERRIDE
143 sendFn_((
string &)*buffer->data());
149 auto *b = gcHub_.alloc();
150 b->data()->assign(s);
151 b->data()->skipMember_ =
this;
161 JSHubFeedback* feedback_;
162 emscripten::val sendFn_;
164 std::unique_ptr<GCAdapterBase> gcAdapter_;
169EMSCRIPTEN_BINDINGS(js_hub_module)
171 emscripten::class_<JSHubPort>(
"JSHubPort")
173 .constructor<
unsigned long, emscripten::val,
unsigned long>()
174 .function(
"recv", &JSHubPort::recv)
175 .function(
"pause", &JSHubPort::pause)
176 .function(
"abandon", &JSHubPort::abandon)
177 .function(
"get_port_num", &JSHubPort::get_port_num)
178 .function(
"resume", &JSHubPort::resume);
180 emscripten::class_<JSHubFeedback>(
"JSHubFeedback")
181 .class_function(
"call_on_error", &JSHubFeedback::call_on_error);
ssize_t recv(int socket, void *buffer, size_t length, int flags)
Receive a message from a connection-mode or connectionless-mode socket.
Base class for all QMember types that hold data in an expandable format.
virtual void send(Buffer< HubData > *message, unsigned priority=UINT_MAX)=0
Entry point to the flow.
Publicly visible API for the gridconnect-to-CAN bridge.
static GCAdapterBase * CreateGridConnectAdapter(HubFlow *gc_side, CanHubFlow *can_side, bool double_bytes)
This function connects an ASCII (GridConnect-format) CAN adapter to a binary CAN adapter,...
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.