Răsfoiți Sursa

mkdir base utils misc

ithewei 6 ani în urmă
părinte
comite
262425e75f

+ 25 - 170
Makefile

@@ -1,178 +1,33 @@
-#+++++++++++++++++++++++++++++++++configure++++++++++++++++++++++++++++++++++++++++
-# OS=Windows,Linux,Android
-# ARCH=x86,x86_64,arm,aarch64
-# CC  = $(CROSS_COMPILE)gcc
-# CXX = $(CROSS_COMPILE)g++
-# CPPFLAGS += $(addprefix -D, $(DEFINES))
-# CPPFLAGS += $(addprefix -I, $(INCDIRS))
-# LDFLAGS  += $(addprefix -L, $(LIBDIRS))
-# LDFLAGS  += $(addprefix -l, $(LIBS))
-#
-# Usage:
-# make all TARGET_TYPE=SHARED \
-# CROSS_COMPILE=arm-linux-androideabi- \
-# DEFINES=USE_OPENCV \
-# LIBS="opencv_core opencv_highgui"
-#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-#BUILD_TYPE=DEBUG,RELEASE
-BUILD_TYPE=DEBUG
-#TARGET_TYPE=EXECUTABLE,SHARED,STATIC
-TARGET_TYPE=EXECUTABLE
-
-CC 	= $(CROSS_COMPILE)gcc
-CXX = $(CROSS_COMPILE)g++
-CPP = $(CC) -E
-AS 	= $(CROSS_COMPILE)as
-LD	= $(CROSS_COMPILE)ld
-AR	= $(CROSS_COMPILE)ar
-NM	= $(CROSS_COMPILE)nm
-STRIP 	= $(CROSS_COMPILE)strip
-OBJCOPY	= $(CROSS_COMPILE)objcopy
-OBJDUMP	= $(CROSS_COMPILE)objdump
-
-MKDIR = mkdir -p
-RM = rm -r
-CP = cp -r
-
-TARGET_PLATFORM=$(shell $(CC) -v 2>&1 | grep Target | sed 's/Target: //')
-ifneq ($(findstring mingw, $(TARGET_PLATFORM)), )
-	OS=Windows
-endif
-ifneq ($(findstring android, $(TARGET_PLATFORM)), )
-	OS=Android
-endif
-ifndef OS
-	OS=Linux
-endif
-
-ifndef ARCH
-ARCH=$(shell echo 'x86_64-linux-gnu' | awk -F'-' '{print $$1}')
-endif
-
-ifeq ($(BUILD_TYPE), DEBUG)
-	DEFAULT_CFLAGS = -g
-endif
-DEFAULT_CFLAGS += -Wall -O3 -fPIC
-ifndef CFLAGS
-CFLAGS := $(DEFAULT_CFLAGS) -std=c99
-endif
-ifndef CXXFLAGS
-CXXFLAGS := $(DEFAULT_CFLAGS) -std=c++11
-endif
-ifndef ARFLAGS
-ARFLAGS := cr
-endif
-
-INCDIR = include
-LIBDIR = lib
-SRCDIR = src
-BINDIR = bin
-DEPDIR = 3rd
-CONFDIR = etc
-DISTDIR = dist
-DOCDIR  = doc
-
-SRCDIRS += $(shell find $(SRCDIR) -type d)
-INCDIRS += $(INCDIR) $(DEPDIR) $(DEPDIR)/include $(SRCDIRS)
-LIBDIRS += $(LIBDIR) $(DEPDIR)/lib $(DEPDIR)/lib/$(TARGET_PLATFORM)
-
-CPPFLAGS += $(addprefix -D, $(DEFINES))
-ifeq ($(OS), Windows)
-	CPPFLAGS += -D_WIN32_WINNT=0x600
-ifeq ($(BUILD_SHARED), true)
-	CPPFLAGS += -DDLL_EXPORTS
-endif
-endif
-CPPFLAGS += $(addprefix -I, $(INCDIRS))
-
-LDFLAGS += $(addprefix -L, $(LIBDIRS))
-ifeq ($(OS), Windows)
-	LDFLAGS += -static-libgcc -static-libstdc++
-endif
-
-#common LIBS
-LDFLAGS += $(addprefix -l, $(LIBS))
-
-ifeq ($(OS), Windows)
-	LDFLAGS += -Wl,-Bdynamic -lwinmm -liphlpapi -lws2_32
-	LDFLAGS += -Wl,-Bstatic -lstdc++ -lpthread -lm
-else
-ifeq ($(OS), Android)
-	LDFLAGS += -Wl,-Bdynamic -lstdc++ -lm -llog
-else
-	LDFLAGS += -lstdc++ -lpthread -lm
-endif
-endif
-
-$(info $(shell $(CC) --version 2>&1 | head -n 1))
-$(info TARGET_PLATFORM=$(TARGET_PLATFORM))
-$(info OS=$(OS))
-$(info ARCH=$(ARCH))
-$(info MAKE=$(MAKE))
-$(info CC=$(CC))
-$(info CXX=$(CXX))
-$(info CPPFLAGS=$(CPPFLAGS))
-$(info CFLAGS=$(CFLAGS))
-$(info CXXFLAGS=$(CXXFLAGS))
-$(info LDFLAGS=$(LDFLAGS))
-
-TARGET_NAME = test
-
-SRCS += $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c $(dir)/*.cc $(dir)/*.cpp))
-ifeq ($(SRCS), )
-	SRCS = $(wildcard *.c *.cc *.cpp)
-endif
-#OBJS += $(patsubst %.c, %.o, $(SRCS))
-#OBJS += $(patsubst %.cc, %.o, $(SRCS))
-#OBJS += $(patsubst %.cpp, %.o, $(SRCS))
-OBJS := $(addsuffix .o, $(basename $(SRCS)))
-
-$(info TARGET_TYPE=$(TARGET_TYPE))
-$(info TARGET_NAME=$(TARGET_NAME))
-$(info SRCS=$(SRCS))
-$(info OBJS=$(OBJS))
-
-default: all
-
-all: prepare $(TARGET_NAME)
-
-prepare:
-	$(MKDIR) $(BINDIR) $(LIBDIR)
-
-$(TARGET_NAME): $(OBJS)
-ifeq ($(TARGET_TYPE), SHARED)
-ifeq ($(OS), Windows)
-	$(CC) -shared $^ -o $(LIBDIR)/$@.dll $(LDFLAGS) -Wl,--output-def,$(LIBDIR)/$(@).def
-else
-	$(CC) -shared $^ -o $(LIBDIR)/$@.so $(LDFLAGS)
-endif
-else
-ifeq ($(TARGET_TYPE), STATIC)
-	$(AR) $(ARFLAGS) $(LIBDIR)/$@.a $^
-else
-ifeq ($(OS), Windows)
-	$(CC) $^ -o $(BINDIR)/$@.exe $(LDFLAGS)
-else
-	$(CC) $^ -o $(BINDIR)/$@ $(LDFLAGS)
-endif
-endif
-endif
+reset:
+	-mkdir -p test
+	-rm test/*.c test/*.cpp test/*.o;
 
 clean:
-	$(RM) $(OBJS)
-	$(RM) $(BINDIR)
-	$(RM) $(LIBDIR)
+	make clean SRCDIRS=". base utils event http test" -f Makefile.in
+
+test: reset
+	cp main.cpp.tmpl test/main.cpp
+	make TARGET="test" SRCDIRS=". base utils test" -f Makefile.in
 
-install:
+client: reset
+	cp event/client.cpp.demo test/client.cpp
+	make TARGET="client" SRCDIRS=". base event test" -f Makefile.in
 
-uninstall:
+server: reset
+	cp event/server.cpp.demo test/server.cpp
+	make TARGET="server" SRCDIRS=". base event test" -f Makefile.in
 
-dist:
-	$(MKDIR) $(DISTDIR)
-	$(CP) $(INCDIR) $(LIBDIR) $(BINDIR) $(DISTDIR)
+curl: reset
+	cp http/curl.cpp.demo test/curl.cpp
+	cp http/http_client.cpp.curl test/http_client.cpp
+	make TARGET="curl" SRCDIRS=". base utils event http test" LIBS="curl" -f Makefile.in
 
-undist:
-	$(RM) $(DISTDIR)
+httpd: reset
+	cp http/httpd.cpp.demo test/httpd.cpp
+	make TARGET="httpd" SRCDIRS=". base utils event http test" -f Makefile.in
 
-.PHONY: default all prepare clean install uninstall dist undist
+webbench: reset
+	cp http/webbench.c.demo test/webbench.c
+	make TARGET="webbench" SRCDIRS="test" -f Makefile.in
 
+.PHONY: reset clean test client server curl httpd

+ 178 - 0
Makefile.in

@@ -0,0 +1,178 @@
+#+++++++++++++++++++++++++++++++++configure++++++++++++++++++++++++++++++++++++++++
+# OS=Windows,Linux,Android
+# ARCH=x86,x86_64,arm,aarch64
+# CC  = $(CROSS_COMPILE)gcc
+# CXX = $(CROSS_COMPILE)g++
+# CPPFLAGS += $(addprefix -D, $(DEFINES))
+# CPPFLAGS += $(addprefix -I, $(INCDIRS))
+# LDFLAGS  += $(addprefix -L, $(LIBDIRS))
+# LDFLAGS  += $(addprefix -l, $(LIBS))
+#
+# Usage:
+# make all TARGET_TYPE=SHARED \
+# CROSS_COMPILE=arm-linux-androideabi- \
+# DEFINES=USE_OPENCV \
+# LIBS="opencv_core opencv_highgui"
+#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+#BUILD_TYPE=DEBUG,RELEASE
+BUILD_TYPE=DEBUG
+#TARGET_TYPE=EXECUTABLE,SHARED,STATIC
+TARGET_TYPE=EXECUTABLE
+
+CC 	= $(CROSS_COMPILE)gcc
+CXX = $(CROSS_COMPILE)g++
+CPP = $(CC) -E
+AS 	= $(CROSS_COMPILE)as
+LD	= $(CROSS_COMPILE)ld
+AR	= $(CROSS_COMPILE)ar
+NM	= $(CROSS_COMPILE)nm
+STRIP 	= $(CROSS_COMPILE)strip
+OBJCOPY	= $(CROSS_COMPILE)objcopy
+OBJDUMP	= $(CROSS_COMPILE)objdump
+
+MKDIR = mkdir -p
+RM = rm -r
+CP = cp -r
+
+TARGET_PLATFORM=$(shell $(CC) -v 2>&1 | grep Target | sed 's/Target: //')
+ifneq ($(findstring mingw, $(TARGET_PLATFORM)), )
+	OS=Windows
+endif
+ifneq ($(findstring android, $(TARGET_PLATFORM)), )
+	OS=Android
+endif
+ifndef OS
+	OS=Linux
+endif
+
+ifndef ARCH
+ARCH=$(shell echo 'x86_64-linux-gnu' | awk -F'-' '{print $$1}')
+endif
+
+ifeq ($(BUILD_TYPE), DEBUG)
+	DEFAULT_CFLAGS = -g
+endif
+DEFAULT_CFLAGS += -Wall -O3 -fPIC
+ifndef CFLAGS
+CFLAGS := $(DEFAULT_CFLAGS) -std=c99
+endif
+ifndef CXXFLAGS
+CXXFLAGS := $(DEFAULT_CFLAGS) -std=c++11
+endif
+ifndef ARFLAGS
+ARFLAGS := cr
+endif
+
+INCDIR = include
+LIBDIR = lib
+SRCDIR = src
+BINDIR = bin
+DEPDIR = 3rd
+CONFDIR = etc
+DISTDIR = dist
+DOCDIR  = doc
+
+SRCDIRS += $(shell find $(SRCDIR) -type d)
+INCDIRS += $(INCDIR) $(DEPDIR) $(DEPDIR)/include $(SRCDIRS)
+LIBDIRS += $(LIBDIR) $(DEPDIR)/lib $(DEPDIR)/lib/$(TARGET_PLATFORM)
+
+CPPFLAGS += $(addprefix -D, $(DEFINES))
+ifeq ($(OS), Windows)
+	CPPFLAGS += -D_WIN32_WINNT=0x600
+ifeq ($(BUILD_SHARED), true)
+	CPPFLAGS += -DDLL_EXPORTS
+endif
+endif
+CPPFLAGS += $(addprefix -I, $(INCDIRS))
+
+LDFLAGS += $(addprefix -L, $(LIBDIRS))
+ifeq ($(OS), Windows)
+	LDFLAGS += -static-libgcc -static-libstdc++
+endif
+
+#common LIBS
+LDFLAGS += $(addprefix -l, $(LIBS))
+
+ifeq ($(OS), Windows)
+	LDFLAGS += -Wl,-Bdynamic -lwinmm -liphlpapi -lws2_32
+	LDFLAGS += -Wl,-Bstatic -lstdc++ -lpthread -lm
+else
+ifeq ($(OS), Android)
+	LDFLAGS += -Wl,-Bdynamic -lstdc++ -lm -llog
+else
+	LDFLAGS += -lstdc++ -lpthread -lm
+endif
+endif
+
+$(info $(shell $(CC) --version 2>&1 | head -n 1))
+$(info TARGET_PLATFORM=$(TARGET_PLATFORM))
+$(info OS=$(OS))
+$(info ARCH=$(ARCH))
+$(info MAKE=$(MAKE))
+$(info CC=$(CC))
+$(info CXX=$(CXX))
+$(info CPPFLAGS=$(CPPFLAGS))
+$(info CFLAGS=$(CFLAGS))
+$(info CXXFLAGS=$(CXXFLAGS))
+$(info LDFLAGS=$(LDFLAGS))
+
+TARGET = test
+
+SRCS += $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c $(dir)/*.cc $(dir)/*.cpp))
+ifeq ($(SRCS), )
+	SRCS = $(wildcard *.c *.cc *.cpp)
+endif
+#OBJS += $(patsubst %.c, %.o, $(SRCS))
+#OBJS += $(patsubst %.cc, %.o, $(SRCS))
+#OBJS += $(patsubst %.cpp, %.o, $(SRCS))
+OBJS := $(addsuffix .o, $(basename $(SRCS)))
+
+$(info TARGET=$(TARGET))
+$(info TARGET_TYPE=$(TARGET_TYPE))
+$(info SRCS=$(SRCS))
+$(info OBJS=$(OBJS))
+
+default: all
+
+all: prepare $(TARGET)
+
+prepare:
+	$(MKDIR) $(BINDIR) $(LIBDIR)
+
+$(TARGET): $(OBJS)
+ifeq ($(TARGET_TYPE), SHARED)
+ifeq ($(OS), Windows)
+	$(CC) -shared $^ -o $(LIBDIR)/$@.dll $(LDFLAGS) -Wl,--output-def,$(LIBDIR)/$(@).def
+else
+	$(CC) -shared $^ -o $(LIBDIR)/$@.so $(LDFLAGS)
+endif
+else
+ifeq ($(TARGET_TYPE), STATIC)
+	$(AR) $(ARFLAGS) $(LIBDIR)/$@.a $^
+else
+ifeq ($(OS), Windows)
+	$(CC) $^ -o $(BINDIR)/$@.exe $(LDFLAGS)
+else
+	$(CC) $^ -o $(BINDIR)/$@ $(LDFLAGS)
+endif
+endif
+endif
+
+clean:
+	$(RM) $(OBJS)
+	$(RM) $(BINDIR)
+	$(RM) $(LIBDIR)
+
+install:
+
+uninstall:
+
+dist:
+	$(MKDIR) $(DISTDIR)
+	$(CP) $(INCDIR) $(LIBDIR) $(BINDIR) $(DISTDIR)
+
+undist:
+	$(RM) $(DISTDIR)
+
+.PHONY: default all prepare clean install uninstall dist undist
+

+ 0 - 0
hbuf.h → base/hbuf.h


+ 1 - 1
hdef.h → base/hdef.h

@@ -38,7 +38,7 @@ typedef void (*procedure_t)(void* userdata);
 #endif
 
 #ifndef INFINITE
-#define INFINITE    0xFFFFFFFF
+#define INFINITE    0xFFFFFFFFU
 #endif
 
 #ifndef CR

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


+ 0 - 0
herr.c → base/herr.c


+ 0 - 0
herr.h → base/herr.h


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


+ 0 - 0
hgui.h → base/hgui.h


+ 0 - 0
hlog.cpp → base/hlog.cpp


+ 0 - 0
hlog.h → base/hlog.h


+ 0 - 0
hmutex.h → base/hmutex.h


+ 0 - 0
hobj.h → base/hobj.h


+ 6 - 5
hplatform.h → base/hplatform.h

@@ -95,16 +95,17 @@
     #define _CRT_NONSTDC_NO_DEPRECATE
     #include <winsock2.h>
     #include <windows.h>
-    #include <process.h> // for getpid,exec
-    #include <direct.h> // for mkdir,rmdir,chdir,getcwd
+    #include <process.h>    // for getpid,exec
+    #include <direct.h>     // for mkdir,rmdir,chdir,getcwd
+    #include <io.h>         // for open,close,read,write,lseek,tell
 
     #define strcasecmp stricmp
     #define strncasecmp strnicmp
     #define MKDIR(dir) mkdir(dir)
 #else
     #include <unistd.h>
-    #include <dirent.h> // for mkdir,rmdir,chdir,getcwd
-    #include <sys/time.h>  // for gettimeofday
+    #include <dirent.h>     // for mkdir,rmdir,chdir,getcwd
+    #include <sys/time.h>   // for gettimeofday
 
     // socket
     #include <sys/types.h>
@@ -114,7 +115,7 @@
     #include <netinet/tcp.h>
     #include <netinet/udp.h>
     #include <fcntl.h>
-    #include <netdb.h> // for gethostbyname
+    #include <netdb.h>  // for gethostbyname
 
     #include <strings.h>
     #define stricmp     strcasecmp

+ 0 - 0
hproc.h → base/hproc.h


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


+ 6 - 2
hsocket.cpp → base/hsocket.cpp

@@ -29,7 +29,7 @@ int Listen(int port) {
         return -11;
     }
     struct sockaddr_in addr;
-    unsigned int addrlen = sizeof(addr);
+    socklen_t addrlen = sizeof(addr);
     memset(&addr, 0, addrlen);
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = htonl(INADDR_ANY);
@@ -50,7 +50,7 @@ int Listen(int port) {
 int Connect(const char* host, int port, int nonblock) {
     // gethostbyname -> socket -> nonblocking -> connect
     struct sockaddr_in addr;
-    int addrlen = sizeof(addr);
+    socklen_t addrlen = sizeof(addr);
     memset(&addr, 0, addrlen);
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = inet_addr(host);
@@ -69,7 +69,11 @@ int Connect(const char* host, int port, int nonblock) {
         nonblocking(connfd);
     }
     int ret = connect(connfd, (struct sockaddr*)&addr, addrlen);
+#ifdef OS_WIN
+    if (ret < 0 && sockerrno != WSAEWOULDBLOCK) {
+#else
     if (ret < 0 && sockerrno != EINPROGRESS) {
+#endif
         perror("connect");
         closesocket(connfd);
         return -30;

+ 3 - 0
hsocket.h → base/hsocket.h

@@ -12,6 +12,7 @@ int Listen(int port);
 int Connect(const char* host, int port, int nonblock = 0);
 
 #ifdef OS_WIN
+typedef int socklen_t;
 inline int blocking(int sockfd) {
     unsigned long nb = 0;
     return ioctlsocket(sockfd, FIONBIO, &nb);
@@ -21,11 +22,13 @@ inline int nonblocking(int sockfd) {
     return ioctlsocket(sockfd, FIONBIO, &nb);
 }
 #define sockerrno   WSAGetLastError()
+#define NIO_EAGAIN  WSAEWOULDBLOCK
 #else
 #define blocking(s)     fcntl(s, F_SETFL, fcntl(s, F_GETFL) & ~O_NONBLOCK)
 #define nonblocking(s)  fcntl(s, F_SETFL, fcntl(s, F_GETFL) |  O_NONBLOCK)
 #define closesocket close
 #define sockerrno   errno
+#define NIO_EAGAIN  EAGAIN
 #endif
 
 inline int tcp_nodelay(int sockfd, int on = 1) {

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


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


+ 0 - 0
hsysinfo.h → base/hsysinfo.h


+ 0 - 0
hthread.h → base/hthread.h


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


+ 0 - 0
htime.c → base/htime.c


+ 0 - 0
htime.h → base/htime.h


+ 0 - 0
hvar.h → base/hvar.h


+ 0 - 0
hversion.c → base/hversion.c


+ 0 - 0
hversion.h → base/hversion.h


+ 0 - 0
list.h → base/list.h


+ 17 - 7
h.h

@@ -5,6 +5,7 @@
  * @copyright 2018 HeWei, all rights reserved.
  */
 
+//-------------------base---------------------------
 // platform
 #include "hplatform.h"
 #include "hdef.h"
@@ -28,19 +29,28 @@
 #include "hgui.h"
 #include "hbuf.h"
 #include "hfile.h"
-#include "hbytearray.h"
 
 #include "hlog.h"
 #include "hstring.h"
 #include "hframe.h"
 #endif
 
-// misc
-/*
-#include "singleton.h"
-#include "base64.h"
-#include "iniparser.h"
+//--------------------utils-----------------------------
+#ifdef WITH_HW_UTILS
+#include "base.h"
+#include "hbytearray.h"
+#include "hframe.h"
 #include "ifconfig.h"
-*/
+#include "iniparser.h"
+#include "json.hpp"
+#include "singleton.h"
+#include "htask.h"
+#include "task_queue.h"
+#endif
+
+//--------------------misc------------------------------
+#ifdef WITH_HW_MISC
+#include "win32_getopt.h"
+#endif
 
 #endif  // HW_H_

+ 0 - 0
win32_getopt.h → misc


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


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


+ 0 - 0
hbytearray.h → utils/hbytearray.h


+ 0 - 0
hframe.cpp → utils/hframe.cpp


+ 0 - 0
hframe.h → utils/hframe.h


+ 0 - 0
hgl.h → utils/hgl.h


+ 2 - 2
hmain.cpp → utils/hmain.cpp

@@ -289,9 +289,9 @@ int create_pidfile() {
     char pid[16] = {0};
     snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
     fwrite(pid, 1, strlen(pid), fp);
-    fclose(fp); atexit(delete_pidfile);
+    fclose(fp);
     hlogi("create_pidfile [%s] pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
-
+    atexit(delete_pidfile);
     return 0;
 }
 

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


+ 0 - 0
htask.h → utils/htask.h


+ 0 - 0
ifconfig.cpp → utils/ifconfig.cpp


+ 0 - 0
ifconfig.h → utils/ifconfig.h


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


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


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


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


+ 0 - 0
task_queue.h → utils/task_queue.h