Sync yasio

This commit is contained in:
halx99 2020-12-08 19:10:58 +08:00
parent e273d77c96
commit 1cf59a76fb
9 changed files with 71 additions and 51 deletions

View File

@ -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);
}
}

View File

@ -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

View File

@ -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

View File

@ -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 <mutex>
// CLASS TEMPLATE shared_lock

View File

@ -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 <typename _CharT, typename _Traits>
@ -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>{}));
}
//--------------------------------------------------------------------------

View File

@ -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]))
/*

View File

@ -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

View File

@ -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;
}
}
@ -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)));
@ -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<int>(SOL_SOCKET, SO_SNDBUF), s->get_optval<int>(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<xxsocket> 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;
}

View File

@ -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<xxsocket> 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> state_;
u_short opmask_;
uint8_t opmask_;
};
#if defined(YASIO_HAVE_SSL)
@ -640,8 +654,6 @@ protected:
privacy::concurrent_queue<send_op_ptr> send_queue_;
// mark whether pollout event registerred.
bool pollout_registerred_ = false;
#if !defined(YASIO_MINIFY_EVENT)
private:
// The user data