diff mbox series

[04/23] mongoose: Forward port mg_mkpipe()

Message ID 20240612144010.470339-5-Michael.Glembotzki@iris-sensing.com
State Changes Requested
Headers show
Series Update Mongoose Version to 7.14 | expand

Commit Message

Michael Glembotzki June 12, 2024, 2:36 p.m. UTC
From: Michael Glembotzki <m.glembo@gmail.com>

The generic function mg_mkpipe is deprecated with 7.12. In order to keep
the change effort as low as possible, the forward port is done in the
mongoose sources themselves.

mongoose rev-id's:
- b1c220e6102bc52656bb2f6a5db17f7c5c51ecf3
- d56152875d3962b57ad8bc1f2774cfed6722e38b

Signed-off-by: Michael Glembotzki <Michael.Glembotzki@iris-sensing.com>
---
 mongoose/mongoose.c | 17 +++++++++++++++++
 mongoose/mongoose.h |  1 +
 2 files changed, 18 insertions(+)

Comments

James Hilliard June 13, 2024, 7:27 a.m. UTC | #1
On Wed, Jun 12, 2024 at 8:40 AM Michael Glembotzki <m.glembo@gmail.com> wrote:
>
> From: Michael Glembotzki <m.glembo@gmail.com>
>
> The generic function mg_mkpipe is deprecated with 7.12. In order to keep
> the change effort as low as possible, the forward port is done in the
> mongoose sources themselves.

Why not just use mg_wakeup instead? It seems like it should work with
minimal changes and should let us avoid having to modify the mongoose
sources.

Docs:
https://mongoose.ws/documentation/#mg_wakeup

Example:
https://github.com/cesanta/mongoose/blob/7.14/tutorials/core/multi-threaded/main.c

