1. 配合zltoolkit自定义socket缓冲区大小.2. 修复http keepalive引发的bug

This commit is contained in:
Alex 2023-11-29 18:03:28 +08:00
parent e81ff307c7
commit 8e1470777e
4 changed files with 68 additions and 8 deletions

View File

@ -151,6 +151,17 @@ if(HAVE_RECVMMSG_API)
list(APPEND COMPILE_DEFINITIONS HAVE_RECVMMSG_API) list(APPEND COMPILE_DEFINITIONS HAVE_RECVMMSG_API)
endif() 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()
set(ToolKit_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/ZLToolKit) set(ToolKit_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/ZLToolKit)
# #
file(GLOB ToolKit_SRC_LIST file(GLOB ToolKit_SRC_LIST

View File

@ -24,11 +24,13 @@
cmake_minimum_required(VERSION 3.1.3) cmake_minimum_required(VERSION 3.1.3)
# #
# Load custom modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
project(ZLMediaKit LANGUAGES C CXX) project(ZLMediaKit LANGUAGES C CXX)
# 使 C++11 # 使 C++11
# Enable C++11
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
option(ENABLE_API "Enable C API SDK" ON) option(ENABLE_API "Enable C API SDK" ON)
@ -58,8 +60,11 @@ option(ENABLE_X264 "Enable x264" OFF)
option(ENABLE_WEPOLL "Enable wepoll" ON) option(ENABLE_WEPOLL "Enable wepoll" ON)
option(DISABLE_REPORT "Disable report to report.zlmediakit.com" off) option(DISABLE_REPORT "Disable report to report.zlmediakit.com" off)
option(USE_SOLUTION_FOLDERS "Enable solution dir supported" ON) option(USE_SOLUTION_FOLDERS "Enable solution dir supported" ON)
############################################################################## ##############################################################################
# socket256k.0socket,使用系统内核默认值(设置为0仅对linux有效)
# Set the default buffer size of the socket to 256k. If set to 0, the default buffer size of the socket will not be set,
# and the system kernel default value will be used (setting to 0 is only valid for linux)
set(SOCKET_DEFAULT_BUF_SIZE 262144 CACHE STRING "Default buffer size for socket" FORCE)
if("${CMAKE_BUILD_TYPE}" STREQUAL "") if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "Debug") set(CMAKE_BUILD_TYPE "Debug")
@ -68,12 +73,14 @@ endif()
message(STATUS "编译类型: ${CMAKE_BUILD_TYPE}") message(STATUS "编译类型: ${CMAKE_BUILD_TYPE}")
# 便, FORCE CACHE, # 便, FORCE CACHE,
# To facilitate the troubleshooting of compilation problems, you need to FORCE CACHE, otherwise you need to set it on the command line to take effect
set(CMAKE_VERBOSE_MAKEFILE ON CACHE INTERNAL "" FORCE) set(CMAKE_VERBOSE_MAKEFILE ON CACHE INTERNAL "" FORCE)
# TODO: include server , # TODO: include server ,
set(CMAKE_INCLUDE_CURRENT_DIR OFF) set(CMAKE_INCLUDE_CURRENT_DIR OFF)
# #
# Install path
if(NOT CMAKE_INSTALL_PREFIX) if(NOT CMAKE_INSTALL_PREFIX)
if(UNIX) if(UNIX)
set(INSTALL_PATH_LIB lib${LIB_SUFFIX}) set(INSTALL_PATH_LIB lib${LIB_SUFFIX})
@ -81,6 +88,7 @@ if(NOT CMAKE_INSTALL_PREFIX)
set(INSTALL_PATH_RUNTIME bin) set(INSTALL_PATH_RUNTIME bin)
elseif(WIN32) elseif(WIN32)
# Windows ? # Windows ?
# Install to the user's home directory under Windows?
set(INSTALL_PATH_LIB $ENV{HOME}/${CMAKE_PROJECT_NAME}/lib) set(INSTALL_PATH_LIB $ENV{HOME}/${CMAKE_PROJECT_NAME}/lib)
set(INSTALL_PATH_INCLUDE $ENV{HOME}/${CMAKE_PROJECT_NAME}/include) set(INSTALL_PATH_INCLUDE $ENV{HOME}/${CMAKE_PROJECT_NAME}/include)
else() else()
@ -95,11 +103,14 @@ endif()
string(TOLOWER ${CMAKE_SYSTEM_NAME} SYSTEM_NAME_LOWER) string(TOLOWER ${CMAKE_SYSTEM_NAME} SYSTEM_NAME_LOWER)
# , bin, lib # , bin, lib
# Windows 32/64 # Windows 32/64
# Set the output directory, including bin, lib and other files
# Windows no longer distinguishes 32/64
set(OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/release/${SYSTEM_NAME_LOWER}/${CMAKE_BUILD_TYPE}) set(OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/release/${SYSTEM_NAME_LOWER}/${CMAKE_BUILD_TYPE})
set(LIBRARY_OUTPUT_PATH ${OUTPUT_DIR}) set(LIBRARY_OUTPUT_PATH ${OUTPUT_DIR})
set(EXECUTABLE_OUTPUT_PATH ${OUTPUT_DIR}) set(EXECUTABLE_OUTPUT_PATH ${OUTPUT_DIR})
# git # git
# Add git version information
set(COMMIT_HASH "Git_Unkown_commit") set(COMMIT_HASH "Git_Unkown_commit")
set(COMMIT_TIME "Git_Unkown_time") set(COMMIT_TIME "Git_Unkown_time")
set(BRANCH_NAME "Git_Unkown_branch") set(BRANCH_NAME "Git_Unkown_branch")
@ -140,6 +151,7 @@ message(STATUS "Git version is ${BRANCH_NAME} ${COMMIT_HASH}/${COMMIT_TIME} ${BU
############################################################################## ##############################################################################
# 便 # 便
# Convenient to modify global variables
function(update_cached name value) function(update_cached name value)
set("${name}" "${value}" CACHE INTERNAL "*** Internal ***" FORCE) set("${name}" "${value}" CACHE INTERNAL "*** Internal ***" FORCE)
endfunction() endfunction()
@ -202,6 +214,7 @@ if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
endif() endif()
# mediakit runtime # mediakit runtime
# mediakit and various runtime dependencies
update_cached(MK_LINK_LIBRARIES "") update_cached(MK_LINK_LIBRARIES "")
update_cached(MK_COMPILE_DEFINITIONS ENABLE_VERSION) update_cached(MK_COMPILE_DEFINITIONS ENABLE_VERSION)
@ -226,9 +239,11 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux")
endif() endif()
# ffmpeg , # ffmpeg ,
# Multiple modules depend on ffmpeg related libraries, unified search
if(ENABLE_FFMPEG) if(ENABLE_FFMPEG)
find_package(PkgConfig QUIET) find_package(PkgConfig QUIET)
# ffmpeg/libutil # ffmpeg/libutil
# find ffmpeg/libutil installed
if(PKG_CONFIG_FOUND) if(PKG_CONFIG_FOUND)
pkg_check_modules(AVUTIL QUIET IMPORTED_TARGET libavutil) pkg_check_modules(AVUTIL QUIET IMPORTED_TARGET libavutil)
if(AVUTIL_FOUND) if(AVUTIL_FOUND)
@ -238,6 +253,7 @@ if(ENABLE_FFMPEG)
endif() endif()
# ffmpeg/libavcodec # ffmpeg/libavcodec
# find ffmpeg/libavcodec installed
if(PKG_CONFIG_FOUND) if(PKG_CONFIG_FOUND)
pkg_check_modules(AVCODEC QUIET IMPORTED_TARGET libavcodec) pkg_check_modules(AVCODEC QUIET IMPORTED_TARGET libavcodec)
if(AVCODEC_FOUND) if(AVCODEC_FOUND)
@ -247,6 +263,7 @@ if(ENABLE_FFMPEG)
endif() endif()
# ffmpeg/libswscale # ffmpeg/libswscale
# find ffmpeg/libswscale installed
if(PKG_CONFIG_FOUND) if(PKG_CONFIG_FOUND)
pkg_check_modules(SWSCALE QUIET IMPORTED_TARGET libswscale) pkg_check_modules(SWSCALE QUIET IMPORTED_TARGET libswscale)
if(SWSCALE_FOUND) if(SWSCALE_FOUND)
@ -256,6 +273,7 @@ if(ENABLE_FFMPEG)
endif() endif()
# ffmpeg/libswresample # ffmpeg/libswresample
# find ffmpeg/libswresample installed
if(PKG_CONFIG_FOUND) if(PKG_CONFIG_FOUND)
pkg_check_modules(SWRESAMPLE QUIET IMPORTED_TARGET libswresample) pkg_check_modules(SWRESAMPLE QUIET IMPORTED_TARGET libswresample)
if(SWRESAMPLE_FOUND) if(SWRESAMPLE_FOUND)
@ -265,6 +283,7 @@ if(ENABLE_FFMPEG)
endif() endif()
# ffmpeg/libutil # ffmpeg/libutil
# find ffmpeg/libutil installed
if(NOT AVUTIL_FOUND) if(NOT AVUTIL_FOUND)
find_package(AVUTIL QUIET) find_package(AVUTIL QUIET)
if(AVUTIL_FOUND) if(AVUTIL_FOUND)
@ -275,6 +294,7 @@ if(ENABLE_FFMPEG)
endif () endif ()
# ffmpeg/libavcodec # ffmpeg/libavcodec
# find ffmpeg/libavcodec installed
if(NOT AVCODEC_FOUND) if(NOT AVCODEC_FOUND)
find_package(AVCODEC QUIET) find_package(AVCODEC QUIET)
if(AVCODEC_FOUND) if(AVCODEC_FOUND)
@ -285,6 +305,7 @@ if(ENABLE_FFMPEG)
endif() endif()
# ffmpeg/libswscale # ffmpeg/libswscale
# find ffmpeg/libswscale installed
if(NOT SWSCALE_FOUND) if(NOT SWSCALE_FOUND)
find_package(SWSCALE QUIET) find_package(SWSCALE QUIET)
if(SWSCALE_FOUND) if(SWSCALE_FOUND)
@ -295,6 +316,7 @@ if(ENABLE_FFMPEG)
endif() endif()
# ffmpeg/libswresample # ffmpeg/libswresample
# find ffmpeg/libswresample installed
if(NOT SWRESAMPLE_FOUND) if(NOT SWRESAMPLE_FOUND)
find_package(SWRESAMPLE QUIET) find_package(SWRESAMPLE QUIET)
if(SWRESAMPLE_FOUND) if(SWRESAMPLE_FOUND)
@ -309,7 +331,7 @@ if(ENABLE_FFMPEG)
update_cached_list(MK_LINK_LIBRARIES ${CMAKE_DL_LIBS}) update_cached_list(MK_LINK_LIBRARIES ${CMAKE_DL_LIBS})
else() else()
set(ENABLE_FFMPEG OFF) set(ENABLE_FFMPEG OFF)
message(WARNING "ffmpeg 相关功能未找到") message(WARNING "ffmpeg related functions not found")
endif() endif()
endif() endif()
@ -317,7 +339,7 @@ if(ENABLE_MEM_DEBUG)
update_cached_list(MK_LINK_LIBRARIES update_cached_list(MK_LINK_LIBRARIES
"-Wl,-wrap,free;-Wl,-wrap,malloc;-Wl,-wrap,realloc;-Wl,-wrap,calloc") "-Wl,-wrap,free;-Wl,-wrap,malloc;-Wl,-wrap,realloc;-Wl,-wrap,calloc")
update_cached_list(MK_COMPILE_DEFINITIONS ENABLE_MEM_DEBUG) update_cached_list(MK_COMPILE_DEFINITIONS ENABLE_MEM_DEBUG)
message(STATUS "已启用内存调试功能") message(STATUS "Memory debugging enabled")
endif() endif()
if(ENABLE_ASAN) if(ENABLE_ASAN)
@ -327,10 +349,11 @@ if(ENABLE_ASAN)
# > In order to use AddressSanitizer you will need to # > In order to use AddressSanitizer you will need to
# > compile and link your program using clang with the -fsanitize=address switch. # > compile and link your program using clang with the -fsanitize=address switch.
update_cached_list(MK_LINK_LIBRARIES "-fsanitize=address") update_cached_list(MK_LINK_LIBRARIES "-fsanitize=address")
message(STATUS "已启用 Address Sanitize") message(STATUS "Address Sanitize enabled")
endif() endif()
# TODO: jemalloc ? # jemalloc
# Static compilation after downloading jemalloc
set(DEP_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdpart/external-${CMAKE_SYSTEM_NAME}) set(DEP_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdpart/external-${CMAKE_SYSTEM_NAME})
if(ENABLE_JEMALLOC_STATIC) if(ENABLE_JEMALLOC_STATIC)
if(NOT EXISTS ${DEP_ROOT_DIR}) if(NOT EXISTS ${DEP_ROOT_DIR})
@ -345,10 +368,12 @@ if(ENABLE_JEMALLOC_STATIC)
include_directories(SYSTEM ${DEP_ROOT_DIR}/${JEMALLOC_NAME}/include/jemalloc) include_directories(SYSTEM ${DEP_ROOT_DIR}/${JEMALLOC_NAME}/include/jemalloc)
link_directories(${DEP_ROOT_DIR}/${JEMALLOC_NAME}/lib) link_directories(${DEP_ROOT_DIR}/${JEMALLOC_NAME}/lib)
# #
# Used to affect subsequent lookup process
set(JEMALLOC_ROOT_DIR "${DEP_ROOT_DIR}/${JEMALLOC_NAME}") set(JEMALLOC_ROOT_DIR "${DEP_ROOT_DIR}/${JEMALLOC_NAME}")
endif() endif()
# jemalloc # jemalloc
# Link the jemalloc library by default to avoid memory fragmentation
find_package(JEMALLOC QUIET) find_package(JEMALLOC QUIET)
if(JEMALLOC_FOUND) if(JEMALLOC_FOUND)
message(STATUS "found library: ${JEMALLOC_LIBRARIES}") message(STATUS "found library: ${JEMALLOC_LIBRARIES}")
@ -363,6 +388,7 @@ if(JEMALLOC_FOUND)
endif() endif()
# openssl # openssl
# find openssl installed
find_package(OpenSSL QUIET) find_package(OpenSSL QUIET)
if(OPENSSL_FOUND AND ENABLE_OPENSSL) if(OPENSSL_FOUND AND ENABLE_OPENSSL)
message(STATUS "found library: ${OPENSSL_LIBRARIES}, ENABLE_OPENSSL defined") message(STATUS "found library: ${OPENSSL_LIBRARIES}, ENABLE_OPENSSL defined")
@ -379,6 +405,7 @@ else()
endif() endif()
# mysql # mysql
# find mysql installed
find_package(MYSQL QUIET) find_package(MYSQL QUIET)
if(MYSQL_FOUND AND ENABLE_MYSQL) if(MYSQL_FOUND AND ENABLE_MYSQL)
message(STATUS "found library: ${MYSQL_LIBRARIES}, ENABLE_MYSQL defined") message(STATUS "found library: ${MYSQL_LIBRARIES}, ENABLE_MYSQL defined")
@ -391,6 +418,7 @@ if(MYSQL_FOUND AND ENABLE_MYSQL)
endif() endif()
# x264 # x264
# find x264 installed
find_package(X264 QUIET) find_package(X264 QUIET)
if(X264_FOUND AND ENABLE_X264) if(X264_FOUND AND ENABLE_X264)
message(STATUS "found library: ${X264_LIBRARIES}, ENABLE_X264 defined") message(STATUS "found library: ${X264_LIBRARIES}, ENABLE_X264 defined")
@ -401,6 +429,7 @@ if(X264_FOUND AND ENABLE_X264)
endif() endif()
# faac # faac
# find faac installed
find_package(FAAC QUIET) find_package(FAAC QUIET)
if(FAAC_FOUND AND ENABLE_FAAC) if(FAAC_FOUND AND ENABLE_FAAC)
message(STATUS "found library:${FAAC_LIBRARIES}, ENABLE_FAAC defined") message(STATUS "found library:${FAAC_LIBRARIES}, ENABLE_FAAC defined")
@ -468,26 +497,31 @@ if(ENABLE_PLAYER AND ENABLE_FFMPEG)
endif() endif()
#MediaServer #MediaServer
#MediaServer main program
if(ENABLE_SERVER) if(ENABLE_SERVER)
add_subdirectory(server) add_subdirectory(server)
endif() endif()
# Android add_subdirectory # Android add_subdirectory
# Android will add_subdirectory and depend on this variable
if(ENABLE_SERVER_LIB AND NOT CMAKE_PARENT_LIST_FILE STREQUAL CMAKE_CURRENT_LIST_FILE) if(ENABLE_SERVER_LIB AND NOT CMAKE_PARENT_LIST_FILE STREQUAL CMAKE_CURRENT_LIST_FILE)
set(MK_LINK_LIBRARIES ${MK_LINK_LIBRARIES} PARENT_SCOPE) set(MK_LINK_LIBRARIES ${MK_LINK_LIBRARIES} PARENT_SCOPE)
endif() endif()
# IOS # IOS
# IOS does not compile executable programs
if(IOS) if(IOS)
return() return()
endif() endif()
#cppdemo #cppdemo
#cpp test demo program
if (ENABLE_TESTS) if (ENABLE_TESTS)
add_subdirectory(tests) add_subdirectory(tests)
endif () endif ()
# www # www
# Copy www folder, configuration file, default certificate
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/www" DESTINATION ${EXECUTABLE_OUTPUT_PATH}) file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/www" DESTINATION ${EXECUTABLE_OUTPUT_PATH})
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/conf/config.ini" DESTINATION ${EXECUTABLE_OUTPUT_PATH}) file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/conf/config.ini" DESTINATION ${EXECUTABLE_OUTPUT_PATH})
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/default.pem" DESTINATION ${EXECUTABLE_OUTPUT_PATH}) file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/default.pem" DESTINATION ${EXECUTABLE_OUTPUT_PATH})

View File

@ -56,10 +56,13 @@ void HttpClient::sendRequest(const string &url) {
splitUrl(host, host, port); splitUrl(host, host, port);
_header.emplace("Host", host_header); _header.emplace("Host", host_header);
_header.emplace("User-Agent", kServerName); _header.emplace("User-Agent", kServerName);
_header.emplace("Connection", "keep-alive");
_header.emplace("Accept", "*/*"); _header.emplace("Accept", "*/*");
_header.emplace("Accept-Language", "zh-CN,zh;q=0.8"); _header.emplace("Accept-Language", "zh-CN,zh;q=0.8");
if(_http_persistent) {
_header.emplace("Connection", "keep-alive");
} else {
_header.emplace("Connection", "close");
}
if (_body && _body->remainSize()) { if (_body && _body->remainSize()) {
_header.emplace("Content-Length", to_string(_body->remainSize())); _header.emplace("Content-Length", to_string(_body->remainSize()));
_header.emplace("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); _header.emplace("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
@ -81,7 +84,7 @@ void HttpClient::sendRequest(const string &url) {
if (isUsedProxy()) { if (isUsedProxy()) {
startConnect(_proxy_host, _proxy_port, _wait_header_ms / 1000.0f); startConnect(_proxy_host, _proxy_port, _wait_header_ms / 1000.0f);
} else { } else {
if (!alive() || host_changed) { if (!alive() || host_changed || !_http_persistent) {
startConnect(host, port, _wait_header_ms / 1000.0f); startConnect(host, port, _wait_header_ms / 1000.0f);
} else { } else {
SockException ex; SockException ex;
@ -188,6 +191,17 @@ void HttpClient::onRecv(const Buffer::Ptr &pBuf) {
} }
void HttpClient::onError(const SockException &ex) { void HttpClient::onError(const SockException &ex) {
if(ex.getErrCode() == Err_reset && _http_persistent){
// 连接被重置,可能是服务器主动断开了连接, 或者服务器内核参数或防火墙的持久连接空闲时间超时或不一致.
// 如果是持久化连接,那么我们可以通过重连来解决这个问题
// The connection was reset, possibly because the server actively disconnected the connection,
// or the persistent connection idle time of the server kernel parameters or firewall timed out or inconsistent.
// If it is a persistent connection, then we can solve this problem by reconnecting
WarnL << "http persistent connect reset, try reconnect";
_http_persistent = false;
sendRequest(_url);
return;
}
onResponseCompleted_l(ex); onResponseCompleted_l(ex);
} }

View File

@ -201,6 +201,7 @@ private:
//for http response //for http response
bool _complete = false; bool _complete = false;
bool _header_recved = false; bool _header_recved = false;
bool _http_persistent = true;
size_t _recved_body_size; size_t _recved_body_size;
ssize_t _total_body_size; ssize_t _total_body_size;
Parser _parser; Parser _parser;