tAdd functions to open/close curl connections - vaccinewars - be a doctor and try to vaccinate the world
git clone git://src.adamsgaard.dk/vaccinewars
Log
Files
Refs
README
LICENSE
---
commit b0ae993251eb84b78cf1855d08d1ea824d5bca51
parent 8b75b68f07617cbf489a17154c034c0e53353469
Author: Ben Webb 
Date:   Thu, 29 Oct 2020 01:25:52 -0700

Add functions to open/close curl connections

Diffstat:
  M src/serverside.c                    |      96 +++++++++++++++++--------------

1 file changed, 52 insertions(+), 44 deletions(-)
---
diff --git a/src/serverside.c b/src/serverside.c
t@@ -110,30 +110,23 @@ static GScanner *Scanner;
 /* Data waiting to be sent to/read from the metaserver */
 CURLM *MetaConnM = NULL;
 
-struct _MetaServerConnection {
+typedef struct _CurlConnection {
   CURL *h;
   gchar *data;
   size_t data_size;
   char Terminator;              /* Character that separates messages */
   char StripChar;               /* Char that should be removed
                                  * from messages */
-};
+} CurlConnection;
 
-struct _MetaServerConnection MetaConn;
+CurlConnection *MetaConn = NULL;
 
 static size_t MetaConnWriteFunc(void *contents, size_t size, size_t nmemb, void *userp)
 {
   size_t realsize = size * nmemb;
-  struct _MetaServerConnection *conn = (struct _MetaServerConnection *)userp;
+  CurlConnection *conn = (CurlConnection *)userp;
  
-  char *ptr = realloc(conn->data, conn->data_size + realsize + 1);
-  if(ptr == NULL) {
-    /* out of memory! */ 
-    printf("not enough memory (realloc returned NULL)\n");
-    return 0;
-  }
- 
-  conn->data = ptr;
+  conn->data = g_realloc(conn->data, conn->data_size + realsize + 1);
   memcpy(&(conn->data[conn->data_size]), contents, realsize);
   conn->data_size += realsize;
   conn->data[conn->data_size] = 0;
t@@ -201,6 +194,40 @@ static gboolean MetaConnectError(CURL *conn)
   return TRUE;
 }
 
