Browse Source

move cpp to cpputil, let base event pure c

hewei.it 4 years ago
parent
commit
fd52ad0e13

+ 22 - 17
CMakeLists.txt

@@ -8,9 +8,10 @@ option(BUILD_STATIC "build static library" ON)
 option(BUILD_EXAMPLES "build examples" ON)
 option(BUILD_UNITTEST "build unittest" OFF)
 
-# see config.mk
+# see config.ini
 option(WITH_PROTOCOL "compile protocol" OFF)
 
+option(WITH_EVPP "compile evpp" ON)
 option(WITH_HTTP "compile http" ON)
 option(WITH_HTTP_SERVER "compile http/server" ON)
 option(WITH_HTTP_CLIENT "compile http/client" ON)
@@ -133,29 +134,33 @@ if(ANDROID)
 endif()
 
 # see Makefile
-set(ALL_SRCDIRS . base utils event protocol http http/client http/server consul examples)
-set(LIBHV_SRCDIRS . base utils event evpp)
+set(ALL_SRCDIRS . base util event protocol cpputil evpp http http/client http/server consul)
+set(LIBHV_SRCDIRS . base util event)
 set(LIBHV_HEADERS hv.h hconfig.h hexport.h)
-set(LIBHV_HEADERS ${LIBHV_HEADERS} ${BASE_HEADERS} ${UTILS_HEADERS} ${EVENT_HEADERS} ${EVPP_HEADERS})
+set(LIBHV_HEADERS ${LIBHV_HEADERS} ${BASE_HEADERS} ${UTIL_HEADERS} ${EVENT_HEADERS})
 
 if(WITH_PROTOCOL)
     set(LIBHV_HEADERS ${LIBHV_HEADERS} ${PROTOCOL_HEADERS})
     set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} protocol)
 endif()
 
-if(WITH_HTTP)
-    set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_HEADERS})
-    set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http)
-    if(WITH_HTTP_SERVER)
-        set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_SERVER_HEADERS})
-        set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/server)
-    endif()
-    if(WITH_HTTP_CLIENT)
-        set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_CLIENT_HEADERS})
-        set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/client)
-        if(WITH_CONSUL)
-            set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CONSUL_HEADERS})
-            set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} consul)
+if(WITH_EVPP)
+    set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CPPUTIL_HEADERS} ${EVPP_HEADERS})
+    set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} cpputil evpp)
+    if(WITH_HTTP)
+        set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_HEADERS})
+        set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http)
+        if(WITH_HTTP_SERVER)
+            set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_SERVER_HEADERS})
+            set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/server)
+        endif()
+        if(WITH_HTTP_CLIENT)
+            set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_CLIENT_HEADERS})
+            set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/client)
+            if(WITH_CONSUL)
+                set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CONSUL_HEADERS})
+                set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} consul)
+            endif()
         endif()
     endif()
 endif()

+ 35 - 37
Makefile

@@ -2,17 +2,20 @@ include config.mk
 include Makefile.vars
 
 MAKEF=$(MAKE) -f Makefile.in
-ALL_SRCDIRS=. base utils event evpp protocol http http/client http/server consul
+ALL_SRCDIRS=. base util event protocol cpputil evpp http http/client http/server consul
 
-LIBHV_SRCDIRS = . base utils event evpp
+LIBHV_SRCDIRS = . base util event
 LIBHV_HEADERS = hv.h hconfig.h hexport.h
-LIBHV_HEADERS += $(BASE_HEADERS) $(UTILS_HEADERS) $(EVENT_HEADERS) $(EVPP_HEADERS)
+LIBHV_HEADERS += $(BASE_HEADERS) $(UTIL_HEADERS) $(EVENT_HEADERS)
 
 ifeq ($(WITH_PROTOCOL), yes)
 LIBHV_HEADERS += $(PROTOCOL_HEADERS)
 LIBHV_SRCDIRS += protocol
 endif
 
+ifeq ($(WITH_EVPP), yes)
+LIBHV_HEADERS += $(CPPUTIL_HEADERS) $(EVPP_HEADERS)
+LIBHV_SRCDIRS += cpputil evpp
 ifeq ($(WITH_HTTP), yes)
 LIBHV_HEADERS += $(HTTP_HEADERS)
 LIBHV_SRCDIRS += http
@@ -29,6 +32,7 @@ LIBHV_SRCDIRS += consul
 endif
 endif
 endif
+endif
 
 default: all
 all: libhv examples
@@ -46,7 +50,7 @@ examples: hmain_test htimer_test hloop_test \
 
 clean:
 	$(MAKEF) clean SRCDIRS="$(ALL_SRCDIRS)"
-	$(RM) examples/*.o
+	$(RM) examples/*.o examples/httpd/*.o examples/nmap/*.o
 	$(RM) include/hv
 
 prepare:
@@ -64,7 +68,7 @@ install:
 	$(CP) lib/libhv.*  $(INSTALL_LIBDIR)
 
 hmain_test: prepare
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils" SRCS="examples/hmain_test.cpp"
+	$(MAKEF) TARGET=$@ SRCDIRS=". base cpputil" SRCS="examples/hmain_test.cpp"
 
 htimer_test: prepare
 	$(MAKEF) TARGET=$@ SRCDIRS=". base event" SRCS="examples/htimer_test.c"
@@ -91,38 +95,32 @@ nc: prepare
 	$(MAKEF) TARGET=$@ SRCDIRS=". base event" SRCS="examples/nc.c"
 
 nmap: prepare
-ifeq ($(OS), Windows)
-	# for nmap on Windows platform, recommand EVENT_POLL, not EVENT_IOCP
-	$(MAKEF) TARGET=$@ SRCDIRS=". base event" SRCS="examples/nmap.cpp" DEFINES="PRINT_DEBUG EVENT_POLL"
-else
-	$(MAKEF) TARGET=$@ SRCDIRS=". base event" SRCS="examples/nmap.cpp" DEFINES="PRINT_DEBUG"
-endif
+	$(MAKEF) TARGET=$@ SRCDIRS=". base event cpputil examples/nmap" SRCS="examples/nmap/nmap_test.cpp" DEFINES="PRINT_DEBUG"
 
 httpd: prepare
-	$(RM) examples/httpd/*.o
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event evpp http http/server examples/httpd"
+	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/server examples/httpd"
 
 curl: prepare
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event evpp http http/client" SRCS="examples/curl.cpp"
-	# $(MAKEF) TARGET=$@ SRCDIRS=". base utils event http http/client" SRCS="examples/curl.cpp" WITH_CURL=yes DEFINES="CURL_STATICLIB"
+	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client" SRCS="examples/curl.cpp"
+	# $(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client" SRCS="examples/curl.cpp" WITH_CURL=yes
 
 wget: prepare
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event evpp http http/client" SRCS="examples/wget.cpp"
+	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client" SRCS="examples/wget.cpp"
 
 http_server_test: prepare
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event evpp http http/server" SRCS="examples/http_server_test.cpp"
+	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/server" SRCS="examples/http_server_test.cpp"
 
 http_client_test: prepare
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event evpp http http/client" SRCS="examples/http_client_test.cpp"
+	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client" SRCS="examples/http_client_test.cpp"
 
 websocket_server_test: prepare
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event evpp http http/server" SRCS="examples/websocket_server_test.cpp"
+	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/server" SRCS="examples/websocket_server_test.cpp"
 
 websocket_client_test: prepare
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event evpp http http/client" SRCS="examples/websocket_client_test.cpp"
+	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client" SRCS="examples/websocket_client_test.cpp"
 
 consul_cli: prepare
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event evpp http http/client consul" SRCS="examples/consul_cli.cpp" DEFINES="PRINT_DEBUG"
+	$(MAKEF) TARGET=$@ SRCDIRS=". base event util cpputil evpp http http/client consul" SRCS="examples/consul_cli.cpp" DEFINES="PRINT_DEBUG"
 
 unittest: prepare
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/mkdir_p           unittest/mkdir_test.c         base/hbase.c
@@ -134,26 +132,26 @@ unittest: prepare
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/hmutex_test       unittest/hmutex_test.c        base/htime.c   -pthread
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/connect_test      unittest/connect_test.c       base/hsocket.c base/htime.c
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/socketpair_test   unittest/socketpair_test.c    base/hsocket.c
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase            -o bin/defer_test        unittest/defer_test.cpp
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase            -o bin/synchronized_test unittest/synchronized_test.cpp -pthread
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase            -o bin/hstring_test      unittest/hstring_test.cpp     base/hstring.cpp
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase            -o bin/threadpool_test   unittest/threadpool_test.cpp  -pthread
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase            -o bin/objectpool_test   unittest/objectpool_test.cpp  -pthread
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase            -o bin/ls                unittest/listdir_test.cpp     base/hdir.cpp
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase            -o bin/ifconfig          unittest/ifconfig_test.cpp    base/ifconfig.cpp
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/hstring_test      unittest/hstring_test.cpp     cpputil/hstring.cpp
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/ls                unittest/listdir_test.cpp     cpputil/hdir.cpp
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/ifconfig          unittest/ifconfig_test.cpp    cpputil/ifconfig.cpp
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/defer_test        unittest/defer_test.cpp
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/synchronized_test unittest/synchronized_test.cpp -pthread
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/threadpool_test   unittest/threadpool_test.cpp  -pthread
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/objectpool_test   unittest/objectpool_test.cpp  -pthread
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase -Iprotocol -o bin/nslookup          unittest/nslookup_test.c      protocol/dns.c
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase -Iprotocol -o bin/ping              unittest/ping_test.c          protocol/icmp.c base/hsocket.c base/htime.c -DPRINT_DEBUG
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase -Iprotocol -o bin/ftp               unittest/ftp_test.c           protocol/ftp.c  base/hsocket.c
-	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase -Iutils -Iprotocol -o bin/sendmail  unittest/sendmail_test.c      protocol/smtp.c base/hsocket.c utils/base64.c
+	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase -Iprotocol -Iutil -o bin/sendmail   unittest/sendmail_test.c      protocol/smtp.c base/hsocket.c util/base64.c
 
 evpp: prepare libhv
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Ievpp -o bin/EventLoop_test           evpp/EventLoop_test.cpp           -Llib -lhv -lpthread
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Ievpp -o bin/EventLoopThread_test     evpp/EventLoopThread_test.cpp     -Llib -lhv -lpthread
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Ievpp -o bin/EventLoopThreadPool_test evpp/EventLoopThreadPool_test.cpp -Llib -lhv -lpthread
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Ievpp -o bin/TcpServer_test           evpp/TcpServer_test.cpp           -Llib -lhv -lpthread
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Ievpp -o bin/TcpClient_test           evpp/TcpClient_test.cpp           -Llib -lhv -lpthread
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Ievpp -o bin/UdpServer_test           evpp/UdpServer_test.cpp           -Llib -lhv -lpthread
-	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Ievpp -o bin/UdpClient_test           evpp/UdpClient_test.cpp           -Llib -lhv -lpthread
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Icpputil -Ievpp -o bin/EventLoop_test           evpp/EventLoop_test.cpp           -Llib -lhv -lpthread
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Icpputil -Ievpp -o bin/EventLoopThread_test     evpp/EventLoopThread_test.cpp     -Llib -lhv -lpthread
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Icpputil -Ievpp -o bin/EventLoopThreadPool_test evpp/EventLoopThreadPool_test.cpp -Llib -lhv -lpthread
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Icpputil -Ievpp -o bin/TcpServer_test           evpp/TcpServer_test.cpp           -Llib -lhv -lpthread
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Icpputil -Ievpp -o bin/TcpClient_test           evpp/TcpClient_test.cpp           -Llib -lhv -lpthread
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Icpputil -Ievpp -o bin/UdpServer_test           evpp/UdpServer_test.cpp           -Llib -lhv -lpthread
+	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Ievent -Icpputil -Ievpp -o bin/UdpClient_test           evpp/UdpClient_test.cpp           -Llib -lhv -lpthread
 
 # UNIX only
 webbench: prepare
@@ -169,4 +167,4 @@ echo-servers:
 	$(CXX) -g -Wall -std=c++11 -o bin/poco_echo       echo-servers/poco_echo.cpp       -lPocoNet -lPocoUtil -lPocoFoundation
 #	$(CXX) -g -Wall -std=c++11 -o bin/muduo_echo      echo-servers/muduo_echo.cpp      -lmuduo_net -lmuduo_base -lpthread
 
-.PHONY: clean prepare libhv install examples nc nmap httpd curl wget consul_cli unittest evpp webbench echo-servers
+.PHONY: clean prepare install libhv examples unittest evpp echo-servers

+ 3 - 2
Makefile.in

@@ -13,10 +13,11 @@
 # make all \
 # TARGET=libxx \
 # TARGET_TYPE=SHARED \
+# BUILD_TYPE=DEBUG \
 # CROSS_COMPILE=arm-linux-androideabi- \
 # SRCDIRS="src/base src/event" \
-# INCDIRS="src/utils" \
-# SRCS="src/utils/hmain.cpp src/utils/iniparser.cpp" \
+# INCDIRS="src/util" \
+# SRCS="src/util/hmain.cpp src/util/iniparser.cpp" \
 # DEFINES=USE_OPENCV \
 # LIBS="opencv_core opencv_highgui"
 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

+ 19 - 20
Makefile.vars

@@ -22,30 +22,29 @@ BASE_HEADERS =  base/hplatform.h\
 				base/hssl.h\
 				base/hlog.h\
 				base/hbuf.h\
-				\
-				base/hmap.h\
-				base/hstring.h\
-				base/hfile.h\
-				base/hdir.h\
-				base/hurl.h\
-				base/hscope.h\
-				base/hthreadpool.h\
-				base/hobjectpool.h\
-				base/ifconfig.h\
-				base/ThreadLocalStorage.h\
+				base/hendian.h\
 
-UTILS_HEADERS = utils/base64.h\
-				utils/md5.h\
-				utils/sha1.h\
-				utils/json.hpp\
-				utils/singleton.h\
-				utils/iniparser.h\
-				utils/hendian.h\
-				utils/hmain.h\
+UTIL_HEADERS =  util/base64.h\
+				util/md5.h\
+				util/sha1.h\
 
 EVENT_HEADERS = event/hloop.h\
 				event/nlog.h\
-				event/nmap.h\
+
+CPPUTIL_HEADERS = cpputil/hmap.h\
+				cpputil/hstring.h\
+				cpputil/hfile.h\
+				cpputil/hdir.h\
+				cpputil/hurl.h\
+				cpputil/hmain.h\
+				cpputil/hscope.h\
+				cpputil/hthreadpool.h\
+				cpputil/hobjectpool.h\
+				cpputil/ifconfig.h\
+				cpputil/iniparser.h\
+				cpputil/json.hpp\
+				cpputil/singleton.h\
+				cpputil/ThreadLocalStorage.h\
 
 EVPP_HEADERS  = evpp/Buffer.h\
 				evpp/Callback.h\

+ 3 - 2
TREE.md

@@ -2,12 +2,13 @@
 
 ```
 .
-├── base        libhv c/c++基础设施模块,如常用宏定义、数据结构、字符串操作、日期时间、文件、目录、线程、进程、日志、套接字
+├── base        libhv基础设施,如常用宏定义、数据结构、日期时间、线程、进程、日志、套接字
 ├── bin         可执行文件安装目录
 ├── build       cmake默认构建目录
 ├── cert        SSL证书存放目录
 ├── cmake       cmake脚本存放目录
 ├── consul      consul服务注册与发现,使用http客户端实现
+├── cpputil     libhv工具类,如命令行解析、json解析、ini解析
 ├── docs        文档存放目录
 ├── echo-servers 包含libevent、libev、libuv、libhv、asio、poco、muduo等多个网络库的tcp echo server写法,并做压力测试
 ├── etc         应用程序配置目录
@@ -29,6 +30,6 @@
 ├── protocol    包含icmp、dns、ftp、smtp等协议的实现
 ├── scripts     shell脚本存放目录
 ├── unittest    单元测试代码
-└── utils       libhv utils模块,如base64、md5、json解析、ini解析
+└── util        libhv工具函数,如base64、md5、sha1
 
 ```

+ 1 - 8
base/README.md

@@ -7,27 +7,20 @@
 ├── hbase.h         基础函数
 ├── hbuf.h          缓存
 ├── hdef.h          常见宏定义
-├── hdir.h          目录(ls实现)
 ├── heap.h          堆
+├── hendian.h       大小端
 ├── herr.h          错误码表
-├── hfile.h         文件类
 ├── hlog.h          日志
 ├── hmath.h         数学函数
 ├── hmutex.h        线程同步锁
-├── hobjectpool.h   对象池
 ├── hplatform.h     平台相关宏
 ├── hproc.h         进程
-├── hscope.h        作用域模板类
 ├── hsocket.h       套接字
 ├── hssl.h          SSL/TLS
-├── hstring.h       字符串操作
 ├── hsysinfo.h      系统信息
 ├── hthread.h       线程
-├── hthreadpool.h   线程池
 ├── htime.h         时间
-├── hurl.h          URL操作
 ├── hversion.h      版本
-├── ifconfig.h      网络配置(ifconfig实现)
 ├── list.h          链表
 └── queue.h         队列
 

+ 0 - 0
utils/hendian.h → base/hendian.h


+ 22 - 20
cmake/vars.cmake

@@ -17,33 +17,35 @@ set(BASE_HEADERS
     base/hssl.h
     base/hlog.h
     base/hbuf.h
-    base/hmap.h
-    base/hstring.h
-    base/hfile.h
-    base/hdir.h
-    base/hurl.h
-    base/hscope.h
-    base/hthreadpool.h
-    base/hobjectpool.h
-    base/ifconfig.h
-    base/ThreadLocalStorage.h
+    base/hendian.h
 )
 
-set(UTILS_HEADERS
-    utils/base64.h
-    utils/md5.h
-    utils/sha1.h
-    utils/json.hpp
-    utils/singleton.h
-    utils/iniparser.h
-    utils/hendian.h
-    utils/hmain.h
+set(UTIL_HEADERS
+    util/base64.h
+    util/md5.h
+    util/sha1.h
 )
 
 set(EVENT_HEADERS
     event/hloop.h
     event/nlog.h
-    event/nmap.h
+)
+
+set(CPPUTIL_HEADERS
+    cpputil/hmap.h
+    cpputil/hstring.h
+    cpputil/hfile.h
+    cpputil/hdir.h
+    cpputil/hurl.h
+    cpputil/hmain.h
+    cpputil/hscope.h
+    cpputil/hthreadpool.h
+    cpputil/hobjectpool.h
+    cpputil/ifconfig.h
+    cpputil/iniparser.h
+    cpputil/json.hpp
+    cpputil/singleton.h
+    cpputil/ThreadLocalStorage.h
 )
 
 set(EVPP_HEADERS

+ 1 - 0
config.ini

@@ -5,6 +5,7 @@
 # include icmp dns ftp smtp
 WITH_PROTOCOL=no
 
+WITH_EVPP=yes
 WITH_HTTP=yes
 WITH_HTTP_SERVER=yes
 WITH_HTTP_CLIENT=yes

+ 2 - 1
config.mk

@@ -1,5 +1,6 @@
 
 WITH_PROTOCOL=no
+WITH_EVPP=yes
 WITH_HTTP=yes
 WITH_HTTP_SERVER=yes
 WITH_HTTP_CLIENT=yes
@@ -12,4 +13,4 @@ WITH_CURL=no
 WITH_NGHTTP2=no
 WITH_OPENSSL=no
 WITH_MBEDTLS=no
-CONFIG_DATE=20210402
+CONFIG_DATE=20210415

+ 1 - 0
configure

@@ -15,6 +15,7 @@ options:
 
 modules:
   --with-protocol       compile protocol module?        (DEFAULT: $WITH_PROTOCOL)
+  --with-evpp           compile evpp module?            (DEFAULT: $WITH_EVPP)
   --with-http           compile http module?            (DEFAULT: $WITH_HTTP)
   --with-http-client    compile http client module?     (DEFAULT: $WITH_HTTP_CLIENT)
   --with-http-server    compile http server module?     (DEFAULT: $WITH_HTTP_SERVER)

+ 0 - 0
base/RAII.cpp → cpputil/RAII.cpp


+ 19 - 0
cpputil/README.md

@@ -0,0 +1,19 @@
+## 目录结构
+
+```
+.
+├── hdir.h          目录(ls实现)
+├── hfile.h         文件类
+├── hmain.h         命令行解析
+├── hobjectpool.h   对象池
+├── hscope.h        作用域模板类
+├── hstring.h       字符串操作
+├── hthreadpool.h   线程池
+├── hurl.h          URL操作
+├── ifconfig.h      网络配置(ifconfig实现)
+├── iniparser.h     INI解析
+├── json.hpp        JSON解析
+├── singleton.h     单例模式宏
+└── ThreadLocalStorage.h 线程本地存储类
+
+```

+ 0 - 0
base/ThreadLocalStorage.cpp → cpputil/ThreadLocalStorage.cpp


+ 0 - 0
base/ThreadLocalStorage.h → cpputil/ThreadLocalStorage.h


+ 0 - 0
base/hdir.cpp → cpputil/hdir.cpp


+ 0 - 0
base/hdir.h → cpputil/hdir.h


+ 0 - 0
base/hfile.h → cpputil/hfile.h


+ 0 - 0
utils/hmain.cpp → cpputil/hmain.cpp


+ 0 - 0
utils/hmain.h → cpputil/hmain.h


+ 0 - 0
base/hmap.h → cpputil/hmap.h


+ 0 - 0
base/hobjectpool.h → cpputil/hobjectpool.h


+ 0 - 0
base/hscope.h → cpputil/hscope.h


+ 0 - 0
base/hstring.cpp → cpputil/hstring.cpp


+ 0 - 0
base/hstring.h → cpputil/hstring.h


+ 0 - 0
base/hthreadpool.h → cpputil/hthreadpool.h


+ 0 - 0
base/hurl.cpp → cpputil/hurl.cpp


+ 0 - 0
base/hurl.h → cpputil/hurl.h


+ 0 - 0
base/ifconfig.cpp → cpputil/ifconfig.cpp


+ 0 - 0
base/ifconfig.h → cpputil/ifconfig.h


+ 0 - 0
utils/iniparser.cpp → cpputil/iniparser.cpp


+ 0 - 0
utils/iniparser.h → cpputil/iniparser.h


+ 0 - 0
utils/json.hpp → cpputil/json.hpp


+ 0 - 0
utils/singleton.h → cpputil/singleton.h


+ 0 - 1
event/README.md

@@ -5,7 +5,6 @@
 ├── hloop.h     事件循环模块对外头文件
 ├── hevent.h    事件结构体定义
 ├── nlog.h      网络日志
-├── nmap.h      主机发现 (nmap实现)
 ├── iowatcher.h IO多路复用统一抽象接口
 ├── select.c    EVENT_SELECT实现
 ├── poll.c      EVENT_POLL实现

+ 13 - 10
examples/CMakeLists.txt

@@ -1,9 +1,7 @@
 list(APPEND EXAMPLES
-    hmain_test
     hloop_test
     htimer_test
     nc
-    nmap
     tcp_echo_server
     tcp_chat_server
     tcp_proxy_server
@@ -11,10 +9,7 @@ list(APPEND EXAMPLES
     udp_proxy_server
 )
 
-include_directories(.. ../base ../utils ../event ../evpp)
-
-add_executable(hmain_test hmain_test.cpp)
-target_link_libraries(hmain_test hv)
+include_directories(.. ../base ../util ../event)
 
 add_executable(hloop_test hloop_test.c)
 target_link_libraries(hloop_test hv)
@@ -25,10 +20,6 @@ target_link_libraries(htimer_test hv)
 add_executable(nc nc.c)
 target_link_libraries(nc hv)
 
-add_executable(nmap nmap.cpp)
-target_compile_definitions(nmap PRIVATE -DPRINT_DEBUG)
-target_link_libraries(nmap hv)
-
 add_executable(tcp_echo_server tcp_echo_server.c)
 target_link_libraries(tcp_echo_server hv)
 
@@ -44,6 +35,17 @@ target_link_libraries(udp_echo_server hv)
 add_executable(udp_proxy_server udp_proxy_server.c)
 target_link_libraries(udp_proxy_server hv)
 
+if(WITH_EVPP)
+    include_directories(../cpputil ../evpp)
+    add_executable(hmain_test hmain_test.cpp)
+    target_link_libraries(hmain_test hv)
+
+    aux_source_directory(nmap NMAP_SRCS)
+    add_executable(nmap ${NMAP_SRCS})
+    target_compile_definitions(nmap PRIVATE -DPRINT_DEBUG)
+    target_link_libraries(nmap hv)
+
+    list(APPEND EXAMPLES hmain_test nmap)
 if(WITH_HTTP)
     include_directories(../http)
 if(WITH_HTTP_SERVER)
@@ -104,5 +106,6 @@ if(WITH_CONSUL)
     list(APPEND EXAMPLES consul_cli)
 endif()
 endif()
+endif()
 
 add_custom_target(examples DEPENDS ${EXAMPLES})

+ 32 - 29
event/nmap.cpp → examples/nmap/nmap.cpp

@@ -1,35 +1,37 @@
 #include "nmap.h"
 #include "hloop.h"
-#include "hevent.h"
 #include "hstring.h"
+#include "hsocket.h"
 #include "netinet.h"
 
 #define MAX_RECVFROM_TIMEOUT    5000 // ms
 #define MAX_SENDTO_PERSOCKET    1024
 
-typedef struct nmap_udata_s {
+typedef struct nmap_ctx_s {
     Nmap*   nmap;
     int     send_cnt;
     int     recv_cnt;
     int     up_cnt;
     int     idle_cnt;
-} nmap_udata_t;
+} nmap_ctx_t;
 
 static void on_idle(hidle_t* idle) {
-    nmap_udata_t* udata = (nmap_udata_t*)idle->loop->userdata;
-    udata->idle_cnt++;
-    if (udata->idle_cnt == 1) {
+    hloop_t* loop = hevent_loop(idle);
+    nmap_ctx_t* ctx = (nmap_ctx_t*)hloop_userdata(loop);
+    ctx->idle_cnt++;
+    if (ctx->idle_cnt == 1) {
         // try again?
     }
-    hloop_stop(idle->loop);
+    hloop_stop(loop);
 }
 
 static void on_timer(htimer_t* timer) {
-    hloop_stop(timer->loop);
+    hloop_t* loop = hevent_loop(timer);
+    hloop_stop(loop);
 }
 
 static void on_recvfrom(hio_t* io, void* buf, int readbytes) {
-    //printd("on_recv fd=%d readbytes=%d\n", io->fd, readbytes);
+    //printd("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
     /*
     char localaddrstr[SOCKADDR_STRLEN] = {0};
     char peeraddrstr[SOCKADDR_STRLEN] = {0};
@@ -37,18 +39,19 @@ static void on_recvfrom(hio_t* io, void* buf, int readbytes) {
             SOCKADDR_STR(hio_localaddr(io), localaddrstr),
             SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
     */
