From 3a2df0f612c0b5155ba13f9a81ff89480fbad002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=A0=91=E6=96=B0?= Date: Tue, 12 Oct 2021 08:56:56 +0800 Subject: [PATCH] add one fiber demo --- lib_fiber/samples-c++1x/client/client.cpp | 121 +++++++++++++ lib_fiber/samples-c++1x/client/client.vcxproj | 159 ++++++++++++++++++ .../client/client.vcxproj.filters | 22 +++ lib_fiber/samples-c++1x/client/stdafx.cpp | 1 + lib_fiber/samples-c++1x/client/stdafx.h | 17 ++ 5 files changed, 320 insertions(+) create mode 100644 lib_fiber/samples-c++1x/client/client.cpp create mode 100644 lib_fiber/samples-c++1x/client/client.vcxproj create mode 100644 lib_fiber/samples-c++1x/client/client.vcxproj.filters create mode 100644 lib_fiber/samples-c++1x/client/stdafx.cpp create mode 100644 lib_fiber/samples-c++1x/client/stdafx.h diff --git a/lib_fiber/samples-c++1x/client/client.cpp b/lib_fiber/samples-c++1x/client/client.cpp new file mode 100644 index 000000000..22ae976d0 --- /dev/null +++ b/lib_fiber/samples-c++1x/client/client.cpp @@ -0,0 +1,121 @@ +#include "stdafx.h" +#include +#include + +////////////////////////////////////////////////////////////////////////////// + +static size_t do_echo(acl::socket_stream& conn, int count) { + acl::string data("hello world!\r\n"); + acl::string buf; + + size_t i; + for (i = 0; i < (size_t) count; i++) { + if (conn.write(data) == -1) { + printf("client write error %s\r\n", acl::last_serror()); + break; + } +#if 0 + struct timeval begin, end; + gettimeofday(&begin, NULL); + int ret = acl_readable(conn.sock_handle()); + gettimeofday(&end, NULL); + double cost = acl::stamp_sub(end, begin); + + if (ret == 0) { + printf("not readable, cost=%.2f\r\n", cost); + } else if (ret == 1) { + printf("readable, cost=%.2f\r\n", cost); + } else { + printf("readable error\r\n"); + } +#endif + if (!conn.gets(buf, false)) { + printf("client read error %s\r\n", acl::last_serror()); + break; + } + } + + printf(">>>count=%d<<<\r\n", i); + return i; +} + +static size_t connect_server(const char* addr, int count) { + acl::socket_stream conn; + if (!conn.open(addr, 0, 0)) { + printf("connect %s error %s\r\n", addr, acl::last_serror()); + return 0; + } + + return do_echo(conn, count); +} + +////////////////////////////////////////////////////////////////////////////// + +static void usage(const char* procname) { + printf("usage: %s -h [help]\r\n" + " -e event_type[kernel|select|poll, default: kernel]\r\n" + " -s server_addr[default: 127.0.0.1:9000]\r\n" + " -c fiber_count[default: 1]\r\n" + " -n count[default: 100]\r\n" + , procname); +} + +int main(int argc, char *argv[]) { + int ch, nfiber = 1, count = 100; + acl::string addr = "127.0.0.1:9000", event_type("kernel"); + + acl::acl_cpp_init(); + acl::log::stdout_open(true); + + while ((ch = getopt(argc, argv, "he:s:c:n:")) > 0) { + switch (ch) { + case 'h': + usage(argv[0]); + return 0; + case 'e': + event_type = optarg; + break; + case 's': + addr = optarg; + break; + case 'c': + nfiber = atoi(optarg); + break; + case 'n': + count = atoi(optarg); + break; + default: + break; + } + } + + acl::fiber_event_t type; + + if (event_type == "select") { + type = acl::FIBER_EVENT_T_SELECT; + } else if (event_type == "poll") { + type = acl::FIBER_EVENT_T_POLL; + } else { + type = acl::FIBER_EVENT_T_KERNEL; + } + + size_t total = 0; + + struct timeval begin; + gettimeofday(&begin, NULL); + + for (int i = 0; i < nfiber; i++) { + go[&] { + total += connect_server(addr, count); + }; + } + + acl::fiber::schedule_with(type); + + struct timeval end; + gettimeofday(&end, NULL); + double cost = acl::stamp_sub(end, begin); + printf("Total count=%zd, cost=%.2f ms, speed=%.2f\r\n", + total, cost, (total * 1000) / (cost > 0 ? cost : 0.1)); + return 0; +} diff --git a/lib_fiber/samples-c++1x/client/client.vcxproj b/lib_fiber/samples-c++1x/client/client.vcxproj new file mode 100644 index 000000000..692861993 --- /dev/null +++ b/lib_fiber/samples-c++1x/client/client.vcxproj @@ -0,0 +1,159 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {2f80a939-e4bd-4ed7-b512-95fc2b563036} + client + 10.0 + + + + Application + true + v142 + MultiByte + + + Application + false + v142 + true + MultiByte + + + Application + true + v142 + MultiByte + + + Application + false + v142 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + ..\;..\..\c\include;..\..\cpp\include;..\..\..\lib_acl\include;..\..\..\lib_acl_cpp\include + + + Console + true + $(OutDir);%(AdditionalLibraryDirectories) + libfiber.lib;libfiber_cpp.lib;lib_acl.lib;lib_protocol.lib;lib_acl_cpp.lib;ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + ..\;..\..\c\include;..\..\cpp\include;..\..\..\lib_acl\include;..\..\..\lib_acl_cpp\include + + + Console + true + true + true + $(OutDir);%(AdditionalLibraryDirectories) + libfiber.lib;libfiber_cpp.lib;lib_acl.lib;lib_protocol.lib;lib_acl_cpp.lib;ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + ..\;..\..\c\include;..\..\cpp\include;..\..\..\lib_acl\include;..\..\..\lib_acl_cpp\include + + + Console + true + $(OutDir);%(AdditionalLibraryDirectories) + libfiber.lib;libfiber_cpp.lib;lib_acl.lib;lib_protocol.lib;lib_acl_cpp.lib;ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + ..\;..\..\c\include;..\..\cpp\include;..\..\..\lib_acl\include;..\..\..\lib_acl_cpp\include + + + Console + true + true + true + libfiber.lib;libfiber_cpp.lib;lib_acl.lib;lib_protocol.lib;lib_acl_cpp.lib;ws2_32.lib;%(AdditionalDependencies) + $(OutDir);%(AdditionalLibraryDirectories) + + + + + + + + + \ No newline at end of file diff --git a/lib_fiber/samples-c++1x/client/client.vcxproj.filters b/lib_fiber/samples-c++1x/client/client.vcxproj.filters new file mode 100644 index 000000000..cb6592d72 --- /dev/null +++ b/lib_fiber/samples-c++1x/client/client.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + \ No newline at end of file diff --git a/lib_fiber/samples-c++1x/client/stdafx.cpp b/lib_fiber/samples-c++1x/client/stdafx.cpp new file mode 100644 index 000000000..fd4f341c7 --- /dev/null +++ b/lib_fiber/samples-c++1x/client/stdafx.cpp @@ -0,0 +1 @@ +#include "stdafx.h" diff --git a/lib_fiber/samples-c++1x/client/stdafx.h b/lib_fiber/samples-c++1x/client/stdafx.h new file mode 100644 index 000000000..e9c20ea18 --- /dev/null +++ b/lib_fiber/samples-c++1x/client/stdafx.h @@ -0,0 +1,17 @@ +// stdafx.h : ׼ϵͳļİļ +// dzõĵĿضİļ +// + +#pragma once + +// TODO: ڴ˴óҪĸͷļ + +#include "lib_acl.h" +#include "acl_cpp/lib_acl.hpp" +#include "fiber/fiber.hpp" +#include "fiber/go_fiber.hpp" + +#ifdef WIN32 +#define snprintf _snprintf +#endif +