How to setup bidirectional communication with worker thread? #1512
-
I would like to use a separate thread which sends and receives data via a websocket. Apparently mg_mkpipe() is not the right thing to use: #1511. What works is creating a worker thread and a struct mg_connection *mg_mkfdconn(struct mg_mgr *mgr, int fd,
mg_event_handler_t fn, void *fn_data) {
struct mg_connection *c = mg_alloc_conn(mgr);
if (c != NULL) {
MG_DEBUG(("mkfdconn %d", fd));
c->fd = S2PTR(fd);
c->pfn = pf1;
c->pfn_data = S2PTR(fd);
c->fn = fn;
c->fn_data = fn_data;
mg_call(c, MG_EV_OPEN, NULL);
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
}
return c;
} In MG_EV_WS_OPEN I simply assign the new connection to the websocket connection: In the MG_EV_CLOSE I simply call |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 9 replies
-
Thanks for your code sniplet. Do you have a complete example somewhere? I would definitely encourage to add that as an example to Mongoose. The current web socket server example is not very helpful for how web sockets are usually used. Thx! |
Beta Was this translation helpful? Give feedback.
-
@sebhub can you elaborate on a use case when such thing is useful, please? Or even as @apuder suggested, share an example? Adding such function won't be an issue, provided it serves a relatively general use case. |
Beta Was this translation helpful? Give feedback.
-
@sebhub Thank you. A somewhat similar mechanism was employed in a recent versions of 7.x see https://github.com/cesanta/mongoose/blob/7.0/examples/multi-threaded/main.c . There, there was no special API, just communication over a socket pair. There was no mgr wakeup, but polling instead. The communication was bidirectional though, however over the UDP pair to preserve message boundaries. The existing API does practically the very same thing as your code does: creates a socket pair, wraps one socket into the We can change the API and leave a wrapped connection in the Would that work for your case? Side note: your WS connection may terminate. An associated fdc must be signalled (ws pointer cleared), otherwise |
Beta Was this translation helpful? Give feedback.
-
@sebhub I think you did not get the point, and did not follow the change. The mt branch has a refactored mg_mkpipe |
Beta Was this translation helpful? Give feedback.
-
You're correct about the local variable - amended PR, passing socket by value. Please take a look. |
Beta Was this translation helpful? Give feedback.
-
We can't use a generic file descriptor, and rely on the sockepair syscall because it wont't work everywhere. What's the issue with an UDP socketpair? On a loopback connection, an in-order data IO is guaranteed. FYI - branch amended, |
Beta Was this translation helpful? Give feedback.
We can't use a generic file descriptor, and rely on the sockepair syscall because it wont't work everywhere.
The API should be functional on all platforms that support BSD sockets, not only on unix-like OSs.
For example, on windows there is no socketpair. Also, you can't
select()
on non-socket fd on windows.What's the issue with an UDP socketpair? On a loopback connection, an in-order data IO is guaranteed.
Also, UDP provides a nice property : if you send a data structure, you'll receive it un-fragmented.
So far I don't see any disadvantages in a UDP socketpair.
There might be one though: if you send data larger than a kernel socket buffer size, it might be dropped, I am not sure what
se…