-    nmap_udata_t* udata = (nmap_udata_t*)io->loop->userdata;
-    if (++udata->recv_cnt == udata->send_cnt) {
-        //hloop_stop(io->loop);
+    hloop_t* loop = hevent_loop(io);
+    nmap_ctx_t* ctx = (nmap_ctx_t*)hloop_userdata(loop);
+    if (++ctx->recv_cnt == ctx->send_cnt) {
+        //hloop_stop(loop);
     }
-    Nmap* nmap = udata->nmap;
-    struct sockaddr_in* peeraddr = (struct sockaddr_in*)io->peeraddr;
+    Nmap* nmap = ctx->nmap;
+    struct sockaddr_in* peeraddr = (struct sockaddr_in*)hio_peeraddr(io);
     auto iter = nmap->find(peeraddr->sin_addr.s_addr);
     if (iter != nmap->end()) {
         if (iter->second == 0) {
             iter->second = 1;
-            if (++udata->up_cnt == nmap->size()) {
-                hloop_stop(io->loop);
+            if (++ctx->up_cnt == nmap->size()) {
+                hloop_stop(loop);
             }
         }
     }
@@ -58,13 +61,13 @@ int nmap_discover(Nmap* nmap) {
     hloop_t* loop = hloop_new(0);
     uint64_t start_hrtime = hloop_now_hrtime(loop);
 
-    nmap_udata_t udata;
-    udata.nmap = nmap;
-    udata.send_cnt = 0;
-    udata.recv_cnt = 0;
-    udata.up_cnt = 0;
-    udata.idle_cnt = 0;
-    loop->userdata = &udata;
+    nmap_ctx_t ctx;
+    ctx.nmap = nmap;
+    ctx.send_cnt = 0;
+    ctx.recv_cnt = 0;
+    ctx.up_cnt = 0;
+    ctx.idle_cnt = 0;
+    hloop_set_userdata(loop, &ctx);
 
     char recvbuf[128];
     // icmp
@@ -80,7 +83,7 @@ int nmap_discover(Nmap* nmap) {
     hio_t* io = NULL;
     for (auto iter = nmap->begin(); iter != nmap->end(); ++iter) {
         if (iter->second == 1) continue;
-        if (udata.send_cnt % MAX_SENDTO_PERSOCKET == 0) {
+        if (ctx.send_cnt % MAX_SENDTO_PERSOCKET == 0) {
             // socket
             int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
             if (sockfd < 0) {
@@ -97,7 +100,7 @@ int nmap_discover(Nmap* nmap) {
 
             io = hio_get(loop, sockfd);
             if (io == NULL) return -1;
-            io->io_type = HIO_TYPE_IP;
+            hio_set_type(io, HIO_TYPE_IP);
             struct sockaddr_in localaddr;
             socklen_t addrlen = sizeof(localaddr);
             memset(&localaddr, 0, addrlen);
@@ -113,8 +116,8 @@ int nmap_discover(Nmap* nmap) {
         peeraddr.sin_family = AF_INET;
         peeraddr.sin_addr.s_addr = iter->first;
         hio_set_peeraddr(io, (struct sockaddr*)&peeraddr, addrlen);
-        hsendto(io->loop, io->fd, sendbuf, sizeof(sendbuf), NULL);
-        ++udata.send_cnt;
+        hsendto(loop, hio_fd(io), sendbuf, sizeof(sendbuf), NULL);
+        ++ctx.send_cnt;
     }
 
     htimer_add(loop, on_timer, MAX_RECVFROM_TIMEOUT, 1);
@@ -133,9 +136,9 @@ int nmap_discover(Nmap* nmap) {
         ++iter;
     }
     printd("Nmap done: %lu IP addresses (%d hosts up) scanned in %.2f seconds\n",
-            nmap->size(), udata.up_cnt, (end_hrtime-start_hrtime)/1000000.0f);
+            nmap->size(), ctx.up_cnt, (end_hrtime-start_hrtime)/1000000.0f);
 
-    return udata.up_cnt;
+    return ctx.up_cnt;
 }
 
 int segment_discover(const char* segment16, Nmap* nmap) {

+ 3 - 6
event/nmap.h → examples/nmap/nmap.h

@@ -3,9 +3,6 @@
 
 #include <map>
 
-#include "hexport.h"
-#include "hsocket.h"
-
 // addr => 0:down 1:up
 typedef std::map<uint32_t, int> Nmap;
 
@@ -14,8 +11,8 @@ typedef std::map<uint32_t, int> Nmap;
 // segment24: 192.168.1.x
 
 // @return up_cnt
-HV_EXPORT int nmap_discover(Nmap* nmap);
-HV_EXPORT int segment_discover(const char* segment16, Nmap* nmap);
-HV_EXPORT int host_discover(const char* segment24, Nmap* nmap);
+int nmap_discover(Nmap* nmap);
+int segment_discover(const char* segment16, Nmap* nmap);
+int host_discover(const char* segment24, Nmap* nmap);
 
 #endif // HV_NMAP_H_

+ 1 - 0
examples/nmap.cpp → examples/nmap/nmap_test.cpp

@@ -4,6 +4,7 @@
 #include <string>
 
 #include "nmap.h"
+#include "hsocket.h"
 #include "hthreadpool.h"
 
 int host_discover_task(std::string segment, void* nmap) {

BIN
html/downloads/echo-servers.png


+ 21 - 18
unittest/CMakeLists.txt

@@ -1,5 +1,6 @@
 add_definitions(-DHV_SOURCE=1)
 
+# ------base------
 add_executable(mkdir_p mkdir_test.c ../base/hbase.c)
 target_include_directories(mkdir_p PRIVATE .. ../base)
 
@@ -27,30 +28,32 @@ target_include_directories(connect_test PRIVATE .. ../base)
 add_executable(socketpair_test socketpair_test.c ../base/hsocket.c)
 target_include_directories(socketpair_test PRIVATE .. ../base)
 
+# ------cpputil------
+add_executable(hstring_test hstring_test.cpp ../cpputil/hstring.cpp)
+target_include_directories(hstring_test PRIVATE .. ../base ../cpputil)
+
+add_executable(ls listdir_test.cpp ../cpputil/hdir.cpp)
+target_include_directories(ls PRIVATE .. ../base ../cpputil)
+
+add_executable(ifconfig ifconfig_test.cpp ../cpputil/ifconfig.cpp)
+target_include_directories(ifconfig PRIVATE .. ../base ../cpputil)
+
 add_executable(defer_test defer_test.cpp)
-target_include_directories(defer_test PRIVATE .. ../base)
+target_include_directories(defer_test PRIVATE .. ../base ../cpputil)
 
 add_executable(synchronized_test synchronized_test.cpp)
-target_include_directories(synchronized_test PRIVATE .. ../base)
+target_include_directories(synchronized_test PRIVATE .. ../base ../cpputil)
 target_link_libraries(synchronized_test -lpthread)
 
-add_executable(hstring_test hstring_test.cpp ../base/hstring.cpp)
-target_include_directories(hstring_test PRIVATE .. ../base)
-
 add_executable(threadpool_test threadpool_test.cpp)
-target_include_directories(threadpool_test PRIVATE .. ../base)
+target_include_directories(threadpool_test PRIVATE .. ../base ../cpputil)
 target_link_libraries(threadpool_test -lpthread)
 
 add_executable(objectpool_test objectpool_test.cpp)
-target_include_directories(objectpool_test PRIVATE .. ../base)
+target_include_directories(objectpool_test PRIVATE .. ../base ../cpputil)
 target_link_libraries(objectpool_test -lpthread)
 
-add_executable(ls listdir_test.cpp ../base/hdir.cpp)
-target_include_directories(ls PRIVATE .. ../base)
-
-add_executable(ifconfig ifconfig_test.cpp ../base/ifconfig.cpp)
-target_include_directories(ifconfig PRIVATE .. ../base)
-
+# ------protocol------
 add_executable(nslookup nslookup_test.c ../protocol/dns.c)
 target_include_directories(nslookup PRIVATE .. ../base ../protocol)
 
@@ -61,8 +64,8 @@ target_include_directories(ping PRIVATE .. ../base ../protocol)
 add_executable(ftp ftp_test.c ../protocol/ftp.c ../base/hsocket.c)
 target_include_directories(ftp PRIVATE .. ../base ../protocol)
 
-add_executable(sendmail sendmail_test.c ../protocol/smtp.c ../base/hsocket.c ../utils/base64.c)
-target_include_directories(sendmail PRIVATE .. ../base ../utils ../protocol)
+add_executable(sendmail sendmail_test.c ../protocol/smtp.c ../base/hsocket.c ../util/base64.c)
+target_include_directories(sendmail PRIVATE .. ../base ../protocol ../util)
 
 if(UNIX)
 add_executable(webbench webbench.c)
@@ -77,13 +80,13 @@ add_custom_target(unittest DEPENDS
     hmutex_test
     connect_test
     socketpair_test
+    hstring_test
+    ls
+    ifconfig
     defer_test
     synchronized_test
-    hstring_test
     threadpool_test
     objectpool_test
-    ls
-    ifconfig
     nslookup
     ping
     ftp

+ 1 - 1
unittest/md5_test.c

@@ -1,5 +1,5 @@
 /*
- * @build: gcc -o bin/md5 unittest/md5_test.c utils/md5.c -I. -Iutils
+ * @build: gcc -o bin/md5 unittest/md5_test.c util/md5.c -I. -Iutil
  *
  */
 

+ 1 - 1
unittest/sha1_test.c

@@ -1,5 +1,5 @@
 /*
- * @build: gcc -o bin/sha1 unittest/sha1_test.c utils/sha1.c -I. -Iutils
+ * @build: gcc -o bin/sha1 unittest/sha1_test.c util/sha1.c -I. -Iutil
  *
  */
 

+ 9 - 0
util/README.md

@@ -0,0 +1,9 @@
+## 目录结构
+
+```
+.
+├── base64.h        BASE64编解码
+├── md5.h           MD5数字摘要
+└── sha1.h          SHA1安全散列算法
+
+```

+ 0 - 0
utils/base64.c → util/base64.c


+ 0 - 0
utils/base64.h → util/base64.h


+ 0 - 0
utils/md5.c → util/md5.c


+ 0 - 0
utils/md5.h → util/md5.h


+ 0 - 0
utils/sha1.c → util/sha1.c


+ 0 - 0
utils/sha1.h → util/sha1.h


+ 0 - 14
utils/README.md

@@ -1,14 +0,0 @@
-## 目录结构
-
-```
-.
-├── base64.h        BASE64编解码
-├── hendian.h       大小端
-├── hmain.h         命令行解析
-├── iniparser.h     INI解析
-├── json.hpp        JSON解析
-├── md5.h           MD5数字摘要
-├── sha1.h          SHA1安全散列算法
-└── singleton.h     单例模式宏
-
-```