ithewei 6 vuotta sitten
vanhempi
commit
62f0d1debb
5 muutettua tiedostoa jossa 94 lisäystä ja 7 poistoa
  1. 13 0
      base/hbase.c
  2. 3 0
      base/hbase.h
  3. 32 4
      base/hthread.h
  4. 10 3
      main.cpp.tmpl
  5. 36 0
      unittest/nslookup_test.c

+ 13 - 0
base/hbase.c

@@ -114,3 +114,16 @@ char* safe_strncat(char* dest, const char* src, size_t n) {
     return ret;
 }
 
+bool getboolean(const char* str) {
+    if (str == NULL) return false;
+    int len = strlen(str);
+    if (len == 0) return false;
+    switch (len) {
+    case 1: return *str == '1' || *str == 'y' || *str == 'Y';
+    case 2: return stricmp(str, "on") == 0;
+    case 3: return stricmp(str, "yes") == 0;
+    case 4: return stricmp(str, "true") == 0;
+    case 6: return stricmp(str, "enable") == 0;
+    default: return false;
+    }
+}

+ 3 - 0
base/hbase.h

@@ -60,6 +60,9 @@ char* safe_strncpy(char* dest, const char* src, size_t n);
 // safe_strncpy n = sizeof(dest_buf)
 char* safe_strncat(char* dest, const char* src, size_t n);
 
+// 1 y on yes true enable
+bool getboolean(const char* str);
+
 #ifdef __cplusplus
 } // extern "C"
 #endif

+ 32 - 4
base/hthread.h

@@ -1,10 +1,6 @@
 #ifndef HW_THREAD_H_
 #define HW_THREAD_H_
 
-#include <thread>
-#include <atomic>
-#include <chrono>
-
 #include "hplatform.h"
 #include "hdef.h"
 
@@ -21,6 +17,33 @@ static inline int gettid() {
 #define gettid  pthread_self
 #endif
 
+#ifdef OS_WIN
+typedef HANDLE      hthread_t;
+typedef DWORD WINAPI (*hthread_routine)(void*);
+#define HTHREAD_ROUTINE(fname) DWORD WINAPI fname(void* userdata)
+static inline hthread_t hthread_create(hthread_routine fn, void* userdata) {
+    return CreateThread(NULL, 0, fn, userdata, 0, NULL);
+}
+
+static inline int hthread_join(hthread_t th) {
+    return WaitForSingleObject(th, INFINITE);
+}
+#else
+typedef pthread_t   hthread_t;
+typedef void* (*hthread_routine)(void*);
+#define HTHREAD_ROUTINE(fname) void* fname(void* userdata)
+static inline hthread_t hthread_create(hthread_routine fn, void* userdata) {
+    pthread_t th;
+    pthread_create(&th, NULL, fn, userdata);
+    return th;
+}
+
+static inline int hthread_join(hthread_t th) {
+    return pthread_join(th, NULL);
+}
+#endif
+
+#ifdef __cplusplus
 /************************************************
  * HThread
  * Status: STOP,RUNNING,PAUSE
@@ -28,6 +51,10 @@ static inline int gettid() {
  * first-level virtual: doTask
  * second-level virtual: run
 ************************************************/
+#include <thread>
+#include <atomic>
+#include <chrono>
+
 class HThread {
  public:
     HThread() {
@@ -147,5 +174,6 @@ class HThread {
     std::chrono::system_clock::time_point base_tp;   // for SLEEP_UNTIL
     uint32_t sleep_ms;
 };
+#endif
 
 #endif  // HW_THREAD_H_

+ 10 - 3
main.cpp.tmpl

@@ -118,7 +118,14 @@ int parse_confile(const char* confile) {
     if (!str.empty()) {
         hlog_set_remain_days(atoi(str.c_str()));
     }
+    // log_fsync
+    str = g_conf_ctx.parser->GetValue("log_fsync");
+    if (!str.empty()) {
+        logger_enable_fsync(hlog, getboolean(str.c_str()));
+    }
+    // first log here
     hlogi("%s version: %s", g_main_ctx.program_name, get_compile_version());
+    hlog_fsync();
 
     // worker_processes
     int worker_processes = 0;
@@ -272,14 +279,14 @@ int main(int argc, char** argv) {
             return ERR_MALLOC;
         }
         memset(g_worker_processes, 0, bytes);
-        for (int i = 0; i < g_worker_processes_num; ++i) {
-            proc_ctx_t* ctx = &g_worker_processes[i];
+        proc_ctx_t* ctx = g_worker_processes;
+        for (int i = 0; i < g_worker_processes_num; ++i, ++ctx) {
             ctx->init = worker_init;
             ctx->init_userdata = NULL;
             ctx->proc = worker_proc;
             ctx->proc_userdata = NULL;
             spawn_proc(ctx);
-            hlogi("worker[%d] start/running, pid=%d", i, ctx->pid);
+            hlogi("workers[%d] start/running, pid=%d", i, ctx->pid);
         }
 
         hlogi("master start/running, pid=%d", g_main_ctx.pid);

+ 36 - 0
unittest/nslookup_test.c

@@ -0,0 +1,36 @@
+#include <stdio.h>
+
+#include "hplatform.h" // inet_ntop
+#include "dns.h" // nslookup
+
+int main(int argc, char* argv[]) {
+    if (argc < 2) {
+        printf("Usage: cmd domain [nameserver]\n");
+        return -1;
+    }
+
+    const char* domain = argv[1];
+    const char* nameserver = "127.0.1.1";
+
+#ifdef OS_WIN
+    WSADATA wsadata;
+    WSAStartup(MAKEWORD(2,2), &wsadata);
+    nameserver = "114.114.114.114";
+#endif
+
+    if (argc > 2) {
+        nameserver = argv[2];
+    }
+
+    uint32_t addrs[16];
+    int naddr = nslookup(domain, addrs, 16, nameserver);
+    if (naddr < 0) {
+        return naddr;
+    }
+    char ip[16];
+    for (int i = 0; i < naddr; ++i) {
+        inet_ntop(AF_INET, (void*)&addrs[i], ip, 16);
+        printf("%s\n", ip);
+    }
+    return 0;
+}