├── Makefile ├── README.md ├── assistthread.cpp ├── assistthread.h ├── bin ├── postgresql │ ├── config.ini │ ├── oneproxy-for-postgresql_win64-1.2.8.zip │ └── oneproxy-for-postgresql_x64-1.2.zip └── sqlserver │ ├── config.ini │ ├── oneproxy-for-sqlserver_win64-1.2.8.zip │ └── oneproxy-for-sqlserver_x64-1.2.zip ├── build └── superMonitor.pcap ├── clientthread.cpp ├── clientthread.h ├── commercial ├── freeimpl.cpp ├── freeimpl.h ├── strategyfactory.cpp └── strategyfactory.h ├── conf ├── config.cpp ├── config.h ├── convertutf.c ├── convertutf.h └── simpleini.h ├── config.ini ├── connection.h ├── connectmanager.cpp ├── connectmanager.h ├── define.h ├── document ├── 平民软件oneproxy-monitor使用手册.docx └── 平民软件oneproxy-monitor使用手册.pdf ├── handlemanager.cpp ├── handlemanager.h ├── httpserver ├── http.cpp ├── http.h ├── httphtml.cpp ├── httphtml.h ├── httpparse.cpp ├── httpparse.h ├── httpresponse.cpp ├── httpresponse.h ├── httpserver.cpp ├── httpserver.h ├── machinestatus.cpp └── machinestatus.h ├── iomultiplex ├── epoll.cpp ├── epoll.h ├── ioev.cpp ├── ioev.h ├── ioevent.cpp ├── ioevent.h ├── ioselect.cpp └── ioselect.h ├── lib ├── libcrypto.a ├── libcryptopp.a ├── libgmp.a ├── libpacket.a ├── libpacket_x64.a ├── libssl.a ├── libtcmalloc_minimal.a ├── libwpcap.a ├── libwpcap_x64.a └── win │ ├── Gdi32.Lib │ ├── libcrypto.a │ ├── libgdi32.a │ ├── libgmp.a │ ├── libsqlite3_win64.a │ └── libssl.a ├── main.cpp ├── managerbase.cpp ├── managerbase.h ├── monitor ├── capturedata.cpp ├── capturedata.h ├── monitor_define.h ├── monitormanager.cpp ├── monitormanager.h ├── monitortool.cpp ├── monitortool.h ├── parsedata.cpp ├── parsedata.h ├── supermonitor.cpp └── supermonitor.h ├── networksocket.cpp ├── networksocket.h ├── oneproxyserver.cpp ├── oneproxyserver.h ├── pidmanager.h ├── protocol ├── connectionpool.cpp ├── connectionpool.h ├── fake │ ├── fakeprotocol.cpp │ └── fakeprotocol.h ├── protocolbase.cpp ├── protocolbase.h ├── protocoldynamiccls.h ├── protocolfactory.cpp ├── protocolfactory.h ├── protocolpacket.cpp └── protocolpacket.h ├── sql ├── sql-tokenizer2.l ├── sqlparser.cpp ├── sqlparser.h └── sqlparser_lex.cpp ├── stats ├── database │ ├── createtable.sql │ ├── db_define.h │ ├── dbbase.cpp │ ├── dbbase.h │ ├── dbmanager.cpp │ ├── dbmanager.h │ ├── libsqlite3.a │ ├── sqlite3.h │ ├── sqlite3ext.h │ ├── sqlitedb.cpp │ ├── sqlitedb.h │ └── tab │ │ ├── sqlexectab.h │ │ ├── sqltab.h │ │ ├── sqltablereltab.h │ │ ├── tabbase.h │ │ ├── tabheader.h │ │ ├── tabletab.h │ │ ├── transinfotab.h │ │ ├── transsqlreltab.h │ │ ├── usersqlreltab.h │ │ └── usertab.h ├── record.cpp ├── record.h ├── record_define.h └── sqlite3.exe ├── tcpclient.cpp ├── tcpclient.h ├── tcpserver.cpp ├── tcpserver.h ├── test ├── testlogger.cpp ├── testlogger.h ├── testmain.cpp ├── testnetworksocket.cpp ├── testnetworksocket.h ├── teststringbuf.cpp ├── teststringbuf.h ├── testtcpclient.cpp ├── testtcpclient.h ├── testtcpserver.cpp ├── testtcpserver.h ├── testtool.cpp └── testtool.h ├── unittest ├── collectoroutput.cpp ├── compileroutput.cpp ├── config.h ├── cpptest-assert.h ├── cpptest-collectoroutput.h ├── cpptest-compileroutput.h ├── cpptest-htmloutput.h ├── cpptest-output.h ├── cpptest-source.h ├── cpptest-suite.h ├── cpptest-textoutput.h ├── cpptest-time.h ├── cpptest.h ├── htmloutput.cpp ├── missing.cpp ├── missing.h ├── source.cpp ├── suite.cpp ├── textoutput.cpp ├── time.cpp ├── utils.cpp ├── utils.h └── winconfig.h └── util ├── hitcache.h ├── keepalive.cpp ├── keepalive.h ├── logger.cpp ├── logger.h ├── md5.cpp ├── md5.h ├── memmanager.cpp ├── memmanager.h ├── mutexlock.cpp ├── mutexlock.h ├── ping.cpp ├── ping.h ├── readwritelock.cpp ├── readwritelock.h ├── socketutil.cpp ├── socketutil.h ├── stringbuf.cpp ├── stringbuf.h ├── systemapi.cpp ├── systemapi.h ├── thread.cpp ├── thread.h ├── threadtask.h ├── tool.cpp ├── tool.h ├── uspinlock.cpp ├── uspinlock.h ├── vip.cpp └── vip.h /Makefile: -------------------------------------------------------------------------------- 1 | BUILD = build 2 | BUILD_TEST_BIN = utest 3 | 4 | ifeq ($(LANG),) #windows 5 | INSTALLDIR = install 6 | LDFLAGS = -static-libgcc -static-libstdc++ -L./lib -L./ 7 | LIBS = ./lib/win/libsqlite3_win64.a ./lib/win/libssl.a ./lib/win/libcrypto.a ./lib/win/libgdi32.a -lwsock32 -lwinmm -lpacket -lwpcap -lws2_32 -lIPHLPAPI ./lib/win/libgmp.a 8 | 9 | INCLUDE = -IC:\openssl-1.0.2e\win64\include -ID:\winlib\winpcap\Include 10 | else #linux 11 | $(shell if [ ! -d $(BUILD)/$(BUILD_TEST_BIN) ]; then mkdir -p $(BUILD)/$(BUILD_TEST_BIN); fi;) 12 | INSTALLDIR = /usr/local/superoneproxy/ 13 | LDFLAGS = 14 | OPENSSL = /usr/local/openssl 15 | LIBEV = /usr/local/libev 16 | LIBPCAP = /usr/local/libpcap 17 | # -lprofiler -lunwind ./lib/libtcmalloc_minimal.a 18 | LIBS = -pthread ./lib/libtcmalloc_minimal.a ./stats/database/libsqlite3.a ./lib/libgmp.a $(OPENSSL)/lib/libssl.a $(OPENSSL)/lib/libcrypto.a $(LIBEV)/lib/libev.a $(LIBPCAP)/lib/libpcap.a -ldl 19 | INCLUDE = -I$(OPENSSL)/include -I$(LIBEV)/include -I$(LIBPCAP)/include 20 | endif 21 | 22 | CXXFLAGS = -Wall -Wformat=0 -Wno-strict-aliasing -g 23 | 24 | APPSOURCEDIR = ./sql \ 25 | ./util \ 26 | ./conf \ 27 | ./stats \ 28 | ./stats/database/ \ 29 | ./stats/database/tab \ 30 | ./monitor \ 31 | ./httpserver \ 32 | ./iomultiplex \ 33 | ./protocol \ 34 | ./protocol/fake \ 35 | ./protocol/sqlserver \ 36 | ./protocol/postgresql \ 37 | ./commercial 38 | 39 | TESTSOURCEDIR = ./test/ ./unittest/ 40 | 41 | SOURCEDIR = $(TESTSOURCEDIR) $(APPSOURCEDIR) 42 | 43 | VPATH = ./ 44 | VPATH += $(foreach tdir, $(SOURCEDIR), :$(tdir)) 45 | 46 | DIR = -I./. 47 | DIR += $(foreach tdir, $(SOURCEDIR), -I$(tdir)) 48 | DIR += $(INCLUDE) 49 | 50 | MAIN_SOURCES = main.cpp 51 | 52 | SOURCES = $(filter-out $(MAIN_SOURCES), $(wildcard *.cpp)) 53 | SOURCES += $(foreach tdir, $(APPSOURCEDIR), $(filter-out $(MAIN_SOURCES), $(wildcard $(tdir)/*.cpp))) 54 | CSOURCES = $(foreach tdir, $(APPSOURCEDIR), $(filter-out $(MAIN_SOURCES), $(wildcard $(tdir)/*.c))) 55 | 56 | HEADERS = $(wildcard ./*.h) 57 | HEADERS += $(foreach tdir, $(APPSOURCEDIR), $(wildcard $(tdir)/*.h)) 58 | 59 | ifeq ($(MAKECMDGOALS), test) 60 | SOURCES += $(wildcard test/*.cpp) $(wildcard unittest/*.cpp) 61 | HEADERS += $(wildcard test/*.h) $(wildcard unittest/*.h) 62 | CXXFLAGS += -Dprivate=public -Dprojected=public -g 63 | else 64 | SOURCES += $(MAIN_SOURCES) 65 | CXXFLAGS += -O2 66 | endif 67 | OBJS = $(patsubst %.cpp, $(BUILD)/%.o, $(notdir $(SOURCES))) 68 | COBJS = $(patsubst %.c, $(BUILD)/%.o, $(notdir $(CSOURCES))) 69 | 70 | ifeq ($(MAKECMDGOALS), test) 71 | ifeq ($(LANG),) 72 | TARGET = $(BUILD)/$(BUILD_TEST_BIN)/unittest_main.exe 73 | else 74 | TARGET = $(BUILD)/$(BUILD_TEST_BIN)/unittest_main 75 | endif 76 | else 77 | ifeq ($(LANG),) 78 | TARGET = $(BUILD)/oneproxy-for-sqlserver.exe 79 | else 80 | TARGET = $(BUILD)/oneproxy-for-sqlserver 81 | endif 82 | endif 83 | 84 | .PHONY: all 85 | all: $(TARGET) 86 | 87 | $(OBJS): $(BUILD)/%.o: %.cpp 88 | $(CXX) $(CXXFLAGS) -c $(DIR) $< -o $@ 89 | 90 | $(COBJS): $(BUILD)/%.o: %.c 91 | $(CXX) $(CXXFLAGS) -c $(DIR) $< -o $@ 92 | 93 | $(TARGET): $(OBJS) $(COBJS) 94 | $(CXX) $(LDFLAGS) -o $(TARGET) $(OBJS) $(COBJS) $(LIBS) 95 | 96 | .PHONY: test 97 | test: $(TARGET) 98 | 99 | install: 100 | -@mkdir $(INSTALLDIR) 101 | -@mkdir $(INSTALLDIR)/include/ 102 | -@mkdir $(INSTALLDIR)/include/conf/ 103 | -@echo $(HEADERS) 104 | -@cp -rf $(HEADERS) $(INSTALLDIR)/include/ 105 | -@cp -rf conf/config.h $(INSTALLDIR)/include/conf/ 106 | -@cp -rf $(TARGET) $(INSTALLDIR)/bin/ 107 | 108 | clean: 109 | -@rm $(BUILD)/*.o $(TARGET) 110 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # oneproxy-monitor 2 | 在实现一个数据库的监控器,需要实现下面的接口,在本框架中提供了一个默认的直接转发的监控部分。
3 | 必须实现的接口:
4 | 1. is_currentDatabase: 这个类是用来选择数据库类的,当connection第一次接收到数据包时,通过此类来判断当前连接是否属于这个数据库的连接,如果是则返回true。
5 | 否则返回false。如果为true,则框架将从配置中读取数据库地址和端口,连接连接,同时转发数据到这个连接上面。
6 | 2. createInstance: 框架通过调用这个函数来创建对应的实例,建议不要使用单例模式来创建,因为在这个实例中有prepared的相关信息,不同的连接可能有相同的句柄。
7 | 3. destoryInstance: 当连接完成时,会调用此函数来释放相关的内存等。
8 | 4. get_packetType:根据包的数据返回包的类型,由于不同数据库包的格式不同,类型的位置和长度不同,故需要不同的数据库协议类自己实现此接口。
9 | 可选实现的接口:
10 | 1. protocol_front: 前端接收到的数据包,都会传递给这个函数,协议类可以通过重新实现这个函数来改变默认的处理方式。框架在调用这个函数后,就直接把数据包转发到服务端了。
11 | 2. protocol_backend: 后端接收到数据包,都会传递给这个函数,协议类可以通过重新实现这个函数改变默认的处理方式。框架在调用这个函数后,就直接把数据包转发到客户端了。
12 | 3. prehandle_frontPacket:默认实现是没有处理数据包的任何内容。如果需要针对接收到的前端数据包进行处理,则需要实现此函数。比如:当客户端的数据包特别大时,被客户 端分成了多个数据包发送到服务端,在进行统计前,需要针对这些数据包进行合并,那么就需要在此包中进行合并的逻辑。
13 | 4. prehandle_backendPacket:与prehandle_frontPacket相同。不同的点是,这个函数针对接收到的后端数据包
14 | 5. protocol_initFrontStatPacket: 默认实现只是把bufpointer的指针指向接收到的数据包。如果在进行执行处理函数前,需要针对数据进行修改,那么需要实现此函数。
15 | 6. protocol_initBackendStatPacket: 与protocol_initFrontStatPacket函数功能相同,不同点是,这个针对后端数据包。
16 | 7. protocol_clearFrontStatPacket:当执行完协议类注册的处理函数后,执行此函数。默认实现是空的。
17 | 8. protocol_clearBackendStatPacket:功能与protocol_clearFrontStatPacket相同,不同的是这个函数针对后端数据包的处理。
18 | 19 | 默认具有如下的功能:
20 | 1. 可以指定多个端口,不同的端口转发到不同的数据库。也就是通过一个监控器管理多个数据库
21 | 2. 可以通过配置控制前端连接数
22 | 3. 自动根据cpu数量设置线程个数
23 | 4. 支持windows和Linux。其中windows只支持select。Linux支持select和epoll,默认为epoll方式
24 | 5. FakeProtocol 是针对包直接进行转发。
25 | 更多特性关键:https://github.com/onexsoft/oneproxy-monitor/wiki 26 | 27 | 目前平民软件基于此架构已经提供sql server, postgresql的监控功能。
28 | 监控效果可以看在线的mysql的监控效果:http://www.onexsoft.com:8080/
29 | 更多信息,请访问我们的官网:http://www.onexsoft.com 或者加入群:数据库监控 521095285。
30 | 31 | oneproxy-for-sqlserver支持功能点: 32 | 1) 直接转发并且统计sql使用情况。 33 | 2) 直接转发不统计sql使用情况。 34 | 3) 支持前后端使用不同的密码 35 | 4) 支持读写分离 36 | 5) 支持连接管理功能(类似连接池) 37 | 6) 支持监控多个端口,前端连接到不同的端口上面 38 | 7) 支持前端固定端口转发到后端固定的数据库上面 39 | 40 | oneproxy-for-postgresql 支持功能点: 41 | 1) 直接转发并且统计sql使用情况。 42 | 2) 直接转发不统计sql使用情况。 43 | 3) 支持连接管理功能(类似连接池) 44 | 4) 支持监控多个端口,前端连接到不同的端口上面 45 | 5) 支持前端固定端口转发到后端固定的数据库上面 46 | 47 | libev 下载地址:http://pkgs.fedoraproject.org/repo/pkgs/libev/ 48 | -------------------------------------------------------------------------------- /assistthread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: assistthread.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年11月9日 下午1:38:52 26 | * 27 | */ 28 | 29 | #ifndef ASSISTTHREAD_H_ 30 | #define ASSISTTHREAD_H_ 31 | 32 | #include "thread.h" 33 | #include "connectionpool.h" 34 | 35 | class AssistThread : public Thread{ 36 | private: 37 | bool is_stop; 38 | public: 39 | AssistThread(); 40 | ~AssistThread(); 41 | 42 | void stop(); 43 | bool get_stop(); 44 | 45 | bool connect_dataBaseIsOk(std::string addr, unsigned int port); 46 | void check_dataBaseActive(); 47 | private: 48 | static thread_start_func(start); 49 | 50 | }; 51 | 52 | #endif /* ASSISTTHREAD_H_ */ 53 | -------------------------------------------------------------------------------- /bin/postgresql/config.ini: -------------------------------------------------------------------------------- 1 | [oneproxy] 2 | logfile = oneproxy_log.log 3 | pidfile = oneproxy_pid.pid 4 | listen_addr = 0.0.0.0 5 | listen_port = 9999,7777,6666 6 | httpserver_addr = 0.0.0.0 7 | httpserver_port = 8080 8 | log_level = error 9 | data_dump = false 10 | log_sql = false 11 | logclientsql=false 12 | clientusername = sa 13 | clientpassword = 0000 14 | keepalive = 1 15 | acceptthreadnum = 1 16 | maxconnectnum = 40000 17 | vip_ifname = em1:0 18 | vip_address = 172.30.12.22 19 | #threadnum = 1 20 | 21 | [database_1433] 22 | host = 192.168.7.236 23 | port = 51915 24 | username = sa 25 | password = 0000 26 | 27 | [database_1434] 28 | host = 192.168.7.236 29 | port = 55555 30 | username = sa 31 | password = abcdefuz 32 | 33 | [database_1435] 34 | host = 192.168.7.236 35 | port = 30867 36 | username = sa 37 | password = 0000 38 | 39 | [database_5432] 40 | host = 172.30.12.12 41 | port = 5432 42 | username = db_user 43 | password = 0000 44 | 45 | [database_7777_5432] 46 | host = 172.30.12.7 47 | port = 5432 48 | username = db_user 49 | password = 0000 50 | 51 | [database_8888_5432] 52 | host = 172.30.12.8 53 | port = 5432 54 | username = db_user 55 | password = 0000 56 | 57 | 58 | [sqlserver_9999] 59 | dbmastergroup = database_1433 60 | dbslavegroup = database_1434 61 | classname = SSProtocol 62 | useconnectionpool = true 63 | readslave = true 64 | passwordseparate = true 65 | 66 | [sqlserver_7777] 67 | dbmastergroup = database_1435 68 | classname = SSProtocol 69 | frontport = 7777 70 | passwordseparate = false 71 | 72 | [postgresql_5432] 73 | dbmastergroup = database_5432 74 | dbslavegroup = database_7777_5432, database_8888_5432 75 | classname = PGProtocol 76 | frontport = 0 77 | passwordseparate=true 78 | useconnectionpool=true 79 | readslave = true 80 | clearbackendconnection = true 81 | 82 | -------------------------------------------------------------------------------- /bin/postgresql/oneproxy-for-postgresql_win64-1.2.8.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/bin/postgresql/oneproxy-for-postgresql_win64-1.2.8.zip -------------------------------------------------------------------------------- /bin/postgresql/oneproxy-for-postgresql_x64-1.2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/bin/postgresql/oneproxy-for-postgresql_x64-1.2.zip -------------------------------------------------------------------------------- /bin/sqlserver/config.ini: -------------------------------------------------------------------------------- 1 | [oneproxy] 2 | logfile = oneproxy_log.log 3 | pidfile = oneproxy_pid.pid 4 | listen_addr = 0.0.0.0 5 | listen_port = 9999,7777,6666 6 | httpserver_addr = 0.0.0.0 7 | httpserver_port = 8080 8 | log_level = debug 9 | data_dump = false 10 | log_sql = false 11 | logclientsql=false 12 | clientusername = sa 13 | clientpassword = 0000 14 | keepalive = 0 15 | acceptthreadnum = 1 16 | maxconnectnum = 40000 17 | usemonitor = 0 18 | monitordevicename = {69E4307F-5EB3-4076-9A18-78290796E5F7} 19 | monitorportclass = SSProtocol:51915;SSProtocol:55555;SSProtocol:30867; 20 | threadnum = 1 21 | 22 | [database_1433] 23 | host = 192.168.7.147 24 | port = 51915 25 | username = sa 26 | password = 0000 27 | 28 | [database_1434] 29 | host = 192.168.7.147 30 | port = 55555 31 | username = sa 32 | password = abcdefuz 33 | 34 | [database_1435] 35 | host = 192.168.7.147 36 | port = 30867 37 | username = sa 38 | password = 0000 39 | 40 | [database_5432] 41 | host = 172.30.12.12 42 | port = 5432 43 | username = db_user 44 | password = 0000 45 | 46 | [database_7777_5432] 47 | host = 172.30.12.7 48 | port = 5432 49 | username = db_user 50 | password = 0000 51 | 52 | [database_8888_5432] 53 | host = 172.30.12.8 54 | port = 5432 55 | username = db_user 56 | password = 0000 57 | 58 | 59 | [sqlserver_9999] 60 | dbmastergroup = database_1433 61 | dbslavegroup = database_1434 62 | classname = SSProtocol 63 | useconnectionpool = true 64 | readslave = true 65 | passwordseparate = true 66 | 67 | #[sqlserver_7777] 68 | #dbmastergroup = database_1435 69 | #classname = SSProtocol 70 | #frontport = 7777 71 | #passwordseparate = false 72 | 73 | #[postgresql_5432] 74 | #dbmastergroup = database_5432 75 | #dbslavegroup = database_7777_5432, database_8888_5432 76 | #classname = PGProtocol 77 | #frontport = 0 78 | #passwordseparate=true 79 | #useconnectionpool=true 80 | #readslave = true 81 | #clearbackendconnection = true 82 | #usemasterassalve = true 83 | 84 | -------------------------------------------------------------------------------- /bin/sqlserver/oneproxy-for-sqlserver_win64-1.2.8.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/bin/sqlserver/oneproxy-for-sqlserver_win64-1.2.8.zip -------------------------------------------------------------------------------- /bin/sqlserver/oneproxy-for-sqlserver_x64-1.2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/bin/sqlserver/oneproxy-for-sqlserver_x64-1.2.zip -------------------------------------------------------------------------------- /build/superMonitor.pcap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/build/superMonitor.pcap -------------------------------------------------------------------------------- /commercial/freeimpl.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: FreeImpl.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年12月7日 下午3:05:08 26 | * 27 | */ 28 | 29 | #include "freeimpl.h" 30 | 31 | FreeImpl::FreeImpl() { 32 | // TODO Auto-generated constructor stub 33 | 34 | } 35 | 36 | FreeImpl::~FreeImpl() { 37 | // TODO Auto-generated destructor stub 38 | } 39 | 40 | int FreeImpl::get_databaseFromGroup(Connection& con) 41 | { 42 | uif(con.database.dataBaseGroup == NULL) 43 | return -1; 44 | 45 | std::vector::iterator it = con.database.dataBaseGroup->get_dbMasterGroupVec().begin(); 46 | for (; it != con.database.dataBaseGroup->get_dbMasterGroupVec().end(); ++it) { 47 | if ((*it)->get_isActive()) { 48 | con.database.masterDataBase = *it; 49 | break; 50 | } 51 | } 52 | if (con.database.masterDataBase == NULL) {//必须需要master 53 | logs(Logger::ERR, "no valid database in config file"); 54 | return -1; 55 | } 56 | 57 | std::vector::iterator sit = con.database.dataBaseGroup->get_dbSlaveGroupVec().begin(); 58 | for (; sit != con.database.dataBaseGroup->get_dbSlaveGroupVec().end(); ++sit) { 59 | if ((*sit)->get_isActive()) { 60 | con.database.slaveDataBase = *sit; 61 | break; 62 | } 63 | } 64 | 65 | //first set current database is master database 66 | con.database.currentDataBase = con.database.masterDataBase; 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /commercial/freeimpl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: FreeImpl.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年12月7日 下午3:05:08 26 | * 27 | */ 28 | 29 | #ifndef FreeImpl_FreeImpl_H_ 30 | #define FreeImpl_FreeImpl_H_ 31 | 32 | #include "connection.h" 33 | #include "define.h" 34 | 35 | class FreeImpl { 36 | public: 37 | FreeImpl(); 38 | virtual ~FreeImpl(); 39 | 40 | virtual int get_databaseFromGroup(Connection& con); 41 | }; 42 | 43 | #endif /* FreeImpl_FreeImpl_H_ */ 44 | -------------------------------------------------------------------------------- /commercial/strategyfactory.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: strategyfactory.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年12月7日 下午5:17:10 26 | * 27 | */ 28 | 29 | #include "strategyfactory.h" 30 | #ifndef GITHUB_OPENSOURCE 31 | #include "commericialimpl.h" 32 | #endif 33 | #include "mutexlock.h" 34 | 35 | FreeImpl* StrategyFactory::impl = NULL; 36 | MutexLock StrategyFactory::mutexLock; 37 | FreeImpl* StrategyFactory::get_strategy() 38 | { 39 | if (impl == NULL) { 40 | mutexLock.lock(); 41 | if (impl == NULL) { 42 | #ifdef GITHUB_OPENSOURCE 43 | impl = new FreeImpl(); 44 | #else 45 | if (userIsFree()) { 46 | impl = new FreeImpl(); 47 | } else { 48 | impl = new CommericialImpl(); 49 | } 50 | #endif 51 | } 52 | mutexLock.unlock(); 53 | } 54 | return impl; 55 | } 56 | 57 | StrategyFactory::~StrategyFactory() { 58 | if (StrategyFactory::impl != NULL) { 59 | delete StrategyFactory::impl; 60 | StrategyFactory::impl = NULL; 61 | } 62 | } 63 | 64 | bool StrategyFactory::userIsFree() { 65 | return false; 66 | } 67 | -------------------------------------------------------------------------------- /commercial/strategyfactory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: strategyfactory.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年12月7日 下午5:15:12 26 | * 27 | */ 28 | 29 | #ifndef COMMERCIAL_STRATEGYFACTORY_H_ 30 | #define COMMERCIAL_STRATEGYFACTORY_H_ 31 | 32 | #include "freeimpl.h" 33 | #include "mutexlock.h" 34 | 35 | class StrategyFactory{ 36 | public: 37 | static FreeImpl* get_strategy(); 38 | private: 39 | ~StrategyFactory(); 40 | 41 | static bool userIsFree(); 42 | 43 | private: 44 | static MutexLock mutexLock; 45 | static FreeImpl* impl; 46 | }; 47 | 48 | 49 | 50 | #endif /* COMMERCIAL_STRATEGYFACTORY_H_ */ 51 | -------------------------------------------------------------------------------- /config.ini: -------------------------------------------------------------------------------- 1 | [oneproxy] 2 | logfile = oneproxy_log.log 3 | pidfile = oneproxy_pid.pid 4 | listen_addr = 0.0.0.0 5 | listen_port = 9999,7777,6666 6 | httpserver_addr = 0.0.0.0 7 | httpserver_port = 8080 8 | log_level = error 9 | data_dump = false 10 | log_sql = false 11 | clientusername = sa 12 | clientpassword = 0000 13 | keepalive = 0 14 | 15 | [database_1433] 16 | host = 192.168.7.52 17 | port = 51915 18 | username = sa 19 | password = 0000 20 | 21 | [database_1434] 22 | host = 192.168.7.52 23 | port = 55555 24 | username = sa 25 | password = abcdefuz 26 | 27 | [database_1435] 28 | host = 192.168.7.52 29 | port = 30867 30 | username = sa 31 | password = 0000 32 | 33 | [database_6666] 34 | host = 127.0.0.1 35 | port = 6666 36 | username = sa 37 | password = 0000 38 | 39 | [database_5432] 40 | host = 172.30.12.12 41 | port = 5432 42 | username = db_user 43 | password = 0000 44 | 45 | [database_7838] 46 | host = 127.0.0.1 47 | port = 7838 48 | username = 1111 49 | password = 1111 50 | 51 | 52 | [sqlserver_9999] 53 | dbmastergroup = database_1435 54 | dbslavegroup = database_1434 55 | classname = SSProtocol 56 | frontport = 9999 57 | useconnectionpool = true 58 | readslave = true 59 | passwordseparate = true 60 | 61 | [sqlserver_7777] 62 | dbmastergroup = database_1435 63 | classname = SSProtocol 64 | frontport = 7777 65 | passwordseparate = false 66 | 67 | [postgresql_5432] 68 | dbmastergroup = database_5432 69 | classname = PGProtocol 70 | frontport = 0 71 | 72 | 73 | -------------------------------------------------------------------------------- /connectmanager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: connectmanager.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月4日 26 | * 27 | */ 28 | 29 | #ifndef CONNECTMANAGER_H_ 30 | #define CONNECTMANAGER_H_ 31 | 32 | 33 | #include "vip.h" 34 | #include "record.h" 35 | #include "thread.h" 36 | #include "httpserver.h" 37 | #include "mutexlock.h" 38 | #include "networksocket.h" 39 | #include "assistthread.h" 40 | #include "oneproxyserver.h" 41 | #include "clientthread.h" 42 | 43 | #include 44 | #include 45 | #include 46 | #ifdef WIN32 47 | #include 48 | #else 49 | #include 50 | #endif 51 | #include "managerbase.h" 52 | 53 | class ConnectManager: public ManagerBase 54 | { 55 | public: 56 | ConnectManager(int threadNum = 5); 57 | ~ConnectManager(); 58 | void add_task(NetworkSocket *clientSocket); 59 | MutexLock* get_mutexLock(); 60 | ClientThread* get_minTaskThread(); 61 | int get_taskSize(); 62 | protected: 63 | virtual void start_child(); 64 | 65 | private: 66 | unsigned int get_allThreadTaskSize(); 67 | void alloc_task();//分配任务 68 | static void handle_signal(int sig); 69 | 70 | private: 71 | declare_type_alias(ThreadMapType, std::map)//key: threadId 72 | declare_type_alias(TaskQueue, std::queue ) 73 | 74 | int threadNum; 75 | static bool stop; 76 | ThreadMapType threadMap; //key: threadId 77 | TaskQueue taskQueue; //need to handle sock 78 | static MutexLock mutexLock; 79 | static AcceptThreadManager acceptThreadManager; 80 | }; 81 | #endif /* CONNECTMANAGER_H_ */ 82 | -------------------------------------------------------------------------------- /define.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: define.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月1日 26 | * 27 | */ 28 | 29 | #ifndef DEFINE_H_ 30 | #define DEFINE_H_ 31 | 32 | #include 33 | #include 34 | 35 | const int cf_tcp_socket_buffer = 0; 36 | const int cf_tcp_keepidle = 0; 37 | const int cf_tcp_keepintvl = 0; 38 | const int cf_tcp_keepcnt = 0; 39 | 40 | const int cf_listen_backlog = 128; 41 | 42 | #define u_uint64 unsigned long long 43 | #define u_uint8 unsigned char 44 | #define u_uint16 unsigned short int 45 | #define u_uint32 unsigned int 46 | #define u_int32 int 47 | #define u_int16 short int 48 | #define u_int8 char 49 | #define u_int64 long long 50 | 51 | #define oneproxy_version() "v1.0.0" 52 | 53 | //定义基本数据类型的类成员宏 54 | #define declare_class_member(type, memberName) \ 55 | private: type m_##memberName; \ 56 | public: type get_##memberName(){ return m_##memberName;} \ 57 | public: void set_##memberName(type value) {m_##memberName = value;} 58 | //定义类对象的类成员宏 59 | #define declare_class_member_co(type, memberName) \ 60 | private: type m_##memberName; \ 61 | public: type& get_##memberName(){ return m_##memberName;} \ 62 | public: void set_##memberName(type& value) {m_##memberName = value;} 63 | 64 | #define declare_type_alias(alias, ...) \ 65 | typedef __VA_ARGS__ alias; 66 | #define declare_clsfunc_pointer(retValType, className, funcPointerName, ...) \ 67 | typedef retValType (className::*funcPointerName)(__VA_ARGS__); 68 | 69 | #define strncmp_c(str1, str2) strncmp((const char*)str1, (const char*)str2, strlen(str2)) 70 | #define strncmp_s(str1, str2) strncmp(str1.c_str(), str2.c_str(), str2.length()) 71 | #define strncasecmp_c(str1, str2) strncasecmp((const char*)str1, (const char*)str2, strlen(str2)) 72 | #define strncasecmp_s(str1, str2) strncasecmp(str1.c_str(), str2.c_str(), str2.length()) 73 | #define strcasecmp_c(str1, str2) strcasecmp((const char*)str1, (const char*)str2) 74 | #define strcasecmp_s(str1, str2) strcasecmp((const char*)str1.c_str(), (const char*)str2.c_str()) 75 | #define cmpdata(type, first, oper, second) ((type)(first) oper (type)(second)) 76 | 77 | #define unlikely(x) __builtin_expect(!!(x), 0) 78 | #define likely(x) __builtin_expect(!!(x), 1) 79 | #define uif(x) if (unlikely(x)) 80 | #define lif(x) if (likely(x)) 81 | #define assert(x) uif(!(x)){logs(Logger::FATAL, "assert %s failed", #x);} 82 | 83 | #define forrange(var, start, end) for(var = start; var < end; ++var) 84 | typedef void (*FreeFunc) (void*); 85 | 86 | #ifdef _WIN32 87 | #ifndef SIGUSR1 88 | #define SIGUSR1 10 89 | #endif 90 | #ifndef SIGUSR2 91 | #define SIGUSR2 12 92 | #endif 93 | #endif 94 | 95 | #ifdef _WIN32 96 | #define AF_LOCAL AF_INET 97 | #endif 98 | 99 | typedef std::map KVStringMap; 100 | 101 | #endif /* DEFINE_H_ */ 102 | -------------------------------------------------------------------------------- /document/平民软件oneproxy-monitor使用手册.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/document/平民软件oneproxy-monitor使用手册.docx -------------------------------------------------------------------------------- /document/平民软件oneproxy-monitor使用手册.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/document/平民软件oneproxy-monitor使用手册.pdf -------------------------------------------------------------------------------- /httpserver/http.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: http.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月15日 26 | * 27 | */ 28 | 29 | 30 | #include "http.h" 31 | #include 32 | #include "../util/systemapi.h" 33 | 34 | int Http::gen_responseHeader() 35 | { 36 | 37 | //first line 38 | char code[4]; 39 | sprintf(code, "%d", this->responseCode); 40 | responseHeader.append(this->httpversion); 41 | responseHeader.append(" " + std::string(code)); 42 | responseHeader.append(" " + this->responseReason + CRLF); 43 | 44 | bool is_keepalive = false; 45 | bool is_close = false; 46 | std::string tv; 47 | tv = this->get_headerOption("Connection"); 48 | if (tv.compare("keep-alive") == 0) { 49 | is_keepalive = true; 50 | } else if (tv.compare("close") == 0) 51 | is_close = true; 52 | 53 | //response header option 54 | if (this->httpversionMajor == 1) { 55 | if (this->httpversionMinor >= 1) { 56 | //add date 57 | char date[50]; 58 | struct tm *cur_p = SystemApi::system_timeTM(); 59 | if (strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S GMT", cur_p) != 0) { 60 | responseHeader.append("Date:" + std::string(date) + CRLF); 61 | } 62 | } 63 | 64 | /* 65 | * if the protocol is 1.0; and the connection was keep-alive 66 | * we need to add a keep-alive header, too. 67 | */ 68 | if (this->httpversionMinor == 0 && is_keepalive) { 69 | responseHeader.append("Connection:" + std::string("keep-alive") + CRLF); 70 | } 71 | 72 | if ((this->httpversionMinor >= 1 || is_keepalive) && this->http_response_needs_body()) { 73 | //add Content-Length 74 | char buf[10]; 75 | sprintf(buf, "%d", this->outputBuf.length()); 76 | responseHeader.append("Content-Length:" + std::string(buf) + CRLF); 77 | } 78 | } 79 | 80 | if (this->http_response_needs_body()) { 81 | responseHeader.append("Content-Type:" + std::string("text/html; charset=ISO-8859-1") + CRLF); 82 | } 83 | 84 | if (is_close && is_keepalive == false) { 85 | responseHeader.append("Connection:" + std::string("close") + CRLF); 86 | } 87 | responseHeader.append(CRLF); 88 | 89 | return 0; 90 | } 91 | 92 | int Http::send_response() 93 | { 94 | if (this->gen_responseHeader()) { 95 | logs(Logger::ERR, "gen response header error"); 96 | return -1; 97 | } 98 | 99 | this->outputBuf.insert(0, this->responseHeader.c_str(), this->responseHeader.length()); 100 | 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /httpserver/httphtml.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: httphtml.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月15日 26 | * 27 | */ 28 | 29 | #ifndef HTTPSERVER_HTTPHTML_H_ 30 | #define HTTPSERVER_HTTPHTML_H_ 31 | 32 | #include "http.h" 33 | 34 | #include 35 | #include 36 | #include 37 | 38 | class Menu{ 39 | public: 40 | Menu(std::string menuName, std::string menuHref = "") { 41 | this->menuName = menuName; 42 | this->menuHref = menuHref; 43 | } 44 | 45 | void add_subMenu(Menu* subMenu) { 46 | this->subMenu.push_back(subMenu); 47 | } 48 | 49 | std::string gen_menu(); 50 | 51 | int get_subMenuSize() { 52 | return this->subMenu.size(); 53 | } 54 | 55 | std::vector& get_subMenu() { 56 | return this->subMenu; 57 | } 58 | 59 | private: 60 | std::string menuName; 61 | std::string menuHref; 62 | std::vector subMenu; 63 | }; 64 | typedef std::vector TopMenu; 65 | 66 | class HttpHtml{ 67 | public: 68 | void response_setTitle(Http& http, std::string title = "OneProxy HTTP Server"); 69 | void response_setMenu(Http& http, TopMenu menu); 70 | void response_endHtmlBody(Http& http); 71 | 72 | private: 73 | void response_setStyle(Http& http); 74 | }; 75 | 76 | 77 | 78 | #endif /* HTTPSERVER_HTTPHTML_H_ */ 79 | -------------------------------------------------------------------------------- /httpserver/httpparse.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: httpparse.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月12日 26 | * 27 | */ 28 | 29 | #ifndef HTTPSERVER_HTTPPARSE_H_ 30 | #define HTTPSERVER_HTTPPARSE_H_ 31 | 32 | #include "stringbuf.h" 33 | #include "http.h" 34 | 35 | class HttpParse{ 36 | public: 37 | int parse_httpRequest(StringBuf& req, Http& http); 38 | 39 | private: 40 | int parse_httpFirstLine(StringBuf& req, Http &http); 41 | int parse_httpUriParam(Http &http); 42 | int parse_httpHeaderOption(StringBuf &req, Http &http); 43 | 44 | int parse_httpVersion(Http &http); 45 | }; 46 | 47 | #endif /* HTTPSERVER_HTTPPARSE_H_ */ 48 | -------------------------------------------------------------------------------- /httpserver/httpserver.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: httpserver.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月11日 26 | * 27 | */ 28 | 29 | #ifndef HTTPSERVER_H_ 30 | #define HTTPSERVER_H_ 31 | 32 | #include "tcpserver.h" 33 | #include "thread.h" 34 | #include "ioevent.h" 35 | #include "httpresponse.h" 36 | 37 | #include 38 | 39 | class HttpServer : public TcpServer, public Thread{ 40 | public: 41 | HttpServer(std::string serverAddr, unsigned int serverPort, std::string threadName); 42 | virtual ~HttpServer(); 43 | virtual void accept_clientRequest(NetworkSocket *clientSocket); 44 | void stop(); 45 | 46 | private: 47 | static thread_start_func(start); 48 | static void client_request(unsigned int fd, unsigned int events, void* args); 49 | void client_finishedRequest(unsigned int fd); 50 | void http_handle_request(NetworkSocket *ns); 51 | 52 | private: 53 | typedef std::map HttpClientMap;//key: fd 54 | HttpClientMap httpClientMap; 55 | HttpResponse httpResponse; 56 | bool isStop; 57 | }; 58 | 59 | #endif /* HTTPSERVER_H_ */ 60 | -------------------------------------------------------------------------------- /iomultiplex/epoll.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: epoll.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年7月31日 26 | * 27 | */ 28 | 29 | #ifndef EPOLL_H_ 30 | #define EPOLL_H_ 31 | 32 | #include "logger.h" 33 | #include "mutexlock.h" 34 | #include "define.h" 35 | #include "record.h" 36 | #include "ioevent.h" 37 | #include "systemapi.h" 38 | 39 | #ifndef WIN32 40 | #include 41 | #endif 42 | #include 43 | #include 44 | 45 | class Epoll : public IoEvent { 46 | public: 47 | Epoll(std::string name); 48 | ~Epoll(); 49 | 50 | //event is EPOLLIN or EPOLLOUT 51 | virtual int add_ioEvent(unsigned int fd, unsigned int event, Func func, void* args); 52 | virtual int add_ioEventRead(unsigned int fd, Func func, void* args); 53 | virtual int add_ioEventWrite(unsigned int fd, Func func, void *args); 54 | virtual int add_ioEventAccept(unsigned int fd, Func func, void *args); 55 | virtual bool is_readEvent(unsigned int event); 56 | virtual bool is_writeEvent(unsigned int event); 57 | //remove fd event 58 | virtual void del_ioEvent(unsigned int fd); 59 | virtual void run_loop(); 60 | private: 61 | void run_eventWait(int epollTimeout); 62 | //run,ms 63 | virtual void run_loopWithTimeout(int epollTimeout); 64 | private: 65 | unsigned int trigerMethod;//EPOLLET or EPOLLLT, default EPOLLET 66 | unsigned int epfd; 67 | MutexLock mutexLock; 68 | const int maxEvents; 69 | }; 70 | 71 | #endif /* EPOLL_H_ */ 72 | -------------------------------------------------------------------------------- /iomultiplex/ioev.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: ioev.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年1月23日 下午4:03:11 26 | * 27 | */ 28 | 29 | #ifndef IOMULTIPLEX_IOEV_H_ 30 | #define IOMULTIPLEX_IOEV_H_ 31 | #include "ioevent.h" 32 | #ifdef linux 33 | #include 34 | 35 | typedef struct _ioev_func_args_t{ 36 | void* func_args; 37 | void* event; 38 | }IOEvFuncArgT; 39 | 40 | class IOEv : public IoEvent{ 41 | public: 42 | IOEv(std::string name); 43 | virtual ~IOEv(); 44 | virtual void stop_event(); 45 | 46 | static void ioev_cb (EV_P_ ev_io *w, int revents); 47 | virtual int add_ioEvent(unsigned int fd, unsigned int event, Func func, void* args); 48 | virtual int add_ioEventRead(unsigned int fd, Func func, void* args); 49 | virtual int add_ioEventWrite(unsigned int fd, Func func, void *args); 50 | virtual int add_ioEventAccept(unsigned int fd, Func func, void *args); 51 | virtual bool is_readEvent(unsigned int event); 52 | virtual bool is_writeEvent(unsigned int event); 53 | virtual void del_ioEvent(unsigned int fd);//remove fd event 54 | 55 | static void timerEv_cb(EV_P_ ev_timer *w, int revents); 56 | virtual int add_timerEvent(double after, double repeat, TimerFunc func, void* args); 57 | 58 | //run,ms 59 | virtual void run_loop(); 60 | virtual void stop_loop(); 61 | virtual void regester_checkQuit(); 62 | private: 63 | struct ev_loop *m_loop; 64 | int m_timerId; 65 | }; 66 | #endif 67 | 68 | #endif /* IOMULTIPLEX_IOEV_H_ */ 69 | -------------------------------------------------------------------------------- /iomultiplex/ioselect.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: ioselect.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月17日 26 | * 27 | */ 28 | 29 | #ifndef IOMULTIPLEX_IOSELECT_H_ 30 | #define IOMULTIPLEX_IOSELECT_H_ 31 | 32 | #include "ioevent.h" 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include "mutexlock.h" 38 | 39 | class IoSelect : public IoEvent { 40 | public: 41 | IoSelect(std::string name); 42 | ~IoSelect(); 43 | 44 | virtual int add_ioEvent(unsigned int fd, unsigned int event, Func func, void* args); 45 | virtual int add_ioEventRead(unsigned int fd, Func func, void* args); 46 | virtual int add_ioEventWrite(unsigned int fd, Func func, void *args); 47 | virtual int add_ioEventAccept(unsigned int fd, Func func, void *args); 48 | virtual bool is_readEvent(unsigned int event); 49 | virtual bool is_writeEvent(unsigned int event); 50 | virtual void del_ioEvent(unsigned int fd);//remove fd event 51 | virtual int add_timerEvent(double after, double repeat, TimerFunc func, void* args); 52 | 53 | virtual void run_loop(); 54 | virtual void regester_checkQuit(); 55 | 56 | private: 57 | //run,ms 58 | void run_loopWithTimeout(int timeout); 59 | private: 60 | MutexLock mutexLock; 61 | fd_set readFdSet; 62 | fd_set writeFdSet; 63 | unsigned int maxfd; 64 | std::set > fdsetSet; 65 | int loopMinTime; 66 | }; 67 | 68 | 69 | 70 | #endif /* IOMULTIPLEX_IOSELECT_H_ */ 71 | -------------------------------------------------------------------------------- /lib/libcrypto.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/libcrypto.a -------------------------------------------------------------------------------- /lib/libcryptopp.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/libcryptopp.a -------------------------------------------------------------------------------- /lib/libgmp.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/libgmp.a -------------------------------------------------------------------------------- /lib/libpacket.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/libpacket.a -------------------------------------------------------------------------------- /lib/libpacket_x64.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/libpacket_x64.a -------------------------------------------------------------------------------- /lib/libssl.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/libssl.a -------------------------------------------------------------------------------- /lib/libtcmalloc_minimal.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/libtcmalloc_minimal.a -------------------------------------------------------------------------------- /lib/libwpcap.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/libwpcap.a -------------------------------------------------------------------------------- /lib/libwpcap_x64.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/libwpcap_x64.a -------------------------------------------------------------------------------- /lib/win/Gdi32.Lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/win/Gdi32.Lib -------------------------------------------------------------------------------- /lib/win/libcrypto.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/win/libcrypto.a -------------------------------------------------------------------------------- /lib/win/libgdi32.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/win/libgdi32.a -------------------------------------------------------------------------------- /lib/win/libgmp.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/win/libgmp.a -------------------------------------------------------------------------------- /lib/win/libsqlite3_win64.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/win/libsqlite3_win64.a -------------------------------------------------------------------------------- /lib/win/libssl.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/lib/win/libssl.a -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: main.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年7月29日 26 | * 27 | */ 28 | #include 29 | 30 | #include "ping.h" 31 | #include "logger.h" 32 | #include "config.h" 33 | #include "keepalive.h" 34 | #include "connection.h" 35 | #include "connectmanager.h" 36 | #include "pidmanager.h" 37 | #include "monitormanager.h" 38 | 39 | //#include "google/profiler.h" 40 | 41 | int main(int argc, char* argv[]) { 42 | // ProfilerStart("oneproxy.prof"); 43 | //处理参数 44 | if (config()->handle_args(argc, argv)) { 45 | logs(Logger::FATAL, "args error"); 46 | return -1; 47 | } 48 | 49 | //set file descriptor number 50 | { 51 | #ifdef linux 52 | //client, master, slave. 53 | unsigned int max_file_descriptor = config()->get_maxConnectNum() * 3 + 1024; 54 | if (SystemApi::system_setFDNum(max_file_descriptor)) { 55 | logs(Logger::FATAL, "set fd number error"); 56 | return -1; 57 | } 58 | #endif 59 | } 60 | 61 | //根据需要keepalive 62 | if (config()->get_keepAlive()) { 63 | KeepAlive keepAlive; 64 | int child_exit_status = EXIT_SUCCESS; 65 | int ret = keepAlive.keepalive(&child_exit_status); 66 | if (ret <= 0) { 67 | return ret; 68 | } 69 | //else we are the child, go on 70 | } 71 | 72 | //初始化网络环境 73 | uif(SystemApi::init_networkEnv()) { 74 | logs(Logger::ERR, "init network environment error, exit(-1)"); 75 | return -1; 76 | } 77 | 78 | if (config()->get_useMonitor()) { 79 | MonitorManager mm; 80 | mm.start(); 81 | } else { 82 | //start to handle client data. 83 | ConnectManager connectManager(config()->get_threadNum()); 84 | connectManager.start(); 85 | } 86 | 87 | //卸载网络环境 88 | SystemApi::clear_networkEnv(); 89 | // ProfilerStop(); 90 | logs(Logger::ERR, "success finished ..."); 91 | 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /managerbase.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: managerbase.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月23日 下午2:50:22 26 | * 27 | */ 28 | 29 | #include "managerbase.h" 30 | #include "conf/config.h" 31 | #include "pidmanager.h" 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | 38 | ManagerBase::ManagerBase(): 39 | vipThread(config()->get_vipIfName(), config()->get_vipAddress(), std::string("vipthread")), 40 | httpServer(config()->get_httpServerAddr(), config()->get_httpServerPort(), std::string("httpserver")) 41 | { 42 | // TODO Auto-generated constructor stub 43 | this->func = NULL; 44 | } 45 | 46 | ManagerBase::~ManagerBase() { 47 | // TODO Auto-generated destructor stub 48 | vipThread.set_isStop(true); 49 | assistThread.stop(); 50 | httpServer.stop(); 51 | } 52 | 53 | void ManagerBase::start() { 54 | SystemApi::system_setThreadName("mainThread"); 55 | 56 | //handle signal 57 | if (this->func) { 58 | signal(SIGTERM, this->func); 59 | signal(SIGINT, this->func); 60 | signal(SIGUSR1, this->func); 61 | signal(SIGUSR2, this->func); 62 | } 63 | 64 | //close old process. 65 | if (config()->get_pidFilePath().size() > 0) { 66 | if (PidManager::handle_reboot(config()->get_pidFilePath().c_str())) { 67 | logs(Logger::ERR, "reboot error"); 68 | return; 69 | } 70 | } 71 | 72 | if (config()->get_pidFilePath().size()) { 73 | if(PidManager::save_pid(config()->get_pidFilePath().c_str())) { 74 | logs(Logger::ERR, "write pid error"); 75 | return; 76 | } 77 | } 78 | 79 | this->start_child(); 80 | 81 | //stop stats 82 | record()->record_stop(); 83 | 84 | // unlink pid file 85 | if (config()->get_pidFilePath().size()) { 86 | PidManager::unlink_pid(config()->get_pidFilePath().c_str()); 87 | } 88 | } 89 | 90 | void ManagerBase::set_signalHandleFunc(SignalHandleFunc func) 91 | { 92 | this->func = func; 93 | } 94 | -------------------------------------------------------------------------------- /managerbase.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: managerbase.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月23日 下午2:50:22 26 | * 27 | */ 28 | 29 | #ifndef MANAGERBASE_H_ 30 | #define MANAGERBASE_H_ 31 | 32 | #include "vip.h" 33 | #include "httpserver.h" 34 | #include "assistthread.h" 35 | #include "define.h" 36 | 37 | typedef void (*SignalHandleFunc)(int); 38 | class ManagerBase { 39 | public: 40 | ManagerBase(); 41 | virtual ~ManagerBase(); 42 | void start(); 43 | protected: 44 | virtual void start_child() = 0; 45 | void set_signalHandleFunc(SignalHandleFunc func); 46 | private: 47 | Vip vipThread; 48 | HttpServer httpServer; 49 | AssistThread assistThread; 50 | SignalHandleFunc func; 51 | }; 52 | 53 | #endif /* MANAGERBASE_H_ */ 54 | -------------------------------------------------------------------------------- /monitor/capturedata.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: capturedata.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月8日 下午4:57:04 26 | * 27 | */ 28 | 29 | #ifndef CAPTUREDATA_H_ 30 | #define CAPTUREDATA_H_ 31 | 32 | #include "define.h" 33 | #include 34 | #include 35 | #include 36 | using namespace std; 37 | 38 | typedef void (*DataHandleFunc)(u_char *, const struct pcap_pkthdr *, const u_char *); 39 | 40 | class CaptureData { 41 | public: 42 | CaptureData(DataHandleFunc func, void* funcArgs, std::string device = std::string(), const int maxPacketSize = BUFSIZ); 43 | virtual ~CaptureData(); 44 | 45 | void start_captureData(); 46 | void stop_captureData(); 47 | void gen_dumpFile(const struct pcap_pkthdr *header, const u_char *packet); 48 | int get_localIpBaseDeviceName(); 49 | int get_deviceNameBaseLocalIp(); 50 | int get_localIpListBaseDeviceName(); 51 | void add_localIp2List(std::string ip); 52 | bool is_localIp(std::string ip); 53 | bool isEmpty_localIp(); 54 | private: 55 | std::string get_deviceName(); 56 | std::string get_deviceNameBaseIp(std::string ip); 57 | void get_deviceLocalIp(); 58 | 59 | private: 60 | declare_class_member(DataHandleFunc, func); 61 | declare_class_member(void*, func_args); 62 | declare_class_member(std::string, device); 63 | declare_class_member(int, maxPacketSize); 64 | declare_class_member(std::string, localIp); 65 | declare_class_member(pcap_t*, pcapHandle); 66 | declare_class_member(pcap_dumper_t*, pdumperHandle); 67 | std::list localIpList;//one nic maybe have more than one ip. 68 | }; 69 | 70 | #endif /* CAPTUREDATA_H_ */ 71 | -------------------------------------------------------------------------------- /monitor/monitormanager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: monitormanager.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月23日 下午12:57:44 26 | * 27 | */ 28 | 29 | #ifndef MONITOR_MONITORMANAGER_H_ 30 | #define MONITOR_MONITORMANAGER_H_ 31 | 32 | #include "conf/config.h" 33 | #include "capturedata.h" 34 | #include "supermonitor.h" 35 | #include "managerbase.h" 36 | #include "pcap.h" 37 | #include "parsedata.h" 38 | 39 | class MonitorManager:public ManagerBase { 40 | public: 41 | MonitorManager(); 42 | virtual ~MonitorManager(); 43 | 44 | protected: 45 | virtual void start_child(); 46 | static void handle_signal(int sig); 47 | static void monitorData(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet); 48 | void stop_childThread(); 49 | public: 50 | ParseData* get_parseData(int port); 51 | int get_parseDataThreadSize(); 52 | private: 53 | CaptureData captureData; 54 | // SuperMonitor sm; 55 | std::vector m_superMonitorVec; 56 | std::vector m_parseDataVec; 57 | static bool m_stop; 58 | }; 59 | 60 | #endif /* MONITOR_MONITORMANAGER_H_ */ 61 | -------------------------------------------------------------------------------- /monitor/monitortool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: monitortool.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年3月15日 上午11:45:01 26 | * 27 | */ 28 | 29 | #ifndef MONITOR_MONITORTOOL_H_ 30 | #define MONITOR_MONITORTOOL_H_ 31 | 32 | #include 33 | #include 34 | 35 | #include "define.h" 36 | #include 37 | #include 38 | #include "monitor_define.h" 39 | 40 | class MonitorTool { 41 | public: 42 | MonitorTool(); 43 | virtual ~MonitorTool(); 44 | std::string get_processName(int port, bool isTcp, bool reload); 45 | private: 46 | //read progress name and port relation. 47 | void load_progress_info(); 48 | #ifdef linux 49 | void extract_type_1_socket_inode(const char lname[], long * inode_p); 50 | void extract_type_2_socket_inode(const char lname[], long * inode_p); 51 | #endif 52 | 53 | void linux_load_progress_info_bylsof(); 54 | int get_inodeBasePort(unsigned short int port, unsigned long& inode, bool isTcp); 55 | int get_inodeBasePort(unsigned short int port, unsigned long& inode, char* fileName); 56 | private: 57 | IntStringMap portNameMap;//port and progress name map. 58 | ULongStringMap nodeNameMap;//node and progress name map. 59 | }; 60 | 61 | #endif /* MONITOR_MONITORTOOL_H_ */ 62 | -------------------------------------------------------------------------------- /monitor/parsedata.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: parsedata.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月16日 下午4:16:08 26 | * 27 | */ 28 | 29 | #ifndef PARSEDATA_H_ 30 | #define PARSEDATA_H_ 31 | 32 | #include "thread.h" 33 | #include "mutexlock.h" 34 | #include "monitor_define.h" 35 | #include "monitortool.h" 36 | 37 | typedef struct _handle_func_param_t{ 38 | MonitorFunc func; 39 | void* func_args; 40 | std::string className;// className and func is exclusion. 41 | }FuncParam; 42 | typedef std::map IntFuncParamMap; 43 | typedef std::map StringFuncParamMap; 44 | 45 | class MonitorManager; 46 | class ParseData : public Thread{ 47 | public: 48 | ParseData(MonitorManager* sm); 49 | virtual ~ParseData(); 50 | void regester_protocol(); 51 | void add_taskData(TaskDataT* taskData); 52 | void add_portHandle(int port, std::string className, MonitorFunc func, void* func_args); 53 | void add_nameHandle(std::string name, std::string className, MonitorFunc func, void* func_args); 54 | void set_stop(); 55 | private: 56 | static thread_start_func(start); 57 | TaskDataT* get_taskData(); 58 | void handle_taskData(TaskDataT* taskData); 59 | void handle_protocolData(TaskDataT* taskData); 60 | void* get_protocolBase(TaskDataT* taskData); 61 | Connection* new_connection(TaskDataT* taskData); 62 | void free_connection(Connection* conn); 63 | private: 64 | declare_class_member(MutexLock, lock); 65 | declare_class_member(TaskDataList, taskDataList); 66 | declare_class_member(bool, isStop) 67 | IntFuncParamMap portHandleMap; 68 | StringFuncParamMap nameHandleMap; 69 | TaskDataKeyConnMap taskDataConnMap;//use in monitor 70 | MonitorManager* m_pMonitorManager; 71 | MonitorTool monitorTool; 72 | }; 73 | 74 | #endif /* PARSEDATA_H_ */ 75 | -------------------------------------------------------------------------------- /monitor/supermonitor.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: supermonitor.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月9日 下午3:33:23 26 | * 27 | */ 28 | 29 | #ifndef SUPERMONITOR_H_ 30 | #define SUPERMONITOR_H_ 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #include "monitor_define.h" 38 | #include "capturedata.h" 39 | #include "parsedata.h" 40 | #include "mutexlock.h" 41 | #include "thread.h" 42 | #include "define.h" 43 | 44 | class MonitorManager; 45 | class SuperMonitor :public Thread{ 46 | public: 47 | SuperMonitor(MonitorManager* mm); 48 | virtual ~SuperMonitor(); 49 | void set_stop(); 50 | static void monitorData(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet); 51 | static thread_start_func(start); 52 | private: 53 | TaskT* get_taskFromList(); 54 | void handle_task(TaskT* task); 55 | private: 56 | TaskList taskList; 57 | MutexLock taskLock; 58 | declare_class_member(bool, stop) 59 | declare_class_member(IntBoolMap, desPortMap)//this map is not empty, only handle this port. 60 | declare_class_member(CaptureData*, captureData) 61 | MonitorManager* m_monitorManager; 62 | }; 63 | 64 | #endif /* SUPERMONITOR_H_ */ 65 | -------------------------------------------------------------------------------- /oneproxyserver.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @ClassName: oneproxyserver.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年7月29日 26 | * 27 | */ 28 | 29 | #include "tool.h" 30 | #include "record.h" 31 | #include "oneproxyserver.h" 32 | #include "connectmanager.h" 33 | //#include "google/profiler.h" 34 | 35 | thread_start_func(OneproxyServer::start) 36 | { 37 | OneproxyServer *os = (OneproxyServer*)args; 38 | 39 | if (os->create_tcpServer()) { 40 | logs(Logger::FATAL, "create tcp server error"); 41 | return 0; 42 | } 43 | os->start_success(); 44 | 45 | os->get_ioEvent().regester_checkQuit(); 46 | os->get_ioEvent().run_loop(); 47 | 48 | return 0; 49 | } 50 | 51 | void OneproxyServer::accept_clientRequest(NetworkSocket *clientSocket) 52 | { 53 | if (this->connectManager == NULL) 54 | return; 55 | 56 | unsigned int clientHashCode = Tool::quick_hash_code(clientSocket->get_address().c_str(), clientSocket->get_address().length()); 57 | clientSocket->set_addressHashCode(clientHashCode); 58 | clientSocket->get_attachData().set_connTime(config()->get_globalMillisecondTime()); 59 | unsigned int connHashCode = Tool::quick_conn_hash_code(clientSocket->get_address(), 60 | clientSocket->get_port(), clientSocket->get_attachData().get_listenAddr(), 61 | clientSocket->get_attachData().get_listenPort(), clientSocket->get_attachData().get_connTime()); 62 | clientSocket->get_attachData().set_connHashCode(connHashCode); 63 | 64 | record()->record_clientQueryAddNewClient(connHashCode, 65 | clientSocket->get_addressHashCode(), 66 | clientSocket->get_address(), 67 | clientSocket->get_port(), 68 | clientSocket->get_attachData().get_listenAddr(), 69 | clientSocket->get_attachData().get_listenPort()); 70 | 71 | logs(Logger::DEBUG, "accept fd: %d", clientSocket->get_fd()); 72 | if (this->connectManager->get_taskSize() > 0) { 73 | this->connectManager->add_task(clientSocket); 74 | } else { 75 | ClientThread* ct = this->connectManager->get_minTaskThread(); 76 | if (ct != NULL) { 77 | if (ct->write_socketPair((void*)(&clientSocket), sizeof(clientSocket))) { 78 | logs(Logger::ERR, "write client connection to thread(%llu) error", ct->get_threadId()); 79 | delete clientSocket; 80 | } 81 | } else { 82 | this->connectManager->add_task(clientSocket); 83 | } 84 | } 85 | record()->record_acceptClientConn(); 86 | } 87 | -------------------------------------------------------------------------------- /protocol/fake/fakeprotocol.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: fakeprotocol.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年9月23日 26 | * 27 | */ 28 | 29 | #include "fakeprotocol.h" 30 | 31 | IMPLEMENT_CLASS(FakeProtocol) 32 | FakeProtocol::FakeProtocol() { 33 | // TODO Auto-generated constructor stub 34 | 35 | } 36 | 37 | FakeProtocol::~FakeProtocol() { 38 | // TODO Auto-generated destructor stub 39 | } 40 | 41 | ProtocolHandleRetVal FakeProtocol::protocol_front(Connection& conn) 42 | { 43 | return HANDLE_RETURN_SUCCESS; 44 | } 45 | 46 | ProtocolHandleRetVal FakeProtocol::protocol_backend(Connection& conn) 47 | { 48 | return HANDLE_RETURN_SUCCESS; 49 | } 50 | 51 | bool FakeProtocol::is_currentDatabase(Connection& conn) 52 | { 53 | return true; 54 | } 55 | 56 | void* FakeProtocol::createInstance() 57 | { 58 | return (void*)new FakeProtocol(); 59 | } 60 | 61 | void FakeProtocol::destoryInstance() 62 | { 63 | delete this; 64 | } 65 | 66 | int FakeProtocol::get_packetType(StringBuf& packet) 67 | { 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /protocol/fake/fakeprotocol.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: fakeprotocol.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年9月23日 26 | * 27 | */ 28 | 29 | #ifndef PROTOCOL_FAKE_FAKEPROTOCOL_H_ 30 | #define PROTOCOL_FAKE_FAKEPROTOCOL_H_ 31 | 32 | #include "protocolbase.h" 33 | 34 | class FakeProtocol : public ProtocolBase { 35 | public: 36 | FakeProtocol(); 37 | virtual ~FakeProtocol(); 38 | virtual ProtocolHandleRetVal protocol_front(Connection& conn); 39 | virtual ProtocolHandleRetVal protocol_backend(Connection& conn); 40 | virtual bool is_currentDatabase(Connection& conn); 41 | static void* createInstance(); 42 | virtual void destoryInstance(); 43 | virtual int get_packetType(StringBuf& packet); 44 | private: 45 | DECLARE_CLASS(FakeProtocol) 46 | }; 47 | 48 | #endif /* PROTOCOL_FAKE_FAKEPROTOCOL_H_ */ 49 | -------------------------------------------------------------------------------- /protocol/protocoldynamiccls.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: protocoldynamiccls.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月19日 26 | * 27 | */ 28 | 29 | #ifndef PROTOCOL_PROTOCOLDYNAMICCLS_H_ 30 | #define PROTOCOL_PROTOCOLDYNAMICCLS_H_ 31 | 32 | #include "protocolfactory.h" 33 | class ProtocolDynamicCls{ 34 | public: 35 | ProtocolDynamicCls(std::string name, createClass method) { 36 | ProtocolFactory::sharedClassFactory().registClass(name, method); 37 | } 38 | }; 39 | 40 | #define DECLARE_CLASS(className) \ 41 | std::string className##Name; \ 42 | static ProtocolDynamicCls* m_##className##dc ; 43 | 44 | #define IMPLEMENT_CLASS(className) \ 45 | ProtocolDynamicCls* className::m_##className##dc = \ 46 | new ProtocolDynamicCls(#className, className::createInstance) ; 47 | 48 | #endif /* PROTOCOL_PROTOCOLDYNAMICCLS_H_ */ 49 | -------------------------------------------------------------------------------- /protocol/protocolfactory.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: protocolfactory.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月19日 26 | * 27 | */ 28 | 29 | #include "protocolfactory.h" 30 | 31 | ProtocolFactory::ProtocolFactory() { 32 | // TODO Auto-generated constructor stub 33 | 34 | } 35 | 36 | ProtocolFactory::~ProtocolFactory() { 37 | // TODO Auto-generated destructor stub 38 | } 39 | 40 | void* ProtocolFactory::getClassByName(std::string className) 41 | { 42 | std::map::const_iterator iter ; 43 | 44 | iter = m_classMap.find(className) ; 45 | if ( iter == m_classMap.end() ) 46 | return NULL ; 47 | else 48 | return iter->second() ; 49 | } 50 | 51 | void ProtocolFactory::registClass(std::string name, createClass method) 52 | { 53 | m_classMap.insert(std::pair(name, method)) ; 54 | } 55 | 56 | ProtocolFactory& ProtocolFactory::sharedClassFactory() 57 | { 58 | static ProtocolFactory _sharedClassFactory ; 59 | return _sharedClassFactory ; 60 | } 61 | -------------------------------------------------------------------------------- /protocol/protocolfactory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: protocolfactory.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月19日 26 | * 27 | */ 28 | 29 | #ifndef PROTOCOL_PROTOCOLFACTORY_H_ 30 | #define PROTOCOL_PROTOCOLFACTORY_H_ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | typedef void* (*createClass)(void); 37 | 38 | class ProtocolFactory { 39 | public: 40 | ProtocolFactory(); 41 | virtual ~ProtocolFactory(); 42 | 43 | void* getClassByName(std::string className); 44 | void registClass(std::string name, createClass method); 45 | static ProtocolFactory& sharedClassFactory() ; 46 | 47 | private: 48 | std::map m_classMap ; 49 | }; 50 | 51 | #endif /* PROTOCOL_PROTOCOLFACTORY_H_ */ 52 | -------------------------------------------------------------------------------- /stats/database/createtable.sql: -------------------------------------------------------------------------------- 1 | create table user( 2 | hashcode int, //hash(caddr+cport+saddr+sport+conndatetime) 3 | caddr varchar(20), //client ip address 4 | cport int, //client port 5 | saddr varchar(20), //server ip address 6 | sport int, //server port 7 | conndatetime datetime, //connect date time 8 | sendata bigint, //the bytes of send data to server 9 | recvdata bigint //the bytes of recv data from server 10 | ); 11 | 12 | create table sql( 13 | hashcode int, //sql hashcode hash(sql) 14 | sql text, 15 | rownum int, //the sql query result 16 | querytime time, 17 | tabs int, //the table of sql 18 | exec int, //the number of sql execute. 19 | trans int,//the sql execute in translation 20 | fails int,//the number of sql execute failes 21 | recvsize int//the result's size of execute sql 22 | ); 23 | 24 | create table user_sql_rel( 25 | userhc int, 26 | sqlhc int, 27 | exectime datetime 28 | ); 29 | 30 | create table sqltable( 31 | hashcode int, //table name hashcode 32 | tabname text, //table name 33 | ); 34 | 35 | create table sql_table_rel( 36 | sqlhc int, //sql hash code 37 | tabhc int, //table hash code 38 | //sqlhc and tabhc is unique. 39 | ); 40 | 41 | create table transinfo( 42 | hashcode int, 43 | exectime datatime, 44 | starttime datetime, 45 | endtime datetime 46 | ); 47 | 48 | create table trans_sql_rel( 49 | transhc int, //transinfo hashcode 50 | sqlhc int, //sql hash code 51 | //transhc and sqlhc is unique. 52 | ); 53 | -------------------------------------------------------------------------------- /stats/database/db_define.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: db_define.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年3月2日 上午11:30:46 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_DB_DEFINE_H_ 30 | #define STATS_DATABASE_DB_DEFINE_H_ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | typedef enum _db_data_type_t{ 37 | DB_DATA_TYPE_INIT, 38 | DB_DATA_TYPE_CLIENT_INFO, 39 | DB_DATA_TYPE_CLIENT_INFO_UPDATE, 40 | DB_DATA_TYPE_CLIENT_ADD_SQL, 41 | DB_DATA_TYPE_CLIENT_EXEC_SQL, 42 | DB_DATA_TYPE_CLIENT_EXEC_SQL_UPDATE_DATASIZE, 43 | DB_DATA_TYPE_CLIENT_EXEC_SQL_UPDATE_TRANS, 44 | DB_DATA_TYPE_CLIENT_EXEC_SQL_UPDATE_ROWNUM, 45 | DB_DATA_TYPE_CLIENT_EXEC_SQL_UPDATE_FAIL, 46 | DB_DATA_TYPE_CLIENT_EXEC_TRANS, 47 | }DBDataTypeT; 48 | 49 | typedef struct db_data_t{ 50 | DBDataTypeT type; 51 | void* data; 52 | db_data_t() { 53 | type = DB_DATA_TYPE_INIT, 54 | data = NULL; 55 | } 56 | }DBDataT; 57 | typedef std::list DBDataList; 58 | 59 | #define create_instance_macro(type) \ 60 | static type* create_instance() {\ 61 | return new type();\ 62 | } 63 | #define free_instance_macro() \ 64 | void free_instance() {\ 65 | delete this;\ 66 | } 67 | #define add_instance_func(type) create_instance_macro(type)\ 68 | free_instance_macro() 69 | 70 | 71 | typedef struct _client_info_t{ 72 | u_uint32 connHashCode; 73 | std::string clientIp; 74 | int cport; 75 | std::string serverIp; 76 | int sport; 77 | u_uint32 hashcode; 78 | u_uint64 start_connect_time; 79 | u_uint64 sendSize; 80 | u_uint64 recvSize; 81 | add_instance_func(_client_info_t); 82 | } ClientInfoT; 83 | 84 | typedef struct _client_sql_ret_t{ 85 | u_uint32 conn_hashcode; 86 | u_uint32 sql_hashcode; 87 | u_uint32 sqlexec_hashcode; 88 | add_instance_func(_client_sql_ret_t); 89 | }ClientSqlRetS; 90 | 91 | typedef struct _sql_info_s{ 92 | u_uint32 connHashCode; 93 | u_uint32 sqlExecHashCode; 94 | u_uint32 sqlHashCode; 95 | std::string sql; 96 | int tabCnt; 97 | int queryType; 98 | u_uint64 execTime; 99 | std::vector tabNameVec; 100 | add_instance_func(_sql_info_s); 101 | }SqlInfoS; 102 | 103 | typedef struct _sql_exec_s{ 104 | u_uint32 sqlExecHashCode; 105 | u_uint32 connHashCode; 106 | int recvSize; 107 | int fail; 108 | int inTrans; 109 | int rowNum; 110 | add_instance_func(_sql_exec_s); 111 | }SqlExecS; 112 | 113 | typedef struct _trans_info_s{ 114 | u_uint32 connHashCode; 115 | u_uint32 transHashCode; 116 | u_uint64 start_time; 117 | u_uint64 end_time; 118 | std::vector sqlVec; 119 | add_instance_func(_trans_info_s); 120 | }TransInfoS; 121 | 122 | #endif /* STATS_DATABASE_DB_DEFINE_H_ */ 123 | -------------------------------------------------------------------------------- /stats/database/dbbase.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: dbbase.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年3月2日 下午3:43:12 26 | * 27 | */ 28 | 29 | #include "dbbase.h" 30 | #include "sqlitedb.h" 31 | #include "logger.h" 32 | 33 | DBBase::DBBase() { 34 | // TODO Auto-generated constructor stub 35 | 36 | } 37 | 38 | DBBase::~DBBase() { 39 | // TODO Auto-generated destructor stub 40 | } 41 | 42 | DBBase* DBBase::createDBObject() { 43 | SqliteDB* sdb = new SqliteDB(std::string("./monitor.db")); 44 | if (sdb == NULL) { 45 | logs(Logger::ERR, "open sqlite db error"); 46 | return NULL; 47 | } 48 | return (DBBase*)sdb; 49 | } 50 | 51 | void DBBase::destoryDBObject(DBBase* db) { 52 | SqliteDB* sdb = (SqliteDB*)db; 53 | if (sdb) { 54 | delete sdb; 55 | sdb = NULL; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /stats/database/dbbase.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: dbbase.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年3月2日 下午3:43:12 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_DBBASE_H_ 30 | #define STATS_DATABASE_DBBASE_H_ 31 | 32 | #include "tabbase.h" 33 | 34 | typedef enum _db_oper_t{ 35 | DB_OPER_SUCCESS, 36 | DB_OPER_ERROR, 37 | }DBOperT; 38 | 39 | class DBBase { 40 | public: 41 | DBBase(); 42 | virtual ~DBBase(); 43 | static DBBase* createDBObject(); 44 | void destoryDBObject(DBBase* db); 45 | 46 | virtual DBOperT save_newClient(TabBase* tabBase) = 0; 47 | virtual DBOperT update_clientData(TabBase* tabBase) = 0; 48 | virtual DBOperT save_clientSqlRel(TabBase* tabBase) = 0; 49 | virtual DBOperT save_sqlExecRecord(TabBase* tabBase) = 0; 50 | virtual DBOperT save_newSql(TabBase* tabBase) = 0; 51 | virtual DBOperT save_newTable(TabBase* tabBase) = 0; 52 | virtual DBOperT save_sqlTableRel(TabBase* tabBase) = 0; 53 | virtual DBOperT update_sqlExec(TabBase* tabBase) = 0; 54 | virtual DBOperT save_transInfo(TabBase* tabBase) = 0; 55 | virtual DBOperT save_transSqlRel(TabBase* tabBase) = 0; 56 | private: 57 | 58 | }; 59 | 60 | #endif /* STATS_DATABASE_DBBASE_H_ */ 61 | -------------------------------------------------------------------------------- /stats/database/dbmanager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: dbmanager.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年3月2日 上午9:59:15 26 | * 27 | */ 28 | 29 | #ifndef STATS_DBMANAGER_H_ 30 | #define STATS_DBMANAGER_H_ 31 | 32 | #include "define.h" 33 | #include "db_define.h" 34 | #include "threadtask.h" 35 | #include "dbbase.h" 36 | 37 | class DBManager : public ThreadTask{ 38 | private: 39 | DBManager(); 40 | public: 41 | static DBManager* instance(); 42 | virtual ~DBManager(); 43 | virtual int init_childThread(); 44 | private: 45 | static void start(void* data, void* args); 46 | struct tm millisecond2tm(u_uint64 mst); 47 | private: 48 | DBBase *dbBase; 49 | static DBManager* pDBManager; 50 | static MutexLock lock; 51 | }; 52 | 53 | #endif /* STATS_DBMANAGER_H_ */ 54 | -------------------------------------------------------------------------------- /stats/database/libsqlite3.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/stats/database/libsqlite3.a -------------------------------------------------------------------------------- /stats/database/sqlitedb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: sqlitedb.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月28日 下午2:53:23 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_SQLITEDB_H_ 30 | #define STATS_DATABASE_SQLITEDB_H_ 31 | 32 | #include "sqlite3.h" 33 | #include "define.h" 34 | #include "dbbase.h" 35 | 36 | class SqliteDB : public DBBase{ 37 | public: 38 | SqliteDB(); 39 | SqliteDB(std::string dbPath); 40 | virtual ~SqliteDB(); 41 | 42 | virtual DBOperT save_newClient(TabBase* tabBase); 43 | virtual DBOperT update_clientData(TabBase* tabBase); 44 | 45 | virtual DBOperT save_clientSqlRel(TabBase* tabBase); 46 | virtual DBOperT save_sqlExecRecord(TabBase* tabBase); 47 | virtual DBOperT save_newSql(TabBase* tabBase); 48 | virtual DBOperT save_newTable(TabBase* tabBase); 49 | virtual DBOperT save_sqlTableRel(TabBase* tabBase); 50 | virtual DBOperT update_sqlExec(TabBase* tabBase); 51 | virtual DBOperT save_transInfo(TabBase* tabBase); 52 | virtual DBOperT save_transSqlRel(TabBase* tabBase); 53 | 54 | private: 55 | DBOperT open_sqlite(); 56 | DBOperT create_tables(); 57 | DBOperT is_existedRecord(std::string sql, int& result); 58 | static int judgeTableExisted(void* param, int nColumn, char** columnName, char** columnValue); 59 | private: 60 | sqlite3* pDB; 61 | declare_class_member(std::string, dbPath); 62 | }; 63 | 64 | #endif /* STATS_DATABASE_SQLITEDB_H_ */ 65 | -------------------------------------------------------------------------------- /stats/database/tab/sqlexectab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: sqlexectab.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年3月6日 下午3:29:33 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_TAB_SQLEXECTAB_H_ 30 | #define STATS_DATABASE_TAB_SQLEXECTAB_H_ 31 | #include "tabbase.h" 32 | #include "define.h" 33 | #include 34 | 35 | class SqlExecTabT : public TabBase { 36 | public: 37 | SqlExecTabT(){ 38 | this->m_connHashCode = 0; 39 | this->m_fails = 0; 40 | this->m_execHashcode = 0; 41 | this->m_recvsize = 0; 42 | this->m_rownum = 0; 43 | this->m_trans = 0; 44 | }; 45 | virtual ~SqlExecTabT(){}; 46 | 47 | virtual std::string createTableSql() { 48 | std::string ct = "create table if not exists sql_exec(" 49 | "connhashcode int4," 50 | "exechashcode int4," 51 | "rownum int," 52 | "querytime time," 53 | "trans bool," 54 | "fails bool," 55 | "recvsize int);"; 56 | return ct; 57 | } 58 | 59 | virtual std::string insertDataSql(void* data) { 60 | assert(data); 61 | SqlExecTabT* sett = (SqlExecTabT*)data; 62 | std::stringstream ss; 63 | ss << "insert into sql_exec(connhashcode, exechashcode, rownum, querytime, trans, fails, recvsize) values("; 64 | ss << sett->get_connHashCode() << ","; 65 | ss << sett->get_execHashcode() << ","; 66 | ss << sett->get_rownum() << ","; 67 | ss << "'" << this->tm2string(sett->get_querytime()) << "',"; 68 | ss << sett->get_trans() << ","; 69 | ss << sett->get_fails() << ","; 70 | ss << sett->get_recvsize() << ");"; 71 | return ss.str(); 72 | } 73 | 74 | virtual std::string updateDataSql(void* data) { 75 | assert(data); 76 | SqlExecTabT* sett = (SqlExecTabT*)data; 77 | std::stringstream ss; 78 | ss << " update " << get_tableName() << " set rownum = rownum + " << sett->get_rownum(); 79 | ss << " , trans = trans + " << sett->get_trans() << ", fails = fails + " << sett->get_fails(); 80 | ss << " , recvsize = recvsize + " << sett->get_recvsize() ; 81 | ss << " where connhashcode=" << sett->get_connHashCode(); 82 | ss << " and exechashcode=" << sett->get_execHashcode() << ";"; 83 | 84 | return ss.str(); 85 | } 86 | 87 | virtual std::string get_tableName() { 88 | return "sql_exec"; 89 | } 90 | private: 91 | declare_class_member(unsigned int , connHashCode)//connection hashcode . 92 | declare_class_member(unsigned int, execHashcode);//sql hashcode hash(sql) 93 | declare_class_member(int, rownum); //the sql query result 94 | declare_class_member(struct tm, querytime); 95 | declare_class_member(bool, trans);//true: Execute in the translation; or false. 96 | declare_class_member(bool, fails);//true: Execute fails, or success. 97 | declare_class_member(int, recvsize);//the result's size of execute sql 98 | }; 99 | 100 | #endif /* STATS_DATABASE_TAB_SQLEXECTAB_H_ */ 101 | -------------------------------------------------------------------------------- /stats/database/tab/sqltab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: sqltab.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月28日 下午3:23:21 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_TAB_SQLTAB_H_ 30 | #define STATS_DATABASE_TAB_SQLTAB_H_ 31 | 32 | #include "tabbase.h" 33 | #include "define.h" 34 | #include 35 | 36 | class SqlTabT : public TabBase { 37 | public: 38 | SqlTabT() { 39 | this->m_hashcode = 0; 40 | this->m_tabs = 0; 41 | this->m_sqlType = 0; 42 | }; 43 | virtual ~SqlTabT(){}; 44 | 45 | virtual std::string createTableSql() { 46 | std::string ct = "create table if not exists sql(" 47 | "hashcode int4," 48 | "sql text," 49 | "tabs int," 50 | "sqlType int" 51 | ");"; 52 | return ct; 53 | } 54 | 55 | virtual std::string insertDataSql(void* data) { 56 | assert(data); 57 | SqlTabT* stt = (SqlTabT*)data; 58 | std::stringstream ss; 59 | ss << "insert into sql(hashcode, sql, tabs, sqltype) values("; 60 | ss << stt->get_hashcode() << ",'"; 61 | ss << stt->get_sql() << "',"; 62 | ss << stt->get_tabs() << ","; 63 | ss << stt->get_sqlType() << ");"; 64 | return ss.str(); 65 | } 66 | 67 | virtual std::string get_tableName() { 68 | return "sql"; 69 | } 70 | private: 71 | declare_class_member(unsigned int, hashcode);//sql hashcode hash(sql) 72 | declare_class_member(std::string, sql); 73 | declare_class_member(int, tabs);//the table of sql 74 | declare_class_member(int, sqlType);//insert, select, udpate, delete. 75 | }; 76 | #endif /* STATS_DATABASE_TAB_SQLTAB_H_ */ 77 | -------------------------------------------------------------------------------- /stats/database/tab/sqltablereltab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: sqltablereltab.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月28日 下午3:25:13 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_TAB_SQLTABLERELTAB_H_ 30 | #define STATS_DATABASE_TAB_SQLTABLERELTAB_H_ 31 | 32 | #include "tabbase.h" 33 | #include "define.h" 34 | 35 | class SqlTableRelTabT: public TabBase{ 36 | public: 37 | SqlTableRelTabT() { 38 | this->m_sqlhc = 0; 39 | this->m_tabhc = 0; 40 | }; 41 | virtual ~SqlTableRelTabT() {}; 42 | 43 | virtual std::string createTableSql() { 44 | std::string ct = 45 | "create table if not exists sql_table_rel(" 46 | "sqlhc int4, tabhc int4);"; 47 | return ct; 48 | } 49 | 50 | virtual std::string insertDataSql(void* data) { 51 | assert(data); 52 | SqlTableRelTabT* strt = (SqlTableRelTabT*)data; 53 | std::stringstream ss; 54 | ss << "insert into sql_table_rel(sqlhc, tabhc)values("; 55 | ss << strt->get_sqlhc() << ","; 56 | ss << strt->get_tabhc() << ");"; 57 | 58 | return ss.str(); 59 | } 60 | 61 | virtual std::string get_tableName() { 62 | return "sql_table_rel"; 63 | } 64 | private: 65 | declare_class_member(unsigned int, sqlhc); 66 | declare_class_member(unsigned int, tabhc); 67 | }; 68 | #endif /* STATS_DATABASE_TAB_SQLTABLERELTAB_H_ */ 69 | -------------------------------------------------------------------------------- /stats/database/tab/tabbase.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: tabbase.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月28日 下午4:15:48 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_TAB_TABBASE_H_ 30 | #define STATS_DATABASE_TAB_TABBASE_H_ 31 | #include 32 | #include 33 | #include 34 | #include 35 | class TabBase{ 36 | public: 37 | TabBase(){}; 38 | virtual ~TabBase(){}; 39 | virtual std::string createTableSql() = 0; 40 | virtual std::string insertDataSql(void* data) = 0; 41 | virtual std::string get_tableName() = 0; 42 | std::string tm2string(struct tm tm_t) { 43 | char buf[32] = {0}; 44 | strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tm_t); 45 | return std::string(buf, strlen(buf)); 46 | } 47 | }; 48 | #endif /* STATS_DATABASE_TAB_TABBASE_H_ */ 49 | -------------------------------------------------------------------------------- /stats/database/tab/tabheader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: tabheader.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年3月2日 下午5:37:59 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_TAB_TABHEADER_H_ 30 | #define STATS_DATABASE_TAB_TABHEADER_H_ 31 | 32 | #include "sqltab.h" 33 | #include "sqltablereltab.h" 34 | #include "tabletab.h" 35 | #include "transinfotab.h" 36 | #include "transsqlreltab.h" 37 | #include "usersqlreltab.h" 38 | #include "usertab.h" 39 | #include "sqlexectab.h" 40 | 41 | #endif /* STATS_DATABASE_TAB_TABHEADER_H_ */ 42 | -------------------------------------------------------------------------------- /stats/database/tab/tabletab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: tabletab.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月28日 下午3:24:38 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_TAB_TABLETAB_H_ 30 | #define STATS_DATABASE_TAB_TABLETAB_H_ 31 | 32 | #include "tabbase.h" 33 | #include "define.h" 34 | 35 | class TableTabT: public TabBase { 36 | public: 37 | TableTabT() { 38 | this->m_hashcode = 0; 39 | }; 40 | virtual ~TableTabT(){}; 41 | 42 | virtual std::string createTableSql() { 43 | std::string ct = 44 | "create table if not exists sqltable(" 45 | "hashcode int4, tabname text);"; 46 | return ct; 47 | } 48 | 49 | virtual std::string insertDataSql(void* data) { 50 | assert(data); 51 | TableTabT* ttt = (TableTabT*)data; 52 | std::stringstream ss; 53 | ss << "insert into sqltable(hashcode, tabname) values("; 54 | ss << ttt->get_hashcode() << ", '" << ttt->get_tabName() << "');"; 55 | return ss.str(); 56 | } 57 | 58 | virtual std::string get_tableName() { 59 | return "sqltable"; 60 | } 61 | private: 62 | declare_class_member(unsigned int, hashcode); 63 | declare_class_member(std::string, tabName); 64 | }; 65 | 66 | #endif /* STATS_DATABASE_TAB_TABLETAB_H_ */ 67 | -------------------------------------------------------------------------------- /stats/database/tab/transinfotab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: transinfotab.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月28日 下午3:26:06 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_TAB_TRANSINFOTAB_H_ 30 | #define STATS_DATABASE_TAB_TRANSINFOTAB_H_ 31 | 32 | #include "define.h" 33 | #include "tabbase.h" 34 | 35 | class TransInfoTabT: public TabBase{ 36 | public: 37 | TransInfoTabT(){ 38 | this->m_transhashcode = 0; 39 | this->m_connhashcode = 0; 40 | this->m_exectime = 0; 41 | }; 42 | virtual ~TransInfoTabT(){}; 43 | virtual std::string createTableSql() { 44 | std::string ct = 45 | "create table if not exists transinfo(" 46 | "connhashcode int4," 47 | "transhashcode int4," 48 | "exectime int4," 49 | "starttime datetime, endtime datetime);"; 50 | return ct; 51 | } 52 | 53 | virtual std::string insertDataSql(void* data) { 54 | assert(data); 55 | TransInfoTabT* titt = (TransInfoTabT*)data; 56 | std::stringstream ss; 57 | ss << "insert into transinfo(connhashcode, transhashcode, exectime, starttime, endtime)values("; 58 | ss << titt->get_connhashcode() << ", " << titt->get_transhashcode() << ", " 59 | << titt->get_exectime() << ", '" 60 | << this->tm2string(titt->get_starttime()) << "','" 61 | << this->tm2string(titt->get_endtime()) << "');"; 62 | 63 | return ss.str(); 64 | } 65 | 66 | virtual std::string get_tableName() { 67 | return "transinfo"; 68 | } 69 | private: 70 | declare_class_member(unsigned int, connhashcode); 71 | declare_class_member(unsigned int, transhashcode); 72 | declare_class_member(unsigned int, exectime); 73 | declare_class_member(struct tm, starttime); 74 | declare_class_member(struct tm, endtime); 75 | }; 76 | 77 | #endif /* STATS_DATABASE_TAB_TRANSINFOTAB_H_ */ 78 | -------------------------------------------------------------------------------- /stats/database/tab/transsqlreltab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: transsqlreltab.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月28日 下午3:26:53 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_TAB_TRANSSQLRELTAB_H_ 30 | #define STATS_DATABASE_TAB_TRANSSQLRELTAB_H_ 31 | 32 | #include "tabbase.h" 33 | #include "define.h" 34 | 35 | class TransSqlRelTabT: public TabBase { 36 | public: 37 | TransSqlRelTabT() { 38 | this->m_sqlhc = 0; 39 | this->m_transhc = 0; 40 | }; 41 | virtual ~TransSqlRelTabT(){}; 42 | 43 | virtual std::string createTableSql() { 44 | std::string ct = "create table if not exists trans_sql_rel(" 45 | "transhc int4, sqlhc int4);"; 46 | return ct; 47 | } 48 | 49 | virtual std::string insertDataSql(void* data) { 50 | std::stringstream ss; 51 | TransSqlRelTabT *tsrtt = (TransSqlRelTabT*)data; 52 | ss << "insert into trans_sql_rel(transhc, sqlhc)values("; 53 | ss << tsrtt->get_transhc() << "," << tsrtt->get_sqlhc() << ");"; 54 | return ss.str(); 55 | } 56 | 57 | virtual std::string get_tableName() { 58 | return "trans_sql_rel"; 59 | } 60 | private: 61 | declare_class_member(unsigned int, transhc); 62 | declare_class_member(unsigned int, sqlhc); 63 | }; 64 | #endif /* STATS_DATABASE_TAB_TRANSSQLRELTAB_H_ */ 65 | -------------------------------------------------------------------------------- /stats/database/tab/usersqlreltab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: usersqlreltab.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月28日 下午3:24:01 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_TAB_USERSQLRELTAB_H_ 30 | #define STATS_DATABASE_TAB_USERSQLRELTAB_H_ 31 | 32 | #include "define.h" 33 | #include "tabbase.h" 34 | 35 | class UserSqlRelTabT: public TabBase{ 36 | public: 37 | UserSqlRelTabT(){ 38 | this->m_sqlhc = 0; 39 | this->m_connhc = 0; 40 | this->m_sqlexechc = 0; 41 | }; 42 | virtual ~UserSqlRelTabT(){}; 43 | 44 | virtual std::string createTableSql() { 45 | std::string ct = 46 | "create table if not exists user_sql_rel(" 47 | "connhc int4, sqlexechc int4, sqlhc int4," 48 | "exectime datetime);"; 49 | return ct; 50 | } 51 | 52 | virtual std::string insertDataSql(void* data) { 53 | assert(data); 54 | UserSqlRelTabT* usrtt = (UserSqlRelTabT*)data; 55 | 56 | std::stringstream ss; 57 | ss << "insert into user_sql_rel(connhc, sqlexechc, sqlhc, exectime) values("; 58 | ss << usrtt->get_connhc() << "," << usrtt->get_sqlexechc() << ","; 59 | ss << usrtt->get_sqlhc() << ",'"<< this->tm2string(usrtt->get_exectime()) << "');"; 60 | 61 | return ss.str(); 62 | } 63 | 64 | virtual std::string get_tableName() { 65 | return "user_sql_rel"; 66 | } 67 | private: 68 | declare_class_member(unsigned int, connhc); 69 | declare_class_member(unsigned int, sqlexechc); 70 | declare_class_member(unsigned int, sqlhc); 71 | declare_class_member(struct tm, exectime); 72 | }; 73 | 74 | #endif /* STATS_DATABASE_TAB_USERSQLRELTAB_H_ */ 75 | -------------------------------------------------------------------------------- /stats/database/tab/usertab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: usertab.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年2月28日 下午3:22:48 26 | * 27 | */ 28 | 29 | #ifndef STATS_DATABASE_TAB_USERTAB_H_ 30 | #define STATS_DATABASE_TAB_USERTAB_H_ 31 | 32 | #include "define.h" 33 | #include "tabbase.h" 34 | 35 | class UserTabT: public TabBase { 36 | public: 37 | UserTabT() { 38 | this->m_connHashCode = 0; 39 | this->m_cport = 0; 40 | this->m_hashcode = 0; 41 | this->m_recvdata = 0; 42 | this->m_senddata = 0; 43 | this->m_sport = 0; 44 | }; 45 | virtual ~UserTabT(){}; 46 | 47 | virtual std::string createTableSql() { 48 | std::string ct = "create table if not exists user(" 49 | "connHashCode int4," 50 | "hashcode int4," 51 | "caddr varchar(20)," 52 | "cport int," 53 | "saddr varchar(20)," 54 | "sport int," 55 | "conndatetime datetime," 56 | "senddata bigint," 57 | "recvdata bigint);"; 58 | return ct; 59 | } 60 | 61 | virtual std::string insertDataSql(void* data) { 62 | assert(data); 63 | UserTabT* utt = (UserTabT*)data; 64 | std::stringstream ss; 65 | ss << "insert into user(connHashCode, hashcode, caddr, cport, saddr, sport, conndatetime, senddata, recvdata) values("; 66 | ss << utt->get_connHashCode() << ","; 67 | ss << utt->get_hashcode() << "," ; 68 | ss << "'" << utt->get_caddr() << "'," << utt->get_cport() << ","; 69 | ss << "'" << utt->get_saddr() << "'," << utt->get_sport() << ","; 70 | ss << "'" << this->tm2string(utt->get_conndatetime()) << "'," << utt->get_senddata() << ","; 71 | ss << utt->get_recvdata() << ");"; 72 | return ss.str(); 73 | } 74 | 75 | std::string updateData(UserTabT* utt) { 76 | assert(utt); 77 | std::stringstream ss; 78 | ss << "update user set senddata = senddata + " << utt->get_senddata(); 79 | ss << ", recvdata = recvdata + " << utt->get_recvdata(); 80 | ss << " where connHashCode=" << utt->get_connHashCode() << ";"; 81 | return ss.str(); 82 | } 83 | 84 | virtual std::string get_tableName() { 85 | return "user"; 86 | } 87 | private: 88 | declare_class_member(unsigned int, connHashCode); 89 | declare_class_member(unsigned int, hashcode); 90 | declare_class_member(std::string, caddr); 91 | declare_class_member(int, cport); 92 | declare_class_member(std::string, saddr); 93 | declare_class_member(int, sport); 94 | declare_class_member(struct tm, conndatetime); 95 | declare_class_member(u_uint64, senddata); 96 | declare_class_member(u_uint64, recvdata); 97 | }; 98 | #endif /* STATS_DATABASE_TAB_USERTAB_H_ */ 99 | -------------------------------------------------------------------------------- /stats/sqlite3.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onexsoft/oneproxy-monitor/474f43eb154c9e587ed3dd65a6b94003059474de/stats/sqlite3.exe -------------------------------------------------------------------------------- /tcpclient.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @ClassName: tcpclient.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月4日 26 | * 27 | */ 28 | 29 | #include "tcpclient.h" 30 | #include "systemapi.h" 31 | 32 | TcpClient::TcpClient() 33 | { 34 | 35 | } 36 | 37 | TcpClient::TcpClient(std::string address, int port) 38 | { 39 | } 40 | 41 | int TcpClient::get_backendConnection(NetworkSocket *ns) 42 | { 43 | //1. parse address 44 | if (ns->parse_address() < 0) { 45 | logs(Logger::ERR, "parse address error"); 46 | return -1; 47 | } 48 | 49 | int cfd = 0; 50 | //2. create sock 51 | if ((cfd = ::socket(PF_INET, SOCK_STREAM, 0)) < 0) { 52 | logs(Logger::ERR, "create socket error(%s)", SystemApi::system_strerror()); 53 | return -1; 54 | } 55 | 56 | //3. set socket option 57 | if (ns->set_sockCommonOpt(cfd, (ns->get_addr().sa.sa_family == AF_UNIX)) < 0) { 58 | logs(Logger::ERR, "set sock(%d) common option error(%s)", cfd, SystemApi::system_strerror()); 59 | ns->closeSocket(cfd); 60 | return -1; 61 | } 62 | 63 | //4. connection to server 64 | int res = 0; 65 | while(1) { 66 | res = ::connect(cfd, &ns->get_addr().sa, sizeof(ns->get_addr().sa)); 67 | if (res < 0 && errno == EINTR) 68 | continue; 69 | else if (res < 0) { 70 | logs(Logger::ERR, "connect error(%s)", SystemApi::system_strerror()); 71 | ns->closeSocket(cfd); 72 | return -1; 73 | } 74 | break; 75 | } 76 | 77 | //5. save cfd 78 | ns->set_fd(cfd); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /tcpclient.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @ClassName: tcpclient.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月4日 26 | * 27 | */ 28 | 29 | #ifndef TCPCLIENT_H_ 30 | #define TCPCLIENT_H_ 31 | 32 | #include "define.h" 33 | #include "networksocket.h" 34 | 35 | class TcpClient{ 36 | public: 37 | TcpClient(); 38 | TcpClient(std::string address, int port); 39 | 40 | int get_backendConnection(NetworkSocket *ns); 41 | }; 42 | 43 | #endif /* TCPCLIENT_H_ */ 44 | -------------------------------------------------------------------------------- /tcpserver.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @ClassName: tcpserver.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月11日 26 | * 27 | */ 28 | 29 | 30 | #ifndef TCPSERVER_H_ 31 | #define TCPSERVER_H_ 32 | 33 | #include "networksocket.h" 34 | #include "ioevent.h" 35 | #include 36 | #include 37 | #include 38 | 39 | typedef struct _socket_end_t{ 40 | unsigned int port; 41 | std::string address; 42 | }SocketEndT; 43 | 44 | class TcpServer{ 45 | public: 46 | TcpServer(); 47 | TcpServer(std::string serverAddr, unsigned int serverPort); 48 | virtual ~TcpServer(); 49 | virtual void accept_clientRequest(NetworkSocket *clientSocket) = 0; 50 | 51 | //提供接口,让外部控制tcp的运行 52 | int create_tcpServer(); 53 | // void run_server(int timeout); 54 | IoEvent& get_ioEvent() {return *ioEvent;} 55 | void set_tcpServer(std::string serverAddr, std::set& portList); 56 | void stop_tcpServer();//停止accept前端的连接,并且关闭ioevent. 57 | void set_listenBackLog(int backLog); 58 | 59 | private: 60 | int create_servers(); 61 | int create_listenSocket(NetworkSocket& ns, int af, const struct sockaddr *sa, int salen); 62 | NetworkSocket* accept_connect(unsigned int sfd); 63 | static void accept_connect(unsigned int fd, unsigned int events, void* args); 64 | 65 | private: 66 | std::vector servSct; 67 | std::map fdPortMap; 68 | IoEvent *ioEvent; 69 | int listenBackLog; 70 | bool isStopServer; 71 | }; 72 | 73 | #endif /* TCPSERVER_H_ */ 74 | -------------------------------------------------------------------------------- /test/testlogger.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * testlogger.cpp 3 | * 4 | * Created on: 2016年8月1日 5 | * Author: hui 6 | */ 7 | 8 | #include "testlogger.h" 9 | 10 | #include "../util/logger.h" 11 | 12 | void TestLogger::log_test() { 13 | logs(Logger::INFO, "test INFO log function"); 14 | logs(Logger::DEBUG, "test DEBUG log function"); 15 | logs(Logger::ERR, "test ERR log function"); 16 | logs(Logger::ERR, "TEST TEST :%d", 12345); 17 | logs(Logger::ERR, "TEST TEST :%s", "xxxxxxxxxxx"); 18 | logs(Logger::ERR, "TEST TEST (%s)", "xxxxxxxxxxx"); 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /test/testlogger.h: -------------------------------------------------------------------------------- 1 | /* 2 | * testlogger.h 3 | * 4 | * Created on: 2016年8月1日 5 | * Author: hui 6 | */ 7 | 8 | #ifndef TEST_TESTLOGGER_H_ 9 | #define TEST_TESTLOGGER_H_ 10 | 11 | #include "unittest/cpptest.h" 12 | class TestLogger:public Test::Suite{ 13 | 14 | public: 15 | TestLogger() { 16 | TEST_ADD(TestLogger::log_test); 17 | } 18 | private: 19 | void log_test(); 20 | }; 21 | 22 | #endif /* TEST_TESTLOGGER_H_ */ 23 | -------------------------------------------------------------------------------- /test/testmain.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * testmain.cpp 3 | * 4 | * Created on: 2016年8月1日 5 | * Author: hui 6 | */ 7 | 8 | #include "cpptest.h" 9 | #include "testlogger.h" 10 | #include "testnetworksocket.h" 11 | #include "teststringbuf.h" 12 | #include "testtcpclient.h" 13 | #include "testtcpserver.h" 14 | #include "testtool.h" 15 | 16 | #include 17 | #include 18 | #include 19 | using namespace std; 20 | 21 | enum OutputType 22 | { 23 | Compiler, 24 | Html, 25 | TextTerse, 26 | TextVerbose 27 | }; 28 | 29 | static void 30 | usage() 31 | { 32 | cout << "usage: mytest [MODE]\n" 33 | << "where MODE may be one of:\n" 34 | << " --compiler\n" 35 | << " --html\n" 36 | << " --text-terse (default)\n" 37 | << " --text-verbose\n"; 38 | exit(0); 39 | } 40 | 41 | static auto_ptr 42 | cmdline(int argc, char* argv[]) 43 | { 44 | if (argc > 2) 45 | usage(); // will not return 46 | 47 | Test::Output* output = 0; 48 | 49 | if (argc == 1) 50 | output = new Test::TextOutput(Test::TextOutput::Verbose); 51 | else 52 | { 53 | const char* arg = argv[1]; 54 | if (strcmp(arg, "--compiler") == 0) 55 | output = new Test::CompilerOutput; 56 | else if (strcmp(arg, "--html") == 0) 57 | output = new Test::HtmlOutput; 58 | else if (strcmp(arg, "--text-terse") == 0) 59 | output = new Test::TextOutput(Test::TextOutput::Terse); 60 | else if (strcmp(arg, "--text-verbose") == 0) 61 | output = new Test::TextOutput(Test::TextOutput::Verbose); 62 | else 63 | { 64 | cout << "invalid commandline argument: " << arg << endl; 65 | usage(); // will not return 66 | } 67 | } 68 | 69 | return auto_ptr(output); 70 | } 71 | 72 | int main(int argc, char* argv[]) 73 | { 74 | try { 75 | Test::Suite ts; 76 | //ts.add(auto_ptr(new TestLogger)); 77 | //ts.add(auto_ptr(new TestNetworkSocket)); 78 | //ts.add(auto_ptr(new TestStringBuf)); 79 | // ts.add(auto_ptr(new TestTcpClient)); 80 | // ts.add(auto_ptr(new TestTcpServer)); 81 | ts.add(auto_ptr(new TestTool)); 82 | 83 | auto_ptr output(cmdline(argc, argv)); 84 | ts.run(*output, true); 85 | 86 | Test::HtmlOutput* const html = dynamic_cast(output.get()); 87 | if (html) 88 | html->generate(cout, true, "MyTest"); 89 | } 90 | catch (...) 91 | { 92 | cout << "unexpected exception encountered\n"; 93 | return EXIT_FAILURE; 94 | } 95 | 96 | system("pause"); 97 | 98 | return EXIT_SUCCESS; 99 | } 100 | 101 | 102 | -------------------------------------------------------------------------------- /test/testnetworksocket.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * testnetworksocket.cpp 3 | * 4 | * Created on: 2016年8月3日 5 | * Author: hui 6 | */ 7 | 8 | #include "testnetworksocket.h" 9 | #include "networksocket.h" 10 | 11 | void TestNetworkSocket::ntopAndpton_test() 12 | { 13 | NetworkSocket ns; 14 | ns.set_portAndAddr(5432 , "127.0.0.1"); 15 | ns.addr_pton(); 16 | ns.addr_ntop(); 17 | 18 | TEST_ASSERT(!ns.get_address().compare("127.0.0.1")); 19 | } 20 | 21 | void TestNetworkSocket::isValidAddress_test() 22 | { 23 | NetworkSocket ns; 24 | ns.set_portAndAddr(10, "127.0.0.1");//is not valid port 25 | TEST_ASSERT(ns.get_port() > 0); //test not assig port 26 | TEST_ASSERT(ns.is_validAddress() == 1); 27 | 28 | ns.set_portAndAddr(20000, std::string());//is not valid address 29 | TEST_ASSERT(ns.is_validAddress() == 0); 30 | 31 | ns.set_portAndAddr(20000, std::string("127.0.0.1"));//is not valid address 32 | TEST_ASSERT(ns.is_validAddress() == 1); 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /test/testnetworksocket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * testnetworksocket.h 3 | * 4 | * Created on: 2016年8月3日 5 | * Author: hui 6 | */ 7 | 8 | #ifndef TEST_TESTNETWORKSOCKET_H_ 9 | #define TEST_TESTNETWORKSOCKET_H_ 10 | 11 | #include "unittest/cpptest.h" 12 | 13 | class TestNetworkSocket: public Test::Suite{ 14 | 15 | public: 16 | TestNetworkSocket() { 17 | TEST_ADD(TestNetworkSocket::ntopAndpton_test); 18 | TEST_ADD(TestNetworkSocket::isValidAddress_test); 19 | } 20 | private: 21 | void ntopAndpton_test(); 22 | void isValidAddress_test(); 23 | }; 24 | 25 | #endif /* TEST_TESTNETWORKSOCKET_H_ */ 26 | -------------------------------------------------------------------------------- /test/teststringbuf.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * teststringbuf.cpp 3 | * 4 | * Created on: 2016年8月15日 5 | * Author: hui 6 | */ 7 | 8 | #include "teststringbuf.h" 9 | #include 10 | #include "../util/logger.h" 11 | #include "../util/stringbuf.h" 12 | 13 | void TestStringBuf::log_appendData() 14 | { 15 | StringBuf sb; 16 | char *a = (char*)"hello "; 17 | sb.append(a, strlen(a)); 18 | 19 | char *b = (char*)"world"; 20 | sb.append(b, strlen(b)); 21 | 22 | TEST_ASSERT(!strcmp(sb.addr(), "hello world")); 23 | } 24 | 25 | void TestStringBuf::log_appendStr() 26 | { 27 | StringBuf sb; 28 | char *a = (char*)"hello "; 29 | sb.append(a); 30 | 31 | char *b = (char*)"world"; 32 | sb.append(b); 33 | 34 | TEST_ASSERT(!strcmp(sb.addr(), "hello world")); 35 | } 36 | 37 | void TestStringBuf::log_insert() 38 | { 39 | StringBuf sb; 40 | char *a = (char*)"world"; 41 | sb.insert(0, (void*)a, strlen(a)); 42 | 43 | char *b = (char*)"hello "; 44 | sb.insert(0, (void*)b, strlen(b)); 45 | 46 | TEST_ASSERT(!strcmp(sb.addr(), "hello world")); 47 | 48 | //pos > 0 && pos < len; 49 | StringBuf sb1; 50 | char* aaa = (char*)"aaa"; 51 | sb1.insert(0, (void*)aaa, strlen(aaa)); 52 | char* bb = (char*)"bb"; 53 | sb1.insert(1, (void*)bb, strlen(bb)); 54 | char* cc = (char*)"cc"; 55 | sb1.insert(sb1.length(), (void*)cc, strlen(cc)); 56 | TEST_ASSERT(!strcmp(sb1.addr(), "abbaacc")); 57 | } 58 | 59 | void TestStringBuf::log_appendFormat() 60 | { 61 | StringBuf sb; 62 | memset(sb.addr(), 0, sb.get_allocateLen()); 63 | sb.appendFormat("%s %d %s %d %s", "hello world", 123, "kkkkkk", 1234567, "ssssss"); 64 | logs(Logger::ERR, "sb.buf: %s", sb.addr()); 65 | TEST_ASSERT(true); 66 | } 67 | 68 | void TestStringBuf::log_erase() 69 | { 70 | StringBuf sb, sb1, sb2; 71 | sb.append("1234567890"); 72 | sb.erase(3, 5); 73 | TEST_ASSERT(!strcmp(sb.addr(), "12367890")); 74 | 75 | sb1.append("1234567890"); 76 | sb1.erase(-1, 5); 77 | TEST_ASSERT(!strcmp(sb1.addr(), "67890")); 78 | 79 | sb2.append("1234567890"); 80 | sb2.erase(-1, 20); 81 | TEST_ASSERT(!strcmp(sb2.addr(), "")); 82 | 83 | sb2.clear(); 84 | sb2.append("1234567890"); 85 | sb2.erase(5, 20); 86 | TEST_ASSERT(!strcmp(sb2.addr(), "12345")); 87 | 88 | sb2.clear(); 89 | sb2.append("1234567890"); 90 | sb2.erase(5, 4); 91 | TEST_ASSERT(!strcmp(sb2.addr(), "1234567890")); 92 | } 93 | 94 | -------------------------------------------------------------------------------- /test/teststringbuf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * teststringbuf.h 3 | * 4 | * Created on: 2016年8月15日 5 | * Author: hui 6 | */ 7 | 8 | #ifndef TEST_TESTSTRINGBUF_H_ 9 | #define TEST_TESTSTRINGBUF_H_ 10 | 11 | #include "unittest/cpptest.h" 12 | class TestStringBuf:public Test::Suite{ 13 | 14 | public: 15 | TestStringBuf() { 16 | TEST_ADD(TestStringBuf::log_appendStr); 17 | TEST_ADD(TestStringBuf::log_appendData); 18 | TEST_ADD(TestStringBuf::log_insert); 19 | TEST_ADD(TestStringBuf::log_appendFormat); 20 | TEST_ADD(TestStringBuf::log_erase); 21 | } 22 | private: 23 | void log_appendStr(); 24 | void log_appendData(); 25 | void log_insert(); 26 | void log_appendFormat(); 27 | void log_erase(); 28 | }; 29 | 30 | #endif /* TEST_TESTSTRINGBUF_H_ */ 31 | -------------------------------------------------------------------------------- /test/testtcpclient.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: testtcpclient.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年9月27日 上午11:36:43 26 | * 27 | */ 28 | 29 | #ifndef TEST_TESTTCPCLIENT_H_ 30 | #define TEST_TESTTCPCLIENT_H_ 31 | 32 | #include "cpptest.h" 33 | 34 | class TestTcpClient: public Test::Suite { 35 | public: 36 | TestTcpClient() { 37 | // TEST_ADD(TestTcpClient::test_getBackendConnection); 38 | TEST_ADD(TestTcpClient::test_sqlserver); 39 | } 40 | private: 41 | void test_getBackendConnection(); 42 | void test_sqlserver(); 43 | }; 44 | 45 | #endif /* TEST_TESTTCPCLIENT_H_ */ 46 | -------------------------------------------------------------------------------- /test/testtcpserver.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: testtcpserver.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年9月30日 上午11:16:28 26 | * 27 | */ 28 | 29 | #include "testtcpserver.h" 30 | #include "oneproxyserver.h" 31 | #include "connectmanager.h" 32 | #include "systemapi.h" 33 | #include "sspacket.h" 34 | #include "sslogin.h" 35 | #include "tcpclient.h" 36 | 37 | void TestTcpServer::test_server() { 38 | #ifdef NOT_IN_GITHUB 39 | SystemApi::init_networkEnv(); 40 | 41 | config()->handle_args(0, NULL); 42 | OneproxyServer tcpServer(std::string("0.0.0.0"), 8888); 43 | tcpServer.create_servers(); 44 | NetworkSocket *cs = NULL; 45 | 46 | std::vector::iterator it = tcpServer.servSct.begin(); 47 | while(1){ 48 | cs = tcpServer.accept_connect(it->get_fd()); 49 | if (cs == NULL) { 50 | logs(Logger::FATAL, "accept error", NULL); 51 | return; 52 | } 53 | 54 | SSLogin login; 55 | if(login.login_init(INIT_ALL_ENV)) { 56 | logs(Logger::FATAL, "init login error"); 57 | return ; 58 | } 59 | 60 | LoginParam loginParam; 61 | std::string userName = std::string("sa"); 62 | std::string password = std::string("0000"); 63 | std::string defaultDBName = std::string("master"); 64 | loginParam.init_loginClientParam(cs, userName, password, 65 | defaultDBName, 4096, 0x0c, 0x00, 0x07, 0xd0, verTDS74, true); 66 | 67 | NetworkSocket sns(std::string("127.0.0.1"), 9999); 68 | TcpClient tcpClient; 69 | if (tcpClient.get_backendConnection(&sns)) { 70 | TEST_ASSERT(false); 71 | return; 72 | } 73 | LoginParam serverParam; 74 | std::string hostName = std::string("HUIH"); 75 | std::string appName = std::string("Microsoft JDBC Driver for SQL Server"); 76 | std::string serverName = std::string("127.0.0.1"); 77 | std::string ctlIntName = std::string("Microsoft JDBC Driver 4.0"); 78 | std::string database = std::string("sqldb"); 79 | std::string userName1 = std::string("sa"); 80 | std::string password1 = std::string("0000"); 81 | serverParam.init_loginServerParam(&sns, userName1, password1, hostName, appName, 82 | serverName, ctlIntName, database, 8000, verTDS74); 83 | 84 | uif(!login.login_redirect(loginParam, serverParam)) { 85 | loginParam.print(); 86 | system("pause"); 87 | break; 88 | } 89 | sns.closeSocket(sns.get_fd()); 90 | } 91 | 92 | cs->closeSocket(cs->get_fd()); 93 | it->closeSocket(it->get_fd()); 94 | SystemApi::clear_networkEnv(); 95 | #endif 96 | } 97 | -------------------------------------------------------------------------------- /test/testtcpserver.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: testtcpserver.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年9月30日 上午11:16:28 26 | * 27 | */ 28 | 29 | #ifndef TEST_TESTTCPSERVER_H_ 30 | #define TEST_TESTTCPSERVER_H_ 31 | #include "cpptest.h" 32 | 33 | class TestTcpServer: public Test::Suite { 34 | public: 35 | TestTcpServer() { 36 | TEST_ADD(TestTcpServer::test_server); 37 | } 38 | virtual ~TestTcpServer() { 39 | 40 | } 41 | private: 42 | void test_server(); 43 | }; 44 | 45 | #endif /* TEST_TESTTCPSERVER_H_ */ 46 | -------------------------------------------------------------------------------- /test/testtool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: testtool.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年10月18日 上午9:47:11 26 | * 27 | */ 28 | 29 | #ifndef TEST_TESTTOOL_H_ 30 | #define TEST_TESTTOOL_H_ 31 | 32 | #include "cpptest.h" 33 | 34 | class TestTool : public Test::Suite{ 35 | public: 36 | TestTool() { 37 | // TEST_ADD(TestTool::test_wstringFormat); 38 | // TEST_ADD(TestTool::test_string2wstring); 39 | // TEST_ADD(TestTool::test_wstring2string); 40 | TEST_ADD(TestTool::test_findSqlKeyWord); 41 | } 42 | virtual ~TestTool() { 43 | 44 | } 45 | private: 46 | void test_wstringFormat(); 47 | void test_string2wstring(); 48 | void test_wstring2string(); 49 | void test_findSqlKeyWord(); 50 | }; 51 | 52 | #endif /* TEST_TESTTOOL_H_ */ 53 | -------------------------------------------------------------------------------- /unittest/collectoroutput.cpp: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: collectoroutput.cpp,v 1.4 2008/07/15 20:33:31 hartwork Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | #if (defined(__WIN32__) || defined(WIN32)) 28 | # include "winconfig.h" 29 | #else 30 | # include "config.h" 31 | #endif 32 | 33 | #include "cpptest-collectoroutput.h" 34 | 35 | using namespace std; 36 | 37 | namespace Test 38 | { 39 | CollectorOutput::TestInfo::TestInfo(const string name) 40 | : _name(name) 41 | {} 42 | 43 | CollectorOutput::SuiteInfo::SuiteInfo(const string& name, int tests) 44 | : _name(name), 45 | _errors(0) 46 | { 47 | _tests.reserve(tests); 48 | } 49 | 50 | /// Constructs a collector object. 51 | /// 52 | CollectorOutput::CollectorOutput() 53 | : Output(), 54 | _total_errors(0) 55 | {} 56 | 57 | void 58 | CollectorOutput::finished(int tests, const Time& time) 59 | { 60 | _total_tests = tests; 61 | _total_time = time; 62 | } 63 | 64 | void 65 | CollectorOutput::suite_start(int tests, const string& name) 66 | { 67 | if (tests > 0) 68 | { 69 | _suites.push_back(SuiteInfo(name, tests)); 70 | _cur_suite = &_suites.back(); 71 | } 72 | } 73 | 74 | void 75 | CollectorOutput::suite_end(int tests, const string&, const Time& time) 76 | { 77 | if (tests > 0) 78 | { 79 | _cur_suite->_time = time; 80 | _total_errors += _cur_suite->_errors; 81 | } 82 | } 83 | 84 | void 85 | CollectorOutput::test_start(const string& name) 86 | { 87 | _cur_suite->_tests.push_back(TestInfo(name)); 88 | _cur_test = &_cur_suite->_tests.back(); 89 | } 90 | 91 | void 92 | CollectorOutput::test_end(const string&, bool ok, const Time& time) 93 | { 94 | if (!(_cur_test->_success = ok)) 95 | ++_cur_suite->_errors; 96 | _cur_test->_time = time; 97 | } 98 | 99 | void 100 | CollectorOutput::assertment(const Source& s) 101 | { 102 | _cur_test->_sources.push_back(s); 103 | } 104 | 105 | } // namespace Test 106 | 107 | -------------------------------------------------------------------------------- /unittest/config.h: -------------------------------------------------------------------------------- 1 | /* config/config.h. Generated from config.h.in by configure. */ 2 | /* config/config.h.in. Generated from configure.ac by autoheader. */ 3 | 4 | /* Define to 1 if you have the header file. */ 5 | #define HAVE_DLFCN_H 1 6 | 7 | /* Define to 1 if you have the `gettimeofday' function. */ 8 | #define HAVE_GETTIMEOFDAY 1 9 | 10 | /* Define to 1 if you have the header file. */ 11 | #define HAVE_INTTYPES_H 1 12 | 13 | /* Define to 1 if you have the header file. */ 14 | #define HAVE_MEMORY_H 1 15 | 16 | /* Defined if the function exists */ 17 | #define HAVE_ROUND /**/ 18 | 19 | /* Define to 1 if you have the header file. */ 20 | #define HAVE_STDINT_H 1 21 | 22 | /* Define to 1 if you have the header file. */ 23 | #define HAVE_STDLIB_H 1 24 | 25 | /* Define to 1 if you have the header file. */ 26 | #define HAVE_STRINGS_H 1 27 | 28 | /* Define to 1 if you have the header file. */ 29 | #define HAVE_STRING_H 1 30 | 31 | /* Define to 1 if you have the header file. */ 32 | #define HAVE_SYS_STAT_H 1 33 | 34 | /* Define to 1 if you have the header file. */ 35 | #define HAVE_SYS_TIME_H 1 36 | 37 | /* Define to 1 if you have the header file. */ 38 | #define HAVE_SYS_TYPES_H 1 39 | 40 | /* Define to 1 if you have the header file. */ 41 | #define HAVE_UNISTD_H 1 42 | 43 | /* Define to the sub-directory in which libtool stores uninstalled libraries. 44 | */ 45 | #define LT_OBJDIR ".libs/" 46 | 47 | /* Name of package */ 48 | #define PACKAGE "cpptest" 49 | 50 | /* Define to the address where bug reports for this package should be sent. */ 51 | #define PACKAGE_BUGREPORT "nilu@users.sourceforge.net" 52 | 53 | /* Define to the full name of this package. */ 54 | #define PACKAGE_NAME "CppTest" 55 | 56 | /* Define to the full name and version of this package. */ 57 | #define PACKAGE_STRING "CppTest 1.1.2" 58 | 59 | /* Define to the one symbol short name of this package. */ 60 | #define PACKAGE_TARNAME "cpptest" 61 | 62 | /* Define to the home page for this package. */ 63 | #define PACKAGE_URL "" 64 | 65 | /* Define to the version of this package. */ 66 | #define PACKAGE_VERSION "1.1.2" 67 | 68 | /* Define to 1 if you have the ANSI C header files. */ 69 | #define STDC_HEADERS 1 70 | 71 | /* Version number of package */ 72 | #define VERSION "1.1.2" 73 | -------------------------------------------------------------------------------- /unittest/cpptest-collectoroutput.h: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: cpptest-collectoroutput.h,v 1.3 2005/06/08 08:08:06 nilu Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | /** \file */ 28 | 29 | #ifndef CPPTEST_COLLECTOROUTPUT_H 30 | #define CPPTEST_COLLECTOROUTPUT_H 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #include "cpptest-output.h" 37 | #include "cpptest-source.h" 38 | #include "cpptest-time.h" 39 | 40 | namespace Test 41 | { 42 | /// \brief Collector output. 43 | /// 44 | /// Base class for output handlers that need to report status when all 45 | /// tests have executed. 46 | /// 47 | class CollectorOutput : public Output 48 | { 49 | public: 50 | virtual void finished(int tests, const Time& time); 51 | virtual void suite_start(int tests, const std::string& name); 52 | virtual void suite_end(int tests, const std::string& name, 53 | const Time& time); 54 | virtual void test_start(const std::string& name); 55 | virtual void test_end(const std::string& name, bool ok, 56 | const Time& time); 57 | virtual void assertment(const Source& s); 58 | 59 | protected: 60 | struct OutputSuiteInfo; 61 | struct OutputTestInfo; 62 | struct OutputErrorTestInfo; 63 | 64 | typedef std::list Sources; 65 | 66 | struct TestInfo 67 | { 68 | std::string _name; 69 | Time _time; 70 | 71 | bool _success : 1; 72 | Sources _sources; 73 | 74 | explicit TestInfo(const std::string name); 75 | }; 76 | 77 | typedef std::vector Tests; 78 | 79 | struct SuiteInfo 80 | { 81 | std::string _name; 82 | int _errors; 83 | Tests _tests; 84 | Time _time; 85 | 86 | SuiteInfo(const std::string& name, int tests); 87 | }; 88 | 89 | typedef std::list Suites; 90 | 91 | Suites _suites; 92 | int _total_errors; 93 | int _total_tests; 94 | Time _total_time; 95 | 96 | CollectorOutput(); 97 | 98 | private: 99 | SuiteInfo* _cur_suite; 100 | TestInfo* _cur_test; 101 | }; 102 | 103 | } // namespace Test 104 | 105 | #endif // #ifndef CPPTEST_COLLECTOROUTPUT_H 106 | 107 | -------------------------------------------------------------------------------- /unittest/cpptest-compileroutput.h: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: cpptest-compileroutput.h,v 1.3 2005/06/08 08:08:06 nilu Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | /** \file */ 28 | 29 | #ifndef CPPTEST_COMPILEROUTPUT_H 30 | #define CPPTEST_COMPILEROUTPUT_H 31 | 32 | #include 33 | #include 34 | 35 | #include "cpptest-output.h" 36 | 37 | namespace Test 38 | { 39 | /// \brief Compiler-like output handler. 40 | /// 41 | /// %Test suite output handler that only outputs failures in compiler 42 | /// warning/error format. This way, you can use your IDE to browse between 43 | /// failures. 44 | /// 45 | /// The output format is configurable to be able to emulate different 46 | /// compiler outputs. The following modifiers exist: 47 | /// - \e %file Outputs the file containing the test function. 48 | /// - \e %line Line number for the the test function. 49 | /// - \e %text Expression (or message) that caused the assertment. 50 | /// Note that each modifier can only be specified once. 51 | /// 52 | class CompilerOutput : public Output 53 | { 54 | public: 55 | /// \brief Compiler output exception. 56 | /// 57 | /// Indicates that an invalid message format was given when creating 58 | /// a compiler output. The failing format may be retrieved using the 59 | /// what() method. 60 | /// 61 | class InvalidFormat : public std::logic_error 62 | { 63 | public: 64 | InvalidFormat(const std::string& what) 65 | : std::logic_error(what) {} 66 | }; 67 | 68 | /// Pre-defined compiler output formats. 69 | /// 70 | enum Format 71 | { 72 | /// Generic compiler format, which equals: 73 | /// %%file:%%line: %%text 74 | /// 75 | Generic, 76 | 77 | /// 78 | /// Borland C++ Compiler (BCC) format, which equals: 79 | /// Error cpptest %%file %%line: %%text. 80 | /// 81 | BCC, 82 | 83 | /// GNU Compiler Collection 84 | /// (GCC) format, which equals: 85 | /// %%file:%%line: %%text 86 | /// 87 | GCC, 88 | 89 | /// Microsoft Visual C++ 90 | /// (MSVC) format, which equals: 91 | /// %%file(%%line) : %%text 92 | /// 93 | MSVC 94 | }; 95 | 96 | explicit CompilerOutput(Format format = Generic, 97 | std::ostream& stream = std::cout); 98 | 99 | explicit CompilerOutput(const std::string& format, 100 | std::ostream& stream = std::cout); 101 | 102 | virtual void assertment(const Source& s); 103 | 104 | private: 105 | std::string _format; 106 | std::ostream& _stream; 107 | }; 108 | 109 | } // namespace Test 110 | 111 | #endif // #ifndef CPPTEST_COMPILEROUTPUT_H 112 | 113 | -------------------------------------------------------------------------------- /unittest/cpptest-htmloutput.h: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: cpptest-htmloutput.h,v 1.3 2005/06/08 08:08:06 nilu Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | /** \file */ 28 | 29 | #ifndef CPPTEST_HTMLOUTPUT_H 30 | #define CPPTEST_HTMLOUTPUT_H 31 | 32 | #include 33 | #include 34 | 35 | #include "cpptest-collectoroutput.h" 36 | 37 | namespace Test 38 | { 39 | /// \brief HTML output. 40 | /// 41 | /// %Output handler that creates a HTML table with detailed information 42 | /// about all tests. 43 | /// 44 | class HtmlOutput : public CollectorOutput 45 | { 46 | public: 47 | void generate(std::ostream& os, bool incl_ok_tests = true, 48 | const std::string& name = ""); 49 | 50 | private: 51 | struct SuiteRow; 52 | struct TestRow; 53 | struct TestSuiteRow; 54 | struct TestResult; 55 | struct TestResultAll; 56 | struct SuiteTestResult; 57 | 58 | friend struct TestSuiteRow; 59 | }; 60 | 61 | } // namespace Test 62 | 63 | #endif // #ifndef CPPTEST_HTMLOUTPUT_H 64 | 65 | -------------------------------------------------------------------------------- /unittest/cpptest-source.h: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: cpptest-source.h,v 1.3 2005/06/08 08:08:06 nilu Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | /** \file */ 28 | 29 | #ifndef CPPTEST_SOURCE_H 30 | #define CPPTEST_SOURCE_H 31 | 32 | #include 33 | 34 | namespace Test 35 | { 36 | class Suite; 37 | 38 | /// \brief Assertment source information. 39 | /// 40 | /// Contains information about an assertment point. 41 | /// 42 | class Source 43 | { 44 | friend class Suite; 45 | 46 | public: 47 | Source(); 48 | Source(const char* file, unsigned int line, const char* msg); 49 | 50 | const std::string& file() const; 51 | unsigned int line() const; 52 | const std::string& message() const; 53 | const std::string& suite() const; 54 | const std::string& test() const; 55 | 56 | private: 57 | unsigned int _line; 58 | std::string _file; 59 | std::string _msg; 60 | std::string _suite; 61 | std::string _test; 62 | }; 63 | 64 | } // namespace Test 65 | 66 | #endif // #ifndef CPPTEST_SOURCE_H 67 | 68 | -------------------------------------------------------------------------------- /unittest/cpptest-textoutput.h: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: cpptest-textoutput.h,v 1.3 2005/06/08 08:08:06 nilu Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | /** \file */ 28 | 29 | #ifndef CPPTEST_TEXTOUTPUT_H 30 | #define CPPTEST_TEXTOUTPUT_H 31 | 32 | #include 33 | #include 34 | 35 | #include "cpptest-source.h" 36 | #include "cpptest-output.h" 37 | 38 | namespace Test 39 | { 40 | /// \brief Text output handler that outputs to the a stream. 41 | /// 42 | /// %Test suite output handler that writes its information as text to a 43 | /// a stream. It it possible to select between two different operational 44 | /// modes that controls the detail level, see Mode. 45 | /// 46 | class TextOutput : public Output 47 | { 48 | public: 49 | /// Output mode. 50 | /// 51 | enum Mode 52 | { 53 | /// Terse output mode, which only shows the number of correct tests. 54 | /// 55 | Terse, 56 | 57 | /// Verbose output mode, which also shows extended assert 58 | /// information for each test that failed. 59 | /// 60 | Verbose 61 | }; 62 | 63 | TextOutput(Mode mode, std::ostream& stream = std::cout); 64 | 65 | virtual void finished(int tests, const Time& time); 66 | virtual void suite_start(int tests, const std::string& name); 67 | virtual void suite_end(int tests, const std::string& name, 68 | const Time& time); 69 | virtual void test_end(const std::string& name, bool ok, 70 | const Time& time); 71 | virtual void assertment(const Source& s); 72 | 73 | private: 74 | typedef std::list ErrorList; 75 | 76 | Mode _mode; 77 | std::ostream& _stream; 78 | ErrorList _suite_error_list; 79 | std::string _suite_name; 80 | int _suite_errors; 81 | int _suite_tests; 82 | int _suite_total_tests; 83 | int _total_errors; 84 | }; 85 | 86 | } // namespace Test 87 | 88 | #endif // #ifndef CPPTEST_TEXTOUTPUT_H 89 | -------------------------------------------------------------------------------- /unittest/cpptest-time.h: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: cpptest-time.h,v 1.3 2005/06/08 08:08:06 nilu Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | /** \file */ 28 | 29 | #ifndef CPPTEST_TIME_H 30 | #define CPPTEST_TIME_H 31 | 32 | #include 33 | #include 34 | 35 | namespace Test 36 | { 37 | /// \brief %Time representation. 38 | /// 39 | /// Encapsulates a time value with microsecond resolution. It is possible 40 | /// to retrieve the current time, add and subtract time values, and output 41 | /// the time to an output stream. 42 | /// 43 | class Time 44 | { 45 | public: 46 | Time(); 47 | Time(unsigned int sec, unsigned int usec); 48 | 49 | unsigned int seconds() const; 50 | unsigned int microseconds() const; 51 | 52 | static Time current(); 53 | 54 | friend Time operator+(const Time& t1, const Time& t2); 55 | friend Time operator-(const Time& t1, const Time& t2); 56 | 57 | friend std::ostream& operator<<(std::ostream& os, const Time& t); 58 | 59 | private: 60 | unsigned int _sec; 61 | unsigned int _usec; 62 | }; 63 | 64 | } // namespace Test 65 | 66 | #endif // #ifndef CPPTEST_TIME_H 67 | -------------------------------------------------------------------------------- /unittest/cpptest.h: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: cpptest.h,v 1.3 2005/06/08 08:08:06 nilu Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | /** \file */ 28 | 29 | #ifndef CPPTEST_H 30 | #define CPPTEST_H 31 | 32 | #include "cpptest-assert.h" 33 | #include "cpptest-source.h" 34 | #include "cpptest-suite.h" 35 | #include "cpptest-time.h" 36 | 37 | #include "cpptest-compileroutput.h" 38 | #include "cpptest-htmloutput.h" 39 | #include "cpptest-textoutput.h" 40 | 41 | #endif // #ifndef CPPTEST_H 42 | 43 | -------------------------------------------------------------------------------- /unittest/missing.cpp: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: missing.cpp,v 1.4 2008/07/15 20:33:31 hartwork Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | #if (defined(__WIN32__) || defined(WIN32)) 28 | # include "winconfig.h" 29 | #else 30 | # include "config.h" 31 | #endif 32 | 33 | #include "missing.h" 34 | 35 | #ifdef HAVE_GETTICKCOUNT 36 | #include 37 | #endif 38 | 39 | #ifndef __BORLANDC__ 40 | #include 41 | #else 42 | #include 43 | #endif 44 | 45 | #include 46 | #include 47 | 48 | namespace Test 49 | { 50 | #ifndef HAVE_GETTIMEOFDAY 51 | 52 | int 53 | gettimeofday(timeval* tv, void*) 54 | { 55 | assert(tv); 56 | 57 | #ifdef HAVE_GETTICKCOUNT 58 | long now = GetTickCount(); 59 | tv->tv_sec = now / 1000; 60 | tv->tv_usec = (now % 1000) * 1000; 61 | #else 62 | tv->tv_sec = time(0); 63 | tv->tv_usec = 0; 64 | #endif // #ifdef HAVE_GETTICKCOUNT 65 | 66 | return 0; 67 | } 68 | 69 | #endif // #ifndef HAVE_GETTIMEOFDAY 70 | 71 | #ifndef HAVE_ROUND 72 | double 73 | round(double d) 74 | { 75 | return d > 0.0 ? floor(d + 0.5) : ceil(d - 0.5); 76 | } 77 | #endif 78 | 79 | } // namespace Test 80 | 81 | -------------------------------------------------------------------------------- /unittest/missing.h: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: missing.h,v 1.4 2008/07/15 20:33:31 hartwork Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | #ifndef CPPTEST_MISSING_H 28 | #define CPPTEST_MISSING_H 29 | 30 | #if (defined(__WIN32__) || defined(WIN32)) 31 | # include "winconfig.h" 32 | #else 33 | # include "config.h" 34 | #endif 35 | 36 | namespace Test 37 | { 38 | #ifndef HAVE_GETTIMEOFDAY 39 | 40 | struct timeval 41 | { 42 | long tv_sec; 43 | long tv_usec; 44 | }; 45 | 46 | extern int gettimeofday(timeval* tv, void*); 47 | 48 | #endif // #ifndef HAVE_GETTIMEOFDAY 49 | 50 | #ifndef HAVE_ROUND 51 | extern double round(double d); 52 | #endif 53 | 54 | } // namespace Test 55 | 56 | #endif // #ifndef CPPTEST_MISSING_H 57 | -------------------------------------------------------------------------------- /unittest/source.cpp: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: source.cpp,v 1.4 2005/06/08 09:25:09 nilu Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | #include "cpptest-source.h" 28 | 29 | using namespace std; 30 | 31 | namespace Test 32 | { 33 | /// Constructs an invalid source object, which filename and message are 34 | /// empty strings and the line equals zero. 35 | /// 36 | Source::Source() 37 | : _line(0) 38 | {} 39 | 40 | /// Constructs a source object. 41 | /// 42 | /// \param file Name of the file containing the failing function. 43 | /// \param line Line where the function starts. 44 | /// \param msg Expression (or message) that caused the failure. 45 | /// 46 | Source::Source(const char* file, unsigned int line, const char* msg) 47 | : _line(line), 48 | _file(file ? file : ""), 49 | _msg(msg ? msg : "") 50 | {} 51 | 52 | /// \return Name of the file containing the failing function. 53 | /// 54 | const string& 55 | Source::file() const 56 | { 57 | return _file; 58 | } 59 | 60 | /// \return Line where the function starts. 61 | /// 62 | unsigned int 63 | Source::line() const 64 | { 65 | return _line; 66 | } 67 | 68 | /// \return Descriptive message. 69 | /// 70 | const string& 71 | Source::message() const 72 | { 73 | return _msg; 74 | } 75 | 76 | /// \return Name of the suite, which the test belongs to. 77 | /// 78 | const string& 79 | Source::suite() const 80 | { 81 | return _suite; 82 | } 83 | 84 | /// \return Name of failing test. 85 | /// 86 | const string& 87 | Source::test() const 88 | { 89 | return _test; 90 | } 91 | 92 | } // namespace Test 93 | 94 | -------------------------------------------------------------------------------- /unittest/textoutput.cpp: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: textoutput.cpp,v 1.4 2008/07/15 20:33:31 hartwork Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | #include 28 | 29 | #if (defined(__WIN32__) || defined(WIN32)) 30 | # include "winconfig.h" 31 | #else 32 | # include "config.h" 33 | #endif 34 | 35 | #include "cpptest-textoutput.h" 36 | #include "cpptest-time.h" 37 | #include "utils.h" 38 | 39 | using namespace std; 40 | 41 | namespace Test 42 | { 43 | namespace 44 | { 45 | // Outputs detailed assert source information. Used in verbose mode. 46 | // 47 | struct ShowSource 48 | { 49 | ostream& _stream; 50 | ShowSource(ostream& stream) : _stream(stream) {} 51 | void operator()(const Source& s) 52 | { 53 | _stream << "\tTest: " << s.test() << endl 54 | << "\tSuite: " << s.suite() << endl 55 | << "\tFile: " << s.file() << endl 56 | << "\tLine: " << s.line() << endl 57 | << "\tMessage: " << s.message() << endl << endl; 58 | 59 | } 60 | }; 61 | 62 | } // anonymous namespace 63 | 64 | /// Constructs a text output handler. 65 | /// 66 | /// \param mode Output mode. 67 | /// \param stream Stream to output to. 68 | /// 69 | TextOutput::TextOutput(Mode mode, ostream& stream) 70 | : _mode(mode), 71 | _stream(stream), 72 | _total_errors(0) 73 | {} 74 | 75 | void 76 | TextOutput::finished(int tests, const Time& time) 77 | { 78 | _stream << "Total: " << tests << " tests, " 79 | << correct(tests, _total_errors) << "% correct" 80 | << " in " << time << " seconds" << endl; 81 | } 82 | 83 | void 84 | TextOutput::suite_start(int tests, const string& name) 85 | { 86 | if (tests > 0) 87 | { 88 | _suite_name = name; 89 | _suite_tests = _suite_errors = 0; 90 | _suite_total_tests = tests; 91 | _suite_error_list.clear(); 92 | 93 | _stream << _suite_name << ": " 94 | << "0/" << _suite_total_tests 95 | << "\r" << flush; 96 | } 97 | } 98 | 99 | void 100 | TextOutput::suite_end(int tests, const string& name, const Time& time) 101 | { 102 | if (tests > 0) 103 | { 104 | _stream << name << ": " << tests << "/" << tests << ", " 105 | << correct(tests, _suite_errors) << "% correct" 106 | << " in " << time << " seconds" << endl; 107 | 108 | if (_mode == Verbose && _suite_errors) 109 | for_each(_suite_error_list.begin(), _suite_error_list.end(), 110 | ShowSource(_stream)); 111 | 112 | _total_errors += _suite_errors; 113 | } 114 | } 115 | 116 | void 117 | TextOutput::test_end(const string&, bool ok, const Time&) 118 | { 119 | _stream << _suite_name << ": " 120 | << ++_suite_tests << "/" << _suite_total_tests 121 | << "\r" << flush; 122 | if (!ok) 123 | ++_suite_errors; 124 | } 125 | 126 | void 127 | TextOutput::assertment(const Source& s) 128 | { 129 | _suite_error_list.push_back(s); 130 | } 131 | 132 | } // namespace Test 133 | -------------------------------------------------------------------------------- /unittest/utils.cpp: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: utils.cpp,v 1.5 2008/07/15 20:33:31 hartwork Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | 28 | #if (defined(__WIN32__) || defined(WIN32)) 29 | # include "winconfig.h" 30 | #else 31 | # include "config.h" 32 | #endif 33 | 34 | #include "missing.h" 35 | #include "utils.h" 36 | 37 | namespace Test 38 | { 39 | // Computes the procentage of correct tests. 40 | // 41 | int 42 | correct(int tests, int errors) 43 | { 44 | if ((errors == 0) || (tests == 0)) 45 | return 100; 46 | 47 | return (tests - errors) * 100 / tests; 48 | } 49 | 50 | } // namespace Test 51 | -------------------------------------------------------------------------------- /unittest/utils.h: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: utils.h,v 1.3 2005/06/08 08:08:06 nilu Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | #ifndef CPPTEST_UTILS_H 28 | #define CPPTEST_UTILS_H 29 | 30 | namespace Test 31 | { 32 | extern int correct(int tests, int errors); 33 | 34 | } // namespace Test 35 | 36 | #endif // #ifndef CPPTEST_UTILS_H 37 | -------------------------------------------------------------------------------- /unittest/winconfig.h: -------------------------------------------------------------------------------- 1 | // --- 2 | // 3 | // $Id: winconfig.h,v 1.1 2008/07/15 20:33:31 hartwork Exp $ 4 | // 5 | // CppTest - A C++ Unit Testing Framework 6 | // Copyright (c) 2003 Niklas Lundell 7 | // 8 | // --- 9 | // 10 | // This library is free software; you can redistribute it and/or 11 | // modify it under the terms of the GNU Lesser General Public 12 | // License as published by the Free Software Foundation; either 13 | // version 2 of the License, or (at your option) any later version. 14 | // 15 | // This library is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | // Lesser General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU Lesser General Public 21 | // License along with this library; if not, write to the 22 | // Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 | // Boston, MA 02111-1307, USA. 24 | // 25 | // --- 26 | 27 | #ifndef TEST_CONFIG_H 28 | #define TEST_CONFIG_H 29 | 30 | // Capabilities 31 | // 32 | #define HAVE_GETTICKCOUNT 33 | 34 | // Compiler specific stuff 35 | // 36 | #if _MSC_VER == 1200 // MS Visual C++ 6.0 37 | #pragma warning (disable: 4786) 38 | #endif 39 | 40 | #if _MSC_VER > 1300 // MS Visual C++ .NET 2002 and above 41 | #pragma warning (disable: 4267) 42 | #endif 43 | 44 | #endif // #ifndef TEST_CONFIG_H 45 | -------------------------------------------------------------------------------- /util/hitcache.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: HitCache.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年1月17日 下午3:36:56 26 | * 27 | */ 28 | 29 | #ifndef UTIL_HITCACHE_H_ 30 | #define UTIL_HITCACHE_H_ 31 | 32 | class HitCache{ 33 | #define CACHE_MAP_COLOMNS 130 34 | #define CACHE_MAP_ROWS 5 35 | 36 | public: 37 | HitCache(int cacheLevel = 5, bool ignoreCase = true) { 38 | this->isIgnoreCase = ignoreCase; 39 | this->cacheLevel = cacheLevel; 40 | if (this->cacheLevel > CACHE_MAP_ROWS) { 41 | this->cacheLevel = CACHE_MAP_ROWS; 42 | } 43 | for (int i = 0; i < CACHE_MAP_ROWS; ++i) { 44 | for (int j = 0; j < CACHE_MAP_COLOMNS; ++j) { 45 | this->cache[i][j] = 0; 46 | } 47 | } 48 | } 49 | 50 | void set_hitCache(const char* name, int nameLen) { 51 | int level = this->cacheLevel; 52 | if (nameLen < level) 53 | level = nameLen; 54 | if (level <= 0) 55 | return; 56 | 57 | for (int i = 0; i < level; ++i) { 58 | char ch = name[i]; 59 | if (ch >= CACHE_MAP_COLOMNS) 60 | continue; 61 | 62 | if (this->isIgnoreCase) { 63 | if (ch >= 'A' && ch <= 'Z') { 64 | cache[i][(int)ch] = 1; 65 | cache[i][(int)ch + 32] = 1; 66 | } else if (ch >= 'a' && ch <= 'z') { 67 | cache[i][(int)ch] = 1; 68 | cache[i][(int)ch - 32] = 1; 69 | } else { 70 | cache[i][(int)ch] = 1; 71 | } 72 | } else { 73 | cache[i][(int)ch] = 1; 74 | } 75 | } 76 | } 77 | 78 | bool is_hit(const char* data, const int dataLen) { 79 | int level = this->cacheLevel; 80 | if (level > dataLen) 81 | level = dataLen; 82 | for(int i = 0; i < level; ++i) { 83 | int ch = (int)data[i]; 84 | if (ch >= CACHE_MAP_COLOMNS || !cache[i][ch]) 85 | return false; 86 | } 87 | return true; 88 | } 89 | 90 | 91 | private: 92 | int cache[CACHE_MAP_ROWS][CACHE_MAP_COLOMNS]; 93 | int cacheLevel;//max 5; 94 | bool isIgnoreCase; 95 | }; 96 | 97 | 98 | 99 | #endif /* UTIL_HITCACHE_H_ */ 100 | -------------------------------------------------------------------------------- /util/keepalive.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: keepalive.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年9月8日 26 | * 27 | */ 28 | 29 | #ifndef UTIL_KEEPALIVE_H_ 30 | #define UTIL_KEEPALIVE_H_ 31 | 32 | class KeepAlive { 33 | public: 34 | KeepAlive(); 35 | virtual ~KeepAlive(); 36 | 37 | 38 | int keepalive(int *child_exit_status); 39 | 40 | /** 41 | * @desc 信号处理函数 42 | * @param sig 信号值 43 | * @return 成功返回0, 否则返回-1. 44 | * **/ 45 | static void handle_signal(int sig); 46 | private: 47 | static bool stop_keepAlive; 48 | }; 49 | 50 | #endif /* UTIL_KEEPALIVE_H_ */ 51 | -------------------------------------------------------------------------------- /util/md5.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MD5 implementation based on RFC1321. 3 | * 4 | * Copyright (c) 2008 Marko Kreen, Skype Technologies OÜ 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | */ 18 | 19 | /** 20 | * @file 21 | * 22 | * MD5 cryptographic hash. 23 | */ 24 | 25 | #ifndef _MD5_H_ 26 | #define _MD5_H_ 27 | 28 | #include "define.h" 29 | /** Block length for MD5 */ 30 | #define MD5_BLOCK_LENGTH 64 31 | 32 | /** Result length for MD5 */ 33 | #define MD5_DIGEST_LENGTH 16 34 | 35 | #define uint64_t u_uint64 36 | #define uint32_t u_uint32 37 | #define uint8_t u_uint8 38 | 39 | 40 | /** MD5 state */ 41 | struct md5_ctx { 42 | uint64_t nbytes; 43 | uint32_t a, b, c, d; 44 | uint32_t buf[16]; 45 | }; 46 | 47 | class MD5Method{ 48 | public: 49 | /** Clean state */ 50 | static void md5_reset(struct md5_ctx *ctx); 51 | 52 | /** Update state with more data */ 53 | static void md5_update(struct md5_ctx *ctx, const void *data, unsigned int len); 54 | 55 | /** Get final result */ 56 | static void md5_final(uint8_t *dst, struct md5_ctx *ctx); 57 | }; 58 | 59 | #endif 60 | 61 | -------------------------------------------------------------------------------- /util/memmanager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: memmanager.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年9月10日 26 | * 27 | */ 28 | 29 | #include "logger.h" 30 | #include "memmanager.h" 31 | 32 | #include 33 | #include 34 | 35 | 36 | MemManager::MemManager() { 37 | // TODO Auto-generated constructor stub 38 | 39 | } 40 | 41 | MemManager::~MemManager() { 42 | // TODO Auto-generated destructor stub 43 | } 44 | 45 | void* MemManager::malloc(unsigned int size) 46 | { 47 | const unsigned int maxMallocMem = 1024 * 1024 * 50; //50M 48 | if (size > maxMallocMem) {//forbit malloc > 50M one times. 49 | logs(Logger::WARNING, "malloc(%u) is too bigger", size); 50 | } 51 | 52 | char* ch = (char*)::malloc(size); 53 | uif (ch == NULL){ 54 | logs(Logger::ERR, "malloc(%u) error", size); 55 | return NULL; 56 | } 57 | return ch; 58 | } 59 | 60 | void MemManager::free(void* pointer) 61 | { 62 | uif(pointer == NULL) { 63 | return; 64 | } 65 | ::free(pointer); 66 | } 67 | -------------------------------------------------------------------------------- /util/memmanager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: memmanager.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年9月10日 26 | * 27 | */ 28 | 29 | #ifndef UTIL_MEMMANAGER_H_ 30 | #define UTIL_MEMMANAGER_H_ 31 | 32 | class MemManager { 33 | public: 34 | MemManager(); 35 | virtual ~MemManager(); 36 | 37 | static void* malloc(unsigned int size); 38 | static void free(void* pointer); 39 | }; 40 | 41 | #endif /* UTIL_MEMMANAGER_H_ */ 42 | -------------------------------------------------------------------------------- /util/mutexlock.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: mutexlock.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月5日 26 | * 27 | */ 28 | 29 | #include "mutexlock.h" 30 | 31 | #include 32 | 33 | MutexLock::MutexLock() 34 | { 35 | initMutex(); 36 | } 37 | 38 | MutexLock::MutexLock(std::string name) 39 | { 40 | this->mutexName = name; 41 | initMutex(); 42 | } 43 | 44 | MutexLock::~MutexLock() 45 | { 46 | this->mutexName.clear(); 47 | #ifdef WIN32 48 | ::CloseHandle(this->mutex); 49 | ::CloseHandle(this->mutexCond); 50 | #else 51 | pthread_mutex_destroy(&this->mutex); 52 | pthread_cond_destroy(&this->mutexCond); 53 | #endif 54 | } 55 | 56 | void MutexLock::lock() 57 | { 58 | #ifdef WIN32 59 | ::WaitForSingleObject(this->mutex, INFINITE); 60 | #else 61 | pthread_mutex_lock(&this->mutex); 62 | #endif 63 | } 64 | 65 | void MutexLock::unlock() 66 | { 67 | #ifdef WIN32 68 | ::ReleaseMutex(this->mutex); 69 | #else 70 | pthread_mutex_unlock(&this->mutex); 71 | #endif 72 | } 73 | 74 | void MutexLock::set_name(std::string name) 75 | { 76 | this->mutexName = name; 77 | } 78 | 79 | std::string MutexLock::get_name() 80 | { 81 | return this->mutexName; 82 | } 83 | 84 | void MutexLock::wait_mutexCond(int timeout) 85 | { 86 | 87 | #ifdef WIN32 88 | if (timeout < 0) { 89 | ::SignalObjectAndWait(this->mutex, this->mutexCond, INFINITE, FALSE); 90 | } else { 91 | ::SignalObjectAndWait(this->mutex, this->mutexCond, timeout, FALSE); 92 | } 93 | this->lock(); 94 | #else 95 | if (timeout < 0){ 96 | pthread_cond_wait(&this->mutexCond, &this->mutex); 97 | } else { 98 | struct timespec t; 99 | struct timeval now; 100 | gettimeofday(&now, NULL); 101 | t.tv_sec = now.tv_sec; 102 | t.tv_nsec = now.tv_usec * 1000 + timeout * 1000000; 103 | pthread_cond_timedwait(&this->mutexCond, &this->mutex, &t); 104 | } 105 | #endif 106 | 107 | } 108 | 109 | void MutexLock::signal_mutexCond() 110 | { 111 | #ifdef WIN32 112 | ::PulseEvent(this->mutexCond); 113 | #else 114 | pthread_cond_signal(&this->mutexCond); 115 | #endif 116 | } 117 | -------------------------------------------------------------------------------- /util/mutexlock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: mutexlock.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月5日 26 | * 27 | */ 28 | 29 | #ifndef UTIL_MUTEXLOCK_H_ 30 | #define UTIL_MUTEXLOCK_H_ 31 | 32 | #include 33 | #include 34 | #ifdef WIN32 35 | #include 36 | #include 37 | #include 38 | #else 39 | #include 40 | #include 41 | #endif 42 | #include 43 | #include 44 | 45 | #ifdef WIN32 46 | typedef HANDLE MutexLockHandle; 47 | typedef HANDLE MutexCondHandle; 48 | #else 49 | typedef pthread_mutex_t MutexLockHandle; 50 | typedef pthread_cond_t MutexCondHandle; 51 | #endif 52 | 53 | class MutexLock{ 54 | public: 55 | MutexLock(); 56 | MutexLock(std::string name); 57 | ~MutexLock(); 58 | 59 | inline void initMutex(); 60 | void lock(); 61 | void unlock(); 62 | void set_name(std::string name); 63 | std::string get_name(); 64 | void wait_mutexCond(int timeout = -1);//单位为毫秒 65 | void signal_mutexCond(); 66 | private: 67 | MutexLockHandle mutex; 68 | MutexCondHandle mutexCond; 69 | std::string mutexName; 70 | }; 71 | void MutexLock::initMutex() 72 | { 73 | #ifdef WIN32 74 | this->mutex = ::CreateMutex(NULL, false, NULL); 75 | this->mutexCond = ::CreateEvent(NULL, false, false, "win mutex condition"); 76 | #else 77 | if (pthread_mutex_init(&this->mutex, NULL)) { 78 | std::cerr << "pthread_mutex_init error " << strerror(errno) << std::endl; 79 | exit(-1); 80 | } 81 | if (pthread_cond_init(&this->mutexCond, NULL)) { 82 | std::cerr << "pthread_cond_init error" << strerror(errno) << std::endl; 83 | exit(-1); 84 | } 85 | #endif 86 | } 87 | 88 | #endif /* UTIL_MUTEXLOCK_H_ */ 89 | -------------------------------------------------------------------------------- /util/ping.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: ping.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年9月8日 26 | * 27 | */ 28 | 29 | #ifndef UTIL_PING_H_ 30 | #define UTIL_PING_H_ 31 | 32 | #include "define.h" 33 | #include "logger.h" 34 | 35 | #define ICMP_ECHO 8 //发送的icmp_type ICMP回显请求报文 36 | #define ICMP_ECHOREPLY 0 //接收的icmp_type ICMP回显应答报文 37 | 38 | #define ICMP_MIN 8 // ICMP报文的最小长度是8字节(仅为首部,不包含icmp_data) 39 | 40 | /* The IP header */ 41 | typedef struct _ip_t 42 | { 43 | unsigned char ip_hl:4; // 4位首部长度 (length of the header) 44 | unsigned char ip_v:4; // IP版本号 45 | unsigned char ip_tos; // 8位服务类型TOS 46 | unsigned short ip_len; // 16位总长度(字节)(total length of the packet) 47 | unsigned short ip_id; // 16位标识 48 | unsigned short ip_off; // 3位标志位 49 | unsigned char ip_ttl; // 8位生存时间 TTL 50 | unsigned char ip_p; // 8位协议 51 | unsigned short ip_sum; // 16位IP首部校验和 52 | unsigned long ip_src;// 32位源IP地址 53 | unsigned long ip_dst; // 32位目的IP地址 54 | }IpHeader; 55 | 56 | // 57 | // ICMP header 58 | // 59 | typedef struct icmp 60 | { 61 | unsigned char icmp_type;// 8位类型 62 | unsigned char icmp_code;// 8位代码 63 | unsigned short icmp_cksum;// 16位校验和 64 | unsigned short icmp_id; //ID标识 65 | unsigned short icmp_seq; // 报文序列号 66 | unsigned long icmp_data; // 时间戳 不属于标准的ICMP头,只是用来记录时间 67 | }IcmpHeader; 68 | 69 | #define STATUS_FAILED 0xFFFF //状态失败 70 | #define DEF_PACKET_SIZE 32 //发送数据包大小 71 | #define DEF_PACKET_NUMBER 4 /* 发送数据报的个数 */ 72 | #define MAX_PACKET 1024 //最大包 73 | 74 | class Ping { 75 | public: 76 | Ping(); 77 | virtual ~Ping(); 78 | 79 | void fill_icmpData(char*, int); 80 | u_uint16 checksum(u_uint16*, int); 81 | int decode_response(char*, int, struct sockaddr_in*); 82 | 83 | int ping(const char* pingAddress, int tryTimes = DEF_PACKET_NUMBER, int packetSize = DEF_PACKET_SIZE); 84 | 85 | }; 86 | 87 | #endif /* UTIL_PING_H_ */ 88 | -------------------------------------------------------------------------------- /util/readwritelock.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: readwritelock.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年12月9日 上午10:01:19 26 | * 27 | */ 28 | 29 | #include "readwritelock.h" 30 | #include "logger.h" 31 | 32 | ReadWriteLock::ReadWriteLock() { 33 | // TODO Auto-generated constructor stub 34 | init_lock(); 35 | } 36 | 37 | ReadWriteLock::~ReadWriteLock() { 38 | // TODO Auto-generated destructor stub 39 | #ifndef WIN32 40 | pthread_rwlock_destroy(&lock); 41 | #endif 42 | } 43 | 44 | int ReadWriteLock::init_lock() 45 | { 46 | #ifndef WIN32 47 | int res = pthread_rwlock_init(&lock, NULL); 48 | if (res != 0) { 49 | logs(Logger::FATAL, "init rwlock error"); 50 | return res; 51 | } 52 | #endif 53 | return 0; 54 | } 55 | 56 | void ReadWriteLock::read_lock() 57 | { 58 | #ifndef WIN32 59 | pthread_rwlock_rdlock(&lock); 60 | #else 61 | lock.lock(); 62 | #endif 63 | } 64 | 65 | void ReadWriteLock::read_unlock() 66 | { 67 | #ifndef WIN32 68 | pthread_rwlock_unlock(&lock); 69 | #else 70 | lock.unlock(); 71 | #endif 72 | } 73 | 74 | void ReadWriteLock::write_lock() 75 | { 76 | #ifndef WIN32 77 | pthread_rwlock_wrlock(&lock); 78 | #else 79 | lock.lock(); 80 | #endif 81 | } 82 | 83 | void ReadWriteLock::write_unlock() 84 | { 85 | this->read_unlock(); 86 | } 87 | -------------------------------------------------------------------------------- /util/readwritelock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: readwritelock.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年12月9日 上午10:01:19 26 | * 27 | */ 28 | 29 | #ifndef UTIL_READWRITELOCK_H_ 30 | #define UTIL_READWRITELOCK_H_ 31 | 32 | #include 33 | #include "mutexlock.h" 34 | 35 | class ReadWriteLock { 36 | public: 37 | ReadWriteLock(); 38 | virtual ~ReadWriteLock(); 39 | 40 | int init_lock(); 41 | 42 | void read_lock(); 43 | void write_lock(); 44 | 45 | void read_unlock(); 46 | void write_unlock(); 47 | private: 48 | #ifdef WIN32 49 | MutexLock lock; 50 | #else 51 | pthread_rwlock_t lock; 52 | #endif 53 | }; 54 | 55 | #endif /* UTIL_READWRITELOCK_H_ */ 56 | -------------------------------------------------------------------------------- /util/socketutil.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: socketutil.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年1月20日 上午10:07:44 26 | * 27 | */ 28 | 29 | #ifndef UTIL_SOCKETUTIL_H_ 30 | #define UTIL_SOCKETUTIL_H_ 31 | 32 | #include "systemapi.h" 33 | #include "stringbuf.h" 34 | 35 | class SocketUtil { 36 | public: 37 | static int socket_writeData(int fd, const void* data, const unsigned int dataLen, int microsecond = 500); 38 | static int socket_readData(int fd, void* data, const unsigned int dataLen, int microsecond = 500); 39 | static int socket_readAllData(int fd, StringBuf& sb, int microsecond=500); 40 | }; 41 | 42 | 43 | #endif /* UTIL_SOCKETUTIL_H_ */ 44 | -------------------------------------------------------------------------------- /util/stringbuf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: stringbuf.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月9日 26 | * 27 | */ 28 | 29 | #ifndef UTIL_STRINGBUF_H_ 30 | #define UTIL_STRINGBUF_H_ 31 | 32 | #undef NULL 33 | #if defined(__cplusplus) 34 | #define NULL 0 35 | #else 36 | #define NULL ((void *)0) 37 | #endif 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | 45 | #include "define.h" 46 | 47 | class StringBuf{ 48 | public: 49 | StringBuf(); 50 | StringBuf(StringBuf const&); 51 | ~StringBuf(); 52 | 53 | int reallocMem(unsigned int size); 54 | void mallocMem(unsigned int size); 55 | void erase(unsigned int startPos, unsigned int endPos); 56 | 57 | int append(const char* str); 58 | int append(const void* data, const unsigned int dataLen); 59 | int appendFormat(const char* fmt, ...); 60 | int insert(const unsigned int pos, const void* data, const int dataLen); 61 | void clear(); 62 | 63 | inline char* addr() { 64 | return this->m_buf; 65 | } 66 | 67 | inline char* get_offsetAddr() { 68 | return (char*)(this->m_buf + this->m_offset); 69 | } 70 | 71 | inline unsigned int get_remailLength() { 72 | if (this->m_length >= this->m_offset) { 73 | return this->m_length - this->m_offset; 74 | } 75 | return 0; 76 | } 77 | 78 | inline unsigned int get_remailAllocLen() { 79 | if (this->m_length <= this->m_allocateLen) 80 | return this->m_allocateLen - this->m_length; 81 | return 0; 82 | } 83 | 84 | unsigned int length() { 85 | return this->m_length; 86 | } 87 | 88 | private: 89 | unsigned int alignment(unsigned int size, unsigned int alignmentLength = 8); 90 | 91 | private: 92 | declare_class_member(char*, buf) 93 | declare_class_member(unsigned int, length) 94 | declare_class_member(unsigned int, offset) 95 | declare_class_member(unsigned int, allocateLen) 96 | }; 97 | #endif /* UTIL_STRINGBUF_H_ */ 98 | -------------------------------------------------------------------------------- /util/thread.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: thread.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月5日 26 | * 27 | */ 28 | 29 | #include "thread.h" 30 | #include "systemapi.h" 31 | #include "signal.h" 32 | 33 | Thread::Thread(ThreadType type, std::string tname) 34 | :threadName(tname) 35 | { 36 | this->type = type; 37 | this->threadId = 0; 38 | this->userFunc = NULL; 39 | this->userFuncArgs = NULL; 40 | } 41 | 42 | Thread::~Thread() 43 | { 44 | } 45 | 46 | void Thread::joinThread() { 47 | if (this->threadId != 0) { 48 | #ifdef WIN32 49 | WaitForSingleObject(this->threadId, INFINITE); 50 | #else 51 | pthread_join(this->threadId, NULL); 52 | #endif 53 | } 54 | } 55 | 56 | int Thread::startThread(ThreadFunc func, void*args) { 57 | this->userFunc = func; 58 | this->userFuncArgs = args; 59 | #ifdef WIN32 60 | this->threadId = ::CreateThread(NULL, 0, Thread::start, this, 0, NULL); 61 | #else 62 | if (pthread_create(&this->threadId, NULL, Thread::start, this) < 0) { 63 | logs(Logger::ERR, "create pthread error"); 64 | return -1; 65 | } 66 | #endif 67 | return 0; 68 | } 69 | 70 | u_uint64 Thread::get_threadId() 71 | { 72 | return (u_uint64)(this->threadId); 73 | } 74 | 75 | ThreadType Thread::get_threadType() 76 | { 77 | return this->type; 78 | } 79 | 80 | std::string Thread::get_threadName() 81 | { 82 | return this->threadName; 83 | } 84 | 85 | void Thread::block_threadSignal() 86 | { 87 | #ifndef __WIN32 88 | sigset_t set; 89 | sigemptyset(&set); 90 | sigaddset(&set, SIGQUIT); 91 | sigaddset(&set, SIGUSR1); 92 | sigaddset(&set, SIGUSR2); 93 | sigaddset(&set, SIGINT); 94 | sigaddset(&set, SIGTERM); 95 | 96 | if(pthread_sigmask(SIG_UNBLOCK, &set, NULL)) { 97 | logs(Logger::FATAL, "sigmask error"); 98 | } 99 | #endif 100 | } 101 | 102 | thread_start_func(Thread::start) 103 | { 104 | Thread* th = (Thread*)args; 105 | 106 | //1. set username 107 | SystemApi::system_setThreadName(th->threadName); 108 | 109 | th->block_threadSignal(); 110 | 111 | return th->userFunc(th->userFuncArgs); 112 | } 113 | -------------------------------------------------------------------------------- /util/thread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: thread.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月5日 26 | * 27 | */ 28 | 29 | #ifndef THREAD_H_ 30 | #define THREAD_H_ 31 | 32 | #ifdef WIN32 33 | #include 34 | #else 35 | #include 36 | #endif 37 | #include 38 | #include 39 | 40 | #include "logger.h" 41 | #include "define.h" 42 | 43 | #ifdef WIN32 44 | #define thread_func_return_type DWORD WINAPI 45 | #define thread_func_args_type LPVOID 46 | #define thread_id_t HANDLE 47 | #else 48 | #define thread_func_return_type void* 49 | #define thread_func_args_type void* 50 | #define thread_id_t pthread_t 51 | #endif 52 | 53 | #define thread_start_func(func_name) thread_func_return_type func_name(thread_func_args_type args) 54 | typedef thread_func_return_type (*ThreadFunc)(thread_func_args_type args); 55 | 56 | typedef enum { 57 | thread_type_init = 0, 58 | thread_type_client, 59 | thread_type_manager, 60 | thread_type_httpserver, 61 | thread_type_vip, 62 | thread_type_accept, 63 | thread_type_work, 64 | thread_type_db, 65 | } ThreadType; 66 | 67 | class Thread{ 68 | public: 69 | Thread(ThreadType type, std::string threadName); 70 | ~Thread(); 71 | 72 | void joinThread(); 73 | int startThread(ThreadFunc func, void* args); 74 | 75 | u_uint64 get_threadId(); 76 | ThreadType get_threadType(); 77 | std::string get_threadName(); 78 | void block_threadSignal(); 79 | private: 80 | static thread_start_func(start); 81 | private: 82 | thread_id_t threadId; 83 | ThreadType type; 84 | std::string threadName; 85 | 86 | ThreadFunc userFunc; 87 | void* userFuncArgs; 88 | }; 89 | 90 | #endif /* THREAD_H_ */ 91 | -------------------------------------------------------------------------------- /util/threadtask.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: threadtask.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2017年3月2日 下午1:49:42 26 | * 27 | */ 28 | 29 | #ifndef UTIL_THREADTASK_H_ 30 | #define UTIL_THREADTASK_H_ 31 | 32 | #include 33 | #include 34 | 35 | #include "thread.h" 36 | #include "mutexlock.h" 37 | 38 | typedef void (*TaskHandleFunc) (void* data, void* args); 39 | 40 | template 41 | class ThreadTask : public Thread{ 42 | public: 43 | ThreadTask(ThreadType threadType, std::string threadName, TaskHandleFunc func, void* func_args); 44 | virtual ~ThreadTask(){} 45 | void add_taskData(T data) { 46 | this->m_lock.lock(); 47 | this->m_dataList.push_back(data); 48 | this->m_lock.signal_mutexCond(); 49 | this->m_lock.unlock(); 50 | } 51 | void handle_taskData(); 52 | void set_stop(){ 53 | this->m_isStop = true; 54 | this->m_lock.lock(); 55 | this->m_lock.signal_mutexCond(); 56 | this->m_lock.unlock(); 57 | } 58 | virtual int init_childThread() { return 0;} 59 | private: 60 | static thread_start_func(start); 61 | private: 62 | typedef std::list TypeList; 63 | TypeList m_dataList; 64 | TaskHandleFunc m_func; 65 | void* m_funcArgs; 66 | bool m_isStop; 67 | MutexLock m_lock; 68 | }; 69 | 70 | template 71 | ThreadTask::ThreadTask(ThreadType threadType, std::string threadName, TaskHandleFunc func, void* func_args) 72 | : Thread(threadType, threadName) { 73 | // TODO Auto-generated constructor stub 74 | this->m_func = func; 75 | this->m_funcArgs = func_args; 76 | this->m_isStop = false; 77 | this->startThread(ThreadTask::start, this); 78 | } 79 | 80 | template 81 | void ThreadTask::handle_taskData() { 82 | T task; 83 | this->m_lock.lock(); 84 | if (!this->m_dataList.empty()) { 85 | task = this->m_dataList.front(); 86 | this->m_dataList.pop_front(); 87 | this->m_lock.unlock(); 88 | } else { 89 | this->m_lock.unlock(); 90 | return; 91 | } 92 | this->m_func((void*)&task, this->m_funcArgs); 93 | return; 94 | } 95 | 96 | template 97 | thread_start_func(ThreadTask::start) { 98 | ThreadTask* tt = (ThreadTask*)args; 99 | 100 | if (tt->init_childThread()){ 101 | logs(Logger::ERR, "init child Thread error"); 102 | return 0; 103 | } 104 | 105 | while(tt->m_isStop == false || !tt->m_dataList.empty()) { 106 | if (!tt->m_dataList.empty()) { 107 | tt->handle_taskData(); 108 | } else { 109 | tt->m_lock.lock(); 110 | tt->m_lock.wait_mutexCond(); 111 | tt->m_lock.unlock(); 112 | } 113 | } 114 | return 0; 115 | } 116 | #endif /* UTIL_THREADTASK_H_ */ 117 | -------------------------------------------------------------------------------- /util/tool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: tool.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年8月19日 26 | * 27 | */ 28 | 29 | #ifndef PROTOCOL_TOOL_H_ 30 | #define PROTOCOL_TOOL_H_ 31 | 32 | #include "define.h" 33 | #include "stringbuf.h" 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | class Tool{ 40 | public: 41 | 42 | static unsigned int quick_hash_code(const char* string, const int len); 43 | static unsigned int quick_conn_hash_code(std::string caddr, int cport, 44 | std::string saddr, int sport, u_uint64 connTime); 45 | 46 | 47 | static int byte2string(const u_uint8* bdata, const unsigned int bdataLen, std::string& str); 48 | static int string2byte(const std::string& str, StringBuf& bdata); 49 | static int byte2wstring(const u_uint8* bdata, const unsigned int bdataLen, std::wstring& wstr); 50 | static int wstring2byte(const std::wstring& wstr, StringBuf& bdata); 51 | 52 | static std::string itoa(const int number); 53 | 54 | static std::string format_string(const char* str, const unsigned int strLen, const unsigned int length); 55 | static void tolower(std::string& str); 56 | static void toupper(std::string &str); 57 | static std::string args2string(const char* fmt, ...); 58 | static void argList2string(const char* fmt, va_list& argList, std::string& result); 59 | static int stringbuf_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap); 60 | static int string2wstring(const std::string& str, std::wstring& wstr); 61 | static int wstring2string(const std::wstring& wstr, std::string& str); 62 | static std::string int2string(int i); 63 | static std::string stringTrim(std::string& str); 64 | 65 | /* 66 | * @desc 查找指定目录下的一个文件,如果有多个目标文件,指需要返回第一个目标文件即可 67 | * @param dir 指定的目录 68 | * fileType 文件类型,比如:*.txt, *.exe, *.ini 69 | * @return 查找到的文件名称 70 | * */ 71 | static std::string search_oneFile(std::string dir, std::string fileType); 72 | 73 | //在src中查找dst,根据ignoreCase来决定是否忽略大小写,返回在src中的位置,如果不存在则返回-1 74 | static int find_sqlKeyWord(const std::string& src, const std::string& dst, bool ignoreCase); 75 | }; 76 | 77 | #endif /* PROTOCOL_TOOL_H_ */ 78 | -------------------------------------------------------------------------------- /util/uspinlock.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: spinlock.cpp 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年12月9日 上午10:27:45 26 | * 27 | */ 28 | 29 | #include "uspinlock.h" 30 | 31 | USpinLock::USpinLock() { 32 | // TODO Auto-generated constructor stub 33 | #ifndef WIN32 34 | pthread_spin_init(&m_lock, 0); 35 | #endif 36 | } 37 | 38 | USpinLock::~USpinLock() { 39 | // TODO Auto-generated destructor stub 40 | #ifndef WIN32 41 | pthread_spin_destroy(&m_lock); 42 | #endif 43 | } 44 | 45 | void USpinLock::lock() { 46 | #ifndef WIN32 47 | pthread_spin_lock(&m_lock); 48 | #else 49 | m_lock.lock(); 50 | #endif 51 | } 52 | 53 | void USpinLock::unlock() { 54 | #ifndef WIN32 55 | pthread_spin_unlock(&m_lock); 56 | #else 57 | m_lock.unlock(); 58 | #endif 59 | } 60 | -------------------------------------------------------------------------------- /util/uspinlock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: spinlock.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年12月9日 上午10:27:45 26 | * 27 | */ 28 | 29 | #ifndef UTIL_USPINLOCK_H_ 30 | #define UTIL_USPINLOCK_H_ 31 | #include "mutexlock.h" 32 | #ifndef WIN32 33 | #include 34 | #endif 35 | #include 36 | 37 | #ifdef WIN32 38 | typedef MutexLock LockHandle; 39 | #else 40 | typedef pthread_spinlock_t LockHandle; 41 | #endif 42 | 43 | class USpinLock { 44 | public: 45 | USpinLock(); 46 | virtual ~USpinLock(); 47 | void lock(); 48 | void unlock(); 49 | private: 50 | LockHandle m_lock; 51 | }; 52 | 53 | #endif /* UTIL_USPINLOCK_H_ */ 54 | -------------------------------------------------------------------------------- /util/vip.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | * @FileName: vip.h 20 | * @Description: TODO 21 | * All rights Reserved, Designed By huih 22 | * @Company: onexsoft 23 | * @Author: hui 24 | * @Version: V1.0 25 | * @Date: 2016年9月8日 26 | * 27 | */ 28 | 29 | #ifndef UTIL_VIP_H_ 30 | #define UTIL_VIP_H_ 31 | 32 | #include "thread.h" 33 | #include "define.h" 34 | 35 | struct ARP_PACKET 36 | { 37 | unsigned char dest_mac[6]; 38 | unsigned char src_mac[6]; 39 | unsigned short type; 40 | unsigned short hw_type; 41 | unsigned short pro_type; 42 | unsigned char hw_len; 43 | unsigned char pro_len; 44 | unsigned short op; 45 | unsigned char from_mac[6]; 46 | unsigned char from_ip[4]; 47 | unsigned char to_mac[6]; 48 | unsigned char to_ip[4]; 49 | }; 50 | 51 | class Vip : public Thread{ 52 | public: 53 | Vip(std::string ifName, std::string vipAddress, std::string threadName); 54 | virtual ~Vip(); 55 | 56 | static thread_start_func(start); 57 | private: 58 | /** 59 | * @desc 发送关于address与mac的arp包到局域网中 60 | * @param ifndex 接口的下标 61 | * address需要绑定的ip地址 62 | * mac 需要绑定的mac地址 63 | * @return 成功返回0, 否则返回-1. 64 | * **/ 65 | int send_arp_packet(int ifndx, unsigned char* address, unsigned char* mac); 66 | 67 | /** 68 | * @desc 广播当前绑定vip的主机的mac地址 69 | * @param vipmac 绑定vip的主机的mac地址 70 | * @return 成功返回0, 否则返回-1. 71 | * **/ 72 | int send_broadcast(char* vipmac); 73 | 74 | /** 75 | * @desc 获取主机的mac地址 76 | * @param ifname 网卡名称 77 | * mac 用于保存获取到的mac地址 78 | * macLen mac缓存的长度 79 | * @return 成功返回0, 否则返回-1. 80 | * **/ 81 | int get_macAddresss(const char *ifname, char* mac, int macLen); 82 | 83 | /** 84 | * @desc 下线网卡 85 | * @param vipIFName 虚拟网卡 86 | * @return 下线成功则返回0, 否则返回-1. 87 | * **/ 88 | int down_vipAddress(const char *vipIFName); 89 | 90 | /** 91 | * @desc 设置VIP地址 92 | * @param ifname 需要设置虚拟网卡的网卡名称 93 | * address 虚拟网卡ip地址 94 | * isdel true表示删除vip,false表示设置vip。 95 | * @return 设置成功返回0, 否则返回-1. 96 | * **/ 97 | int set_vip_address(const char *ifname, const char *address, bool isdel); 98 | 99 | /** 100 | * @desc 从局域网中获取vip对应的mac地址 101 | * @param bfid sock的id 102 | * mac 保存mac地址的缓存 103 | * macLen mac地址缓存的长度 104 | * @return 成功返回0, 否则返回-1. 105 | * **/ 106 | int get_vipMacAddress(int bfid, char* mac, int macLen); 107 | 108 | declare_class_member(std::string, ifname); 109 | declare_class_member(std::string, vipAddress); 110 | declare_class_member(bool, isStop); 111 | }; 112 | 113 | #endif /* UTIL_VIP_H_ */ 114 | --------------------------------------------------------------------------------