ithewei 6 years ago
parent
commit
bf45b98c3d
18 changed files with 169 additions and 141 deletions
  1. 0 0
      base64.c
  2. 10 4
      base64.h
  3. 20 12
      h.h
  4. 31 0
      hdef.h
  5. 23 0
      herr.c
  6. 0 60
      herr.cpp
  7. 10 10
      herr.h
  8. 2 2
      hlog.cpp
  9. 1 1
      hlog.h
  10. 3 2
      hmain.cpp
  11. 5 0
      hplatform.h
  12. 5 8
      hproc.h
  13. 23 0
      htime.c
  14. 11 24
      htime.h
  15. 1 1
      ifconfig.cpp
  16. 3 3
      ifconfig.h
  17. 3 4
      iniparser.cpp
  18. 18 10
      main.cpp.tmpl

+ 0 - 0
base64.cpp → base64.c


+ 10 - 4
base64.h

@@ -3,12 +3,18 @@
 
 enum {BASE64_OK = 0, BASE64_INVALID};
 
-#define BASE64_ENCODE_OUT_SIZE(s)	(((s) + 2) / 3 * 4)
-#define BASE64_DECODE_OUT_SIZE(s)	(((s)) / 4 * 3)
+#define BASE64_ENCODE_OUT_SIZE(s)   (((s) + 2) / 3 * 4)
+#define BASE64_DECODE_OUT_SIZE(s)   (((s)) / 4 * 3)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 int base64_encode(const unsigned char *in, unsigned int inlen, char *out);
 int base64_decode(const char *in, unsigned int inlen, unsigned char *out);
 
+#ifdef __cplusplus
+} // extern "C"
+#endif
 
-#endif /* __BASE64_H__ */
-
+#endif // __BASE64_H__

+ 20 - 12
h.h

@@ -5,35 +5,43 @@
  * @copyright 2018 HeWei, all rights reserved.
  */
 
+// platform
 #include "hplatform.h"
 #include "hdef.h"
 #include "hversion.h"
 
-#include "herr.h"
-#include "hlog.h"
+// c
+#include "hsysinfo.h"
+#include "hproc.h"
 
 #include "htime.h"
-#include "hstring.h"
-#include "hfile.h"
-#include "hbuf.h"
-#include "hbytearray.h"
+#include "herr.h"
 
-#include "hscope.h"
+// cpp
+#ifdef __cplusplus
 #include "hmutex.h"
 #include "hthread.h"
 #include "hthreadpool.h"
-
+#include "hscope.h"
 #include "hvar.h"
 #include "hobj.h"
-
 #include "hgui.h"
+#include "hbuf.h"
+#include "hfile.h"
+#include "hbytearray.h"
 
+#include "hlog.h"
+#include "hstring.h"
 #include "hmain.h"
-#include "hproc.h"
-#include "hsysinfo.h"
-#include "hifconf.h"
+#include "hframe.h"
+#endif
 
+// misc
+/*
 #include "singleton.h"
+#include "base64.h"
 #include "iniparser.h"
+#include "ifconfig.h"
+*/
 
 #endif  // HW_H_

+ 31 - 0
hdef.h

@@ -38,11 +38,42 @@ typedef int (*method_t)(void* userdata);
 typedef void (*procedure_t)(void* userdata);
 
 #ifdef __cplusplus
+
 #include <string>
 #include <map>
 typedef std::map<std::string, std::string> keyval_t;
+
+#ifndef BEGIN_NAMESPACE
+#define BEGIN_NAMESPACE(ns) namespace ns {
+#endif
+
+#ifndef END_NAMESPACE
+#define END_NAMESPACE(ns)   } // ns
+#endif
+
+#ifndef EXTERN_C
+#define EXTERN_C            extern "C"
+#endif
+
+#ifndef BEGIN_EXTERN_C
+#define BEGIN_EXTERN_C      extern "C" {
 #endif
 
+#ifndef END_EXTERN_C
+#define END_EXTERN_C        } // extern "C"
+#endif
+
+#else
+
+#define BEGIN_NAMESPACE(ns)
+#define END_NAMESPACE(ns)
+
+#define EXTERN_C
+#define BEGIN_EXTERN_C
+#define END_EXTERN_C
+
+#endif // __cplusplus
+
 #ifndef MAX_PATH
 #define MAX_PATH          260
 #endif

+ 23 - 0
herr.c