+gboolean OpenCurlConnection(CurlConnection **c, char *URL, char *body, CURLM *mh)
+{
+  CurlConnection *conn;
+
+  conn = g_new0(CurlConnection, 1);
+  conn->h = curl_easy_init();
+  conn->Terminator = '\n';
+  conn->StripChar = '\r';
+  conn->data_size = 0;
+  if (conn->h) {
+    int still_running;
+    conn->data = g_malloc(1);
+    curl_easy_setopt(conn->h, CURLOPT_COPYPOSTFIELDS, body);
+    curl_easy_setopt(conn->h, CURLOPT_URL, URL);
+    curl_easy_setopt(conn->h, CURLOPT_WRITEFUNCTION, MetaConnWriteFunc);
+    curl_easy_setopt(conn->h, CURLOPT_WRITEDATA, conn);
+    curl_multi_add_handle(mh, conn->h);
+    curl_multi_perform(mh, &still_running);
+    *c = conn;
+    return TRUE;
+  } else {
+    g_free(conn);
+    return FALSE;
+  }
+}
+
+void CloseCurlConnection(CurlConnection *c, CURLM *mh)
+{
+  curl_multi_remove_handle(mh, c->h);
+  curl_easy_cleanup(c->h);
+  g_free(c->data);
+  g_free(c);
+}
+
 static void ServerHttpAuth(HttpConnection *conn, gboolean proxyauth,
                            gchar *realm, gpointer data)
 {
t@@ -278,12 +305,8 @@ void RegisterWithMetaServer(gboolean Up, gboolean SendData,
 
   /* If the previous connect hung for so long that it's still active, then
    * break the connection before we start a new one */
-  if (MetaConn.h) {
-    curl_multi_remove_handle(MetaConnM, MetaConn.h);
-    curl_easy_cleanup(MetaConn.h);
-    MetaConn.h = NULL;
-    free(MetaConn.data);
-  }
+  if (MetaConn)
+    CloseCurlConnection(MetaConn, MetaConnM);
 
   body = g_string_new("");
 
t@@ -319,23 +342,10 @@ void RegisterWithMetaServer(gboolean Up, gboolean SendData,
     }
   }
 
-  MetaConn.h = curl_easy_init();
-  MetaConn.data = malloc(1);
-  MetaConn.Terminator = '\n';
-  MetaConn.StripChar = '\r';
-  MetaConn.data_size = 0;
-  if (MetaConn.h) {
-    int still_running;
-    curl_easy_setopt(MetaConn.h, CURLOPT_COPYPOSTFIELDS, body->str);
-    curl_easy_setopt(MetaConn.h, CURLOPT_URL, MetaServer.URL);
-    curl_easy_setopt(MetaConn.h, CURLOPT_WRITEFUNCTION, MetaConnWriteFunc);
-    curl_easy_setopt(MetaConn.h, CURLOPT_WRITEDATA, &MetaConn);
- 
-    curl_multi_add_handle(MetaConnM, MetaConn.h);
-    curl_multi_perform(MetaConnM, &still_running);
-    dopelog(2, LF_SERVER, _("Waiting for connect to metaserver at %s..."),
-            MetaServer.URL);
-  }
+  retval = OpenCurlConnection(&MetaConn, MetaServer.URL, body->str, MetaConnM);
+
+  dopelog(2, LF_SERVER, _("Waiting for connect to metaserver at %s..."),
+          MetaServer.URL);
   g_string_free(body, TRUE);
 
 /*  MetaConnectError(MetaConn);
t@@ -988,7 +998,7 @@ void RequestServerShutdown(void)
  */
 gboolean IsServerShutdown(void)
 {
-  return (WantQuit && !FirstServer && !MetaConn.h);
+  return (WantQuit && !FirstServer && !MetaConn);
 }
 
 static GPrintFunc StartServerReply(NetworkBuffer *netbuf)
t@@ -1347,7 +1357,7 @@ void ServerLoop(struct CMDLINE *cmdline)
     if (IsServerShutdown())
       break;
 #endif
-    if (MetaConn.h) {
+    if (MetaConn) {
       int still_running;
       curl_multi_perform(MetaConnM, &still_running);
 //    dopelog(2, LF_SERVER, "MetaServer multi_perform: %d", still_running);
t@@ -1372,12 +1382,12 @@ void ServerLoop(struct CMDLINE *cmdline)
           break;
       } */
       if (still_running == 0) {
-        char *ch = MetaConn.data;
+        char *ch = MetaConn->data;
         while(ch && *ch) {
-          char *sep_pt = strchr(ch, MetaConn.Terminator);
+          char *sep_pt = strchr(ch, MetaConn->Terminator);
           if (sep_pt) {
             *sep_pt = '\0';
-            if (sep_pt > ch && sep_pt[-1] == MetaConn.StripChar) {
+            if (sep_pt > ch && sep_pt[-1] == MetaConn->StripChar) {
               sep_pt[-1] = '\0';
             }
             sep_pt++;
t@@ -1388,10 +1398,8 @@ void ServerLoop(struct CMDLINE *cmdline)
           ch = sep_pt;
         }
         dopelog(4, LF_SERVER, "MetaServer: (closed)\n");
-        curl_multi_remove_handle(MetaConnM, MetaConn.h);
-        curl_easy_cleanup(MetaConn.h);
-        MetaConn.h = NULL;
-        free(MetaConn.data);
+        CloseCurlConnection(MetaConn, MetaConnM);
+        MetaConn = NULL;
         if (IsServerShutdown())
           break;
       }