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 1 commit
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
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_struct_has_member("struct mmsghdr" msg_hdr sys/socket.h HAVE_MMSG_HDR)
check_symbol_exists(sendmmsg sys/socket.h HAVE_SENDMMSG_API)
check_symbol_exists(recvmmsg sys/socket.h HAVE_RECVMMSG_API)

# 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(NOT DEFINED SOCKET_DEFAULT_BUFFER_SIZE)
add_definitions(-DSOCKET_DEFAULT_BUFFER_SIZE=262144)
else()
add_definitions(-DSOCKET_DEFAULT_BUFFER_SIZE=${SOCKET_DEFAULT_BUFFER_SIZE})
endif()
if(HAVE_MMSG_HDR)
add_definitions(-DHAVE_MMSG_HDR)
endif()
Expand Down
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 @@ static SockException toSockException(int error) {
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
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 @@ -103,7 +103,7 @@
if (fd == -1) {
return false;
}
close(fd);

Check warning on line 106 in src/Network/sockutil.cpp

View workflow job for this annotation

GitHub Actions / build

'argument': conversion from 'SOCKET' to 'int', possible loss of data
return true;
}

Expand Down Expand Up @@ -279,6 +279,10 @@
}

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::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 Expand Up @@ -656,7 +663,7 @@
if (ip != "127.0.0.1" && ip != "0.0.0.0") {
/*获取一个有效IP*/
address = ip;
uint32_t addressInNetworkOrder = htonl(inet_addr(ip.data()));

Check warning on line 666 in src/Network/sockutil.cpp

View workflow job for this annotation

GitHub Actions / build

'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
if (/*(addressInNetworkOrder >= 0x0A000000 && addressInNetworkOrder < 0x0E000000) ||*/
(addressInNetworkOrder >= 0xAC100000 && addressInNetworkOrder < 0xAC200000) ||
(addressInNetworkOrder >= 0xC0A80000 && addressInNetworkOrder < 0xC0A90000)) {
Expand Down Expand Up @@ -934,7 +941,7 @@
//找到该网卡
IP_ADDR_STRING *ipAddr = &(adapter->IpAddressList);
in_addr broadcast;
broadcast.S_un.S_addr = (inet_addr(ipAddr->IpAddress.String) & inet_addr(ipAddr->IpMask.String)) | (~inet_addr(ipAddr->IpMask.String));

Check warning on line 944 in src/Network/sockutil.cpp

View workflow job for this annotation

GitHub Actions / build

'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
ret = SockUtil::inet_ntoa(broadcast);
return true;
}
Expand Down Expand Up @@ -965,7 +972,7 @@

bool SockUtil::in_same_lan(const char *myIp, const char *dstIp) {
string mask = get_ifr_mask(get_ifr_name(myIp).data());
return ip_addr_netcmp(inet_addr(myIp), inet_addr(dstIp), inet_addr(mask.data()));

Check warning on line 975 in src/Network/sockutil.cpp

View workflow job for this annotation

GitHub Actions / build

'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
}

static void clearMulticastAllSocketOption(int socket) {
Expand Down Expand Up @@ -995,7 +1002,7 @@
int ret = -1;
#if defined(IP_MULTICAST_IF)
struct in_addr addr;
addr.s_addr = inet_addr(local_ip);

Check warning on line 1005 in src/Network/sockutil.cpp

View workflow job for this annotation

GitHub Actions / build

'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (char *) &addr, sizeof(addr));
if (ret == -1) {
TraceL << "setsockopt IP_MULTICAST_IF failed";
Expand All @@ -1022,8 +1029,8 @@
int ret = -1;
#if defined(IP_ADD_MEMBERSHIP)
struct ip_mreq imr;
imr.imr_multiaddr.s_addr = inet_addr(addr);

Check warning on line 1032 in src/Network/sockutil.cpp

View workflow job for this annotation

GitHub Actions / build

'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
imr.imr_interface.s_addr = inet_addr(local_ip);

Check warning on line 1033 in src/Network/sockutil.cpp

View workflow job for this annotation

GitHub Actions / build

'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &imr, sizeof(struct ip_mreq));
if (ret == -1) {
TraceL << "setsockopt IP_ADD_MEMBERSHIP failed: " << get_uv_errmsg(true);
Expand All @@ -1037,8 +1044,8 @@
int ret = -1;
#if defined(IP_DROP_MEMBERSHIP)
struct ip_mreq imr;
imr.imr_multiaddr.s_addr = inet_addr(addr);

Check warning on line 1047 in src/Network/sockutil.cpp

View workflow job for this annotation

GitHub Actions / build

'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
imr.imr_interface.s_addr = inet_addr(local_ip);

Check warning on line 1048 in src/Network/sockutil.cpp

View workflow job for this annotation

GitHub Actions / build

'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
ret = setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *) &imr, sizeof(struct ip_mreq));
if (ret == -1) {
TraceL << "setsockopt IP_DROP_MEMBERSHIP failed: " << get_uv_errmsg(true);
Expand Down
16 changes: 15 additions & 1 deletion src/Network/sockutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,22 @@ namespace toolkit {
int ioctl(int fd, long cmd, u_long *ptr);
int close(int fd);
#endif // defined(_WIN32)

#define SOCKET_DEFAULT_BUF_SIZE (256 * 1024)
#if defined(SOCKET_DEFAULT_BUFFER_SIZE)
#if SOCKET_DEFAULT_BUFFER_SIZE == 0
#if 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 0
xia-chu marked this conversation as resolved.
Show resolved Hide resolved
#endif
#else
#undef SOCKET_DEFAULT_BUF_SIZE
#define SOCKET_DEFAULT_BUF_SIZE SOCKET_DEFAULT_BUFFER_SIZE
#endif
#endif

#define TCP_KEEPALIVE_INTERVAL 30
#define TCP_KEEPALIVE_PROBE_TIMES 9
#define TCP_KEEPALIVE_TIME 120
Expand Down
Loading