From e0601e582f448d1fbfe95bed36ca2d0a4e58dc06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=A0=91=E6=96=B0?= Date: Mon, 23 Aug 2021 18:41:23 +0800 Subject: [PATCH] test some fiber module on windows --- acl_cpp_vc2019.sln | 14 +++ lib_fiber/c/include/fiber/fiber_cond.h | 3 - lib_fiber/c/include/fiber/fiber_event.h | 4 - lib_fiber/c/src/common/atomic.c | 25 ++--- lib_fiber/c/src/common/iostuff.h | 16 ++- lib_fiber/c/src/common/socketpair.c | 96 +++++++++++++++++- lib_fiber/c/src/fbase_event.c | 18 +++- lib_fiber/c/src/fiber_cond.c | 10 +- lib_fiber/c/src/fiber_event.c | 12 ++- lib_fiber/c/src/stdafx.h | 10 +- lib_fiber/cpp/include/fiber/fiber_cond.hpp | 3 - lib_fiber/cpp/include/fiber/fiber_event.hpp | 4 - lib_fiber/cpp/include/fiber/fiber_tbox.hpp | 3 - lib_fiber/cpp/src/fiber_cond.cpp | 3 - lib_fiber/cpp/src/fiber_event.cpp | 3 - lib_fiber/samples/WinFiber/WinFiber.rc | Bin 11382 -> 11500 bytes lib_fiber/samples/WinFiber/WinFiberDlg.cpp | 43 ++++++++ lib_fiber/samples/WinFiber/WinFiberDlg.h | 3 + .../samples/WinFiber/WinFiber_vc2019.vcxproj | 8 +- lib_fiber/samples/WinFiber/pch.h | 1 + lib_fiber/samples/WinFiber/resource.h | 4 +- 21 files changed, 222 insertions(+), 61 deletions(-) diff --git a/acl_cpp_vc2019.sln b/acl_cpp_vc2019.sln index 6effa18fb..7e72ca21e 100644 --- a/acl_cpp_vc2019.sln +++ b/acl_cpp_vc2019.sln @@ -45,8 +45,22 @@ EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "server", "lib_fiber\samples-c++1x\server\server.vcxproj", "{EE518BE5-94B2-4F8E-82CC-C08503BBD6B2}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "master_fiber", "lib_fiber\samples\master_fiber\master_fiber_vc2019.vcxproj", "{3E725148-0807-4CBA-8FC1-7FCA46F605F3}" + ProjectSection(ProjectDependencies) = postProject + {F2479E2C-7267-436C-A1F1-A63B39E7CB30} = {F2479E2C-7267-436C-A1F1-A63B39E7CB30} + {6EC1F44E-6A6A-48E9-B699-D7E89B63C8DC} = {6EC1F44E-6A6A-48E9-B699-D7E89B63C8DC} + {AD99B75A-40BF-46DC-844B-23417FDC8690} = {AD99B75A-40BF-46DC-844B-23417FDC8690} + {B40213C2-507C-4C7F-A6E1-B850C9BDC27B} = {B40213C2-507C-4C7F-A6E1-B850C9BDC27B} + {FE724EF7-3763-4E78-BDF5-BCBC075719FD} = {FE724EF7-3763-4E78-BDF5-BCBC075719FD} + EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WinFiber", "lib_fiber\samples\WinFiber\WinFiber_vc2019.vcxproj", "{141DDEA4-DDEF-4D7E-9FE1-5FEFE0D95222}" + ProjectSection(ProjectDependencies) = postProject + {F2479E2C-7267-436C-A1F1-A63B39E7CB30} = {F2479E2C-7267-436C-A1F1-A63B39E7CB30} + {6EC1F44E-6A6A-48E9-B699-D7E89B63C8DC} = {6EC1F44E-6A6A-48E9-B699-D7E89B63C8DC} + {AD99B75A-40BF-46DC-844B-23417FDC8690} = {AD99B75A-40BF-46DC-844B-23417FDC8690} + {B40213C2-507C-4C7F-A6E1-B850C9BDC27B} = {B40213C2-507C-4C7F-A6E1-B850C9BDC27B} + {FE724EF7-3763-4E78-BDF5-BCBC075719FD} = {FE724EF7-3763-4E78-BDF5-BCBC075719FD} + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/lib_fiber/c/include/fiber/fiber_cond.h b/lib_fiber/c/include/fiber/fiber_cond.h index 9c6e34b6f..95d541bad 100644 --- a/lib_fiber/c/include/fiber/fiber_cond.h +++ b/lib_fiber/c/include/fiber/fiber_cond.h @@ -4,8 +4,6 @@ #include "fiber_define.h" #include "fiber_event.h" -#if !defined(_WIN32) && !defined(_WIN64) - #ifdef __cplusplus extern "C" { #endif @@ -61,4 +59,3 @@ FIBER_API int acl_fiber_cond_signal(ACL_FIBER_COND *cond); #endif // !defined(_WIN32) && !defined(_WIN64) -#endif diff --git a/lib_fiber/c/include/fiber/fiber_event.h b/lib_fiber/c/include/fiber/fiber_event.h index 4cfaa7904..8e0e0e872 100644 --- a/lib_fiber/c/include/fiber/fiber_event.h +++ b/lib_fiber/c/include/fiber/fiber_event.h @@ -3,8 +3,6 @@ #include "fiber_define.h" -#if !defined(_WIN32) && !defined(_WIN64) - #ifdef __cplusplus extern "C" { #endif @@ -70,6 +68,4 @@ FIBER_API int acl_fiber_event_notify(ACL_FIBER_EVENT *event); } #endif -#endif // !defined(_WIN32) && !defined(_WIN64) - #endif diff --git a/lib_fiber/c/src/common/atomic.c b/lib_fiber/c/src/common/atomic.c index a78d58278..4a71ca8e6 100644 --- a/lib_fiber/c/src/common/atomic.c +++ b/lib_fiber/c/src/common/atomic.c @@ -11,14 +11,9 @@ # undef HAS_ATOMIC #endif -#if defined(_WIN32) || defined(_WIN64) -# ifndef WINDOWS -# define WINDOWS -# endif -#endif - -#if defined(WINDOWS) -# define HAS_ATOMIC +#if defined(SYS_WIN) +//# define HAS_ATOMIC +#include "common/pthread_patch.h" #endif /* @@ -60,7 +55,7 @@ void atomic_set(ATOMIC *self, void *value) pthread_mutex_lock(&self->lock); self->value = value; pthread_mutex_unlock(&self->lock); -#elif defined(WINDOWS) +#elif defined(SYS_WIN) InterlockedExchangePointer((volatile PVOID*) &self->value, value); #else (void) __sync_lock_test_and_set(&self->value, value); @@ -79,7 +74,7 @@ void *atomic_cas(ATOMIC *self, void *cmp, void *value) pthread_mutex_unlock(&self->lock); return old; -#elif defined(WINDOWS) +#elif defined(SYS_WIN) return InterlockedCompareExchangePointer( (volatile PVOID*)&self->value, value, cmp); #else @@ -98,7 +93,7 @@ void *atomic_xchg(ATOMIC *self, void *value) pthread_mutex_unlock(&self->lock); return old; -#elif defined(WINDOWS) +#elif defined(SYS_WIN) return InterlockedExchangePointer((volatile PVOID*)&self->value, value); #else return __sync_lock_test_and_set(&self->value, value); @@ -111,7 +106,7 @@ void atomic_int64_set(ATOMIC *self, long long n) pthread_mutex_lock(&self->lock); *((long long *) self->value) = n; pthread_mutex_unlock(&self->lock); -#elif defined(WINDOWS) +#elif defined(SYS_WIN) InterlockedExchangePointer((volatile PVOID*) self->value, (PVOID) n); #else (void) __sync_lock_test_and_set((long long *) self->value, n); @@ -126,7 +121,7 @@ long long atomic_int64_fetch_add(ATOMIC *self, long long n) *((long long *) self->value) = v + n; pthread_mutex_unlock(&self->lock); return v; -#elif defined(WINDOWS) +#elif defined(SYS_WIN) return InterlockedExchangeAdd64((volatile LONGLONG*) self->value, n); #else return (long long) __sync_fetch_and_add((long long *) self->value, n); @@ -141,7 +136,7 @@ long long atomic_int64_add_fetch(ATOMIC *self, long long n) *((long long *) self->value) = v; pthread_mutex_unlock(&self->lock); return v; -#elif defined(WINDOWS) +#elif defined(SYS_WIN) return n + InterlockedExchangeAdd64((volatile LONGLONG*) self->value, n); #else return (long long) __sync_add_and_fetch((long long *) self->value, n); @@ -157,7 +152,7 @@ long long atomic_int64_cas(ATOMIC *self, long long cmp, long long n) *((long long *) self->value) = n; pthread_mutex_unlock(&self->lock); return old; -#elif defined(WINDOWS) +#elif defined(SYS_WIN) return InterlockedCompareExchange64( (volatile LONGLONG*)&self->value, n, cmp); #else diff --git a/lib_fiber/c/src/common/iostuff.h b/lib_fiber/c/src/common/iostuff.h index 33c53c36c..218741ae2 100644 --- a/lib_fiber/c/src/common/iostuff.h +++ b/lib_fiber/c/src/common/iostuff.h @@ -54,8 +54,20 @@ void tcp_nodelay(socket_t fd, int onoff); // in read_wait.c int read_wait(socket_t fd, int delay); -#ifdef SYS_UNIX -int sane_socketpair(int domain, int type, int protocol, int result[2]); +/** + * socket + * @param domain {int} + * @param type {int} + * @param protocol {int} + * @param result {int[2]} + * @return {int} + */ +int sane_socketpair(int domain, int type, int protocol, socket_t result[2]); + +#if defined(_WIN32) || defined(_WIN64) +# define CLOSE_SOCKET closesocket +#else +# define CLOSE_SOCKET close #endif #ifdef __cplusplus diff --git a/lib_fiber/c/src/common/socketpair.c b/lib_fiber/c/src/common/socketpair.c index ff2ff74e1..cdeadb669 100644 --- a/lib_fiber/c/src/common/socketpair.c +++ b/lib_fiber/c/src/common/socketpair.c @@ -3,7 +3,101 @@ #include "msg.h" #include "iostuff.h" -#ifdef SYS_UNIX +#ifdef SYS_WIN + +static socket_t inet_listen(const char *addr, int port, int backlog) +{ + socket_t s; + struct sockaddr_in in; + + in.sin_addr.s_addr = inet_addr(addr); + in.sin_port = htons(port); + in.sin_family = AF_INET; + + s = socket(AF_INET, SOCK_STREAM, 0); + if (s == INVALID_SOCKET) { + msg_error("%s(%d), %s: create listen socket error=%s", + __FILE__, __LINE__, __FUNCTION__, last_serror()); + return INVALID_SOCKET; + } + + if (bind(s, (const struct sockaddr*)&in, sizeof(in)) < 0) { + msg_error("%s(%d), %s: bind %s error %s", + __FILE__, __LINE__, __FUNCTION__, addr, last_serror()); + closesocket(s); + return INVALID_SOCKET; + } + + if (listen(s, backlog) < 0) { + msg_error("%s(%d), %s: listen %s error %s", + __FILE__, __LINE__, __FUNCTION__, addr, last_serror()); + closesocket(s); + return INVALID_SOCKET; + } + return s; +} + +int sane_socketpair(int domain, int type, int protocol, socket_t result[2]) +{ + socket_t listener = inet_listen("127.0.0.1", 0, 10); + struct sockaddr_in addr; + struct sockaddr *sa = (struct sockaddr*) &addr; + socklen_t len = sizeof(addr); + + if (listener == INVALID_SOCKET) { + return -1; + } + + result[0] = INVALID_SOCKET; + result[1] = INVALID_SOCKET; + + if (listener == INVALID_SOCKET) { + msg_error("%s(%d), %s: listen error %s", + __FILE__, __LINE__, __FUNCTION__, last_serror()); + return -1; + } + + tcp_nodelay(listener, 1); + if (getsockname(listener, sa, &len) < 0) { + msg_error("%s(%d), %s: getoskname error %s", + __FILE__, __LINE__, __FUNCTION__, last_serror()); + CLOSE_SOCKET(listener); + return -1; + } + + result[0] = socket(AF_INET, SOCK_STREAM, 0); + if (result[0] == INVALID_SOCKET) { + msg_error("%s(%d), %s: create socket %s error %s", + __FILE__, __LINE__, __FUNCTION__, addr, last_serror()); + CLOSE_SOCKET(listener); + return -1; + } + if (connect(result[0], sa, len) == -1) { + msg_error("%s(%d), %s: connect error %s", + __FILE__, __LINE__, __FUNCTION__, last_serror()); + CLOSE_SOCKET(listener); + CLOSE_SOCKET(result[0]); + result[0] = INVALID_SOCKET; + return -1; + } + + result[1] = accept(listener, NULL, 0); + CLOSE_SOCKET(listener); + + if (result[1] == INVALID_SOCKET) { + msg_error("%s(%d), %s: accept error %s", + __FILE__, __LINE__, __FUNCTION__, last_serror()); + CLOSE_SOCKET(result[0]); + result[0] = INVALID_SOCKET; + return -1; + } + + tcp_nodelay(result[0], 1); + tcp_nodelay(result[1], 1); + return 0; +} + +#elif defined(SYS_UNIX) /* sane_socketpair - sanitize socketpair() error returns */ diff --git a/lib_fiber/c/src/fbase_event.c b/lib_fiber/c/src/fbase_event.c index 3e63a0e48..23c5aac03 100644 --- a/lib_fiber/c/src/fbase_event.c +++ b/lib_fiber/c/src/fbase_event.c @@ -1,8 +1,6 @@ #include "stdafx.h" #include "common.h" -#ifdef SYS_UNIX - #if defined(__linux__) # include # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) @@ -14,6 +12,8 @@ #endif #include "fiber/libfiber.h" +#include "fiber/fiber_hook.h" +#include "common/iostuff.h" #include "fiber.h" void fbase_event_open(FIBER_BASE *fbase) @@ -53,10 +53,10 @@ void fbase_event_open(FIBER_BASE *fbase) void fbase_event_close(FIBER_BASE *fbase) { if (fbase->event_in >= 0) { - close(fbase->event_in); + CLOSE_SOCKET(fbase->event_in); } if (fbase->event_out != fbase->event_in && fbase->event_out >= 0) { - close(fbase->event_out); + CLOSE_SOCKET(fbase->event_out); } fbase->event_in = -1; fbase->event_out = -2; @@ -73,7 +73,11 @@ int fbase_event_wait(FIBER_BASE *fbase) } while (1) { +#ifdef SYS_WIN + ret = (int) recv(fbase->event_in, (char*) &n, sizeof(n), 0); +#else ret = (int) acl_fiber_read(fbase->event_in, &n, sizeof(n)); +#endif if (ret == sizeof(n)) { break; } @@ -126,7 +130,12 @@ int fbase_event_wakeup(FIBER_BASE *fbase) } while (1) { +#ifdef SYS_WIN + ret = (int) acl_fiber_send(fbase->event_out, (char*) &n, sizeof(n), 0); +#else ret = (int) acl_fiber_write(fbase->event_out, &n, sizeof(n)); +#endif + if (ret == sizeof(n)) { break; } @@ -155,4 +164,3 @@ int fbase_event_wakeup(FIBER_BASE *fbase) return 0; } -#endif // SYS_UNIX diff --git a/lib_fiber/c/src/fiber_cond.c b/lib_fiber/c/src/fiber_cond.c index 1cee59bdb..857c98b45 100644 --- a/lib_fiber/c/src/fiber_cond.c +++ b/lib_fiber/c/src/fiber_cond.c @@ -1,10 +1,12 @@ #include "stdafx.h" #include "common.h" -#ifdef SYS_UNIX +//#ifdef SYS_UNIX +#if 1 #include "fiber/libfiber.h" #include "fiber/fiber_cond.h" +#include "common/pthread_patch.h" #include "fiber.h" struct ACL_FIBER_COND { @@ -16,7 +18,9 @@ struct ACL_FIBER_COND { ACL_FIBER_COND *acl_fiber_cond_create(unsigned flag fiber_unused) { +#ifdef SYS_UNIX pthread_mutexattr_t attr; +#endif ACL_FIBER_COND *cond = (ACL_FIBER_COND *) mem_calloc(1, sizeof(ACL_FIBER_COND)); @@ -25,10 +29,14 @@ ACL_FIBER_COND *acl_fiber_cond_create(unsigned flag fiber_unused) atomic_set(cond->atomic, &cond->value); atomic_int64_set(cond->atomic, 0); +#ifdef SYS_UNIX pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&cond->mutex, &attr); pthread_mutexattr_destroy(&attr); +#else + pthread_mutex_init(&cond->mutex, NULL); +#endif return cond; } diff --git a/lib_fiber/c/src/fiber_event.c b/lib_fiber/c/src/fiber_event.c index f3b24bd69..40a8f1aab 100644 --- a/lib_fiber/c/src/fiber_event.c +++ b/lib_fiber/c/src/fiber_event.c @@ -1,8 +1,6 @@ #include "stdafx.h" #include "common.h" -#ifdef SYS_UNIX - #include "fiber/libfiber.h" #include "fiber.h" @@ -57,11 +55,15 @@ ACL_FIBER_EVENT *acl_fiber_event_create(unsigned flag) atomic_int64_set(event->atomic, 0); if ((flag & FIBER_FLAG_USE_MUTEX)) { +#ifdef SYS_WIN + pthread_mutex_init(&event->lock.tlock, NULL); +#else pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutex_init(&event->lock.tlock, &attr); pthread_mutexattr_destroy(&attr); +#endif } else { event->lock.atomic.alock = atomic_new(); atomic_set(event->lock.atomic.alock, &event->lock.atomic.value); @@ -119,7 +121,11 @@ int acl_fiber_event_wait(ACL_FIBER_EVENT *event) FIBER_BASE *fbase; unsigned wakeup; +#ifdef SYS_WIN + if (atomic_int64_cas(event->atomic, 0, 1) == 0) { +#else if (LIKELY(atomic_int64_cas(event->atomic, 0, 1) == 0)) { +#endif __ll_lock(event); event->owner = fiber ? &fiber->base : NULL; event->tid = __pthread_self(); @@ -254,5 +260,3 @@ int acl_fiber_event_notify(ACL_FIBER_EVENT *event) return 0; } - -#endif // SYS_UNIX diff --git a/lib_fiber/c/src/stdafx.h b/lib_fiber/c/src/stdafx.h index 43c59fc58..08b6f39ab 100644 --- a/lib_fiber/c/src/stdafx.h +++ b/lib_fiber/c/src/stdafx.h @@ -3,12 +3,12 @@ #include "define.h" -#if 1 -#define LIKELY(x) __builtin_expect(!!(x), 1) -#define UNLIKELY(x) __builtin_expect(!!(x), 0) +#if defined(_WIN32) || defined(_WIN64) +# define LIKELY +# define UNLIKELY #else -#define LIKELY -#define UNLIKELY +# define LIKELY(x) __builtin_expect(!!(x), 1) +# define UNLIKELY(x) __builtin_expect(!!(x), 0) #endif #ifndef _GNU_SOURCE diff --git a/lib_fiber/cpp/include/fiber/fiber_cond.hpp b/lib_fiber/cpp/include/fiber/fiber_cond.hpp index d876377cc..d1cf642f4 100644 --- a/lib_fiber/cpp/include/fiber/fiber_cond.hpp +++ b/lib_fiber/cpp/include/fiber/fiber_cond.hpp @@ -1,8 +1,6 @@ #pragma once #include "fiber_cpp_define.hpp" -#if !defined(_WIN32) && !defined(_WIN64) - struct ACL_FIBER_COND; namespace acl { @@ -52,4 +50,3 @@ private: } -#endif diff --git a/lib_fiber/cpp/include/fiber/fiber_event.hpp b/lib_fiber/cpp/include/fiber/fiber_event.hpp index d0887aea1..2b88c8208 100644 --- a/lib_fiber/cpp/include/fiber/fiber_event.hpp +++ b/lib_fiber/cpp/include/fiber/fiber_event.hpp @@ -1,8 +1,6 @@ #pragma once #include "fiber_cpp_define.hpp" -#if !defined(_WIN32) && !defined(_WIN64) - struct ACL_FIBER_EVENT; namespace acl { @@ -65,5 +63,3 @@ private: } // namespace acl -#endif - diff --git a/lib_fiber/cpp/include/fiber/fiber_tbox.hpp b/lib_fiber/cpp/include/fiber/fiber_tbox.hpp index d91dcdf53..df52d75d7 100644 --- a/lib_fiber/cpp/include/fiber/fiber_tbox.hpp +++ b/lib_fiber/cpp/include/fiber/fiber_tbox.hpp @@ -5,8 +5,6 @@ #include "fiber_event.hpp" #include "fiber_cond.hpp" -#if !defined(_WIN32) && !defined(_WIN64) - namespace acl { /** @@ -209,4 +207,3 @@ private: } // namespace acl -#endif diff --git a/lib_fiber/cpp/src/fiber_cond.cpp b/lib_fiber/cpp/src/fiber_cond.cpp index c409478c8..d71ad0e33 100644 --- a/lib_fiber/cpp/src/fiber_cond.cpp +++ b/lib_fiber/cpp/src/fiber_cond.cpp @@ -2,8 +2,6 @@ #include "fiber/fiber_event.hpp" #include "fiber/fiber_cond.hpp" -#if !defined(_WIN32) && !defined(_WIN64) - namespace acl { fiber_cond::fiber_cond(void) @@ -38,4 +36,3 @@ bool fiber_cond::notify(void) } // namespace acl -#endif // !defined(_WIN32) && !defined(_WIN64) diff --git a/lib_fiber/cpp/src/fiber_event.cpp b/lib_fiber/cpp/src/fiber_event.cpp index 8c6996d86..5ce1964a1 100644 --- a/lib_fiber/cpp/src/fiber_event.cpp +++ b/lib_fiber/cpp/src/fiber_event.cpp @@ -1,8 +1,6 @@ #include "stdafx.hpp" #include "fiber/fiber_event.hpp" -#if !defined(_WIN32) && !defined(_WIN64) - namespace acl { fiber_event::fiber_event(bool use_mutex /* = true */, @@ -38,4 +36,3 @@ bool fiber_event::notify(void) } // namespace acl -#endif diff --git a/lib_fiber/samples/WinFiber/WinFiber.rc b/lib_fiber/samples/WinFiber/WinFiber.rc index 60d81f8b78da1bfde661456e042e838d4a91dc01..f864a0d123d56eb12fd5b14589ae88c85a4e9025 100644 GIT binary patch delta 76 zcmews@g{PEoy6ofqFUCD4CM@o44Dii3@!|Q48aUa3_1*+K(;eOJcA=cI1qa>gaFwP WSseyb1_Pi9Lk438%gwqHo+1F!bq`bk delta 19 bcmaD8`7L6Doy6oaVZKQU8#Wh7RtWEnableWindow(FALSE); acl::fiber* fb = new CFiberHttpd(m_httpdAddr.c_str()); fb->start(); + //GetDlgItem(IDC_START_HTTPD)->EnableWindow(FALSE); } void CWinFiberDlg::OnBnClickedOk() @@ -319,3 +322,43 @@ void CWinFiberDlg::OnBnClickedCancel() CDialogEx::OnCancel(); StopFiber(); // 停止协程调度过程 } + +bool CWinFiberDlg::ResolveDNS(const char* name, std::vector* addrs) +{ + struct hostent *ent = gethostbyname(name); + if (ent == NULL) { + printf("gethostbyname error: %s, name=%s\r\n", acl::last_serror(), name); + return false; + } + + for (int i = 0; ent->h_addr_list[i]; i++) { + char* addr = ent->h_addr_list[i]; + char ip[64]; + const char* ptr = inet_ntop(ent->h_addrtype, addr, ip, sizeof(ip)); + if (ptr) { + addrs->push_back(ptr); + } else { + printf(">>>inet_ntop error\r\n"); + } + } + + return true; +} + +void CWinFiberDlg::OnBnClickedAwaitDns() +{ + // TODO: 在此添加控件通知处理程序代码 + std::string name = "www.google.com"; + std::vector addrs; + go_wait[&]{ + if (!ResolveDNS(name.c_str(), &addrs)) { + printf(">>>resolve DNS error, name=%s\r\n", name.c_str()); + } + }; + + printf(">>>name=%s, result count=%zd\r\n", name.c_str(), addrs.size()); + for (std::vector::const_iterator cit = addrs.begin(); + cit != addrs.end(); ++cit) { + printf(">>>ip=%s\r\n", (*cit).c_str()); + } +} diff --git a/lib_fiber/samples/WinFiber/WinFiberDlg.h b/lib_fiber/samples/WinFiber/WinFiberDlg.h index 6946b9628..b832a3a6b 100644 --- a/lib_fiber/samples/WinFiber/WinFiberDlg.h +++ b/lib_fiber/samples/WinFiber/WinFiberDlg.h @@ -48,6 +48,8 @@ private: void InitFiber(); void StopFiber(); + bool ResolveDNS(const char* name, std::vector* addrs); + public: void OnFiberConnectExit(acl::fiber* fb); @@ -59,4 +61,5 @@ public: afx_msg void OnBnClickedStartHttpd(); afx_msg void OnBnClickedOk(); afx_msg void OnBnClickedCancel(); + afx_msg void OnBnClickedAwaitDns(); }; diff --git a/lib_fiber/samples/WinFiber/WinFiber_vc2019.vcxproj b/lib_fiber/samples/WinFiber/WinFiber_vc2019.vcxproj index a0e3b4bf8..1a0981f90 100644 --- a/lib_fiber/samples/WinFiber/WinFiber_vc2019.vcxproj +++ b/lib_fiber/samples/WinFiber/WinFiber_vc2019.vcxproj @@ -162,7 +162,7 @@ Use Level3 true - WIN32;_WINDOWS;_DEBUG;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + WIN32;_WINDOWS;_DEBUG;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions) pch.h ..\..\..\lib_acl_cpp\include;..\..\..\lib_acl\include;..\..\..\lib_fiber\c\include;..\..\..\lib_fiber\cpp\include @@ -187,7 +187,7 @@ Use Level3 true - WIN32;_WINDOWS;_DEBUG;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + WIN32;_WINDOWS;_DEBUG;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions) pch.h ..\..\..\lib_acl_cpp\include;..\..\..\lib_acl\include;..\..\..\lib_fiber\c\include;..\..\..\lib_fiber\cpp\include @@ -262,7 +262,7 @@ true true true - WIN32;_WINDOWS;NDEBUG;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + WIN32;_WINDOWS;NDEBUG;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions) pch.h ..\..\..\lib_acl_cpp\include;..\..\..\lib_acl\include;..\..\..\lib_fiber\c\include;..\..\..\lib_fiber\cpp\include @@ -291,7 +291,7 @@ true true true - WIN32;_WINDOWS;NDEBUG;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + WIN32;_WINDOWS;NDEBUG;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions) pch.h ..\..\..\lib_acl_cpp\include;..\..\..\lib_acl\include;..\..\..\lib_fiber\c\include;..\..\..\lib_fiber\cpp\include diff --git a/lib_fiber/samples/WinFiber/pch.h b/lib_fiber/samples/WinFiber/pch.h index 6fdd676f6..1b117d5ed 100644 --- a/lib_fiber/samples/WinFiber/pch.h +++ b/lib_fiber/samples/WinFiber/pch.h @@ -14,5 +14,6 @@ #include "acl_cpp/lib_acl.hpp" #include "fiber/lib_fiber.h" #include "fiber/lib_fiber.hpp" +#include "fiber/go_fiber.hpp" #endif //PCH_H diff --git a/lib_fiber/samples/WinFiber/resource.h b/lib_fiber/samples/WinFiber/resource.h index fd4a1ec96..a88f69fb9 100644 --- a/lib_fiber/samples/WinFiber/resource.h +++ b/lib_fiber/samples/WinFiber/resource.h @@ -12,6 +12,8 @@ #define IDC_CONNECT 1002 #define IDC_CREATE_TIMER 1003 #define IDC_START_HTTPD 1004 +#define IDC_BUTTON1 1005 +#define IDC_AWAIT_DNS 1005 // Next default values for new objects // @@ -19,7 +21,7 @@ #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 130 #define _APS_NEXT_COMMAND_VALUE 32771 -#define _APS_NEXT_CONTROL_VALUE 1005 +#define _APS_NEXT_CONTROL_VALUE 1006 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif