From 52319e360d61eed1278c33c16341168f0886ab30 Mon Sep 17 00:00:00 2001 From: halx99 Date: Sun, 24 Nov 2024 02:52:51 +0800 Subject: [PATCH] Update lua example --- 1k/1kiss.ps1 | 100 ++++++++++++----------- 1k/install-pwsh.sh | 6 +- examples/lua/main.cpp | 23 +----- examples/lua/scripts/example.lua | 12 ++- extensions/yasio_http/HttpCookie.cpp | 3 +- manifest.json | 2 +- tests/ssl/main.cpp | 2 +- yasio/config.hpp | 2 +- yasio/{fsutils.hpp => file.hpp} | 114 +++++++++++++-------------- yasio/io_service.cpp | 23 +++--- yasio/io_service.hpp | 1 + 11 files changed, 144 insertions(+), 144 deletions(-) rename yasio/{fsutils.hpp => file.hpp} (97%) diff --git a/1k/1kiss.ps1 b/1k/1kiss.ps1 index 77e9e983a..7875d4805 100644 --- a/1k/1kiss.ps1 +++ b/1k/1kiss.ps1 @@ -195,6 +195,7 @@ $1k = [_1kiss]::new() # x.y.z~x2.y2.z2 : range $manifest = @{ msvc = '14.39+'; # cl.exe @link.exe 14.39 VS2022 17.9.x + vs = '12.0+'; ndk = 'r23c'; xcode = '13.0.0+'; # range # _EMIT_STL_ERROR(STL1000, "Unexpected compiler version, expected Clang xx.x.x or newer."); @@ -597,7 +598,8 @@ function find_prog($name, $path = $null, $mode = 'ONLY', $cmd = $null, $params = else { if (!$preferredVer.Contains('*')) { $checkVerCond = '$(version_eq $foundVer $preferredVer)' - } else { + } + else { $wildcardVer = $preferredVer $preferredVer = $wildcardVer.TrimEnd('.*') $checkVerCond = '$(version_like $foundVer $wildcardVer)' @@ -752,6 +754,46 @@ function fetch_pkg($url, $exrep = $null) { if ($pfn_rename) { &$pfn_rename } } + +# +# Find latest installed: Visual Studio 12 2013 + +# installationVersion +# installationPath +# instanceId: used for EnterDevShell +# result: +# $Global:VS_INST +# +$Global:VS_INST = $null +function find_vs() { + if (!$Global:VS_INST) { + $VSWHERE_EXE = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" + $eap = $ErrorActionPreference + $ErrorActionPreference = 'SilentlyContinue' + + $required_vs_ver = $manifest['vs'] + if (!$required_vs_ver) { $required_vs_ver = '12.0+' } + + $require_comps = @('Microsoft.Component.MSBuild', 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64') + $vs_installs = ConvertFrom-Json "$(&$VSWHERE_EXE -version $required_vs_ver.TrimEnd('+') -format 'json' -requires $require_comps)" + $ErrorActionPreference = $eap + + if ($vs_installs) { + $vs_inst_latest = $null + $vs_ver = '' + foreach ($vs_inst in $vs_installs) { + $inst_ver = [VersionEx]$vs_inst.installationVersion + if ($vs_ver -lt $inst_ver) { + $vs_ver = $inst_ver + $vs_inst_latest = $vs_inst + } + } + $Global:VS_INST = $vs_inst_latest + } else { + throw "No suitable visual studio installed, required: $required_vs_ver" + } + } +} + # setup nuget, not add to path function setup_nuget() { if (!$manifest['nuget']) { return $null } @@ -1231,11 +1273,12 @@ function setup_emsdk() { function setup_msvc() { $cl_prog, $cl_ver = find_prog -name 'msvc' -cmd 'cl' -silent $true -usefv $true if (!$cl_prog) { - if ($VS_INST) { - Import-Module "$VS_PATH\Common7\Tools\Microsoft.VisualStudio.DevShell.dll" + if ($Global:VS_INST) { + $vs_path = $Global:VS_INST.installationPath + Import-Module "$vs_path\Common7\Tools\Microsoft.VisualStudio.DevShell.dll" $dev_cmd_args = "-arch=$target_cpu -host_arch=x64 -no_logo" if (!$manifest['msvc'].EndsWith('+')) { $dev_cmd_args += " -vcvars_ver=$cl_ver" } - Enter-VsDevShell -VsInstanceId $VS_INST.instanceId -SkipAutomaticLocation -DevCmdArguments $dev_cmd_args + Enter-VsDevShell -VsInstanceId $Global:VS_INST.instanceId -SkipAutomaticLocation -DevCmdArguments $dev_cmd_args $cl_prog, $cl_ver = find_prog -name 'msvc' -cmd 'cl' -silent $true -usefv $true $1k.println("Using msvc: $cl_prog, version: $cl_ver") @@ -1297,44 +1340,6 @@ function setup_gclient() { $env:DEPOT_TOOLS_WIN_TOOLCHAIN = 0 } -# -# Find latest installed: Visual Studio 12 2013 + -# installationVersion -# instanceId EnterDevShell can use it -# result: -# $Global:VS_VERSION -# $Global:VS_INST -# $Global:VS_PATH -# -$Global:VS_VERSION = $null -$Global:VS_PATH = $null -$Global:VS_INST = $null -function find_vs_latest() { - $vs_version = [VersionEx]'12.0' - if (!$Global:VS_INST) { - $VSWHERE_EXE = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" - $eap = $ErrorActionPreference - $ErrorActionPreference = 'SilentlyContinue' - - $vs_installs = ConvertFrom-Json "$(&$VSWHERE_EXE -version '12.0' -format 'json')" - $ErrorActionPreference = $eap - - if ($vs_installs) { - $vs_inst_latest = $null - foreach ($vs_inst in $vs_installs) { - $inst_ver = [VersionEx]$vs_inst.installationVersion - if ($vs_version -lt $inst_ver) { - $vs_version = $inst_ver - $vs_inst_latest = $vs_inst - } - } - $Global:VS_PATH = $vs_inst_latest.installationPath - $Global:VS_INST = $vs_inst_latest - } - } - $Global:VS_VERSION = $vs_version -} - # preprocess methods: # -inputOptions [CMAKE_OPTIONS] function preprocess_win([string[]]$inputOptions) { @@ -1350,13 +1355,15 @@ function preprocess_win([string[]]$inputOptions) { $arch = if ($options.a -eq 'x86') { 'Win32' } else { $options.a } # arch - if ($VS_VERSION -ge [VersionEx]'16.0') { + $vs_ver = [VersionEx]$Global:VS_INST.installationVersion + if ($vs_ver -ge [VersionEx]'16.0') { $outputOptions += '-A', $arch if ($TOOLCHAIN_VER) { $outputOptions += "-Tv$TOOLCHAIN_VER" } } else { + if (!$TOOLCHAIN_VER) { $TOOLCHAIN_VER = "$($vs_ver.Major)0" } $gens = @{ '120' = 'Visual Studio 12 2013'; '140' = 'Visual Studio 14 2015' @@ -1364,7 +1371,7 @@ function preprocess_win([string[]]$inputOptions) { } $Script:cmake_generator = $gens[$TOOLCHAIN_VER] if (!$Script:cmake_generator) { - throw "Unsupported toolchain: $TOOLCHAIN" + throw "Unsupported toolchain: $TOOLCHAIN$TOOLCHAIN_VER" } if ($options.a -eq "x64") { $Script:cmake_generator += ' Win64' @@ -1558,7 +1565,7 @@ $null = setup_glslcc $cmake_prog, $Script:cmake_ver = setup_cmake if ($Global:is_win_family) { - find_vs_latest + find_vs $nuget_prog = setup_nuget } @@ -1614,7 +1621,8 @@ if (!$setupOnly) { if ($is_host_target) { if (!$is_host_cpu) { $out_dir = "${prefix}${TARGET_CPU}" - } else { + } + else { $out_dir = $prefix.TrimEnd("_") } } diff --git a/1k/install-pwsh.sh b/1k/install-pwsh.sh index fb86bec71..e9cd29f29 100755 --- a/1k/install-pwsh.sh +++ b/1k/install-pwsh.sh @@ -12,7 +12,7 @@ mkdir -p $cacheDir pwsh_ver=$1 if [ "$pwsh_ver" = "" ] ; then - pwsh_ver='7.4.4' + pwsh_ver='7.4.6' fi pwsh_min_ver=$2 @@ -64,8 +64,8 @@ elif [ $HOST_OS = 'Linux' ] ; then curl -L "https://github.com/PowerShell/PowerShell/releases/download/v$pwsh_ver/$pwsh_pkg" -o "$pwsh_pkg_out" fi sudo_cmd=$(which sudo) - $sudo_cmd dpkg -i "$pwsh_pkg_out" - $sudo_cmd apt-get install -f + $sudo_cmd dpkg -i --ignore-depends=libicu72 "$pwsh_pkg_out" + $sudo_cmd apt-get install -f powershell elif command -v pacman > /dev/null; then # Linux distro: Arch # refer: https://ephos.github.io/posts/2018-9-17-Pwsh-ArchLinux # available pwsh version, refer to: https://aur.archlinux.org/packages/powershell-bin diff --git a/examples/lua/main.cpp b/examples/lua/main.cpp index 10b9617f7..ca216fcb0 100644 --- a/examples/lua/main.cpp +++ b/examples/lua/main.cpp @@ -13,7 +13,7 @@ #if defined(_WIN32) # include -# pragma comment(lib, "winmm.lib") +# include "yasio/wtimer_hres.hpp" #endif int main(int argc, char** argv) @@ -22,15 +22,8 @@ int main(int argc, char** argv) SetConsoleOutputCP(CP_UTF8); #endif -#if defined(_WIN32) && WINAPI_FAMILY != WINAPI_FAMILY_APP - UINT TARGET_RESOLUTION = 1; // 1 millisecond target resolution - TIMECAPS tc; - UINT wTimerRes = 0; - if (TIMERR_NOERROR == timeGetDevCaps(&tc, sizeof(TIMECAPS))) - { - wTimerRes = (std::min)((std::max)(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax); - timeBeginPeriod(wTimerRes); - } +#if defined(_WIN32) + yasio::wtimer_hres whres; #endif #if YASIO__HAS_CXX14 @@ -78,15 +71,5 @@ int main(int argc, char** argv) std::this_thread::sleep_for(std::chrono::milliseconds(50)); } while (!update(50.0 / 1000)); #endif - -#if defined(_WIN32) && WINAPI_FAMILY != WINAPI_FAMILY_APP - /////////////////////////////////////////////////////////////////////////// - /////////////// restoring timer resolution - /////////////////////////////////////////////////////////////////////////// - if (wTimerRes != 0) - { - timeEndPeriod(wTimerRes); - } -#endif return 0; } diff --git a/examples/lua/scripts/example.lua b/examples/lua/scripts/example.lua index 6e3d8f153..0acec3c04 100644 --- a/examples/lua/scripts/example.lua +++ b/examples/lua/scripts/example.lua @@ -1,4 +1,5 @@ --- yasio 4.0.0 demo +-- yasio 4.3.x demo + local proto = require 'protocol_enc' local yasio = require 'yasio' -- constants @@ -159,4 +160,11 @@ if(yasio.loop) then end) end -return yasio_update +local function yasio_stop() + client:stop() + server:stop() +end + +print('yasio - example started, version:' .. yasio.version) + +return {update = yasio_update, stop = yasio_stop} diff --git a/extensions/yasio_http/HttpCookie.cpp b/extensions/yasio_http/HttpCookie.cpp index a64fc88ea..a60890b4d 100644 --- a/extensions/yasio_http/HttpCookie.cpp +++ b/extensions/yasio_http/HttpCookie.cpp @@ -27,8 +27,9 @@ #include "yasio_http/HttpCookie.h" #include "yasio_http/Uri.h" #include "yasio/utils.hpp" -#include "yasio/fsutils.hpp" + #include "yasio/split.hpp" +#include "yasio/file.hpp" #include #include diff --git a/manifest.json b/manifest.json index 433f64a24..d4473f4d0 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "versions": { - "1kdist": "v90" + "1kdist": "v93" }, "mirrors": { "github": { diff --git a/tests/ssl/main.cpp b/tests/ssl/main.cpp index ac72f8eeb..769702627 100644 --- a/tests/ssl/main.cpp +++ b/tests/ssl/main.cpp @@ -77,7 +77,7 @@ void yasioTest() obs.write_bytes("User-Agent: Mozilla/5.0 (Windows NT 10.0; " "WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " - "Chrome/108.0.5359.125 Safari/537.36\r\n"); + "Chrome/131.0.0.0 Safari/537.36\r\n"); obs.write_bytes("Accept: */*;q=0.8\r\n"); obs.write_bytes("Connection: Close\r\n\r\n"); diff --git a/yasio/config.hpp b/yasio/config.hpp index c41511f5e..f8e3b5ebb 100644 --- a/yasio/config.hpp +++ b/yasio/config.hpp @@ -205,7 +205,7 @@ SOFTWARE. /* ** The yasio version macros */ -#define YASIO_VERSION_NUM 0x040301 +#define YASIO_VERSION_NUM 0x040302 /* ** The macros used by io_service. diff --git a/yasio/fsutils.hpp b/yasio/file.hpp similarity index 97% rename from yasio/fsutils.hpp rename to yasio/file.hpp index 971ce13ae..46d20abb6 100644 --- a/yasio/fsutils.hpp +++ b/yasio/file.hpp @@ -1,57 +1,57 @@ -////////////////////////////////////////////////////////////////////////////////////////// -// A multi-platform support c++11 library with focus on asynchronous socket I/O for any -// client application. -////////////////////////////////////////////////////////////////////////////////////////// -/* -The MIT License (MIT) - -Copyright (c) 2012-2024 HALX99 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -#pragma once - -#include "yasio/string.hpp" -#include "yasio/string_view.hpp" -#include - -namespace yasio -{ -inline yasio::string read_text_file(cxx17::string_view file_path) -{ - std::ifstream fin(file_path.data(), std::ios_base::binary); - if (fin.is_open()) - { - fin.seekg(std::ios_base::end); - auto n = static_cast(fin.tellg()); - if (n > 0) - { - yasio::string ret; - ret.resize_and_overwrite(n, [&fin](char* out, size_t outlen) { - fin.seekg(std::ios_base::beg); - fin.read(out, outlen); - return outlen; - }); - return ret; - } - } - return yasio::string{}; -} -} // namespace yasio +////////////////////////////////////////////////////////////////////////////////////////// +// A multi-platform support c++11 library with focus on asynchronous socket I/O for any +// client application. +////////////////////////////////////////////////////////////////////////////////////////// +/* +The MIT License (MIT) + +Copyright (c) 2012-2024 HALX99 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#pragma once + +#include "yasio/string.hpp" +#include "yasio/string_view.hpp" +#include + +namespace yasio +{ +inline yasio::string read_text_file(cxx17::string_view file_path) +{ + std::ifstream fin(file_path.data(), std::ios_base::binary); + if (fin.is_open()) + { + fin.seekg(std::ios_base::end); + auto n = static_cast(fin.tellg()); + if (n > 0) + { + yasio::string ret; + ret.resize_and_overwrite(n, [&fin](char* out, size_t outlen) { + fin.seekg(std::ios_base::beg); + fin.read(out, outlen); + return outlen; + }); + return ret; + } + } + return yasio::string{}; +} +} // namespace yasio diff --git a/yasio/io_service.cpp b/yasio/io_service.cpp index 9578e1fe3..b3625c000 100644 --- a/yasio/io_service.cpp +++ b/yasio/io_service.cpp @@ -533,11 +533,7 @@ int io_transport_ssl::do_ssl_handshake(int& error) return -1; }; this->write_cb_ = [this](const void* data, int len, const ip::endpoint*, int& error) { return yssl_write(ssl_, data, len, error); }; - - YASIO_KLOGD("[index: %d] the connection #%u <%s> --> <%s> is established.", ctx_->index_, this->id_, this->local_endpoint().to_string().c_str(), - this->remote_endpoint().to_string().c_str()); - get_service().fire_event(ctx_->index_, YEK_ON_OPEN, 0, this); - + get_service().notify_connect_succeed(this); error = EWOULDBLOCK; } else @@ -1708,16 +1704,19 @@ void io_service::active_transport(transport_handle_t t) #endif } if (!yasio__testbits(ctx->properties_, YCM_SSL)) - { - YASIO__UNUSED_PARAM(s); - YASIO_KLOGV("[index: %d] sndbuf=%d, rcvbuf=%d", ctx->index_, s->get_optval(SOL_SOCKET, SO_SNDBUF), s->get_optval(SOL_SOCKET, SO_RCVBUF)); - YASIO_KLOGD("[index: %d] the connection #%u <%s> --> <%s> is established.", ctx->index_, t->id_, t->local_endpoint().to_string().c_str(), - t->remote_endpoint().to_string().c_str()); - this->fire_event(ctx->index_, YEK_ON_OPEN, 0, t); - } + notify_connect_succeed(t); else if (yasio__testbits(ctx->properties_, YCM_CLIENT)) this->wakeup(); } +void io_service::notify_connect_succeed(transport_handle_t t) +{ + auto& s = t->socket_; + auto ctx = t->ctx_; + YASIO_KLOGD("[index: %d] the connection #%u <%s> --> <%s> is established(sndbuf=%d, rcvbuf=%d).", ctx->index_, t->id_, + t->local_endpoint().to_string().c_str(), t->remote_endpoint().to_string().c_str(), s->get_optval(SOL_SOCKET, SO_SNDBUF), + s->get_optval(SOL_SOCKET, SO_RCVBUF)); + fire_event(ctx->index_, YEK_ON_OPEN, 0, t); +} transport_handle_t io_service::allocate_transport(io_channel* ctx, xxsocket_ptr&& s) { transport_handle_t transport; diff --git a/yasio/io_service.hpp b/yasio/io_service.hpp index f16b0f777..c4ea2396b 100644 --- a/yasio/io_service.hpp +++ b/yasio/io_service.hpp @@ -1186,6 +1186,7 @@ class YASIO_API io_service // lgtm [cpp/class-many-fields] YASIO__DECL void handle_connect_succeed(transport_handle_t); YASIO__DECL void handle_connect_failed(io_channel*, int ec); YASIO__DECL void active_transport(transport_handle_t); + YASIO__DECL void notify_connect_succeed(transport_handle_t); YASIO__DECL transport_handle_t allocate_transport(io_channel*, xxsocket_ptr&&); YASIO__DECL void deallocate_transport(transport_handle_t);