>
> mongoose rev-id's:
> - b1c220e6102bc52656bb2f6a5db17f7c5c51ecf3
> - d56152875d3962b57ad8bc1f2774cfed6722e38b
>
> Signed-off-by: Michael Glembotzki <Michael.Glembotzki@iris-sensing.com>
> ---
>  mongoose/mongoose.c | 17 +++++++++++++++++
>  mongoose/mongoose.h |  1 +
>  2 files changed, 18 insertions(+)
>
> diff --git a/mongoose/mongoose.c b/mongoose/mongoose.c
> index 44066893..9e0bb6fb 100644
> --- a/mongoose/mongoose.c
> +++ b/mongoose/mongoose.c
> @@ -7819,6 +7819,23 @@ static bool mg_socketpair(MG_SOCKET_TYPE sp[2], union usa usa[2]) {
>    return success;
>  }
>
> +int mg_mkpipe(struct mg_mgr *mgr, mg_event_handler_t fn, void *fn_data) {
> +  union usa usa[2];
> +  MG_SOCKET_TYPE sp[2] = {MG_INVALID_SOCKET, MG_INVALID_SOCKET};
> +  struct mg_connection *c = NULL;
> +  if (!mg_socketpair(sp, usa)) {
> +    MG_ERROR(("Cannot create socket pair"));
> +  } else if ((c = mg_wrapfd(mgr, (int) sp[1], fn, fn_data)) == NULL) {
> +    closesocket(sp[0]);
> +    closesocket(sp[1]);
> +    sp[0] = sp[1] = MG_INVALID_SOCKET;
> +  } else {
> +    tomgaddr(&usa[0], &c->rem, false);
> +    MG_DEBUG(("%lu %p pipe %lu", c->id, c->fd, (unsigned long) sp[0]));
> +  }
> +  return (int) sp[0];
> +}
> +
>  // mg_wakeup() event handler
>  static void wufn(struct mg_connection *c, int ev, void *ev_data) {
>    if (ev == MG_EV_READ) {
> diff --git a/mongoose/mongoose.h b/mongoose/mongoose.h
> index 6b71240f..29064ab9 100644
> --- a/mongoose/mongoose.h
> +++ b/mongoose/mongoose.h
> @@ -2062,6 +2062,7 @@ bool mg_send(struct mg_connection *, const void *, size_t);
>  size_t mg_printf(struct mg_connection *, const char *fmt, ...);
>  size_t mg_vprintf(struct mg_connection *, const char *fmt, va_list *ap);
>  bool mg_aton(struct mg_str str, struct mg_addr *addr);
> +int mg_mkpipe(struct mg_mgr *, mg_event_handler_t, void *fn_data);
>
>  // These functions are used to integrate with custom network stacks
>  struct mg_connection *mg_alloc_conn(struct mg_mgr *);
> --
> 2.35.7
>
> --
> You received this message because you are subscribed to the Google Groups "swupdate" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to swupdate+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/swupdate/20240612144010.470339-5-Michael.Glembotzki%40iris-sensing.com.
Michael Glembotzki June 14, 2024, 7:30 a.m. UTC | #2
> James Hilliard schrieb am Donnerstag, 13. Juni 2024 um 09:27:46 UTC+2:
> 
> On Wed, Jun 12, 2024 at 8:40 AM Michael Glembotzki <m.gl...@gmail.com> 
wrote: 
> > 
> > From: Michael Glembotzki <m.gl...@gmail.com> 
> > 
> > The generic function mg_mkpipe is deprecated with 7.12. In order to 
keep 
> > the change effort as low as possible, the forward port is done in the 
> > mongoose sources themselves. 
> 
> Why not just use mg_wakeup instead? It seems like it should work with 
> minimal changes and should let us avoid having to modify the mongoose 
> sources. 

Will be adjusted in V2

> Docs: 
> https://mongoose.ws/documentation/#mg_wakeup 
> 
> Example: 
> 
https://github.com/cesanta/mongoose/blob/7.14/tutorials/core/multi-threaded/main.c
Michael Glembotzki June 14, 2024, 2:38 p.m. UTC | #3
> Michael Glembotzki schrieb am Freitag, 14. Juni 2024 um 09:30:47 UTC+2:
> 
> > James Hilliard schrieb am Donnerstag, 13. Juni 2024 um 09:27:46 UTC+2:
> > 
> > On Wed, Jun 12, 2024 at 8:40 AM Michael Glembotzki <m.gl...@gmail.com> 
wrote: 
> > > 
> > > From: Michael Glembotzki <m.gl...@gmail.com> 
> > > 
> > > The generic function mg_mkpipe is deprecated with 7.12. In order to 
keep 
> > > the change effort as low as possible, the forward port is done in the 
> > > mongoose sources themselves. 
> > 
> > Why not just use mg_wakeup instead? It seems like it should work with 
> > minimal changes and should let us avoid having to modify the mongoose 
> > sources. 
> 
> Will be adjusted in V2

Hi James,

I don't think mg_wakeup_init / mg_wakeup will be able to help us because it
solves a different problem.

In our design, the threads broadcast_message_thread / 
broadcast_progress_thread
wait for new data in a blocked state with ipc_notify_receive /
progress_ipc_receive and send the data via ws_pipe (using broadcast()). The
broadcast_callback, in turn, receives the data at the other end of the pipe 
and
writes it out via WebSocket.

The new design pursues a different goal. It aims to help execute 
long-running
tasks in a thread in the event handler itself. At the end of the thread, an
event is generated that can be processed in the event handler, as shown in
example [1]. The problem here is that to trigger an MG_EV_WAKEUP, a
connection id is needed [2], and the connection id must also be assigned to 
a
connection [3].

I think the new mechanism would not work easily with the existing
broadcast_message_thread / broadcast_progress_thread threads. One would have
to get rid of the for(;;){} threads and integrate the fd as a connection in
the event handler. Since the PR is already big enough, I wouldn't want
to complicate the issue further.

[1] 
https://github.com/cesanta/mongoose/blob/7.14/tutorials/core/multi-threaded/main.c
[2] https://github.com/cesanta/mongoose/blob/master/mongoose.c#L7902
[3] https://github.com/cesanta/mongoose/blob/master/mongoose.c#L7865


Best regards
Michael
James Hilliard June 14, 2024, 5:54 p.m. UTC | #4
On Fri, Jun 14, 2024 at 8:38 AM Michael Glembotzki <m.glembo@gmail.com> wrote:
>
> > Michael Glembotzki schrieb am Freitag, 14. Juni 2024 um 09:30:47 UTC+2:
> >
> > > James Hilliard schrieb am Donnerstag, 13. Juni 2024 um 09:27:46 UTC+2:
> > >
> > > On Wed, Jun 12, 2024 at 8:40 AM Michael Glembotzki <m.gl...@gmail.com> wrote:
> > > >
> > > > From: Michael Glembotzki <m.gl...@gmail.com>
> > > >
> > > > The generic function mg_mkpipe is deprecated with 7.12. In order to keep
> > > > the change effort as low as possible, the forward port is done in the
> > > > mongoose sources themselves.
> > >
> > > Why not just use mg_wakeup instead? It seems like it should work with
> > > minimal changes and should let us avoid having to modify the mongoose
> > > sources.
> >
> > Will be adjusted in V2
>
> Hi James,
>
> I don't think mg_wakeup_init / mg_wakeup will be able to help us because it
> solves a different problem.
>
> In our design, the threads broadcast_message_thread / broadcast_progress_thread
> wait for new data in a blocked state with ipc_notify_receive /
> progress_ipc_receive and send the data via ws_pipe (using broadcast()). The
> broadcast_callback, in turn, receives the data at the other end of the pipe and
> writes it out via WebSocket.
>
> The new design pursues a different goal. It aims to help execute long-running
> tasks in a thread in the event handler itself. At the end of the thread, an
> event is generated that can be processed in the event handler, as shown in
> example [1]. The problem here is that to trigger an MG_EV_WAKEUP, a
> connection id is needed [2], and the connection id must also be assigned to a
> connection [3].

Supposedly it should work if you create the broadcast threads and
then assign them the listener connection id:
https://github.com/cesanta/mongoose/blob/7.14/tutorials/core/multi-threaded-12m/main.c#L47-L52

>
> I think the new mechanism would not work easily with the existing
> broadcast_message_thread / broadcast_progress_thread threads. One would have
> to get rid of the for(;;){} threads and integrate the fd as a connection in
> the event handler. Since the PR is already big enough, I wouldn't want
> to complicate the issue further.

The docs indicate that it should work for this use case:
https://mongoose.ws/documentation/tutorials/core/multi-threaded/#the-one-to-many-case

>
> [1] https://github.com/cesanta/mongoose/blob/7.14/tutorials/core/multi-threaded/main.c
> [2] https://github.com/cesanta/mongoose/blob/master/mongoose.c#L7902
> [3] https://github.com/cesanta/mongoose/blob/master/mongoose.c#L7865
>
>
> Best regards
> Michael
>
> --
> You received this message because you are subscribed to the Google Groups "swupdate" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to swupdate+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/swupdate/74201880-0cea-4a8d-9921-5166c12560dfn%40googlegroups.com.
diff mbox series

Patch

diff --git a/mongoose/mongoose.c b/mongoose/mongoose.c
index 44066893..9e0bb6fb 100644
--- a/mongoose/mongoose.c
+++ b/mongoose/mongoose.c
@@ -7819,6 +7819,23 @@  static bool mg_socketpair(MG_SOCKET_TYPE sp[2], union usa usa[2]) {
   return success;
 }
 
+int mg_mkpipe(struct mg_mgr *mgr, mg_event_handler_t fn, void *fn_data) {
+  union usa usa[2];
+  MG_SOCKET_TYPE sp[2] = {MG_INVALID_SOCKET, MG_INVALID_SOCKET};
+  struct mg_connection *c = NULL;
+  if (!mg_socketpair(sp, usa)) {
+    MG_ERROR(("Cannot create socket pair"));
+  } else if ((c = mg_wrapfd(mgr, (int) sp[1], fn, fn_data)) == NULL) {
+    closesocket(sp[0]);
+    closesocket(sp[1]);
+    sp[0] = sp[1] = MG_INVALID_SOCKET;
+  } else {
+    tomgaddr(&usa[0], &c->rem, false);
+    MG_DEBUG(("%lu %p pipe %lu", c->id, c->fd, (unsigned long) sp[0]));
+  }
+  return (int) sp[0];
+}
+
 // mg_wakeup() event handler
 static void wufn(struct mg_connection *c, int ev, void *ev_data) {
   if (ev == MG_EV_READ) {
diff --git a/mongoose/mongoose.h b/mongoose/mongoose.h
index 6b71240f..29064ab9 100644
--- a/mongoose/mongoose.h
+++ b/mongoose/mongoose.h
@@ -2062,6 +2062,7 @@  bool mg_send(struct mg_connection *, const void *, size_t);
 size_t mg_printf(struct mg_connection *, const char *fmt, ...);
 size_t mg_vprintf(struct mg_connection *, const char *fmt, va_list *ap);
 bool mg_aton(struct mg_str str, struct mg_addr *addr);
+int mg_mkpipe(struct mg_mgr *, mg_event_handler_t, void *fn_data);
 
 // These functions are used to integrate with custom network stacks
 struct mg_connection *mg_alloc_conn(struct mg_mgr *);