瀏覽代碼

ncd: NCDRequestClient: add support to connecting to TCP servers

ambrop7 14 年之前
父節點
當前提交
1070473752
共有 4 個文件被更改,包括 41 次插入8 次删除
  1. 1 1
      ncd-request/ncd-request.c
  2. 21 5
      ncd/NCDRequestClient.c
  3. 18 1
      ncd/NCDRequestClient.h
  4. 1 1
      ncd/modules/sys_request_client.c

+ 1 - 1
ncd-request/ncd-request.c

@@ -86,7 +86,7 @@ int main (int argc, char *argv[])
         goto fail2;
     }
     
-    if (!NCDRequestClient_Init(&client, socket_path, &reactor, NULL, client_handler_error, client_handler_connected)) {
+    if (!NCDRequestClient_Init(&client, NCDREQUESTCLIENT_UNIX_ADDR(socket_path), &reactor, NULL, client_handler_error, client_handler_connected)) {
         BLog(BLOG_ERROR, "NCDRequestClient_Init failed");
         goto fail3;
     }

+ 21 - 5
ncd/NCDRequestClient.c

@@ -451,11 +451,10 @@ static void req_qflow_send_iface_handler_done (struct NCDRequestClient_req *req)
     }
 }
 
-int NCDRequestClient_Init (NCDRequestClient *o, const char *socket_path, BReactor *reactor, void *user,
+int NCDRequestClient_Init (NCDRequestClient *o, struct NCDRequestClient_addr addr, BReactor *reactor, void *user,
                            NCDRequestClient_handler_error handler_error,
                            NCDRequestClient_handler_connected handler_connected)
 {
-    ASSERT(socket_path)
     ASSERT(handler_error)
     ASSERT(handler_connected)
     
@@ -466,9 +465,26 @@ int NCDRequestClient_Init (NCDRequestClient *o, const char *socket_path, BReacto
     o->handler_connected = handler_connected;
     
     // init connector
-    if (!BConnector_InitUnix(&o->connector, socket_path, reactor, o, (BConnector_handler)connector_handler)) {
-        BLog(BLOG_ERROR, "BConnector_InitUnix failed");
-        goto fail0;
+    switch (addr.type) {
+        case NCDREQUESTCLIENT_ADDR_TYPE_UNIX: {
+            ASSERT(addr.u.unix_socket_path)
+            
+            if (!BConnector_InitUnix(&o->connector, addr.u.unix_socket_path, reactor, o, (BConnector_handler)connector_handler)) {
+                BLog(BLOG_ERROR, "BConnector_InitUnix failed");
+                goto fail0;
+            }
+        } break;
+        
+        case NCDREQUESTCLIENT_ADDR_TYPE_TCP: {
+            BAddr_Assert(&addr.u.tcp_baddr);
+            
+            if (!BConnector_Init(&o->connector, addr.u.tcp_baddr, reactor, o, (BConnector_handler)connector_handler)) {
+                BLog(BLOG_ERROR, "BConnector_InitUnix failed");
+                goto fail0;
+            }
+        } break;
+        
+        default: ASSERT(0);
     }
     
     // init reqs tree

+ 18 - 1
ncd/NCDRequestClient.h

@@ -94,7 +94,24 @@ struct NCDRequestClient_req {
     int state;
 };
 
-int NCDRequestClient_Init (NCDRequestClient *o, const char *socket_path, BReactor *reactor, void *user,
+#define NCDREQUESTCLIENT_ADDR_TYPE_UNIX 1
+#define NCDREQUESTCLIENT_ADDR_TYPE_TCP 2
+
+struct NCDRequestClient_addr {
+    int type;
+    union {
+        const char *unix_socket_path;
+        BAddr tcp_baddr;
+    } u;
+};
+
+#define NCDREQUESTCLIENT_UNIX_ADDR(socket_path) \
+  (struct NCDRequestClient_addr){.type = NCDREQUESTCLIENT_ADDR_TYPE_UNIX, .u.unix_socket_path = (socket_path)}
+
+#define NCDREQUESTCLIENT_TCP_ADDR(baddr) \
+  (struct NCDRequestClient_addr){.type = NCDREQUESTCLIENT_ADDR_TYPE_TCP, .u.tcp_baddr = (baddr)}
+
+int NCDRequestClient_Init (NCDRequestClient *o, struct NCDRequestClient_addr addr, BReactor *reactor, void *user,
                            NCDRequestClient_handler_error handler_error,
                            NCDRequestClient_handler_connected handler_connected) WARN_UNUSED;
 void NCDRequestClient_Free (NCDRequestClient *o);

+ 1 - 1
ncd/modules/sys_request_client.c

@@ -488,7 +488,7 @@ static void func_new (NCDModuleInst *i)
     char *socket_path = NCDValue_StringValue(socket_path_arg);
     
     // init client
-    if (!NCDRequestClient_Init(&o->client, socket_path, i->params->reactor, o,
+    if (!NCDRequestClient_Init(&o->client, NCDREQUESTCLIENT_UNIX_ADDR(socket_path), i->params->reactor, o,
         (NCDRequestClient_handler_error)client_handler_error,
         (NCDRequestClient_handler_connected)client_handler_connected)) {
         ModuleLog(o->i, BLOG_ERROR, "NCDRequestClient_Init failed");