1
0
ithewei 6 жил өмнө
parent
commit
47b2b8bb39

+ 10 - 9
Makefile

@@ -12,34 +12,35 @@ prepare:
 	-mkdir -p $(TMPDIR)
 
 test: prepare
-	-rm $(TMPDIR)/*.o $(TMPDIR)/*.c $(TMPDIR)/*.cpp
+	-rm $(TMPDIR)/*.o $(TMPDIR)/*.h $(TMPDIR)/*.c $(TMPDIR)/*.cpp
 	cp main.cpp.tmpl $(TMPDIR)/main.cpp
 	$(MAKEF) TARGET=$@ SRCDIRS=". base utils $(TMPDIR)"
 
 client: prepare
 	-rm $(TMPDIR)/*.o $(TMPDIR)/*.c $(TMPDIR)/*.cpp
-	cp event/client.cpp.demo $(TMPDIR)/client.cpp
+	cp examples/client.cpp $(TMPDIR)/client.cpp
 	$(MAKEF) TARGET=$@ SRCDIRS=". base event $(TMPDIR)"
 
 server: prepare
 	-rm $(TMPDIR)/*.o $(TMPDIR)/*.c $(TMPDIR)/*.cpp
-	cp event/server.cpp.demo $(TMPDIR)/server.cpp
+	cp examples/server.cpp $(TMPDIR)/server.cpp
 	$(MAKEF) TARGET=$@ SRCDIRS=". base event $(TMPDIR)"
 
 httpd: prepare
 	-rm $(TMPDIR)/*.o $(TMPDIR)/*.c $(TMPDIR)/*.cpp
-	cp http/httpd.cpp.demo $(TMPDIR)/httpd.cpp
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event http $(TMPDIR)"
+	cp examples/httpd.cpp $(TMPDIR)/httpd.cpp
+	cp examples/httpd_conf.h $(TMPDIR)/httpd_conf.h
+	cp examples/http_api_test.h $(TMPDIR)/http_api_test.h
+	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event http http/server $(TMPDIR)"
 
 webbench: prepare
 	-rm $(TMPDIR)/*.o $(TMPDIR)/*.c $(TMPDIR)/*.cpp
-	cp http/webbench.c.demo $(TMPDIR)/webbench.c
+	cp examples/webbench.c $(TMPDIR)/webbench.c
 	$(MAKEF) TARGET=$@ SRCS="$(TMPDIR)/webbench.c"
 
 curl: prepare
 	-rm $(TMPDIR)/*.o $(TMPDIR)/*.c $(TMPDIR)/*.cpp
-	cp http/curl.cpp.demo $(TMPDIR)/curl.cpp
-	cp http/http_client.cpp.curl $(TMPDIR)/http_client.cpp
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event http $(TMPDIR)" LIBS="curl"
+	cp examples/curl.cpp $(TMPDIR)/curl.cpp
+	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event http http/client $(TMPDIR)" LIBS="curl"
 
 .PHONY: clean prepare test client server curl httpd webbench

+ 3 - 0
README.md

@@ -57,5 +57,8 @@ hw 是一套跨平台c++工具集,类名以H开头
 ## BUILD
 
 ```
+# all: test client server httpd webbench
 make all
+# curl deps libcurl
+make curl
 ```

+ 0 - 0
event/client.cpp.demo → examples/client.cpp


+ 0 - 0
http/curl.cpp.demo → examples/curl.cpp


+ 0 - 0
http/api_test.h → examples/http_api_test.h


+ 1 - 11
http/httpd.cpp.demo → examples/httpd.cpp

@@ -2,7 +2,7 @@
 #include "hmain.h"
 #include "httpd_conf.h"
 #include "http_server.h"
-#include "api_test.h"
+#include "http_api_test.h"
 
 httpd_conf_ctx_t g_conf_ctx;
 HttpService g_http_service;
@@ -134,16 +134,6 @@ int parse_confile(const char* confile) {
     if (str.size() != 0) {
         g_http_service.error_page = str;
     }
-    // file_stat_interval
-    str = g_conf_ctx.parser->GetValue("file_stat_interval");
-    if (str.size() != 0) {
-        g_conf_ctx.file_stat_interval = MAX(10, atoi(str.c_str()));
-    }
-    // file_cached_time
-    str = g_conf_ctx.parser->GetValue("file_cached_time");
-    if (str.size() != 0) {
-        g_conf_ctx.file_cached_time = MAX(60, atoi(str.c_str()));
-    }
 
     return 0;
 }

+ 0 - 4
http/httpd_conf.h → examples/httpd_conf.h

@@ -9,8 +9,6 @@ typedef struct httpd_conf_ctx_s {
     int loglevel;
     int worker_processes;
     int port;
-    int file_stat_interval;
-    int file_cached_time;
 } httpd_conf_ctx_t;
 
 extern httpd_conf_ctx_t g_conf_ctx;
@@ -20,8 +18,6 @@ inline void conf_ctx_init(httpd_conf_ctx_t* ctx) {
     ctx->loglevel = LOG_LEVEL_DEBUG;
     ctx->worker_processes = 0;
     ctx->port = 0;
-    ctx->file_stat_interval = 10;
-    ctx->file_cached_time   = 60;
 }
 
 #endif

+ 0 - 0
event/server.cpp.demo → examples/server.cpp


+ 0 - 0
http/webbench.c.demo → examples/webbench.c


+ 0 - 0
http/http_client.cpp.curl → http/client/http_client.cpp


+ 0 - 0
http/http_client.h → http/client/http_client.h


+ 10 - 2
http/FileCache.h → http/server/FileCache.h

@@ -8,7 +8,6 @@
 #include "hfile.h"
 #include "md5.h"
 #include "HttpRequest.h" // for get_content_type_str_by_suffix
-#include "httpd_conf.h"
 
 #ifndef INVALID_FD
 #define INVALID_FD  -1
@@ -34,10 +33,19 @@ typedef struct file_cache_s {
 // filepath => file_cache_t
 typedef std::map<std::string, file_cache_t*> FileCacheMap;
 
+#define DEFAULT_FILE_STAT_INTERVAL  10 // s
+#define DEFAULT_FILE_CACHED_TIME    60 // s
 class FileCache {
 public:
+    int file_stat_interval;
+    int file_cached_time;
     FileCacheMap cached_files;
 
+    FileCache() {
+        file_stat_interval  = DEFAULT_FILE_STAT_INTERVAL;
+        file_cached_time    = DEFAULT_FILE_CACHED_TIME;
+    }
+
     ~FileCache() {
         for (auto& pair : cached_files) {
             delete pair.second;
@@ -51,7 +59,7 @@ public:
         if (fc) {
             time_t tt;
             time(&tt);
-            if (tt - fc->stat_time > g_conf_ctx.file_stat_interval) {
+            if (tt - fc->stat_time > file_stat_interval) {
                 struct timespec mtime = fc->st.st_mtim;
                 stat(filepath, &fc->st);
                 fc->stat_time = tt;

+ 0 - 0
http/HttpService.h → http/server/HttpService.h


+ 2 - 3
http/http_server.cpp → http/server/http_server.cpp

@@ -6,7 +6,6 @@
 
 #include "HttpParser.h"
 #include "FileCache.h"
-#include "httpd_conf.h"
 
 #define RECV_BUFSIZE    4096
 #define SEND_BUFSIZE    4096
@@ -301,7 +300,7 @@ void handle_cached_files(htimer_t* timer, void* userdata) {
     auto iter = pfc->cached_files.begin();
     while (iter != pfc->cached_files.end()) {
         fc = iter->second;
-        if (tt - fc->stat_time > g_conf_ctx.file_cached_time) {
+        if (tt - fc->stat_time > pfc->file_cached_time) {
             delete fc;
             iter = pfc->cached_files.erase(iter);
             continue;
@@ -315,7 +314,7 @@ static void worker_proc(void* userdata) {
     int listenfd = server->listenfd;
     hloop_t loop;
     hloop_init(&loop);
-    htimer_add(&loop, handle_cached_files, &s_filecache, MAX(60000, g_conf_ctx.file_cached_time*1000));
+    htimer_add(&loop, handle_cached_files, &s_filecache, s_filecache.file_cached_time*1000);
     hevent_accept(&loop, listenfd, on_accept, server);
     hloop_run(&loop);
 }

+ 2 - 2
http/http_server.h → http/server/http_server.h

@@ -6,16 +6,16 @@
 #define DEFAULT_HTTP_PORT   80
 typedef struct http_server_s {
     int port;
-    int worker_processes;
     HttpService* service;
+    int worker_processes;
 //private:
     int listenfd;
 
 #ifdef __cplusplus
     http_server_s() {
         port = DEFAULT_HTTP_PORT;
-        worker_processes = 0;
         service = NULL;
+        worker_processes = 0;
         listenfd = -1;
     }
 #endif