@@ -0,0 +1,23 @@
+#include "herr.h"
+
+#include <string.h> // for strerror
+
+#ifndef SYS_NERR
+#define SYS_NERR    133
+#endif
+
+// errcode => errmsg
+const char* get_errmsg(int err) {
+    if (err >= 0 && err <= SYS_NERR) {
+        return strerror(err);
+    }
+
+    switch (err) {
+#define CASE_ERR(macro, errcode, errmsg) \
+    case errcode: return errmsg;
+    FOREACH_ERR(CASE_ERR)
+#undef CASE_ERR
+    default:
+        return "Undefined error";
+    }
+}

+ 0 - 60
herr.cpp

@@ -1,60 +0,0 @@
-#include "herr.h"
-
-#include <string.h> // for strerror
-
-#include <map>
-
-#include "hthread.h"    // for gettid
-
-#define SYS_NERR    133
-
-// id => errcode
-static std::map<int, int>   s_mapErr;
-
-void set_id_errcode(int id, int errcode) {
-    s_mapErr[id] = errcode;
-}
-
-int  get_id_errcode(int id) {
-    auto iter = s_mapErr.find(id);
-    if (iter != s_mapErr.end()) {
-        // note: erase after get
-        s_mapErr.erase(iter);
-        return iter->second;
-    }
-    return ERR_OK;
-}
-
-void set_last_errcode(int errcode) {
-#if defined(OS_WIN) || defined(OS_LINUX)
-    int id = gettid();
-#else
-    int id = getpid();
-#endif
-    set_id_errcode(id, errcode);
-}
-
-int  get_last_errcode() {
-#if defined(OS_WIN) || defined(OS_LINUX)
-    int id = gettid();
-#else
-    int id = getpid();
-#endif
-    return get_id_errcode(id);
-}
-
-const char* get_errmsg(int err) {
-    if (err >= 0 && err <= SYS_NERR) {
-        return strerror(err);
-    }
-
-    switch (err) {
-#define CASE_ERR(macro, errcode, errmsg) \
-    case errcode: \
-        return errmsg;
-    FOREACH_ERR(CASE_ERR)
-#undef CASE_ERR
-    default:
-        return "Undefined error";
-    }
-}

+ 10 - 10
herr.h

@@ -77,22 +77,22 @@
     FOREACH_ERR_SERVICE(F)  \
     FOREACH_ERR_GRPC(F)
 
