Skip to content

Commit e28bb1b

Browse files
添加websocket动态库实现
1 parent ea65189 commit e28bb1b

11 files changed

+263
-150
lines changed

3rd/glog/CMakeLists.txt

Lines changed: 114 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -764,72 +764,73 @@ if (BUILD_TESTING)
764764
endif (APPLE)
765765
endif (WITH_CUSTOM_PREFIX)
766766

767-
add_executable (stl_logging_unittest
768-
src/stl_logging_unittest.cc
769-
)
770-
771-
target_link_libraries (stl_logging_unittest PRIVATE ${_GLOG_TEST_LIBS})
772-
773-
if (HAVE_NO_DEPRECATED)
774-
set_property (TARGET stl_logging_unittest APPEND PROPERTY COMPILE_OPTIONS
775-
-Wno-deprecated)
776-
endif (HAVE_NO_DEPRECATED)
777-
778-
if (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
779-
target_compile_definitions (stl_logging_unittest PRIVATE
780-
GLOG_STL_LOGGING_FOR_UNORDERED)
781-
endif (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
782-
783-
if (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
784-
target_compile_definitions (stl_logging_unittest PRIVATE
785-
GLOG_STL_LOGGING_FOR_TR1_UNORDERED)
786-
endif (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
787-
788-
if (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
789-
target_compile_definitions (stl_logging_unittest PRIVATE
790-
GLOG_STL_LOGGING_FOR_EXT_HASH)
791-
endif (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
792-
793-
if (HAVE_EXT_SLIST)
794-
target_compile_definitions (stl_logging_unittest PRIVATE
795-
GLOG_STL_LOGGING_FOR_EXT_SLIST)
796-
endif (HAVE_EXT_SLIST)
797-
798-
if (HAVE_SYMBOLIZE)
799-
add_executable (symbolize_unittest
800-
src/symbolize_unittest.cc
801-
)
802-
803-
target_link_libraries (symbolize_unittest PRIVATE ${_GLOG_TEST_LIBS})
804-
endif (HAVE_SYMBOLIZE)
805-
806-
add_executable (demangle_unittest
807-
src/demangle_unittest.cc
808-
)
809-
810-
target_link_libraries (demangle_unittest PRIVATE ${_GLOG_TEST_LIBS})
811-
812-
if (HAVE_STACKTRACE)
813-
add_executable (stacktrace_unittest
814-
src/stacktrace_unittest.cc
815-
)
816-
817-
target_link_libraries (stacktrace_unittest PRIVATE ${_GLOG_TEST_LIBS})
818-
endif (HAVE_STACKTRACE)
819-
820-
add_executable (utilities_unittest
821-
src/utilities_unittest.cc
822-
)
823-
824-
target_link_libraries (utilities_unittest PRIVATE ${_GLOG_TEST_LIBS})
825-
826-
if (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
827-
add_executable (signalhandler_unittest
828-
src/signalhandler_unittest.cc
829-
)
830-
831-
target_link_libraries (signalhandler_unittest PRIVATE ${_GLOG_TEST_LIBS})
832-
endif (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
767+
#
768+
# add_executable (stl_logging_unittest
769+
# src/stl_logging_unittest.cc
770+
# )
771+
#
772+
# target_link_libraries (stl_logging_unittest PRIVATE ${_GLOG_TEST_LIBS})
773+
#
774+
# if (HAVE_NO_DEPRECATED)
775+
# set_property (TARGET stl_logging_unittest APPEND PROPERTY COMPILE_OPTIONS
776+
# -Wno-deprecated)
777+
# endif (HAVE_NO_DEPRECATED)
778+
#
779+
# if (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
780+
# target_compile_definitions (stl_logging_unittest PRIVATE
781+
# GLOG_STL_LOGGING_FOR_UNORDERED)
782+
# endif (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
783+
#
784+
# if (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
785+
# target_compile_definitions (stl_logging_unittest PRIVATE
786+
# GLOG_STL_LOGGING_FOR_TR1_UNORDERED)
787+
# endif (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
788+
#
789+
# if (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
790+
# target_compile_definitions (stl_logging_unittest PRIVATE
791+
# GLOG_STL_LOGGING_FOR_EXT_HASH)
792+
# endif (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
793+
#
794+
# if (HAVE_EXT_SLIST)
795+
# target_compile_definitions (stl_logging_unittest PRIVATE
796+
# GLOG_STL_LOGGING_FOR_EXT_SLIST)
797+
# endif (HAVE_EXT_SLIST)
798+
#
799+
# if (HAVE_SYMBOLIZE)
800+
# add_executable (symbolize_unittest
801+
# src/symbolize_unittest.cc
802+
# )
803+
#
804+
# target_link_libraries (symbolize_unittest PRIVATE ${_GLOG_TEST_LIBS})
805+
# endif (HAVE_SYMBOLIZE)
806+
#
807+
# add_executable (demangle_unittest
808+
# src/demangle_unittest.cc
809+
# )
810+
#
811+
# target_link_libraries (demangle_unittest PRIVATE ${_GLOG_TEST_LIBS})
812+
#
813+
# if (HAVE_STACKTRACE)
814+
# add_executable (stacktrace_unittest
815+
# src/stacktrace_unittest.cc
816+
# )
817+
#
818+
# target_link_libraries (stacktrace_unittest PRIVATE ${_GLOG_TEST_LIBS})
819+
# endif (HAVE_STACKTRACE)
820+
#
821+
# add_executable (utilities_unittest
822+
# src/utilities_unittest.cc
823+
# )
824+
#
825+
# target_link_libraries (utilities_unittest PRIVATE ${_GLOG_TEST_LIBS})
826+
#
827+
# if (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
828+
# add_executable (signalhandler_unittest
829+
# src/signalhandler_unittest.cc
830+
# )
831+
#
832+
# target_link_libraries (signalhandler_unittest PRIVATE ${_GLOG_TEST_LIBS})
833+
# endif (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
833834

834835
add_test (NAME demangle COMMAND demangle_unittest)
835836
add_test (NAME logging COMMAND logging_unittest)
@@ -861,16 +862,16 @@ if (BUILD_TESTING)
861862
add_test (NAME symbolize COMMAND symbolize_unittest)
862863
endif (TARGET symbolize_unittest)
863864

864-
if (HAVE_LIB_GMOCK)
865-
add_executable (mock-log_unittest
866-
src/mock-log_unittest.cc
867-
src/mock-log.h
868-
)
869-
870-
target_link_libraries (mock-log_unittest PRIVATE ${_GLOG_TEST_LIBS})
871-
872-
add_test (NAME mock-log COMMAND mock-log_unittest)
873-
endif (HAVE_LIB_GMOCK)
865+
# if (HAVE_LIB_GMOCK)
866+
# add_executable (mock-log_unittest
867+
# src/mock-log_unittest.cc
868+
# src/mock-log.h
869+
# )
870+
#
871+
# target_link_libraries (mock-log_unittest PRIVATE ${_GLOG_TEST_LIBS})
872+
#
873+
# add_test (NAME mock-log COMMAND mock-log_unittest)
874+
# endif (HAVE_LIB_GMOCK)
874875

875876
# Generate an initial cache
876877

@@ -926,56 +927,56 @@ if (BUILD_TESTING)
926927
set_tests_properties (cmake_package_config_build PROPERTIES
927928
FIXTURES_REQUIRED "cmake_package_config;cmake_package_config_working")
928929

929-
add_executable (cleanup_immediately_unittest
930-
src/cleanup_immediately_unittest.cc)
931-
932-
target_link_libraries (cleanup_immediately_unittest PRIVATE ${_GLOG_TEST_LIBS})
933-
934-
add_executable (cleanup_with_absolute_prefix_unittest
935-
src/cleanup_with_absolute_prefix_unittest.cc)
936-
937-
target_link_libraries (cleanup_with_absolute_prefix_unittest PRIVATE ${_GLOG_TEST_LIBS})
938-
939-
add_executable (cleanup_with_relative_prefix_unittest
940-
src/cleanup_with_relative_prefix_unittest.cc)
941-
942-
target_link_libraries (cleanup_with_relative_prefix_unittest PRIVATE ${_GLOG_TEST_LIBS})
930+
# add_executable (cleanup_immediately_unittest
931+
# src/cleanup_immediately_unittest.cc)
932+
#
933+
# target_link_libraries (cleanup_immediately_unittest PRIVATE ${_GLOG_TEST_LIBS})
934+
#
935+
# add_executable (cleanup_with_absolute_prefix_unittest
936+
# src/cleanup_with_absolute_prefix_unittest.cc)
937+
#
938+
# target_link_libraries (cleanup_with_absolute_prefix_unittest PRIVATE ${_GLOG_TEST_LIBS})
939+
#
940+
# add_executable (cleanup_with_relative_prefix_unittest
941+
# src/cleanup_with_relative_prefix_unittest.cc)
942+
943+
# target_link_libraries (cleanup_with_relative_prefix_unittest PRIVATE ${_GLOG_TEST_LIBS})
943944

944945
set (CLEANUP_LOG_DIR ${CMAKE_CURRENT_BINARY_DIR}/cleanup_tests)
945946

946947
add_test (NAME cleanup_init COMMAND
947948
${CMAKE_COMMAND} -E make_directory ${CLEANUP_LOG_DIR})
948949
add_test (NAME cleanup_logdir COMMAND
949950
${CMAKE_COMMAND} -E remove_directory ${CLEANUP_LOG_DIR})
950-
add_test (NAME cleanup_immediately COMMAND
951-
${CMAKE_COMMAND}
952-
-DLOGCLEANUP=$<TARGET_FILE:cleanup_immediately_unittest>
953-
# NOTE The trailing slash is important
954-
-DTEST_DIR=${CLEANUP_LOG_DIR}/
955-
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/RunCleanerTest1.cmake
956-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
957-
add_test (NAME cleanup_with_absolute_prefix COMMAND
958-
${CMAKE_COMMAND}
959-
-DLOGCLEANUP=$<TARGET_FILE:cleanup_with_absolute_prefix_unittest>
960-
-DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/
961-
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/RunCleanerTest2.cmake
962-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
963-
add_test (NAME cleanup_with_relative_prefix COMMAND
964-
${CMAKE_COMMAND}
965-
-DLOGCLEANUP=$<TARGET_FILE:cleanup_with_relative_prefix_unittest>
966-
-DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/
967-
-DTEST_SUBDIR=test_subdir/
968-
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/RunCleanerTest3.cmake
969-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
951+
# add_test (NAME cleanup_immediately COMMAND
952+
# ${CMAKE_COMMAND}
953+
# -DLOGCLEANUP=$<TARGET_FILE:cleanup_immediately_unittest>
954+
# # NOTE The trailing slash is important
955+
# -DTEST_DIR=${CLEANUP_LOG_DIR}/
956+
# -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/RunCleanerTest1.cmake
957+
# WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
958+
# add_test (NAME cleanup_with_absolute_prefix COMMAND
959+
# ${CMAKE_COMMAND}
960+
# -DLOGCLEANUP=$<TARGET_FILE:cleanup_with_absolute_prefix_unittest>
961+
# -DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/
962+
# -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/RunCleanerTest2.cmake
963+
# WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
964+
# add_test (NAME cleanup_with_relative_prefix COMMAND
965+
# ${CMAKE_COMMAND}
966+
# -DLOGCLEANUP=$<TARGET_FILE:cleanup_with_relative_prefix_unittest>
967+
# -DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/
968+
# -DTEST_SUBDIR=test_subdir/
969+
# -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/RunCleanerTest3.cmake
970+
# WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
970971

971972
# Fixtures setup
972973
set_tests_properties (cleanup_init PROPERTIES FIXTURES_SETUP logcleanuptest)
973974
## Fixtures cleanup
974975
set_tests_properties (cleanup_logdir PROPERTIES FIXTURES_CLEANUP logcleanuptest)
975976
# Fixture requirements
976-
set_tests_properties (cleanup_immediately PROPERTIES FIXTURES_REQUIRED logcleanuptest)
977-
set_tests_properties (cleanup_with_absolute_prefix PROPERTIES FIXTURES_REQUIRED logcleanuptest)
978-
set_tests_properties (cleanup_with_relative_prefix PROPERTIES FIXTURES_REQUIRED logcleanuptest)
977+
# set_tests_properties (cleanup_immediately PROPERTIES FIXTURES_REQUIRED logcleanuptest)
978+
# set_tests_properties (cleanup_with_absolute_prefix PROPERTIES FIXTURES_REQUIRED logcleanuptest)
979+
# set_tests_properties (cleanup_with_relative_prefix PROPERTIES FIXTURES_REQUIRED logcleanuptest)
979980
endif (BUILD_TESTING)
980981

981982
install (TARGETS glog

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ target_link_directories(dlms
4343
target_link_libraries(dlms
4444
PUBLIC glog)
4545

46-
46+
add_subdirectory(test)
4747

4848

4949

plugins/CMakeLists.txt

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11

2-
# 定义动态库,虽然最终是生成so文件,也被称为可执行文件的一种,但是动态库并不需要链接全部文件,有些依赖的文件只需要给出头文件即可
3-
add_library(websocket SHARED
4-
websocket/websocket_plugin.cpp
5-
websocket/websocket_server.cpp
6-
websocket/websocket_client.cpp)
7-
8-
#add_executable(websocket
9-
# websocket/websocket_plugin.cpp
10-
# websocket/websocket_server.cpp
11-
# websocket/websocket_client.cpp)
12-
target_link_libraries(websocket
13-
PUBLIC pthread)
14-
target_include_directories(websocket
15-
PUBLIC ${PROJECT_SOURCE_DIR}/inc
16-
PUBLIC ${PROJECT_SOURCE_DIR}/3rd/civetweb/include
17-
PUBLIC ${PROJECT_SOURCE_DIR}/utils)
18-
target_compile_definitions(websocket
19-
PUBLIC pthread
20-
PUBLIC dl)
2+
3+
4+
add_subdirectory(websocket)
5+
216

227

238

plugins/websocket/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 定义动态库,虽然最终是生成so文件,也被称为可执行文件的一种,但是动态库并不需要链接全部文件,有些依赖的文件只需要给出头文件即可
2+
add_library(websocket SHARED
3+
websocket_plugin.cpp
4+
websocket_server.cpp
5+
websocket_client.cpp)
6+
7+
target_link_libraries(websocket
8+
PUBLIC pthread)
9+
target_include_directories(websocket
10+
PUBLIC ${PROJECT_SOURCE_DIR}/inc
11+
PUBLIC ${PROJECT_SOURCE_DIR}/3rd/civetweb/include
12+
PUBLIC ${PROJECT_SOURCE_DIR}/utils)
13+
target_compile_definitions(websocket
14+
PUBLIC pthread
15+
PUBLIC dl)

plugins/websocket/websocket_plugin.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Status Websocket::Init(IContext *lpIDlms, std::string &pluginName) {
1515
mg_init_library(0);
1616

1717

18-
1918
return Status::Ok();
2019
}
2120

plugins/websocket/websocket_server.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ Status WebsocketServer::Start() {
1515
"document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0};
1616

1717
std::vector<std::string> cpp_options;
18-
for (int i=0; i<(sizeof(options)/sizeof(options[0])-1); i++) {
19-
cpp_options.push_back(options[i]);
18+
cpp_options.reserve(sizeof(options) / sizeof(options[0]) - 1);
19+
for (int i = 0; i < (sizeof(options) / sizeof(options[0]) - 1); i++) {
20+
cpp_options.emplace_back(options[i]);
2021
}
21-
lpCivetServer = new (std::nothrow) CivetServer(cpp_options);
22+
lpCivetServer = new(std::nothrow) CivetServer(cpp_options);
2223
if (nullptr == lpCivetServer) {
2324
return Status::OutOfMemory("New civet server failed!");
2425
}
2526

26-
lpWebSocketHandler = new (std::nothrow) WebSocketHandler;
27+
lpWebSocketHandler = new(std::nothrow) WebSocketHandler;
2728
if (nullptr == lpWebSocketHandler) {
2829
return Status::OutOfMemory("New web socket handler failed!");
2930
}
@@ -34,7 +35,7 @@ Status WebsocketServer::Start() {
3435
return Status::Ok();
3536
}
3637

37-
int32_t WebsocketServer::Init(IContext *lpIDlms, std::string &pluginName) {
38+
int32_t WebsocketServer::Init(IContext *lpContext, std::string &pluginName) {
3839

3940

4041
return 0;

plugins/websocket/websocket_server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class WebSocketHandler : public CivetWebSocketHandler {
5050
class WebsocketServer {
5151
public:
5252

53-
int32_t Init(IContext *lpIDlms, std::string& pluginName);
53+
int32_t Init(IContext *lpContext, std::string& pluginName);
5454

5555
Status Start();
5656

src/dlms/dlms.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,15 @@ int main(int argc, char* argv[]) {
2121
FLAGS_log_dir = "./log";
2222
LOG(INFO) << "Hello, world!";
2323

24-
CContext dlms;
24+
CContext context;
2525
// 1. 解析命令行参数
26-
dlms.ParseCommandLine(argc, argv);
27-
28-
dlms.Init();
29-
dlms.Start();
30-
dlms.Stop();
31-
dlms.Reset();
32-
33-
26+
context.ParseCommandLine(argc, argv);
3427

28+
context.Init();
29+
context.Start();
3530

31+
context.Stop();
32+
context.Reset();
3633

3734

3835

@@ -60,6 +57,8 @@ Status CContext::Init() {
6057
}
6158

6259
Status CContext::Start() {
60+
61+
6362
return Status::Ok();
6463
}
6564

0 commit comments

Comments
 (0)