From 1cf59a76fb54c1f55640261e198415b915338eae Mon Sep 17 00:00:00 2001 From: halx99 Date: Tue, 8 Dec 2020 19:10:58 +0800 Subject: [PATCH] Sync yasio --- external/yasio/bindings/lyasio.cpp | 15 ++++++------ external/yasio/bindings/lyasio.h | 3 +++ external/yasio/compiler/feature_test.hpp | 15 ++++++++++++ external/yasio/cxx17/shared_mutex.hpp | 2 +- external/yasio/cxx17/string_view.hpp | 7 +++--- external/yasio/detail/config.hpp | 11 --------- external/yasio/platform/yasio_ue4.hpp | 14 ++++++++---- external/yasio/yasio.cpp | 29 +++++++++++------------- external/yasio/yasio.hpp | 26 +++++++++++++++------ 9 files changed, 71 insertions(+), 51 deletions(-) diff --git a/external/yasio/bindings/lyasio.cpp b/external/yasio/bindings/lyasio.cpp index 7d2e62903f..d34b3d00b8 100644 --- a/external/yasio/bindings/lyasio.cpp +++ b/external/yasio/bindings/lyasio.cpp @@ -706,13 +706,14 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L) #endif /* YASIO__HAS_CXX17 */ extern "C" { -YASIO_LUA_API void lyasio_set_print_fn(void* inst, void (*pfn)(const char*)) +YASIO_LUA_API void luaregister_yasio(lua_State* L) { - if (inst) - { - auto service = (io_service*)inst; - print_fn_t custom_print = pfn; - service->set_option(YOPT_S_PRINT_FN, &custom_print); - } + lua_getglobal(L, "package"); + lua_getfield(L, -1, "preload"); + + lua_pushcfunction(L, luaopen_yasio); + lua_setfield(L, -2, "yasio"); + + lua_pop(L, 2); } } diff --git a/external/yasio/bindings/lyasio.h b/external/yasio/bindings/lyasio.h index 1c009eaa74..404194e517 100644 --- a/external/yasio/bindings/lyasio.h +++ b/external/yasio/bindings/lyasio.h @@ -45,8 +45,11 @@ SOFTWARE. #if defined(__cplusplus) extern "C" { #endif +#if !defined(NS_SLUA) struct lua_State; +#endif YASIO_LUA_API int luaopen_yasio(lua_State* L); +YASIO_LUA_API void luaregister_yasio(lua_State* L); // register yasio to package.preload #if defined(__cplusplus) } #endif diff --git a/external/yasio/compiler/feature_test.hpp b/external/yasio/compiler/feature_test.hpp index d056814529..7c58312861 100644 --- a/external/yasio/compiler/feature_test.hpp +++ b/external/yasio/compiler/feature_test.hpp @@ -115,6 +115,21 @@ SOFTWARE. # define YASIO__64BITS 0 #endif +// Try detect compiler exceptions +#if !defined(__cpp_exceptions) +# define YASIO__NO_EXCEPTIONS 1 +#endif + +#if !defined(YASIO__NO_EXCEPTIONS) +# define YASIO__THROW(x, retval) throw(x) +# define YASIO__THROW0(x) throw(x) +# define YASIO__THROWV(x, val) throw(x), (val) +#else +# define YASIO__THROW(x, retval) return (retval) +# define YASIO__THROW0(x) return +# define YASIO__THROWV(x, val) (val) +#endif + // Compatibility with non-clang compilers... #ifndef __has_attribute # define __has_attribute(x) 0 diff --git a/external/yasio/cxx17/shared_mutex.hpp b/external/yasio/cxx17/shared_mutex.hpp index ef8cbee056..e032f2db71 100644 --- a/external/yasio/cxx17/shared_mutex.hpp +++ b/external/yasio/cxx17/shared_mutex.hpp @@ -57,7 +57,7 @@ SOFTWARE. # define yasio__smtx_unlock_shared(rwlock) pthread_rwlock_unlock(rwlock) # define yasio__smtx_unlock_exclusive(rwlock) pthread_rwlock_unlock(rwlock) # endif -# define yaso__throw_error(e) throw std::system_error(std::make_error_code(e), "") +# define yaso__throw_error(e) YASIO__THROW0(std::system_error(std::make_error_code(e), "")) # include // CLASS TEMPLATE shared_lock diff --git a/external/yasio/cxx17/string_view.hpp b/external/yasio/cxx17/string_view.hpp index cc2c0600d1..6d69b5f72b 100644 --- a/external/yasio/cxx17/string_view.hpp +++ b/external/yasio/cxx17/string_view.hpp @@ -777,8 +777,7 @@ inline typename basic_string_view<_CharT, _Traits>::const_reference basic_string_view<_CharT, _Traits>::at(size_t pos) const { return pos < m_size ? m_str[pos] - : throw std::out_of_range("Input out of range in basic_string_view::at"), - m_str[pos]; + : YASIO__THROWV(std::out_of_range("Input out of range in basic_string_view::at"), 0); } template @@ -848,7 +847,7 @@ inline typename basic_string_view<_CharT, _Traits>::size_type basic_string_view<_CharT, _Traits>::copy(char_type* dest, size_type count, size_type pos) const { if (pos >= m_size) - throw std::out_of_range("Index out of range in basic_string_view::copy"); + YASIO__THROW(std::out_of_range("Index out of range in basic_string_view::copy"), 0); const size_type rcount = (std::min)(m_size - pos, count + 1); std::copy(m_str + pos, m_str + pos + rcount, dest); @@ -863,7 +862,7 @@ basic_string_view<_CharT, _Traits>::substr(size_t pos, size_t len) const return pos < m_size ? basic_string_view<_CharT, _Traits>(m_str + pos, len > max_length ? max_length : len) - : throw std::out_of_range("Index out of range in basic_string_view::substr"); + : YASIO__THROWV(std::out_of_range("Index out of range in basic_string_view::substr"), (basic_string_view<_CharT, _Traits>{})); } //-------------------------------------------------------------------------- diff --git a/external/yasio/detail/config.hpp b/external/yasio/detail/config.hpp index 44a2282bd0..a5a37072c5 100644 --- a/external/yasio/detail/config.hpp +++ b/external/yasio/detail/config.hpp @@ -108,11 +108,6 @@ SOFTWARE. */ // #define YASIO_MINIFY_EVENT 1 -/* -** Uncomment or add compiler flag -DYASIO_NO_EXCEPTIONS to disable exceptions -*/ -// #define YASIO_NO_EXCEPTIONS 1 - #if defined(YASIO_HEADER_ONLY) # define YASIO__DECL inline #else @@ -139,12 +134,6 @@ SOFTWARE. # define YASIO_LOGV YASIO_LOG #endif -#if !defined(YASIO_NO_EXCEPTIONS) -# define YASIO__THROW(x, retval) throw(x) -#else -# define YASIO__THROW(x, retval) return (retval) -#endif - #define YASIO_ARRAYSIZE(A) (sizeof(A) / sizeof((A)[0])) /* diff --git a/external/yasio/platform/yasio_ue4.hpp b/external/yasio/platform/yasio_ue4.hpp index 1fee5c971d..cf1937ac3c 100644 --- a/external/yasio/platform/yasio_ue4.hpp +++ b/external/yasio/platform/yasio_ue4.hpp @@ -24,15 +24,17 @@ SOFTWARE. #ifndef YASIO__UE4_HPP #define YASIO__UE4_HPP -THIRD_PARTY_INCLUDES_START -#pragma push_macro("check") -#undef check -#define YASIO_HEADER_ONLY 1 /* UE4 builtin namespace 'UI' conflicit with openssl typedef strcut st_UI UI; ossl_typ.h(143): error C2365: 'UI': redefinition; previous definition was 'namespace' */ -// #define YASIO_HAVE_SSL 1 +#define UI UI_ST + +THIRD_PARTY_INCLUDES_START +#pragma push_macro("check") +#undef check +#define YASIO_HEADER_ONLY 1 +#define YASIO_HAVE_SSL 1 #include "yasio/yasio.hpp" #include "yasio/obstream.hpp" #include "yasio/ibstream.hpp" @@ -41,4 +43,6 @@ using namespace yasio::inet; #pragma pop_macro("check") THIRD_PARTY_INCLUDES_END +#undef UI + #endif diff --git a/external/yasio/yasio.cpp b/external/yasio/yasio.cpp index d0dc7e8b1c..8cc0c199f4 100644 --- a/external/yasio/yasio.cpp +++ b/external/yasio/yasio.cpp @@ -465,9 +465,7 @@ bool io_transport::do_write(highp_time_t& wait_duration) auto& v = *wrap; if (call_write(v.get(), error) < 0) { - set_last_errno(error); - YASIO_KLOGE("[index: %d] the connection #%u will lost due to write failed, ec=%d, detail:%s", this->cindex(), this->id_, error, - io_service::strerror(error)); + this->set_last_errno(error, yasio::net::io_base::error_stage::WRITE); break; } } @@ -533,8 +531,8 @@ int io_transport::call_write(io_send_op* op, int& error) n = 0; else if (yasio__testbits(ctx_->properties_, YCM_UDP)) { // UDP: don't cause handle_close, simply drop the op - this->complete_op(op, error); - n = 0; + this->complete_op(op, error); + n = 0; } } return n; @@ -676,8 +674,8 @@ void io_transport_udp::set_primitives() if (n < 0) { auto error = xxsocket::get_last_errno(); - if (YASIO__SEND_FAIL(error)) - YASIO_KLOGI("[index: %d] write udp socket failed, ec=%d, detail:%s", this->cindex(), error, io_service::strerror(error)); + if (YASIO__SEND_FAIL(error)) + YASIO_KLOGI("[index: %d] write udp socket failed, ec=%d, detail:%s", this->cindex(), error, io_service::strerror(error)); } return n; }; @@ -1126,7 +1124,8 @@ void io_service::handle_close(transport_handle_t thandle) auto ec = thandle->error_; // @Because we can't retrive peer endpoint when connect reset by peer, so use id to trace. - YASIO_KLOGD("[index: %d] the connection #%u(%p) is lost, ec=%d, detail:%s", ctx->index_, thandle->id_, thandle, ec, io_service::strerror(ec)); + YASIO_KLOGD("[index: %d] the connection #%u(%p) is lost, ec=%d, where=%d, detail:%s", ctx->index_, thandle->id_, thandle, ec, (int)thandle->error_stage_, + io_service::strerror(ec)); // @Notify connection lost this->handle_event(event_ptr(new io_event(ctx->index_, YEK_CONNECTION_LOST, ec, thandle))); @@ -1631,7 +1630,7 @@ void io_service::do_nonblocking_accept_completion(io_channel* ctx, fd_set* fds_a { error = xxsocket::get_last_errno(); if (YASIO__RECV_FAIL(error)) - YASIO_KLOGE("[index: %d] recvfrom failed, ec=%d, detail:%s", ctx->index_, error, this->strerror(error)); + YASIO_KLOGE("[index: %d] recvfrom failed, ec=%d, detail:%s", ctx->index_, error, this->strerror(error)); } } } @@ -1701,8 +1700,8 @@ void io_service::notify_connect_succeed(transport_handle_t t) auto& s = t->socket_; 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(%p) [%s] --> [%s] is established.", ctx->index_, t->id_, t, - t->local_endpoint().to_string().c_str(), t->remote_endpoint().to_string().c_str()); + YASIO_KLOGD("[index: %d] the connection #%u(%p) [%s] --> [%s] is established.", ctx->index_, t->id_, t, t->local_endpoint().to_string().c_str(), + t->remote_endpoint().to_string().c_str()); this->handle_event(event_ptr(new io_event(ctx->index_, YEK_CONNECT_RESPONSE, 0, t))); } transport_handle_t io_service::allocate_transport(io_channel* ctx, std::shared_ptr socket) @@ -1785,11 +1784,11 @@ bool io_service::do_read(transport_handle_t transport, fd_set* fds_array) YASIO_MAX_PDU_BUFFER_SIZE)); // #perfomance, avoid memory reallocte. unpack(transport, transport->expected_size_, n, bytes_to_strip); } - else if (length == 0) // header insufficient, wait readfd ready at next event step. + else if (length == 0) // header insufficient, wait readfd ready at next event frame. transport->wpos_ += n; else { - transport->set_last_errno(yasio::errc::invalid_packet); + transport->set_last_errno(yasio::errc::invalid_packet, yasio::net::io_base::error_stage::READ); break; } } @@ -1800,9 +1799,7 @@ bool io_service::do_read(transport_handle_t transport, fd_set* fds_array) } else { // n < 0, regard as connection should close - transport->set_last_errno(error); - YASIO_KLOGE("[index: %d] the connection #%u will lost due to read failed, ec=%d, detail:%s", transport->cindex(), transport->id_, error, - io_service::strerror(error)); + transport->set_last_errno(error, yasio::net::io_base::error_stage::READ); break; } diff --git a/external/yasio/yasio.hpp b/external/yasio/yasio.hpp index 1c8e1fca93..847ec110eb 100644 --- a/external/yasio/yasio.hpp +++ b/external/yasio/yasio.hpp @@ -378,21 +378,35 @@ public: }; struct io_base { - enum class state : u_short + enum class state : uint8_t { CLOSED, OPENING, OPEN, }; + enum class error_stage : uint8_t + { + NONE, + READ, + WRITE, + }; io_base() : error_(0), state_(state::CLOSED), opmask_(0) {} virtual ~io_base() {} - void set_last_errno(int error) { error_ = error; } + void set_last_errno(int error, error_stage stage = error_stage::NONE) + { + error_ = error; + error_stage_ = stage; + } std::shared_ptr socket_; int error_; // socket error(>= -1), application error(< -1) + // 0: none, 1: read, 2: write + error_stage error_stage_ = error_stage::NONE; + // mark whether pollout event registerred. + bool pollout_registerred_ = false; std::atomic state_; - u_short opmask_; + uint8_t opmask_; }; #if defined(YASIO_HAVE_SSL) @@ -640,8 +654,6 @@ protected: privacy::concurrent_queue send_queue_; - // mark whether pollout event registerred. - bool pollout_registerred_ = false; #if !defined(YASIO_MINIFY_EVENT) private: // The user data @@ -1080,7 +1092,7 @@ private: highp_time_t dns_queries_timeout_ = 5LL * std::micro::den; int dns_queries_tries_ = 5; - bool dns_dirty_ = true; // only for c-ares + bool dns_dirty_ = true; // only for c-ares bool deferred_event_ = true; @@ -1092,7 +1104,7 @@ private: int probs = 10; } tcp_keepalive_; - bool no_new_thread_ = false; + bool no_new_thread_ = false; // The resolve function resolv_fn_t resolv_;