+enum H_ERR {
 #define ENUM_ERR(macro, errcode, _) macro = errcode,
-enum E_ERR{
     FOREACH_ERR(ENUM_ERR)
+#undef ENUM_ERR
     ERR_LAST
 };
-#undef ENUM_ERR
 
-// id => errcode
-void set_id_errcode(int id, int errcode);
-int  get_id_errcode(int id);
-
-// id = gettid()
-void set_last_errcode(int errcode);
-int  get_last_errcode();
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 // errcode => errmsg
-const char* get_errmsg(int errcode);
+const char* get_errmsg(int err);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
 
 #endif  // HW_ERR_H_

+ 2 - 2
hlog.cpp

@@ -84,8 +84,8 @@ void hlog_set_remain_days(int days) {
     s_remain_days = days;
 }
 
-void hlog_enable_color(bool enable) {
-    s_logcolor = enable;
+void hlog_enable_color(int on) {
+    s_logcolor = on;
 }
 
 int hlog_printf(int level, const char* fmt, ...) {

+ 1 - 1
hlog.h

@@ -45,7 +45,7 @@ enum LOG_LEVEL {
 int     hlog_set_file(const char* file);
 void    hlog_set_level(int level);
 void    hlog_set_remain_days(int days);
-void    hlog_enable_color(bool enable);
+void    hlog_enable_color(int on);
 int     hlog_printf(int level, const char* fmt, ...);
 
 #define hlogd(fmt, ...) hlog_printf(LOG_LEVEL_DEBUG, fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)

+ 3 - 2
hmain.cpp

@@ -327,6 +327,7 @@ void signal_handler(int signo) {
     case SIGNAL_TERMINATE:
         hlogi("killall processes");
         signal(SIGCHLD, SIG_IGN);
+        // master send SIGKILL => workers
         for (int i = 0; i < g_worker_processes_num; ++i) {
             if (g_worker_processes[i].pid <= 0) break;
             kill(g_worker_processes[i].pid, SIGKILL);
@@ -338,7 +339,7 @@ void signal_handler(int signo) {
         if (s_reload_fn) {
             s_reload_fn(s_reload_userdata);
             if (getpid_from_pidfile() == getpid()) {
-                // master raise SIGNAL_RELOAD => workers
+                // master send SIGNAL_RELOAD => workers
                 for (int i = 0; i < g_worker_processes_num; ++i) {
                     if (g_worker_processes[i].pid <= 0) break;
                     kill(g_worker_processes[i].pid, SIGNAL_RELOAD);
@@ -355,7 +356,7 @@ void signal_handler(int signo) {
             for (int i = 0; i < g_worker_processes_num; ++i) {
                 if (g_worker_processes[i].pid == pid) {
                     g_worker_processes[i].pid = -1;
-                    create_proc(&g_worker_processes[i]);
+                    spawn_proc(&g_worker_processes[i]);
                     break;
                 }
             }

+ 5 - 0
hplatform.h

@@ -51,6 +51,11 @@
 // _MSC_VER
 // __MINGW32__
 // __GNUC__
+#ifdef __GNUC__
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE 1
+#endif
+#endif
 // __clang__
 
 // ARCH

+ 5 - 8
hproc.h

@@ -3,7 +3,6 @@
 
 #include "hplatform.h"
 #include "hdef.h"
-#include "hlog.h"
 
 typedef struct proc_ctx_s {
     pid_t           pid; // tid in win32
@@ -17,14 +16,14 @@ typedef struct proc_ctx_s {
 
 #ifdef OS_UNIX
 // unix use multi-processes
-inline int create_proc(proc_ctx_t* ctx) {
+inline int spawn_proc(proc_ctx_t* ctx) {
     pid_t pid = fork();
     if (pid < 0) {
-        hloge("fork error: %d", errno);
+        perror("fork");
         return -1;
     } else if (pid == 0) {
         // child proc
-        hlogi("proc start/running, pid=%d", getpid());
+        ctx->pid = getpid();
         if (ctx->init) {
             ctx->init(ctx->init_userdata);
         }
@@ -37,14 +36,13 @@ inline int create_proc(proc_ctx_t* ctx) {
         exit(0);
     } else if (pid > 0) {
         // parent proc
+        ctx->pid = pid;
     }
-    ctx->pid = pid;
     return pid;
 }
 #elif defined(OS_WIN)
 // win32 use multi-threads
 static void win_thread(void* userdata) {
-    hlogi("proc start/running, tid=%d", GetCurrentThreadId());
     proc_ctx_t* ctx = (proc_ctx_t*)userdata;
     if (ctx->init) {
         ctx->init(ctx->init_userdata);
@@ -56,10 +54,9 @@ static void win_thread(void* userdata) {
         ctx->exit(ctx->exit_userdata);
     }
 }
-inline int create_proc(proc_ctx_t* ctx) {
+inline int spawn_proc(proc_ctx_t* ctx) {
     HANDLE h = (HANDLE)_beginthread(win_thread, 0, ctx);
     if (h == NULL) {
-        hloge("_beginthread error: %d", errno);
         return -1;
     }
     int tid = GetThreadId(h);

+ 23 - 0
htime.cpp → htime.c

@@ -1,5 +1,28 @@
 #include "htime.h"
 
+inline unsigned long long gethrtime() {
+#ifdef OS_WIN
+    static LONGLONG s_freq = 0;
+    if (s_freq == 0) {
+        LARGE_INTEGER freq;
+        QueryPerformanceFrequency(&freq);
+        s_freq = freq.QuadPart;
+    }
+    if (s_freq != 0) {
+        LARGE_INTEGER count;
+        QueryPerformanceCounter(&count);
+        return count.QuadPart / (double)s_freq * 1000000;
+    }
+    return 0;
+#elif defined(OS_LINUX)
+    struct timespec ts;
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+    return ts.tv_sec*(unsigned long long)1000000 + ts.tv_nsec / 1000;
+#else
+    return clock()* / (double)CLOCKS_PER_SEC * 1000000;
+#endif
+}
+
 datetime_t get_datetime() {
     datetime_t  dt;
 #ifdef OS_WIN

+ 11 - 24
htime.h

@@ -36,34 +36,21 @@ inline unsigned int gettick() {
 #endif
 }
 
-// us
-inline unsigned long long gethrtime() {
-#ifdef OS_WIN
-    static LONGLONG s_freq = 0;
-    if (s_freq == 0) {
-        LARGE_INTEGER freq;
-        QueryPerformanceFrequency(&freq);
-        s_freq = freq.QuadPart;
-    }
-    if (s_freq != 0) {
-        LARGE_INTEGER count;
-        QueryPerformanceCounter(&count);
-        return count.QuadPart / (double)s_freq * 1000000;
-    }
-    return 0;
-#elif defined(OS_LINUX)
-    struct timespec ts;
-    clock_gettime(CLOCK_MONOTONIC, &ts);
-    return ts.tv_sec*(unsigned long long)1000000 + ts.tv_nsec / 1000;
-#else
-    return clock()* / (double)CLOCKS_PER_SEC * 1000000;
+#ifdef __cplusplus
+extern "C" {
 #endif
-}
 
-int month_atoi(const char* month);
-const char* month_itoa(int month);
+// us
+unsigned long long gethrtime();
 
 datetime_t get_datetime();
 datetime_t get_compile_datetime();
 
+int month_atoi(const char* month);
+const char* month_itoa(int month);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
 #endif  // HW_TIME_H_

+ 1 - 1
hifconf.cpp → ifconfig.cpp

@@ -1,4 +1,4 @@
-#include "hifconf.h"
+#include "ifconfig.h"
 
 #include <stdio.h>
 #include <stdlib.h>

+ 3 - 3
hifconf.h → ifconfig.h

@@ -1,5 +1,5 @@
-#ifndef H_IFCONFIG_H_
-#define H_IFCONFIG_H_
+#ifndef IFCONFIG_H_
+#define IFCONFIG_H_
 
 #include <vector>
 
@@ -24,4 +24,4 @@ typedef struct ifconfig_s {
  */
 int ifconfig(std::vector<ifconfig_t>& ifcs);
 
-#endif // H_IFCONFIG_H_
+#endif // IFCONFIG_H_

+ 3 - 4
iniparser.cpp

@@ -32,10 +32,9 @@ int IniParser::LoadFromFile(const char* filepath) {
         return ERR_OPEN_FILE;
     }
 
-    hbuf_t buf;
-    file.readall(buf);
-
-    return LoadFromMem((const char*)buf.base);
+    std::string str;
+    file.readall(str);
+    return LoadFromMem(str.c_str());
 }
 
 int IniParser::LoadFromMem(const char* data) {

+ 18 - 10
main.cpp.tmpl

@@ -1,5 +1,5 @@
 #include "h.h"
-#include "hmain.h"
+#include "iniparser.h"
 
 typedef struct conf_ctx_s {
     IniParser* parser;
@@ -21,6 +21,7 @@ static void print_help();
 
 static int  parse_confile(const char* confile);
 
+static void master_init(void* userdata);
 static void master_proc(void* userdata);
 static void worker_init(void* userdata);
 static void worker_proc(void* userdata);
@@ -127,6 +128,15 @@ int parse_confile(const char* confile) {
     return 0;
 }
 
+void master_init(void* userdata) {
+#ifdef OS_UNIX
+    char proctitle[256] = {0};
+    snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
+    setproctitle(proctitle);
+    signal(SIGNAL_RELOAD, signal_handler);
+#endif
+}
+
 void worker_init(void* userdata) {
 #ifdef OS_UNIX
     char proctitle[256] = {0};
@@ -230,7 +240,6 @@ int main(int argc, char** argv) {
 
     // pidfile
     create_pidfile();
-    hlogi("%s start/running, pid=%d", g_main_ctx.program_name, g_main_ctx.pid);
 
     if (g_conf_ctx.worker_processes == 0) {
         // single process
@@ -251,13 +260,12 @@ int main(int argc, char** argv) {
             ctx->init_userdata = NULL;
             ctx->proc = worker_proc;
             ctx->proc_userdata = NULL;
-            create_proc(ctx);
+            spawn_proc(ctx);
+            hlogi("worker[%d] start/running, pid=%d", i, ctx->pid);
         }
-#ifdef OS_UNIX
-        char proctitle[256] = {0};
-        snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
-        setproctitle(proctitle);
-#endif
+
+        hlogi("master start/running, pid=%d", g_main_ctx.pid);
+        master_init(NULL);
         master_proc(NULL);
     }
 
@@ -265,9 +273,9 @@ int main(int argc, char** argv) {
 }
 
 void master_proc(void* userdata) {
-    while(1) msleep(1000);
+    while(1) sleep(1);
 }
 
 void worker_proc(void* userdata) {
-    while(1) msleep(1000);
+    while(1) sleep(1);
 }