gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] 255/335: clean up example


From: gnunet
Subject: [libmicrohttpd] 255/335: clean up example
Date: Sat, 27 Jul 2024 22:02:31 +0200

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to tag stf-m2
in repository libmicrohttpd.

commit 8a8fd51909bd8acd4f4f7d56d78bff780cc854c8
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Sun Jul 21 17:11:54 2024 +0200

    clean up example
---
 src/examples/minimal_example2.c       | 72 ++++++++++++++++++++---------------
 src/tests/basic/libtest_convenience.c | 32 +++++++++++-----
 2 files changed, 64 insertions(+), 40 deletions(-)

diff --git a/src/examples/minimal_example2.c b/src/examples/minimal_example2.c
index 6973fcf3..b743c535 100644
--- a/src/examples/minimal_example2.c
+++ b/src/examples/minimal_example2.c
@@ -36,13 +36,17 @@ req_cb (void *cls,
 {
   static const char res_msg[] = "Hello there!";
 
-  (void) cls; (void) path; (void) method; (void) upload_size; /* Unused */
+  (void) cls;
+  (void) path;
+  (void) method;
+  (void) upload_size; /* Unused */
 
   return MHD_action_from_response (
     request,
-    MHD_response_from_buffer_static (MHD_HTTP_STATUS_OK,
-                                     sizeof(res_msg) / sizeof(char) - 1,
-                                     res_msg));
+    MHD_response_from_buffer_static (
+      MHD_HTTP_STATUS_OK,
+      sizeof(res_msg) / sizeof(char) - 1,
+      res_msg));
 }
 
 
@@ -55,7 +59,9 @@ main (int argc,
 
   if (argc != 2)
   {
-    fprintf (stderr,"Usage:\n%s PORT\n", argv[0]);
+    fprintf (stderr,
+             "Usage:\n%s PORT\n",
+             argv[0]);
     return 1;
   }
   port = atoi (argv[1]);
@@ -65,33 +71,39 @@ main (int argc,
              "The port must be a number between 1 and 65535.\n");
     return 2;
   }
-  d = MHD_daemon_create (&req_cb, NULL);
-  if (NULL != d)
+  d = MHD_daemon_create (&req_cb,
+                         NULL);
+  if (NULL == d)
+  {
+    fprintf (stderr,
+             "Failed to create MHD daemon.\n");
+    return 3;
+  }
+  if (MHD_SC_OK !=
+      MHD_DAEMON_SET_OPTIONS (
+        d,
+        MHD_D_OPTION_WM_WORKER_THREADS (1),
+        MHD_D_OPTION_BIND_PORT (MHD_AF_AUTO,
+                                (uint_least16_t) port)))
   {
-    if (MHD_SC_OK ==
-        MHD_DAEMON_SET_OPTIONS (d,
-                                MHD_D_OPTION_WM_WORKER_THREADS (1),
-                                MHD_D_OPTION_BIND_PORT (MHD_AF_AUTO,
-                                                        (uint_least16_t) 
port)))
-    {
-      if (MHD_SC_OK == MHD_daemon_start (d))
-      {
-        printf ("The MHD daemon is listening on port %d\n"
-                "Press ENTER to stop.\n", port);
-        (void) fgetc (stdin);
-      }
-      else
-        fprintf (stderr, "Failed to start MHD daemon.\n");
-    }
-    else
-      fprintf (stderr, "Failed to set MHD daemon run parameters.\n");
-    printf ("Stopping... ");
+    fprintf (stderr,
+             "Failed to set MHD daemon run parameters.\n");
     MHD_daemon_destroy (d);
-    printf ("OK\n");
-
+    return 4;
   }
-  else
-    fprintf (stderr, "Failed to create MHD daemon.\n");
-
+  if (MHD_SC_OK !=
+      MHD_daemon_start (d))
+  {
+    fprintf (stderr,
+             "Failed to start MHD daemon.\n");
+    MHD_daemon_destroy (d);
+    return 5;
+  }
+  printf ("The MHD daemon is listening on port %d\n"
+          "Press ENTER to stop.\n", port);
+  (void) fgetc (stdin);
+  printf ("Stopping... ");
+  MHD_daemon_destroy (d);
+  printf ("OK\n");
   return 0;
 }
diff --git a/src/tests/basic/libtest_convenience.c 
b/src/tests/basic/libtest_convenience.c
index d7701b3c..65b37e83 100644
--- a/src/tests/basic/libtest_convenience.c
+++ b/src/tests/basic/libtest_convenience.c
@@ -38,9 +38,11 @@ MHDT_server_setup_minimal (void *cls,
                            struct MHD_Daemon *d)
 {
   if (MHD_SC_OK !=
-      MHD_DAEMON_SET_OPTIONS (d,
-                              MHD_D_OPTION_BIND_PORT (MHD_AF_DUAL,
-                                                      0)))
+      MHD_DAEMON_SET_OPTIONS (
+        d,
+        MHD_D_OPTION_WM_WORKER_THREADS (1),
+        MHD_D_OPTION_BIND_PORT (MHD_AF_AUTO,
+                                0)))
     return "Failed to bind to port 0!";
   return NULL;
 }
@@ -83,20 +85,30 @@ MHDT_server_run_blocking (void *cls,
                           int finsig,
                           struct MHD_Daemon *d)
 {
-  char e;
+  fd_set r;
 
-  while (-1 ==
-         read (finsig,
-               &e,
-               1))
+  FD_ZERO (&r);
+  FD_SET (finsig, &r);
+  while (1)
   {
-    if (EAGAIN != errno)
+    struct timeval timeout = {
+      .tv_usec = 1000 /* 1000 microseconds */
+    };
+
+    if ( (-1 ==
+          select (finsig + 1,
+                  &r,
+                  NULL,
+                  NULL,
+                  &timeout)) &&
+         (EAGAIN != errno) )
     {
       fprintf (stderr,
-               "Failure reading termination signal: %s\n",
+               "Failure waiting on termination signal: %s\n",
                strerror (errno));
       break;
     }
+
     if (MHD_SC_OK !=
         MHD_daemon_process_blocking (d,
                                      1000))

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]