diff --git a/tests/epoll/epoll.xcodeproj/project.pbxproj b/tests/epoll/epoll.xcodeproj/project.pbxproj deleted file mode 100644 index 778b4a8..0000000 --- a/tests/epoll/epoll.xcodeproj/project.pbxproj +++ /dev/null @@ -1,284 +0,0 @@ -// !$*UTF8*$! -{ - archiveVersion = 1; - classes = { - }; - objectVersion = 46; - objects = { - -/* Begin PBXBuildFile section */ - FE1E12621FA3138F00DADFF8 /* time_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE1E12611FA3138F00DADFF8 /* time_test.cpp */; }; - FE8CF8F41FA21E2300AB854F /* queue_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE8CF8F31FA21E2300AB854F /* queue_test.cpp */; }; - FE8CF8F71FA2A8FC00AB854F /* time.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE8CF8F51FA2A8FC00AB854F /* time.cpp */; }; - FEBC99511F9C264200D0C58E /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FEBC99501F9C264200D0C58E /* main.cpp */; }; - FED4E0FD1F9EAACB002A2F9A /* queue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FED4E0FB1F9EAACB002A2F9A /* queue.cpp */; }; - FEF4DCE41FA3E1D3003A0B69 /* wing.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FEF4DCE31FA3E1D3003A0B69 /* wing.cpp */; }; -/* End PBXBuildFile section */ - -/* Begin PBXCopyFilesBuildPhase section */ - FEBC994B1F9C264200D0C58E /* CopyFiles */ = { - isa = PBXCopyFilesBuildPhase; - buildActionMask = 2147483647; - dstPath = /usr/share/man/man1/; - dstSubfolderSpec = 0; - files = ( - ); - runOnlyForDeploymentPostprocessing = 1; - }; -/* End PBXCopyFilesBuildPhase section */ - -/* Begin PBXFileReference section */ - FE1E12611FA3138F00DADFF8 /* time_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = time_test.cpp; sourceTree = ""; }; - FE8CF8F31FA21E2300AB854F /* queue_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = queue_test.cpp; sourceTree = ""; }; - FE8CF8F51FA2A8FC00AB854F /* time.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = time.cpp; sourceTree = ""; }; - FE8CF8F61FA2A8FC00AB854F /* time.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = time.hpp; sourceTree = ""; }; - FEBC994D1F9C264200D0C58E /* epoll */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = epoll; sourceTree = BUILT_PRODUCTS_DIR; }; - FEBC99501F9C264200D0C58E /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; - FED4E0FB1F9EAACB002A2F9A /* queue.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = queue.cpp; sourceTree = ""; }; - FED4E0FC1F9EAACB002A2F9A /* queue.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = queue.hpp; sourceTree = ""; }; - FED4E0FE1F9ED121002A2F9A /* wing.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = wing.h; sourceTree = ""; }; - FEF4DCE31FA3E1D3003A0B69 /* wing.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wing.cpp; sourceTree = ""; }; - FEF4DCE51FA3E385003A0B69 /* wing.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = wing.hpp; sourceTree = ""; }; -/* End PBXFileReference section */ - -/* Begin PBXFrameworksBuildPhase section */ - FEBC994A1F9C264200D0C58E /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXFrameworksBuildPhase section */ - -/* Begin PBXGroup section */ - FEBC99441F9C264200D0C58E = { - isa = PBXGroup; - children = ( - FEBC994F1F9C264200D0C58E /* epoll */, - FEBC994E1F9C264200D0C58E /* Products */, - ); - sourceTree = ""; - }; - FEBC994E1F9C264200D0C58E /* Products */ = { - isa = PBXGroup; - children = ( - FEBC994D1F9C264200D0C58E /* epoll */, - ); - name = Products; - sourceTree = ""; - }; - FEBC994F1F9C264200D0C58E /* epoll */ = { - isa = PBXGroup; - children = ( - FEBC99501F9C264200D0C58E /* main.cpp */, - FED4E0FB1F9EAACB002A2F9A /* queue.cpp */, - FED4E0FC1F9EAACB002A2F9A /* queue.hpp */, - FED4E0FE1F9ED121002A2F9A /* wing.h */, - FE8CF8F31FA21E2300AB854F /* queue_test.cpp */, - FE8CF8F51FA2A8FC00AB854F /* time.cpp */, - FE8CF8F61FA2A8FC00AB854F /* time.hpp */, - FE1E12611FA3138F00DADFF8 /* time_test.cpp */, - FEF4DCE31FA3E1D3003A0B69 /* wing.cpp */, - FEF4DCE51FA3E385003A0B69 /* wing.hpp */, - ); - path = epoll; - sourceTree = ""; - }; -/* End PBXGroup section */ - -/* Begin PBXNativeTarget section */ - FEBC994C1F9C264200D0C58E /* epoll */ = { - isa = PBXNativeTarget; - buildConfigurationList = FEBC99541F9C264200D0C58E /* Build configuration list for PBXNativeTarget "epoll" */; - buildPhases = ( - FEBC99491F9C264200D0C58E /* Sources */, - FEBC994A1F9C264200D0C58E /* Frameworks */, - FEBC994B1F9C264200D0C58E /* CopyFiles */, - ); - buildRules = ( - ); - dependencies = ( - ); - name = epoll; - productName = epoll; - productReference = FEBC994D1F9C264200D0C58E /* epoll */; - productType = "com.apple.product-type.tool"; - }; -/* End PBXNativeTarget section */ - -/* Begin PBXProject section */ - FEBC99451F9C264200D0C58E /* Project object */ = { - isa = PBXProject; - attributes = { - LastUpgradeCheck = 0810; - ORGANIZATIONNAME = yuyi; - TargetAttributes = { - FEBC994C1F9C264200D0C58E = { - CreatedOnToolsVersion = 8.1; - ProvisioningStyle = Automatic; - }; - }; - }; - buildConfigurationList = FEBC99481F9C264200D0C58E /* Build configuration list for PBXProject "epoll" */; - compatibilityVersion = "Xcode 3.2"; - developmentRegion = English; - hasScannedForEncodings = 0; - knownRegions = ( - en, - ); - mainGroup = FEBC99441F9C264200D0C58E; - productRefGroup = FEBC994E1F9C264200D0C58E /* Products */; - projectDirPath = ""; - projectRoot = ""; - targets = ( - FEBC994C1F9C264200D0C58E /* epoll */, - ); - }; -/* End PBXProject section */ - -/* Begin PBXSourcesBuildPhase section */ - FEBC99491F9C264200D0C58E /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - FE8CF8F71FA2A8FC00AB854F /* time.cpp in Sources */, - FEBC99511F9C264200D0C58E /* main.cpp in Sources */, - FEF4DCE41FA3E1D3003A0B69 /* wing.cpp in Sources */, - FE8CF8F41FA21E2300AB854F /* queue_test.cpp in Sources */, - FED4E0FD1F9EAACB002A2F9A /* queue.cpp in Sources */, - FE1E12621FA3138F00DADFF8 /* time_test.cpp in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXSourcesBuildPhase section */ - -/* Begin XCBuildConfiguration section */ - FEBC99521F9C264200D0C58E /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - CLANG_ANALYZER_NONNULL = YES; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_DOCUMENTATION_COMMENTS = YES; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INFINITE_RECURSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_SUSPICIOUS_MOVES = YES; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - CODE_SIGN_IDENTITY = "-"; - COPY_PHASE_STRIP = NO; - DEBUG_INFORMATION_FORMAT = dwarf; - ENABLE_STRICT_OBJC_MSGSEND = YES; - ENABLE_TESTABILITY = YES; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_DYNAMIC_NO_PIC = NO; - GCC_NO_COMMON_BLOCKS = YES; - GCC_OPTIMIZATION_LEVEL = 0; - GCC_PREPROCESSOR_DEFINITIONS = ( - "DEBUG=1", - "$(inherited)", - ); - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; - MTL_ENABLE_DEBUG_INFO = YES; - ONLY_ACTIVE_ARCH = YES; - SDKROOT = macosx; - }; - name = Debug; - }; - FEBC99531F9C264200D0C58E /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - CLANG_ANALYZER_NONNULL = YES; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_DOCUMENTATION_COMMENTS = YES; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INFINITE_RECURSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_SUSPICIOUS_MOVES = YES; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - CODE_SIGN_IDENTITY = "-"; - COPY_PHASE_STRIP = NO; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - ENABLE_NS_ASSERTIONS = NO; - ENABLE_STRICT_OBJC_MSGSEND = YES; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_NO_COMMON_BLOCKS = YES; - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; - MTL_ENABLE_DEBUG_INFO = NO; - SDKROOT = macosx; - }; - name = Release; - }; - FEBC99551F9C264200D0C58E /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - MACOSX_DEPLOYMENT_TARGET = 10.11; - PRODUCT_NAME = "$(TARGET_NAME)"; - }; - name = Debug; - }; - FEBC99561F9C264200D0C58E /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - MACOSX_DEPLOYMENT_TARGET = 10.11; - PRODUCT_NAME = "$(TARGET_NAME)"; - }; - name = Release; - }; -/* End XCBuildConfiguration section */ - -/* Begin XCConfigurationList section */ - FEBC99481F9C264200D0C58E /* Build configuration list for PBXProject "epoll" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - FEBC99521F9C264200D0C58E /* Debug */, - FEBC99531F9C264200D0C58E /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - FEBC99541F9C264200D0C58E /* Build configuration list for PBXNativeTarget "epoll" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - FEBC99551F9C264200D0C58E /* Debug */, - FEBC99561F9C264200D0C58E /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; -/* End XCConfigurationList section */ - }; - rootObject = FEBC99451F9C264200D0C58E /* Project object */; -} diff --git a/tests/epoll/epoll.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/tests/epoll/epoll.xcodeproj/project.xcworkspace/contents.xcworkspacedata deleted file mode 100644 index de21a9b..0000000 --- a/tests/epoll/epoll.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ /dev/null @@ -1,7 +0,0 @@ - - - - - diff --git a/tests/epoll/epoll.xcodeproj/project.xcworkspace/xcuserdata/yuyi.xcuserdatad/UserInterfaceState.xcuserstate b/tests/epoll/epoll.xcodeproj/project.xcworkspace/xcuserdata/yuyi.xcuserdatad/UserInterfaceState.xcuserstate deleted file mode 100644 index 988c361..0000000 Binary files a/tests/epoll/epoll.xcodeproj/project.xcworkspace/xcuserdata/yuyi.xcuserdatad/UserInterfaceState.xcuserstate and /dev/null differ diff --git a/tests/epoll/epoll.xcodeproj/xcuserdata/yuyi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/tests/epoll/epoll.xcodeproj/xcuserdata/yuyi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist deleted file mode 100644 index b38fc34..0000000 --- a/tests/epoll/epoll.xcodeproj/xcuserdata/yuyi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - diff --git a/tests/epoll/epoll.xcodeproj/xcuserdata/yuyi.xcuserdatad/xcschemes/epoll.xcscheme b/tests/epoll/epoll.xcodeproj/xcuserdata/yuyi.xcuserdatad/xcschemes/epoll.xcscheme deleted file mode 100644 index a52ffd6..0000000 --- a/tests/epoll/epoll.xcodeproj/xcuserdata/yuyi.xcuserdatad/xcschemes/epoll.xcscheme +++ /dev/null @@ -1,91 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/epoll/epoll.xcodeproj/xcuserdata/yuyi.xcuserdatad/xcschemes/xcschememanagement.plist b/tests/epoll/epoll.xcodeproj/xcuserdata/yuyi.xcuserdatad/xcschemes/xcschememanagement.plist deleted file mode 100644 index f4af070..0000000 --- a/tests/epoll/epoll.xcodeproj/xcuserdata/yuyi.xcuserdatad/xcschemes/xcschememanagement.plist +++ /dev/null @@ -1,22 +0,0 @@ - - - - - SchemeUserState - - epoll.xcscheme - - orderHint - 0 - - - SuppressBuildableAutocreation - - FEBC994C1F9C264200D0C58E - - primary - - - - - diff --git a/tests/epoll/epoll/main.cpp b/tests/epoll/epoll/main.cpp deleted file mode 100644 index 5000f38..0000000 --- a/tests/epoll/epoll/main.cpp +++ /dev/null @@ -1,480 +0,0 @@ -// -// main.cpp -// epoll -// -// Created by yuyi on 17/10/22. -// Copyright © 2017年 yuyi. All rights reserved. -// -// kqueue demo -// @link http://blog.csdn.net/bytxl/article/details/17526351 -// @link https://www.ibm.com/developerworks/cn/aix/library/1105_huangrg_kqueue/ -// @link https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2 -// @link http://www.cnblogs.com/moonz-wu/p/4740908.html -// @link http://www.cnblogs.com/promise6522/archive/2012/03/03/2377935.html -// - -#include -#include "queue.hpp" -#include "assert.h" -#include "wing.h" -#include "wing.hpp" -#include "time.hpp" - -#define READ_EVENT 1 -#define WRITE_EVENT 2 - -//const int kReadEvent = 1; -//const int kWriteEvent = 2; -int listenfd; - - -/** - * 将socket设置为非阻塞 - * @params fd socket资源句柄 - */ -void set_non_block(int fd) -{ - int flags = fcntl(fd, F_GETFL, 0); - exit_if(flags < 0, "fcntl failed"); - - int r = fcntl(fd, F_SETFL, flags | O_NONBLOCK); - exit_if(r < 0, "fcntl failed"); - debug("%d set no block", fd); -} - -static void del_event(int qfd, int fd, int mask) -{ - struct kevent ke; - - if (mask & READ_EVENT) { - debug("%d del read event", fd); - EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); - kevent(qfd, &ke, 1, NULL, 0, NULL); - } - - if (mask & WRITE_EVENT) { - debug("%d del write event", fd); - EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); - kevent(qfd, &ke, 1, NULL, 0, NULL); - } -} - -static int add_event(int qfd, client_node* client, int mask) -{ - struct kevent ke; - - if (mask & READ_EVENT) { - debug("%d add read event", client->fd); - EV_SET(&ke, client->fd, EVFILT_READ, EV_ADD, 0, 0, (void*)client); - if (kevent(qfd, &ke, 1, NULL, 0, NULL) == -1) return -1; - } - - if (mask & WRITE_EVENT) { - debug("%d add write event", client->fd); - EV_SET(&ke, client->fd, EVFILT_WRITE, EV_ADD, 0, 0, (void*)client); - if (kevent(qfd, &ke, 1, NULL, 0, NULL) == -1) return -1; - } - return 0; -} - -/** - * accept接受连接 - * @param efd kqueue - * @param fd listen的socket句柄 - * @param nums 新连接的个数 - */ -void handle_accept(int efd, int fd, long nums) -{ - struct sockaddr_in raddr; - socklen_t rsz = sizeof(raddr); - int cfd, i, r; - sockaddr_in peer; - socklen_t alen = sizeof(peer); - - for (i = 0; i < nums; i++) { - memset(&raddr, 0, rsz); - cfd = accept(fd,(struct sockaddr *)&raddr, &rsz); - - if (cfd < 0) { - debug("accept failed, do continue"); - continue; - } - //exit_if(cfd < 0, "accept failed"); - memset(&peer, 0, alen); - r = getpeername(cfd, (sockaddr*)&peer, &alen); - if (r < 0) { - close(cfd); - debug("%d getpeername failed, do continue", cfd); - continue; - } - debug("%d accept a new connection %d from %s:%d", fd, cfd, inet_ntoa(raddr.sin_addr), raddr.sin_port); - - //屏蔽sigpipe - const int value = 1; - r = setsockopt(cfd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(int)); - if (r != 0) { - debug("%d set SO_NOSIGPIPE failed, do continue", cfd); - close(cfd); - continue; - } - - debug("%d set SO_NOSIGPIPE", cfd); - set_keepalive(cfd); - debug("%d set_keepalive", cfd); - - client_node *client = init_client(cfd, inet_ntoa(raddr.sin_addr), raddr.sin_port); - - set_non_block(cfd); - add_event(efd, client, WRITE_EVENT|READ_EVENT); - } -} - -/** - * 读事件回调 - * @param efd kqueue - * @param client client_node - * @param bytes 缓冲区可读字节数 - */ -void handle_read(int efd, client_node* client, long bytes) -{ - //如果可读字节数等于0,说明对端关闭了,这个时候可以释放连接了 - if (bytes <= 0) { - debug("1=>receive error fd %d closed\n", client->fd); - del_event(efd, client->fd, READ_EVENT|WRITE_EVENT); - free_client(client); - return; - } - - //接下来读取bytes字节的数据 - char buf[bytes + 1]; - memset(buf, 0, bytes + 1); - ssize_t n = 0, m; - - while (1) { - m = recv(client->fd, buf, bytes, 0); - //读完或者遇到错误,直接中断 - if (m <= 0 || n >= bytes) { - break; - } - n += m; - debug("read %ld => %ld \n", bytes, n); - } - - //读取时添加写事件,可写时触发 - add_event(efd, client, WRITE_EVENT); - debug("read %zd bytes:\n%s errno: %d\n\n", n, buf, errno); - - //返回0或者EPIPE和ECONNRESET均代表客户端已经断开连接了 - //EPIPE 32 Broken pipe - //ECONNRESET 54 Connection reset by peer - if (m == 0 || errno == EPIPE || errno == ECONNRESET) { - debug("2=>receive error %d closed\n", client->fd); - del_event(efd, client->fd, READ_EVENT|WRITE_EVENT); - free_client(client); - return; - } - - /*char *msg = (char*)"HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhello world"; - size_t send_size = strlen(msg); - ssize_t sended_size = 0; - - //这里的for循环仅仅是为了测试send - //int i = 0; - //for (i = 0; i < 1000000; i++) - //发送回应数据 - sended_size = send_data(efd, client, msg, send_size, 0); //写出读取的数据 - */ - //只有当receive buffer为空时,blocking模式才会等待,而nonblock模式下会立即返回-1(errno = EAGAIN或EWOULDBLOCK - if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { - return; - } - - //ENOTSOCK 38 Socket operation on non-socket - //这个错误一般由于socket被close释放掉了,然后还进行read操作造成的 - if (errno == ENOTSOCK) { - return; - } - - exit_if(n < 0, "read error"); //实际应用中,n<0应当检查各类错误,如EINTR -} - -/** - * 可写事件回调 - * @param efd kqueue - * @param client client_node - * @param left_size 剩余的发送缓冲区大小 - */ -void handle_write(int efd, client_node* client, size_t left_size) -{ - //缓冲区满,不可写 - if (left_size <= 0) { - debug("% is not writeable", client->fd); - return; - } - - //发送队列pop数据 - node* n = pop_queue(client->queue); - if (n == NULL) { - debug("queue is empty"); - } - - //如果存在未发送完成的数据,尝试重发 - while (n != NULL) { - debug("队列发送"); - send_node(efd, client, n); - n = pop_queue(client->queue); - } - - del_event(efd, client->fd, WRITE_EVENT); -} - -static size_t _send(int fd, const char* buf, size_t buf_size) -{ - ssize_t sended = 0; - ssize_t size = 0; - char *sbuf = (char*)buf; - - //nonblock write则是返回能够放下的字节数,之后调用则返回-1(errno = EAGAIN或EWOULDBLOCK) - while (sended < buf_size) { - //size = send(client->fd, sbuf, buf_size, MSG_NOSIGNAL); - //mac没有这个参数MSG_NOSIGNAL,这个MSG_NOSIGNAL参数是为了解决内核抛出sigpipe信号异常的 - size = write(fd, sbuf, buf_size); - debug("%d send msg %zu bytes:\n%s\n\n", errno, buf_size, sbuf); - - //如果出现EINTR即errno为4,错误描述Interrupted system call,操作也应该继续。 - if (size < 0 && errno == EINTR) { - debug("系统中断\n"); - continue; - } - - //缓冲区满 - //EAGAIN 35 Resource temporarily unavailable - //EAGAIN 的意思也很明显,就是要你再次尝试。 - if (size < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { - debug("缓冲区满\n"); - continue; - } - - //EPIPE 32 对端关闭 - //ECONNRESET 54 Connection reset by peer - if (size < 0) { - break; - } - sended += size; - sbuf = (char*)(buf + sended); - - } - - return sended; -} - -static void _push_to_queue(queue* q, const char* buf, size_t buf_size, int send_times) -{ - //创建一个数据节点 - send_queue_node* _node = create_send_queue_node((char*)buf, buf_size, send_times + 1); - - node* n = create_node(q, (void*)_node); - if (n != NULL) { - push_queue(q, n); - } else { - free_send_queue_node((void*)_node); - } - -} - -/** - * 如果失败会自动进入重试队列,待客户端可写,重试发送,最多重试100次 - */ -ssize_t send_data(int efd, client_node* client, const char* buf, size_t buf_size, int send_times) -{ - if (send_times > 5) { - debug("resend max times error close %d\n", client->fd); - del_event(efd, client->fd, READ_EVENT|WRITE_EVENT); - free_client(client); - return 0; - } - - size_t size = _send(client->fd, buf, buf_size); - - if (size == -1 && errno != EPIPE && errno != ECONNRESET) { - //发送失败,直接进入重试队列 - _push_to_queue(client->queue, buf, buf_size, send_times); - } - - //EPIPE 32 d对端关闭 - if (errno == EPIPE || errno == ECONNRESET) { - del_event(efd, client->fd, READ_EVENT|WRITE_EVENT); - free_client(client); - } - return size; -} - -ssize_t send_node(int efd, client_node* client, node* n) -{ - send_queue_node* sn = (send_queue_node*)n->data; - - if (sn->send_times > 5) { - free_node(n, free_send_queue_node); - debug("resend max times error close %d\n", client->fd); - del_event(efd, client->fd, READ_EVENT|WRITE_EVENT); - free_client(client); - return 0; - } - - sn->send_times = sn->send_times+1; - size_t size = _send(client->fd, sn->msg, sn->msg_size); - - if (size == -1 && errno != EPIPE && errno != ECONNRESET) { - //发送失败,直接进入重试队列 - push_queue(client->queue, n); - } else { - free_node(n, free_send_queue_node); - } - - //update_events(efd, client, kWriteEvent, false); - - //EPIPE 32 d对端关闭 - if (errno == EPIPE || errno == ECONNRESET) { - del_event(efd, client->fd, READ_EVENT|WRITE_EVENT); - free_client(client); - errno = 0; - } - return size; -} - -/** - * 事件轮询 - * @param efd kqueue - * @param server server节点 - * @param waitms 超时时间 - */ -void loop(int efd, client_node* server, int waitms) -{ - struct timespec timeout; - const int max_enents = 20; - - timeout.tv_sec = waitms / 1000; - timeout.tv_nsec = (waitms % 1000) * 1000 * 1000; - - struct kevent active_events[max_enents]; - //获取ready的fd,类似epoll_wait - int n = kevent(efd, NULL, 0, active_events, max_enents, &timeout); - //debug("epoll_wait return %d", n); - - for (int i = 0; i < n; i ++) { - //用户数据 - client_node* client = (client_node*)active_events[i].udata; - //这里的data在不同的事件里面有不同的意义,accept里面代表等待连接的客户端 - //read事件里面代表可读的数据字节数 - long data = active_events[i].data; - uint16_t flags = active_events[i].flags; - int events = active_events[i].filter; - - debug("events = %d flags = %d data = %ld ", events, flags, data); - - //处理出错的socket - if (flags & EV_ERROR) { - debug("ev error close %d\n", client->fd); - del_event(efd, client->fd, READ_EVENT|WRITE_EVENT); - free_client(client); - continue; - } - - if (events == EVFILT_READ) { - //如果触发的socket等于监听的socket,说明有新的连接 - if (client->fd == server->fd) { - debug("new connect"); - handle_accept(efd, server->fd, data); - } else { - debug("%d read event", client->fd); - handle_read(efd, client, data); - } - } - - else if (events == EVFILT_WRITE) { - debug("%d is writeable", client->fd); - handle_write(efd, client, data); - } - - else { - debug("unknown event %d\r\n", events); - goto error; - break; - } - } - - return; - -error: - debug("server error close %d\n", server->fd); - del_event(efd, server->fd, READ_EVENT); - free_client(server); - exit_if(1, "unknown event"); -} - - -void sig_handle(int sig) -{ - debug("%d sig received ", sig); - close(listenfd); - exit(0); -} - -int main() -{ - debug("server start ......"); - //queue_test(); - //return 0; - - //time_test(); - //return 0; - - short port = 9998; - int epollfd = kqueue(); - exit_if(epollfd < 0, "epoll_create failed"); - - listenfd = socket(AF_INET, SOCK_STREAM, 0); - exit_if(listenfd < 0, "socket failed"); - - struct sockaddr_in addr; - memset(&addr, 0, sizeof addr); - - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - addr.sin_addr.s_addr = INADDR_ANY; - - int r = ::bind(listenfd,(struct sockaddr *)&addr, sizeof(struct sockaddr)); - exit_if(r, "%d bind to 0.0.0.0:%d failed", listenfd, port); - debug("%d bind to 0.0.0.0:%d", listenfd, port); - - r = listen(listenfd, 20); - exit_if(r, "listen failed %d %s", errno, strerror(errno)); - debug("%d listening at %d", listenfd, port); - - client_node* server = init_client(listenfd, (char*)"0.0.0.0", port); - - //屏蔽sigpipe - const int value = 1; - setsockopt(listenfd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(int)); - debug("%d set SO_NOSIGPIPE", listenfd); - setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(int)); - debug("%d set SO_REUSEADDR", listenfd); - set_keepalive(listenfd); - debug("%d set_keepalive", listenfd); - - set_non_block(listenfd); - //update_events(epollfd, server, kReadEvent, false); - add_event(epollfd, server, READ_EVENT); - signal(SIGPIPE, SIG_IGN); - signal(SIGINT, sig_handle); - signal(SIGQUIT, sig_handle); - signal(SIGHUP, sig_handle); - signal(SIGTERM, sig_handle); - - for (;;) { //实际应用应当注册信号处理函数,退出时清理资源 - loop(epollfd, server, 1000); - } - - return 0; -} - diff --git a/tests/epoll/epoll/queue.cpp b/tests/epoll/epoll/queue.cpp deleted file mode 100644 index c0b42bb..0000000 --- a/tests/epoll/epoll/queue.cpp +++ /dev/null @@ -1,322 +0,0 @@ -// -// queue.cpp -// epoll -// -// Created by yuyi on 17/10/24. -// Copyright © 2017年 yuyi. All rights reserved. -// - -#include "queue.hpp" -#include "stdlib.h" -#include "string.h" - -/** - * 创建内存池,返回开始指针,这里2次malloc - */ -mem_block *_create_mem_block(unsigned long size) -{ - if (size <= 0) { - return NULL; - } - size = (int(size/sizeof(node))+1) * sizeof(node); - - void* start = malloc(size); - memset(start, 0, size); - - mem_block* block = (mem_block*)malloc(sizeof(mem_block)); - if (block == NULL) { - return NULL; - } - memset(block, 0, sizeof(mem_block)); - - block->start = start; - block->offset = 0; - block->empty_offset = 0; - block->max_size = size; - - return block; -} - -/** - * 释放内存池,2次free,对应创建内存池的2次malloc - */ -void _free_mem_block(mem_block* block) -{ - if (!block) { - return; - } - if (block->start) { - free(block->start); - block->start = NULL; - } - free(block); - block = NULL; -} - -/** - * 内存移位,对齐操作 - */ -void _mem_move(mem_block* block) -{ - if (block->empty_offset <= 0) { - //不需要进行内存对齐操作 - return; - } - - size_t _start = (size_t)block->start; - size_t size = sizeof(node); - size_t end = _start + block->offset; - - //开始有数据的位置 - unsigned long start = block->empty_offset + _start; - - while (start < end) { - memset((void*)_start, 0, size); - memcpy((void*)_start, (void*)(start), size); - - _start += size; - start += size; - block->offset -= size; - } - - memset((void*)(_start+block->offset), 0, block->max_size - block->offset); - block->empty_offset = 0; -} - - -/** - * 暂时没用到 - */ -void mem_move_ext(mem_block* block, size_t _start) -{ - size_t size = sizeof(node); - size_t end = _start + block->offset; - - //开始有数据的位置 - unsigned long start = _start + size; - - while (start < end) { - memset((void*)_start, 0, size); - memcpy((void*)_start, (void*)(start), size); - - _start += size; - start += size; - block->offset -= size; - } - - memset((void*)(_start+block->offset), 0, block->max_size - block->offset); - block->empty_offset = 0; -} - - -/** - * 创建一个队列,1次malloc - * @return queue* - */ -queue* create_queue(size_t max_size) -{ - queue *q = (queue*)malloc(sizeof(queue)); - q->first = NULL; - q->last = NULL; - q->length = 0; - q->block = _create_mem_block(max_size); - return q; -} - -/** - * 释放队列,1次free - */ -void _free_queue(queue* q) -{ - if (!q) { - return; - } - _free_mem_block(q->block); - q->block = NULL; - free(q); - q = NULL; -} - - -/** - * 释放一个队列,释放所有的节点,最后释放队列 - * @param q queue*需要释放的队列 - * @param free_data 释放节点数据的函数指针 - */ -void free_queue(queue* q, void (*free_data)(void*)) -{ - if (!q) { - return; - } - node* current = q->first; - node* temp = NULL; - - if (current) { - while (1) { - temp = current->next; - free_node(current, free_data); - q->length--; - current = temp; - - if (current == NULL) { - break; - } - } - } - _free_queue(q); -} - -/** - * 追加元素到队列尾部 - * @param q queue*需要追加元素的队列 - * @param n node*添加的节点 - */ -int push_queue(queue* q, node *n) -{ - if (!q || !n) { - return 0; - } - - printf("push queue ========== %lu\n", (q->length+1)); - - node *t = q->last; - - if (t == NULL) { - q->last = n; - q->first = n; - q->length++; - return 1; - } - - t->next = n; - n->prev = t; - q->last = n; - q->length++; - return 1; -} - -node* peek_queue(queue* q) -{ - if (q == NULL) { - return NULL; - } - - return q->first; -} - -/** - * 从队列首部弹出一个元素 - * @param q queue*队列 - * @return node* - */ -node* pop_queue(queue* q) -{ - if (!q) { - return NULL; - } - - node *t = q->first; - if (t == NULL) { - return NULL; - } - - q->first = t->next; - - if (q->first) { - q->first->prev = NULL; - } - - q->length--; - q->block->empty_offset += sizeof(node); - - //触发内存移位的条件为有一半空闲了 - if (q->block->empty_offset >= q->block->max_size/2) { - _mem_move(q->block); - } - - //如果内存满,pop掉之后立马进行内存对齐,这样就可以有剩余的内存了 - if (q->block->offset >= q->block->max_size) { - _mem_move(q->block); - } - - return t; -} - -/** - * 删除任意节点,暂时没有用到 - * @param q 队列 - * @param free_data 释放节点的函数指针 - */ -void del_node(queue* q, node* n, void (*free_data)(void *)) -{ - node* prev = n->prev; - node* next = n->next; - - if (prev) { - prev->next = next; - } - - if (next) { - next->prev = prev; - } - - mem_move_ext(q->block, n->offset); - free_node(n, free_data); -} - -/** - * 获取队列长度 - * @param q queue*队列 - * @return int - */ -unsigned long length(queue* q) -{ - return q->length; -} - -/** - * 创建一个节点 - */ -node* create_node(queue *q, void* data) -{ - if (!q) { - return NULL; - } - - if (!q->block) { - return NULL; - } - - if (q->block->offset >= q->block->max_size) { - //内存池满 - return NULL; - } - - node* t = (node*)((unsigned long)q->block->start + q->block->offset); - t->data = data; - t->next = NULL; - t->prev = NULL; - t->offset = q->block->offset; - - q->block->offset += sizeof(node); - - if (q->block->offset >= q->block->max_size) { - //内存整理 - _mem_move(q->block); - } - - return t; -} - -/** - * 释放一个节点,其实只需要释放节点的数据就可以了,因为节点都在内存池里面,无需操心内存问题 - */ -void free_node(node* n, void (*free_data)(void*)) -{ - if (n->data) { - free_data(n->data); - } -} - - - diff --git a/tests/epoll/epoll/queue.hpp b/tests/epoll/epoll/queue.hpp deleted file mode 100644 index c24ca1b..0000000 --- a/tests/epoll/epoll/queue.hpp +++ /dev/null @@ -1,72 +0,0 @@ -// -// queue.hpp -// epoll -// -// Created by yuyi on 17/10/24. -// Copyright © 2017年 yuyi. All rights reserved. -// - -#ifndef queue_hpp -#define queue_hpp - -#include -#include "wing.h" - -//队列节点 -typedef struct _node { - void* data; - _node* prev; - _node* next; - unsigned long offset; -} node; - -//队列节点内存池 -typedef struct _mem_block { - void *start; - unsigned long empty_offset; - unsigned long max_size; - unsigned long offset; -} mem_block; - -//队列 -typedef struct _queue { - _node *first; - _node *last; - _mem_block* block; - unsigned long length; -} queue; - - -//创建一大块内存 返回开始指针 -//mem_block *create_mem_block(unsigned long size); -//内存移位 -void _mem_move(mem_block* mem_block); - -//创建一个队列 -queue* create_queue(size_t max_size); - -//释放一个队列 -void free_queue(queue* q, void (*free_data)(void*)); - -//队列尾部追加元素 -int push_queue(queue* q, node *n); - -//队列顶部弹出元素 -node* pop_queue(queue* q); - -//读取首部元素 -node* peek_queue(queue* q); - -//队列长度 -unsigned long length(queue* q); - -//删除节点 -void del_node(queue* q, node* n, void (*free_data)(void *)); - -//创建一个节点 -node* create_node(queue* q, void* data); - -//释放一个节点 -void free_node(node* n, void (*free_data)(void*)); - -#endif /* queue_hpp */ diff --git a/tests/epoll/epoll/queue_test.cpp b/tests/epoll/epoll/queue_test.cpp deleted file mode 100644 index f48da3e..0000000 --- a/tests/epoll/epoll/queue_test.cpp +++ /dev/null @@ -1,92 +0,0 @@ -// -// queue_test.cpp -// epoll -// -// Created by yuyi on 17/10/26. -// Copyright © 2017年 yuyi. All rights reserved. -// - -#include -#include -#include "wing.h" -#include "string.h" -#include "queue.hpp" - - -//用于释放队列数据的函数指针 -//这里仅用户测试队列,这里的data就是一个char* -void free_data(void* data) -{ - if (data) { - free(data); - } - data = NULL; -} - -/** - * 队列测试 - */ -int queue_test() -{ - //创建一个队列 - queue* q = create_queue(SEND_QUEUE_BUF_SIZE); - - //源数据 - char* data = (char*)malloc(6); - memset(data, 0, 6); - sprintf(data, "%s", "hello1"); - - //创建一个节点,这个节点的内存直接来自于上面的内存块 - node* n = create_node(q, data); - //push到队列尾部 - push_queue(q, n); - - //第二个源数据 - data = (char*)malloc(6); - memset(data, 0, 6); - sprintf(data, "%s", "hello2"); - - //创建第二个节点 - n = create_node(q, data); - //将第二个节点push到队列尾部 - push_queue(q, n); - - exit_if(q->length != 2, "队列出错"); - - printf("queue length = %lu\n", q->length); - - //队列遍历操作 - node* c = q->first; - printf("======================\n"); - while(c->next != NULL) { - printf("%s\n", c->data); - c = c->next; - } - if (c) { - printf("%s\n", c->data); - } - //遍历操作-end - - printf("======================\n"); - - printf("offset = %lu\n", q->block->offset); - - //pop一个元素 - node* n1 = pop_queue(q); - printf("%s\n", n1->data); - printf("%lu\n", q->block->empty_offset); - - //执行内存对齐操作 - _mem_move(q->block); - - //确认对齐结果 - node* n2 = (node*)q->block->start; - printf("%ld ==> %s\n", q->block->offset, n2->data); - printf("%lu\n", q->block->empty_offset); - - //释放队列和内存块 - free_queue(q, free_data); - - return 0; -} - diff --git a/tests/epoll/epoll/time.cpp b/tests/epoll/epoll/time.cpp deleted file mode 100644 index 83fd1fa..0000000 --- a/tests/epoll/epoll/time.cpp +++ /dev/null @@ -1,54 +0,0 @@ -// -// time.cpp -// epoll -// -// Created by yuyi on 17/10/27. -// Copyright © 2017年 yuyi. All rights reserved. -// - -#include "time.hpp" -#include "time.h" -#include "string.h" -#include "stdio.h" -#include "stdlib.h" - -/** - * 获取时间戳 - * @return long 当前时间戳 - */ -long get_timestamp() -{ - time_t t = time(NULL); - return time(&t); - - /*struct timespec time; - memset(&time, 0, sizeof(time)); - clock_gettime(CLOCK_REALTIME, &time); - //毫秒时间戳和纳秒时间戳 - //printf("%ld----->%ld\n", time.tv_sec, time.tv_nsec); - - if (nsec) { - return time.tv_nsec; - } - - return time.tv_sec;*/ -} - -/** - * @demo - * char time[24]; - * get_daytime((char*)time, 24); - * printf("current time: %s\n", time); - * @param nowtime 用于容纳当前时间的内存指针 - * @param size nowtime的大小 - */ -void get_daytime(char *nowtime, size_t size) -{ - time_t t; - t = time(NULL); - struct tm *lt; - t = time(NULL); - lt = localtime(&t); - memset(nowtime, 0, size); - strftime(nowtime, size, "%Y-%m-%d %H:%M:%S", lt); -} diff --git a/tests/epoll/epoll/time.hpp b/tests/epoll/epoll/time.hpp deleted file mode 100644 index 53ad654..0000000 --- a/tests/epoll/epoll/time.hpp +++ /dev/null @@ -1,18 +0,0 @@ -// -// time.hpp -// epoll -// -// Created by yuyi on 17/10/27. -// Copyright © 2017年 yuyi. All rights reserved. -// - -#ifndef time_hpp -#define time_hpp - -#include - -long get_timestamp(); -void get_daytime(char *nowtime, size_t size); -void time_test(); - -#endif /* time_hpp */ diff --git a/tests/epoll/epoll/time_test.cpp b/tests/epoll/epoll/time_test.cpp deleted file mode 100644 index 339647e..0000000 --- a/tests/epoll/epoll/time_test.cpp +++ /dev/null @@ -1,31 +0,0 @@ -// -// time_test.cpp -// epoll -// -// Created by yuyi on 17/10/27. -// Copyright © 2017年 yuyi. All rights reserved. -// - -#include -#include "time.hpp" -#include "time.h" -#include "string.h" -#include "stdio.h" -#include "stdlib.h" - - -/** - * 时间测试 - */ -void time_test() -{ - long t1 = get_timestamp(); - //long t2 = get_timestamp(1); - printf("秒时间戳=%ld\n", t1); - // printf("纳秒时间戳=%ld\n", t2); - // printf("秒时间戳=%ld\n", t2/1000000); - - char time[24]; - get_daytime((char*)time, 24); - printf("当前时间=%s\n", time); -} diff --git a/tests/epoll/epoll/wing.cpp b/tests/epoll/epoll/wing.cpp deleted file mode 100644 index 6384b19..0000000 --- a/tests/epoll/epoll/wing.cpp +++ /dev/null @@ -1,126 +0,0 @@ -// -// wing.cpp -// epoll -// -// Created by yuyi on 17/10/28. -// Copyright © 2017年 yuyi. All rights reserved. -// - -#include -#include "wing.h" -#include "wing.hpp" - -/** - * 初始化客户端节点资源 - * @param fd socket句柄 - * @param ip 连接ip - * @param port 连接端口 - * @return client_node* - */ -client_node* init_client(int fd, char* ip, int port) -{ - client_node *client = (client_node*)malloc(sizeof(client_node)); - memset(client, 0, sizeof(client_node)); - - client->fd = fd; - - //123.123.123.123 - memset(client->ip, 0, 16); - sprintf(client->ip, "%s", ip); - client->port = port; - client->queue = create_queue(SEND_QUEUE_BUF_SIZE); - client->last_revc_time = 0; - client->last_send_time = 0; - - return client; -} - -/** - * 释放客户端 - */ -void free_client(client_node* client) -{ - if (client == NULL) { - debug("client is null"); - return; - } - shutdown(client->fd, SHUT_RDWR); - debug("%d shutdown", client->fd); - close(client->fd); - debug("%d close", client->fd); - // client->fd = 0; - free_queue(client->queue, free_send_queue_node); - client->queue = NULL; - - if (client) { - free(client); - } - client = NULL; -} - - -/** - * 创建一个发送队列节点数据,2个malloc - */ -send_queue_node* create_send_queue_node(char* buf, size_t buf_size, int send_times) -{ - send_queue_node* node = (send_queue_node*)malloc(sizeof(send_queue_node)); - node->send_times = send_times + 1; - node->msg = (char*)malloc(buf_size+1); - - memset(node->msg, 0, buf_size + 1); - sprintf(node->msg, buf, buf_size); - node->msg_size = buf_size; - - return node; -} - - -/** - * 释放发送的队列数据,2个free - */ -void free_send_queue_node(void* n) -{ - send_queue_node* sn = (send_queue_node*)n; - if(sn->msg) { - free(sn->msg); - sn->msg = NULL; - } - free(n); - n = NULL; -} - - -/** - * 设置socket的keepalive - * @param sockfd socket句柄 - */ -void set_keepalive(int sockfd) -{ - const uint32_t keepaliveIntervalSec = 10; - -#ifdef _WIN32 - tcp_keepalive keepaliveParams; - DWORD ret = 0; - keepaliveParams.onoff = 1; - keepaliveParams.keepaliveinterval = keepaliveParams.keepalivetime = keepaliveIntervalSec * 1000; - WSAIoctl(sockfd, SIO_KEEPALIVE_VALS, &keepaliveParams, sizeof(keepaliveParams), NULL, 0, &ret, NULL, NULL); - -#elif __APPLE__ - int on = 1, secs = keepaliveIntervalSec; - setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)); - setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPALIVE, &secs, sizeof(secs)); - -#elif __linux - int32_t optval; - socklen_t optlen = sizeof(optval); - optval = 1; - setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen); - const uint32_t keepaliveProbeCount = 20; - optval = keepaliveIntervalSec; - setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, &optval, optlen); - setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, &optval, optlen); - optval = keepaliveProbeCount; - setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, &optval, optlen); -#endif -} diff --git a/tests/epoll/epoll/wing.h b/tests/epoll/epoll/wing.h deleted file mode 100644 index fb5bb32..0000000 --- a/tests/epoll/epoll/wing.h +++ /dev/null @@ -1,40 +0,0 @@ -// -// wing.h -// epoll -// -// Created by yuyi on 17/10/24. -// Copyright © 2017年 yuyi. All rights reserved. -// - -#ifndef wing_h -#define wing_h -#include "errno.h" -#include "time.hpp" -#include - - -#define exit_if(r, ...) \ -if (r) {\ - char time[24];\ - get_daytime((char*)time, 24);\ - printf("%s ", time);\ - printf(__VA_ARGS__); \ - printf(" error no: %d error msg %s\n", errno, strerror(errno)); \ - exit(1);\ -} - -#define SEND_QUEUE_BUF_SIZE 10240 //队列最大容量 -#define MAX_SEND_TIMES 100 //发送失败最大的重试次数 - -#define debug(...) \ -{\ - char time[24];\ - get_daytime((char*)time, 24);\ - printf("%s ", time);\ - printf(__VA_ARGS__);\ - if (errno) \ - printf("\nerror happened: %d %s\n", errno, strerror(errno)); \ - printf("\n");\ -} - -#endif /* wing_h */ diff --git a/tests/epoll/epoll/wing.hpp b/tests/epoll/epoll/wing.hpp deleted file mode 100644 index fdd53a6..0000000 --- a/tests/epoll/epoll/wing.hpp +++ /dev/null @@ -1,95 +0,0 @@ -// -// wing.hpp -// epoll -// -// Created by yuyi on 17/10/28. -// Copyright © 2017年 yuyi. All rights reserved. -// - -#ifndef wing_hpp -#define wing_hpp - -#include -#include -#include "time.hpp" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "queue.hpp" - -#define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */ -#define TCP_MAXSEG 0x02 /* set maximum segment size */ -#define TCP_NOPUSH 0x04 /* don't push last block of write */ -#define TCP_NOOPT 0x08 /* don't use TCP options */ -#define TCP_KEEPALIVE 0x10 /* idle time used when SO_KEEPALIVE is enabled */ -#define TCP_CONNECTIONTIMEOUT 0x20 /* connection timeout */ -#define PERSIST_TIMEOUT 0x40 /* time after which a connection in -* persist timeout will terminate. -* see draft-ananth-tcpm-persist-02.txt -*/ -#define TCP_RXT_CONNDROPTIME 0x80 /* time after which tcp retransmissions will be -* stopped and the connection will be dropped -*/ -#define TCP_RXT_FINDROP 0x100 /* when this option is set, drop a connection -* after retransmitting the FIN 3 times. It will -* prevent holding too many mbufs in socket -* buffer queues. -*/ -#define TCP_KEEPINTVL 0x101 /* interval between keepalives */ -#define TCP_KEEPCNT 0x102 /* number of keepalives before close */ -#define TCP_SENDMOREACKS 0x103 /* always ack every other packet */ -#define TCP_ENABLE_ECN 0x104 /* Enable ECN on a connection */ -#define TCP_FASTOPEN 0x105 /* Enable/Disable TCP Fastopen on this socket */ -#define TCP_CONNECTION_INFO 0x106 /* State of TCP connection */ - - -#ifndef SOL_TCP -#define SOL_TCP IPPROTO_TCP -#endif - -//客户端节点 -typedef struct _client { - int fd; //socket句柄 - queue* queue; //队列指针 - char ip[16]; //客户端ip - int port; //客户端连接端口 - int last_send_time; //最后的发送时间 - int last_revc_time; //最后的读取时间 -} client_node; - -//发送队列节点数据 -typedef struct _send_queue_node { - client_node* client; //客户端节点 - char* msg; //待发送的数据 - unsigned long msg_size; //待发送的数据大小 - int send_times; //发送计数器,当达到MAX_SEND_TIMES时直接放弃重试 -} send_queue_node; - -//发送数据,自带重试机制 -ssize_t send_data(int efd, client_node* client, const char* buf, size_t buf_size, int send_times); -//释放发送队列节点数据 -void free_send_queue_node(void* n); -//发送一个队列节点,一般用于失败重试 -ssize_t send_node(int efd, client_node* client, node* n); -//启用和设置socket的keepalive模式 -void set_keepalive(int sockfd); -client_node* init_client(int fd, char* ip, int port); -void free_client(client_node* client); -send_queue_node* create_send_queue_node(char* buf, size_t buf_size, int send_times); -void free_send_queue_node(void* n); -void set_keepalive(int sockfd); -#endif /* wing_h */