Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Jul 8, 2019
1 parent 032940b commit 47b2b8b
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 31 deletions.
19 changes: 10 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@ hw 是一套跨平台c++工具集,类名以H开头
## BUILD

```
# all: test client server httpd webbench
make all
# curl deps libcurl
make curl
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 1 addition & 11 deletions http/httpd.cpp.demo → examples/httpd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 0 additions & 4 deletions http/httpd_conf.h → examples/httpd_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 10 additions & 2 deletions http/FileCache.h → http/server/FileCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -51,7 +59,7 @@ class FileCache {
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;
Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions http/http_server.cpp → http/server/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "HttpParser.h"
#include "FileCache.h"
#include "httpd_conf.h"

#define RECV_BUFSIZE 4096
#define SEND_BUFSIZE 4096
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions http/http_server.h → http/server/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 47b2b8b

Please sign in to comment.