Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

缓冲区大小cmake定义, 增加Err_reset错误 #196

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,39 @@ if(HAVE_RECVMMSG_API)
add_definitions(-DHAVE_RECVMMSG_API)
endif()


# check the socket buffer size set by the upper cmake project, if it is set, use the setting of the upper cmake project, otherwise set it to 256K
# if the socket buffer size is set to 0, it means that the socket buffer size is not set, and the kernel default value is used(just for linux)
if(DEFINED SOCKET_DEFAULT_BUF_SIZE)
if (SOCKET_DEFAULT_BUF_SIZE EQUAL 0)
message(STATUS "Socket default buffer size is not set, use the kernel default value")
else()
message(STATUS "Socket default buffer size is set to ${SOCKET_DEFAULT_BUF_SIZE}")
endif ()
add_definitions(-DSOCKET_DEFAULT_BUF_SIZE=${SOCKET_DEFAULT_BUF_SIZE})
endif()
#加载自定义模块
#Load custom modules
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
#设置库文件路径
#Set the library file path
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
#设置可执行程序路径
#Set the executable program path
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
#设置子目录
#Set subdirectories
set(SUB_DIR_LIST "Network" "Poller" "Thread" "Util")

if(WIN32)
list(APPEND SUB_DIR_LIST "win32")
#防止Windows.h包含Winsock.h
#Prevent Windows.h from including Winsock.h
add_definitions(-DWIN32_LEAN_AND_MEAN)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()

#安装目录
#Installation directory
if(WIN32)
set(INSTALL_PATH_LIB $ENV{HOME}/${PROJECT_NAME}/lib)
set(INSTALL_PATH_INCLUDE $ENV{HOME}/${PROJECT_NAME}/include)
Expand All @@ -47,12 +62,15 @@ endif()

foreach(SUB_DIR ${SUB_DIR_LIST})
#遍历源文件
#Traverse source file
aux_source_directory(src/${SUB_DIR} SRC_LIST)
#安装头文件至系统目录
#Install header file to system directory
install(DIRECTORY src/${SUB_DIR} DESTINATION ${INSTALL_PATH_INCLUDE} FILES_MATCHING PATTERN "*.h")
endforeach()

#非苹果平台移除.mm类型的文件
#Remove .mm type files from non-apple platforms
if(NOT APPLE)
list(REMOVE_ITEM SRC_LIST "src/Network/Socket_ios.mm")
endif()
Expand All @@ -69,6 +87,7 @@ set(ENABLE_MYSQL ON CACHE BOOL "enable mysql")


#查找openssl是否安装
#Find out if openssl is installed
find_package(OpenSSL QUIET)
if(OPENSSL_FOUND AND ENABLE_OPENSSL)
message(STATUS "找到openssl库:\"${OPENSSL_INCLUDE_DIR}\",ENABLE_OPENSSL宏已打开")
Expand All @@ -78,6 +97,7 @@ if(OPENSSL_FOUND AND ENABLE_OPENSSL)
endif()

#查找mysql是否安装
#Find out if mysql is installed
find_package(MYSQL QUIET)
if(MYSQL_FOUND AND ENABLE_MYSQL)
message(STATUS "找到mysqlclient库:\"${MYSQL_INCLUDE_DIR}\",ENABLE_MYSQL宏已打开")
Expand All @@ -88,8 +108,10 @@ if(MYSQL_FOUND AND ENABLE_MYSQL)
endif()

#打印库文件
#Print library files
message(STATUS "将链接依赖库:${LINK_LIB_LIST}")
#使能c++11
#Enable c++11
set(CMAKE_CXX_STANDARD 11)

if(NOT WIN32)
Expand All @@ -98,6 +120,7 @@ if(NOT WIN32)
endif()

#编译动态库
#Compile dynamic library
if(NOT IOS AND NOT ANDROID AND NOT WIN32)
add_library(${PROJECT_NAME}_shared SHARED ${SRC_LIST})
target_include_directories(${PROJECT_NAME}_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
Expand All @@ -107,15 +130,19 @@ if(NOT IOS AND NOT ANDROID AND NOT WIN32)
endif()

#编译静态库
#Compile static library
add_library(${PROJECT_NAME}_static STATIC ${SRC_LIST})
#引用头文件路径
#Reference header file path
target_include_directories(${PROJECT_NAME}_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
set_target_properties(${PROJECT_NAME}_static PROPERTIES OUTPUT_NAME "${PROJECT_NAME}")
#安装静态库至系统目录
#Install static library to system directory
install(TARGETS ${PROJECT_NAME}_static ARCHIVE DESTINATION ${INSTALL_PATH_LIB})


#测试程序
#Test program
if(NOT IOS)
add_subdirectory(tests)
endif()
1 change: 1 addition & 0 deletions src/Network/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
case UV_EAGAIN: return SockException(Err_success, "success");
case UV_ECONNREFUSED: return SockException(Err_refused, uv_strerror(error), error);
case UV_ETIMEDOUT: return SockException(Err_timeout, uv_strerror(error), error);
case UV_ECONNRESET: return SockException(Err_reset, uv_strerror(error), error);
default: return SockException(Err_other, uv_strerror(error), error);
}
}
Expand Down Expand Up @@ -273,7 +274,7 @@

while (_enable_recv) {
do {
nread = recvfrom(sock->rawFd(), data, capacity, 0, (struct sockaddr *)&addr, &len);

Check warning on line 277 in src/Network/Socket.cpp

View workflow job for this annotation

GitHub Actions / build

'argument': conversion from 'size_t' to 'int', possible loss of data
} while (-1 == nread && UV_EINTR == get_uv_error(true));

if (nread == 0) {
Expand Down
13 changes: 7 additions & 6 deletions src/Network/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ namespace toolkit {

//错误类型枚举
typedef enum {
Err_success = 0, //成功
Err_success = 0, //成功 success
Err_eof, //eof
Err_timeout, //超时
Err_refused,//连接被拒绝
Err_dns,//dns解析失败
Err_shutdown,//主动关闭
Err_other = 0xFF,//其他错误
Err_timeout, //超时 socket timeout
Err_refused,//连接被拒绝 socket refused
Err_reset,//连接被重置 socket reset
Err_dns,//dns解析失败 dns resolve failed
Err_shutdown,//主动关闭 socket shutdown
Err_other = 0xFF,//其他错误 other error
} ErrCode;

//错误信息类
Expand Down
7 changes: 7 additions & 0 deletions src/Network/sockutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ int SockUtil::setNoBlocked(int fd, bool noblock) {
}

int SockUtil::setRecvBuf(int fd, int size) {
if(size <= 0){
alexliyu7352 marked this conversation as resolved.
Show resolved Hide resolved
// use the system default value
return 0;
}
int ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size));
if (ret == -1) {
TraceL << "setsockopt SO_RCVBUF failed";
Expand All @@ -287,6 +291,9 @@ int SockUtil::setRecvBuf(int fd, int size) {
}

int SockUtil::setSendBuf(int fd, int size) {
if(size <= 0){
alexliyu7352 marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}
int ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size));
if (ret == -1) {
TraceL << "setsockopt SO_SNDBUF failed";
Expand Down
10 changes: 10 additions & 0 deletions src/Network/sockutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,17 @@ int ioctl(int fd, long cmd, u_long *ptr);
int close(int fd);
#endif // defined(_WIN32)

#if !defined(SOCKET_DEFAULT_BUF_SIZE)
#define SOCKET_DEFAULT_BUF_SIZE (256 * 1024)
#else
#if SOCKET_DEFAULT_BUF_SIZE == 0 && !defined(__linux__)
// just for linux, because in some high-throughput environments,
// kernel control is more efficient and reasonable than program
// settings. For example, refer to cloudflare's blog
#undef SOCKET_DEFAULT_BUF_SIZE
#define SOCKET_DEFAULT_BUF_SIZE (256 * 1024)
#endif
#endif
#define TCP_KEEPALIVE_INTERVAL 30
#define TCP_KEEPALIVE_PROBE_TIMES 9
#define TCP_KEEPALIVE_TIME 120
Expand Down
Loading