Sync yasio to 3.35.0 [ci build]

This commit is contained in:
halx99 2020-12-21 18:12:13 +08:00
parent 208d07697f
commit aded15aac7
9 changed files with 473 additions and 381 deletions

2
external/README.md vendored
View File

@ -218,7 +218,7 @@
## yasio
- Upstream: https://github.com/yasio/yasio
- Version: 3.34.1
- Version: 3.35.0
- License: MIT WITH Anti-996
## zlib

View File

@ -29,13 +29,14 @@ SOFTWARE.
#include "yasio/obstream.hpp"
#include "yasio/yasio.hpp"
#include "yasio/bindings/lyasio.h"
#include "yasio/bindings/yasio_sol.hpp"
using namespace yasio;
using namespace yasio::inet;
namespace lyasio
{
static auto obstream_write_v = [](yasio::obstream* obs, cxx17::string_view val,
int length_field_bits) {
template <typename _Stream> static void obstream_write_v(_Stream* obs, cxx17::string_view val, int length_field_bits)
{
// default: Use variant length of length field, just like .net BinaryWriter.Write(String),
// see:
// https://github.com/mono/mono/blob/master/mcs/class/referencesource/mscorlib/system/io/binarywriter.cs
@ -52,7 +53,8 @@ static auto obstream_write_v = [](yasio::obstream* obs, cxx17::string_view val,
return obs->write_v8(val);
}
};
static auto ibstream_read_v = [](yasio::ibstream* ibs, int length_field_bits) {
template <typename _Stream> static cxx17::string_view ibstream_read_v(_Stream* ibs, int length_field_bits)
{
// default: Use variant length of length field, just like .net BinaryReader.ReadString,
// see:
// https://github.com/mono/mono/blob/master/mcs/class/referencesource/mscorlib/system/io/binaryreader.cs
@ -73,40 +75,55 @@ static auto ibstream_read_v = [](yasio::ibstream* ibs, int length_field_bits) {
#if YASIO__HAS_CXX14
# if !YASIO__HAS_CXX20
# include "sol/sol.hpp"
# else
# include "sol3/sol.hpp"
namespace lyasio
{
template <typename _Stream> static void register_obstream(sol::table& lib, const char* usertype)
{
lib.new_usertype<_Stream>(
usertype, "push32", &_Stream::push32, "pop32",
sol::overload(static_cast<void (_Stream ::*)()>(&_Stream::pop32), static_cast<void (_Stream ::*)(uint32_t)>(&_Stream::pop32)), "push16", &_Stream::push16,
"pop16", sol::overload(static_cast<void (_Stream ::*)()>(&_Stream::pop16), static_cast<void (_Stream ::*)(uint16_t)>(&_Stream::pop16)), "push8",
&_Stream::push8, "pop8", sol::overload(static_cast<void (_Stream ::*)()>(&_Stream::pop8), static_cast<void (_Stream ::*)(uint8_t)>(&_Stream::pop8)),
"write_ix", &_Stream::template write_ix<int64_t>, "write_bool", &_Stream::template write<bool>, "write_i8", &_Stream::template write<int8_t>, "write_i16",
&_Stream::template write<int16_t>, "write_i32", &_Stream::template write<int32_t>, "write_i64", &_Stream::template write<int64_t>, "write_u8",
&_Stream::template write<uint8_t>, "write_u16", &_Stream::template write<uint16_t>, "write_u32", &_Stream::template write<uint32_t>, "write_u64",
&_Stream::template write<uint64_t>,
# if defined(YASIO_HAVE_HALF_FLOAT)
"write_f16", &_Stream::template write<fp16_t>,
# endif
"write_f", &_Stream::template write<float>, "write_lf", &_Stream::template write<double>, "write_v",
[](_Stream* obs, cxx17::string_view sv, sol::variadic_args args) {
int lfl = -1;
if (args.size() > 0)
lfl = static_cast<int>(args[0]);
return obstream_write_v<_Stream>(obs, sv, lfl);
},
"write_bytes", static_cast<void (_Stream::*)(cxx17::string_view)>(&_Stream::write_bytes), "length", &_Stream::length, "to_string",
[](_Stream* obs) { return cxx17::string_view(obs->data(), obs->length()); }, "save", &_Stream::save);
}
# if !YASIO__HAS_CXX17
namespace sol
template <typename _Stream, typename _StreamView, typename _OStream> static void register_ibstream(sol::table& lib, const char* usertype)
{
namespace stack
{
template <> struct pusher<cxx17::string_view>
{
static int push(lua_State* L, const cxx17::string_view& str)
{
lua_pushlstring(L, !str.empty() ? str.c_str() : "", str.length());
return 1;
}
};
template <> struct getter<cxx17::string_view>
{
static cxx17::string_view get(lua_State* L, int index, record& tracking)
{
tracking.use(1); // THIS IS THE ONLY BIT THAT CHANGES
size_t len = 0;
const char* s = lua_tolstring(L, index, &len);
return cxx17::string_view(s, len);
}
};
} // namespace stack
template <> struct lua_type_of<cxx17::string_view> : std::integral_constant<type, type::string>
{};
} // namespace sol
lib.new_usertype<_Stream>(
usertype, sol::constructors<_Stream(), _Stream(std::vector<char>), _Stream(const _OStream*)>(), "load", &_Stream::load, "read_ix",
&_Stream::template read_ix<int64_t>, "read_bool", &_Stream::template read<bool>, "read_i8", &_Stream::template read<int8_t>, "read_i16",
&_Stream::template read<int16_t>, "read_i32", &_Stream::template read<int32_t>, "read_i64", &_Stream::template read<int64_t>, "read_u8",
&_Stream::template read<uint8_t>, "read_u16", &_Stream::template read<uint16_t>, "read_u32", &_Stream::template read<uint32_t>, "read_u64",
&_Stream::template read<uint64_t>,
# if defined(YASIO_HAVE_HALF_FLOAT)
"read_f16", &_Stream::template read<fp16_t>,
# endif
"read_f", &_Stream::template read<float>, "read_lf", &_Stream::template read<double>, "read_v",
[](_Stream* ibs, sol::variadic_args args) {
int lfl = -1;
if (args.size() > 0)
lfl = static_cast<int>(args[0]);
return ibstream_read_v<_Stream>(ibs, lfl);
},
"read_bytes", static_cast<cxx17::string_view (_Stream::*)(int)>(&_Stream::read_bytes), "seek", &_StreamView::seek, "length", &_StreamView::length,
"to_string", [](_Stream* ibs) { return cxx17::string_view(ibs->data(), ibs->length()); });
}
} // namespace lyasio
extern "C" {
@ -115,18 +132,17 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L)
sol::state_view state_view(L);
# if !YASIO_LUA_ENABLE_GLOBAL
auto lyasio = state_view.create_table();
auto yasio_lib = state_view.create_table();
# else
auto lyasio = state_view.create_named_table("yasio");
auto yasio_lib = state_view.create_named_table("yasio");
# endif
lyasio.new_usertype<io_event>(
yasio_lib.new_usertype<io_event>(
"io_event", "kind", &io_event::kind, "status", &io_event::status, "packet",
[](io_event* ev, sol::variadic_args args) {
bool copy = false;
if (args.size() >= 2)
copy = args[1];
return std::unique_ptr<yasio::ibstream>(!copy ? new yasio::ibstream(std::move(ev->packet()))
: new yasio::ibstream(ev->packet()));
return std::unique_ptr<yasio::ibstream>(!copy ? new yasio::ibstream(std::move(ev->packet())) : new yasio::ibstream(ev->packet()));
},
"cindex", &io_event::cindex, "transport", &io_event::transport
# if !defined(YASIO_MINIFY_EVENT)
@ -135,36 +151,28 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L)
# endif
);
lyasio.new_usertype<io_service>(
yasio_lib.new_usertype<io_service>(
"io_service", "new",
sol::initializers(
[](io_service& uninitialized_memory) { return new (&uninitialized_memory) io_service(); },
[](io_service& uninitialized_memory, int n) {
return new (&uninitialized_memory) io_service(n);
},
[](io_service& uninitialized_memory, sol::table channel_eps) {
std::vector<io_hostent> hosts;
auto host = channel_eps["host"];
if (host.valid())
hosts.push_back(io_hostent(host.get<cxx17::string_view>(), channel_eps["port"]));
else
{
for (auto item : channel_eps)
{
auto ep = item.second.as<sol::table>();
hosts.push_back(io_hostent(ep["host"].get<cxx17::string_view>(), ep["port"]));
}
}
return new (&uninitialized_memory)
io_service(!hosts.empty() ? &hosts.front() : nullptr,
(std::max)(static_cast<int>(hosts.size()), 1));
}),
sol::meta_function::garbage_collect,
sol::destructor([](io_service& memory_from_lua) { memory_from_lua.~io_service(); }), "start",
[](io_service* service, sol::function cb) {
service->start([=](event_ptr ev) { cb(std::move(ev)); });
},
"stop", &io_service::stop, "set_option",
sol::initializers([](io_service& uninitialized_memory) { return new (&uninitialized_memory) io_service(); },
[](io_service& uninitialized_memory, int n) { return new (&uninitialized_memory) io_service(n); },
[](io_service& uninitialized_memory, sol::table channel_eps) {
std::vector<io_hostent> hosts;
auto host = channel_eps["host"];
if (host.valid())
hosts.push_back(io_hostent(host.get<cxx17::string_view>(), channel_eps["port"]));
else
{
for (auto item : channel_eps)
{
auto ep = item.second.as<sol::table>();
hosts.push_back(io_hostent(ep["host"].get<cxx17::string_view>(), ep["port"]));
}
}
return new (&uninitialized_memory)
io_service(!hosts.empty() ? &hosts.front() : nullptr, (std::max)(static_cast<int>(hosts.size()), 1));
}),
sol::meta_function::garbage_collect, sol::destructor([](io_service& memory_from_lua) { memory_from_lua.~io_service(); }), "start",
[](io_service* service, sol::function cb) { service->start([=](event_ptr ev) { cb(std::move(ev)); }); }, "stop", &io_service::stop, "set_option",
[](io_service* service, int opt, sol::variadic_args args) {
switch (opt)
{
@ -183,21 +191,16 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L)
case YOPT_C_ENABLE_MCAST:
case YOPT_C_LOCAL_ENDPOINT:
case YOPT_C_REMOTE_ENDPOINT:
service->set_option(opt, static_cast<int>(args[0]), args[1].as<const char*>(),
static_cast<int>(args[2]));
service->set_option(opt, static_cast<int>(args[0]), args[1].as<const char*>(), static_cast<int>(args[2]));
break;
case YOPT_C_MOD_FLAGS:
service->set_option(opt, static_cast<int>(args[0]),
static_cast<int>(args[1]),
static_cast<int>(args[2]));
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]), static_cast<int>(args[2]));
break;
case YOPT_S_TCP_KEEPALIVE:
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]),
static_cast<int>(args[2]));
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]), static_cast<int>(args[2]));
break;
case YOPT_C_LFBFD_PARAMS:
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]),
static_cast<int>(args[2]), static_cast<int>(args[3]),
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]), static_cast<int>(args[2]), static_cast<int>(args[3]),
static_cast<int>(args[4]));
break;
case YOPT_S_EVENT_CB:
@ -213,95 +216,39 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L)
}
},
"dispatch", &io_service::dispatch, "open", &io_service::open, "is_open",
sol::overload(
static_cast<bool (io_service::*)(int) const>(&io_service::is_open),
static_cast<bool (io_service::*)(transport_handle_t) const>(&io_service::is_open)),
sol::overload(static_cast<bool (io_service::*)(int) const>(&io_service::is_open),
static_cast<bool (io_service::*)(transport_handle_t) const>(&io_service::is_open)),
"close",
sol::overload(static_cast<void (io_service::*)(transport_handle_t)>(&io_service::close),
static_cast<void (io_service::*)(int)>(&io_service::close)),
sol::overload(static_cast<void (io_service::*)(transport_handle_t)>(&io_service::close), static_cast<void (io_service::*)(int)>(&io_service::close)),
"write",
sol::overload(
[](io_service* service, transport_handle_t transport, cxx17::string_view s) {
return service->write(transport, std::vector<char>(s.data(), s.data() + s.length()));
},
[](io_service* service, transport_handle_t transport, yasio::obstream* obs) {
return service->write(transport, std::move(obs->buffer()));
}),
[](io_service* service, transport_handle_t transport, yasio::obstream* obs) { return service->write(transport, std::move(obs->buffer())); }),
"write_to",
sol::overload(
[](io_service* service, transport_handle_t transport, cxx17::string_view s,
cxx17::string_view ip, u_short port) {
return service->write_to(transport, std::vector<char>(s.data(), s.data() + s.length()),
ip::endpoint{ip.data(), port});
[](io_service* service, transport_handle_t transport, cxx17::string_view s, cxx17::string_view ip, u_short port) {
return service->write_to(transport, std::vector<char>(s.data(), s.data() + s.length()), ip::endpoint{ip.data(), port});
},
[](io_service* service, transport_handle_t transport, yasio::obstream* obs,
cxx17::string_view ip, u_short port) {
return service->write_to(transport, std::move(obs->buffer()),
ip::endpoint{ip.data(), port});
[](io_service* service, transport_handle_t transport, yasio::obstream* obs, cxx17::string_view ip, u_short port) {
return service->write_to(transport, std::move(obs->buffer()), ip::endpoint{ip.data(), port});
}),
"native_ptr", [](io_service* service) { return (void*)service; });
// ##-- obstream
lyasio.new_usertype<yasio::obstream>(
"obstream", "push32", &yasio::obstream::push32, "pop32",
sol::overload(static_cast<void (yasio::obstream ::*)()>(&yasio::obstream::pop32),
static_cast<void (yasio::obstream ::*)(uint32_t)>(&yasio::obstream::pop32)),
"push16", &yasio::obstream::push16, "pop16",
sol::overload(static_cast<void (yasio::obstream ::*)()>(&yasio::obstream::pop16),
static_cast<void (yasio::obstream ::*)(uint16_t)>(&yasio::obstream::pop16)),
"push8", &yasio::obstream::push8, "pop8",
sol::overload(static_cast<void (yasio::obstream ::*)()>(&yasio::obstream::pop8),
static_cast<void (yasio::obstream ::*)(uint8_t)>(&yasio::obstream::pop8)),
"write_ix", &yasio::obstream::write_ix<int64_t>,
"write_bool", &yasio::obstream::write<bool>, "write_i8", &yasio::obstream::write<int8_t>,
"write_i16", &yasio::obstream::write<int16_t>,
"write_i32", &yasio::obstream::write<int32_t>, "write_i64",
&yasio::obstream::write<int64_t>, "write_u8", &yasio::obstream::write<uint8_t>,
"write_u16", &yasio::obstream::write<uint16_t>, "write_u32",
&yasio::obstream::write<uint32_t>, "write_u64", &yasio::obstream::write<uint64_t>,
"write_f", &yasio::obstream::write<float>, "write_lf", &yasio::obstream::write<double>,
"write_v",
[](yasio::obstream* obs, cxx17::string_view sv, sol::variadic_args args) {
int lfl = -1;
if (args.size() > 0)
lfl = static_cast<int>(args[0]);
return lyasio::obstream_write_v(obs, sv, lfl);
},
"write_bytes",
static_cast<void (yasio::obstream::*)(cxx17::string_view)>(&yasio::obstream::write_bytes),
"length", &yasio::obstream::length,
"to_string", [](yasio::obstream* obs) { return cxx17::string_view(obs->data(), obs->length()); },
"save", &yasio::obstream::save);
lyasio::register_obstream<obstream>(yasio_lib, "obstream");
lyasio::register_obstream<fast_obstream>(yasio_lib, "fast_obstream");
// ##-- yasio::ibstream
lyasio.new_usertype<yasio::ibstream>(
"ibstream", sol::constructors<yasio::ibstream(), yasio::ibstream(std::vector<char>),
yasio::ibstream(const yasio::obstream*)>(),
"load", &yasio::ibstream::load,
"read_ix", &yasio::ibstream::read_ix<int64_t>,
"read_bool", &yasio::ibstream::read<bool>, "read_i8", &yasio::ibstream::read<int8_t>,
"read_i16", &yasio::ibstream::read<int16_t>,
"read_i32", &yasio::ibstream::read<int32_t>, "read_i64", &yasio::ibstream::read<int64_t>,
"read_u8", &yasio::ibstream::read<uint8_t>, "read_u16", &yasio::ibstream::read<uint16_t>,
"read_u32", &yasio::ibstream::read<uint32_t>,
"read_u64", &yasio::ibstream::read<uint64_t>, "read_f", &yasio::ibstream::read<float>,
"read_lf", &yasio::ibstream::read<double>, "read_v",
[](yasio::ibstream* ibs, sol::variadic_args args) {
int lfl = -1;
if (args.size() > 0)
lfl = static_cast<int>(args[0]);
return lyasio::ibstream_read_v(ibs, lfl);
},
"read_bytes",
static_cast<cxx17::string_view (yasio::ibstream::*)(int)>(&yasio::ibstream::read_bytes),
"seek", &yasio::ibstream_view::seek, "length", &yasio::ibstream_view::length, "to_string",
[](yasio::ibstream* ibs) { return cxx17::string_view(ibs->data(), ibs->length()); });
lyasio::register_ibstream<ibstream, ibstream_view, obstream>(yasio_lib, "ibstream");
lyasio::register_ibstream<fast_ibstream, fast_ibstream_view, fast_obstream>(yasio_lib, "fast_ibstream");
lyasio["highp_clock"] = &highp_clock<steady_clock_t>;
lyasio["highp_time"] = &highp_clock<system_clock_t>;
yasio_lib["highp_clock"] = &highp_clock<steady_clock_t>;
yasio_lib["highp_time"] = &highp_clock<system_clock_t>;
// ##-- yasio enums
# define YASIO_EXPORT_ENUM(v) lyasio[# v] = v
# define YASIO_EXPORT_ENUM(v) yasio_lib[# v] = v
YASIO_EXPORT_ENUM(YCK_TCP_CLIENT);
YASIO_EXPORT_ENUM(YCK_TCP_SERVER);
YASIO_EXPORT_ENUM(YCK_UDP_CLIENT);
@ -342,7 +289,7 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L)
YASIO_EXPORT_ENUM(SEEK_SET);
YASIO_EXPORT_ENUM(SEEK_END);
return lyasio.push(); /* return 'yasio' table */
return yasio_lib.push(); /* return 'yasio' table */
}
} /* extern "C" */
@ -354,8 +301,7 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L)
namespace kaguya
{
// cxx17::string_view
template <> struct lua_type_traits<cxx17::string_view>
{
template <> struct lua_type_traits<cxx17::string_view> {
typedef cxx17::string_view get_type;
typedef cxx17::string_view push_type;
@ -374,8 +320,7 @@ template <> struct lua_type_traits<cxx17::string_view>
}
};
// std::vector<char>
template <> struct lua_type_traits<std::vector<char>>
{
template <> struct lua_type_traits<std::vector<char>> {
typedef std::vector<char> get_type;
typedef const std::vector<char>& push_type;
@ -394,20 +339,13 @@ template <> struct lua_type_traits<std::vector<char>>
}
};
// transport_handle_t
template <> struct lua_type_traits<yasio::inet::transport_handle_t>
{
template <> struct lua_type_traits<yasio::inet::transport_handle_t> {
typedef yasio::inet::transport_handle_t get_type;
typedef yasio::inet::transport_handle_t push_type;
static bool strictCheckType(lua_State* l, int index)
{
return lua_type(l, index) == LUA_TLIGHTUSERDATA;
}
static bool strictCheckType(lua_State* l, int index) { return lua_type(l, index) == LUA_TLIGHTUSERDATA; }
static bool checkType(lua_State* l, int index) { return lua_islightuserdata(l, index) != 0; }
static get_type get(lua_State* l, int index)
{
return reinterpret_cast<get_type>(lua_touserdata(l, index));
}
static get_type get(lua_State* l, int index) { return reinterpret_cast<get_type>(lua_touserdata(l, index)); }
static int push(lua_State* l, push_type s)
{
lua_pushlightuserdata(l, s);
@ -415,8 +353,7 @@ template <> struct lua_type_traits<yasio::inet::transport_handle_t>
}
};
template <> struct lua_type_traits<std::vector<yasio::inet::io_hostent>>
{
template <> struct lua_type_traits<std::vector<yasio::inet::io_hostent>> {
typedef std::vector<yasio::inet::io_hostent> get_type;
typedef std::vector<yasio::inet::io_hostent> push_type;
@ -433,9 +370,7 @@ template <> struct lua_type_traits<std::vector<yasio::inet::io_hostent>>
hosts.push_back(yasio::inet::io_hostent(host, channel_eps["port"]));
else
{
channel_eps.foreach_table<int, kaguya::LuaTable>([&](int, kaguya::LuaTable ep) {
hosts.push_back(yasio::inet::io_hostent(ep["host"], ep["port"]));
});
channel_eps.foreach_table<int, kaguya::LuaTable>([&](int, kaguya::LuaTable ep) { hosts.push_back(yasio::inet::io_hostent(ep["host"], ep["port"])); });
}
lua_pop(l, 1);
@ -448,217 +383,219 @@ template <> struct lua_type_traits<std::vector<yasio::inet::io_hostent>>
return 1;
}
};
# if defined(YASIO_HAVE_HALF_FLOAT)
template <> struct lua_type_traits<fp16_t> {
typedef fp16_t get_type;
typedef fp16_t push_type;
static bool strictCheckType(lua_State* l, int index) { return lua_type(l, index) == LUA_TNUMBER; }
static bool checkType(lua_State* l, int index) { return lua_isnumber(l, index) != 0; }
static get_type get(lua_State* l, int index) { return fp16_t{static_cast<float>(lua_tonumber(l, index))}; }
static int push(lua_State* l, push_type value)
{
lua_pushnumber(l, static_cast<float>(value));
return 1;
}
};
# endif
}; // namespace kaguya
namespace lyasio
{
# define kaguya_obstream_class(_Stream) kaguya::UserdataMetatable<_Stream>().setConstructors<_Stream(), _Stream(int)>()
# define kaguya_ibstream_view_class(_StreamView, _OStream) \
kaguya::UserdataMetatable<_StreamView>().setConstructors<_StreamView(), _StreamView(const void*, int), _StreamView(const _OStream*)>()
# define kaguya_ibstream_class(_Stream, _StreamView, _OStream) \
kaguya::UserdataMetatable<_Stream, _StreamView>().setConstructors<_Stream(std::vector<char>), _Stream(const _OStream*)>()
template <typename _Stream> static void register_obstream(kaguya::LuaTable& lib, const char* usertype, kaguya::UserdataMetatable<_Stream>& userclass)
{
lib[usertype].setClass(
userclass.addFunction("push32", &_Stream::push32)
.addOverloadedFunctions("pop32", static_cast<void (_Stream ::*)()>(&_Stream::pop32), static_cast<void (_Stream ::*)(uint32_t)>(&_Stream::pop32))
.addFunction("push16", &_Stream::push16)
.addOverloadedFunctions("pop16", static_cast<void (_Stream ::*)()>(&_Stream::pop16), static_cast<void (_Stream ::*)(uint16_t)>(&_Stream::pop16))
.addFunction("push8", &_Stream::push8)
.addOverloadedFunctions("pop8", static_cast<void (_Stream ::*)()>(&_Stream::pop8), static_cast<void (_Stream ::*)(uint8_t)>(&_Stream::pop8))
.addFunction("write_ix", &_Stream::template write_ix<int64_t>)
.addFunction("write_bool", &_Stream::template write<bool>)
.addFunction("write_i8", &_Stream::template write<int8_t>)
.addFunction("write_i16", &_Stream::template write<int16_t>)
.addFunction("write_i32", &_Stream::template write<int32_t>)
.addFunction("write_i64", &_Stream::template write<int64_t>)
.addFunction("write_u8", &_Stream::template write<uint8_t>)
.addFunction("write_u16", &_Stream::template write<uint16_t>)
.addFunction("write_u32", &_Stream::template write<uint32_t>)
.addFunction("write_u64", &_Stream::template write<uint64_t>)
# if defined(YASIO_HAVE_HALF_FLOAT)
.addFunction("write_f16", &_Stream::template write<fp16_t>)
# endif
.addFunction("write_f", &_Stream::template write<float>)
.addFunction("write_lf", &_Stream::template write<double>)
.addStaticFunction("write_v",
[](_Stream* obs, cxx17::string_view sv, kaguya::VariadicArgType args) {
int lfl = -1;
if (args.size() > 0)
lfl = static_cast<int>(args[0]);
return lyasio::obstream_write_v(obs, sv, lfl);
})
.addFunction("write_bytes", static_cast<void (_Stream::*)(cxx17::string_view)>(&_Stream::write_bytes))
.addFunction("length", &_Stream::length)
.addStaticFunction("to_string", [](_Stream* obs) { return cxx17::string_view(obs->data(), obs->length()); }));
}
template <typename _Stream, typename _StreamView, typename _OStream>
static void register_ibstream(kaguya::LuaTable& lib, const char* usertype, const char* basetype, kaguya::UserdataMetatable<_Stream, _StreamView>& userclass,
kaguya::UserdataMetatable<_StreamView>& baseclass)
{
lib[basetype].setClass(baseclass.addFunction("read_ix", &_StreamView::template read_ix<int64_t>)
.addFunction("read_bool", &_StreamView::template read<bool>)
.addFunction("read_i8", &_StreamView::template read<int8_t>)
.addFunction("read_i16", &_StreamView::template read<int16_t>)
.addFunction("read_i32", &_StreamView::template read<int32_t>)
.addFunction("read_i64", &_StreamView::template read<int64_t>)
.addFunction("read_u8", &_StreamView::template read<uint8_t>)
.addFunction("read_u16", &_StreamView::template read<uint16_t>)
.addFunction("read_u32", &_StreamView::template read<uint32_t>)
.addFunction("read_u64", &_StreamView::template read<uint64_t>)
# if defined(YASIO_HAVE_HALF_FLOAT)
.addFunction("read_f16", &_Stream::template read<fp16_t>)
# endif
.addFunction("read_f", &_StreamView::template read<float>)
.addFunction("read_lf", &_StreamView::template read<double>)
.addStaticFunction("read_v",
[](_StreamView* ibs, kaguya::VariadicArgType args) {
int length_field_bits = -1;
if (args.size() > 0)
length_field_bits = static_cast<int>(args[0]);
return lyasio::ibstream_read_v(ibs, length_field_bits);
})
.addFunction("read_bytes", static_cast<cxx17::string_view (_StreamView::*)(int)>(&_StreamView::read_bytes))
.addFunction("seek", &_StreamView::seek)
.addFunction("length", &_StreamView::length)
.addStaticFunction("to_string", [](_StreamView* ibs) { return cxx17::string_view(ibs->data(), ibs->length()); }));
// ##-- ibstream
lib[usertype].setClass(userclass);
}
} // namespace lyasio
extern "C" {
YASIO_LUA_API int luaopen_yasio(lua_State* L)
{
kaguya::State state(L);
auto lyasio = state.newTable();
auto yasio_lib = state.newTable();
# if YASIO_LUA_ENABLE_GLOBAL
state["yasio"] = lyasio;
state["yasio"] = yasio_lib;
# endif
// No any interface need export, only for holder
// lyasio["io_transport"].setClass(kaguya::UserdataMetatable<io_transport>());
// yasio_lib["io_transport"].setClass(kaguya::UserdataMetatable<io_transport>());
lyasio["io_event"].setClass(
kaguya::UserdataMetatable<io_event>()
yasio_lib["io_event"].setClass(kaguya::UserdataMetatable<io_event>()
.addFunction("kind", &io_event::kind)
.addFunction("status", &io_event::status)
.addStaticFunction("packet",
[](io_event* ev, bool /*raw*/, bool copy) {
return std::unique_ptr<yasio::ibstream>(
!copy ? new yasio::ibstream(std::move(ev->packet()))
: new yasio::ibstream(ev->packet()));
})
.addFunction("cindex", &io_event::cindex)
.addFunction("transport", &io_event::transport)
.addFunction("timestamp", &io_event::timestamp));
.addFunction("kind", &io_event::kind)
.addFunction("status", &io_event::status)
.addStaticFunction("packet",
[](io_event* ev, bool /*raw*/, bool copy) {
return std::unique_ptr<yasio::ibstream>(!copy ? new yasio::ibstream(std::move(ev->packet()))
: new yasio::ibstream(ev->packet()));
})
.addFunction("cindex", &io_event::cindex)
.addFunction("transport", &io_event::transport)
.addFunction("timestamp", &io_event::timestamp));
lyasio["io_service"].setClass(
yasio_lib["io_service"].setClass(
kaguya::UserdataMetatable<io_service>()
.setConstructors<io_service(), io_service(int),
io_service(const std::vector<io_hostent>&)>()
.setConstructors<io_service(), io_service(int), io_service(const std::vector<io_hostent>&)>()
.addStaticFunction("start",
[](io_service* service, kaguya::LuaFunction cb) {
io_event_cb_t fnwrap = [=](event_ptr e) mutable -> void {
cb(e.get());
};
io_event_cb_t fnwrap = [=](event_ptr e) mutable -> void { cb(e.get()); };
service->start(std::move(fnwrap));
})
.addFunction("stop", &io_service::stop)
.addFunction("dispatch", &io_service::dispatch)
.addFunction("open", &io_service::open)
.addOverloadedFunctions(
"is_open", static_cast<bool (io_service::*)(int) const>(&io_service::is_open),
static_cast<bool (io_service::*)(transport_handle_t) const>(&io_service::is_open))
.addOverloadedFunctions(
"close", static_cast<void (io_service::*)(transport_handle_t)>(&io_service::close),
static_cast<void (io_service::*)(int)>(&io_service::close))
.addOverloadedFunctions("is_open", static_cast<bool (io_service::*)(int) const>(&io_service::is_open),
static_cast<bool (io_service::*)(transport_handle_t) const>(&io_service::is_open))
.addOverloadedFunctions("close", static_cast<void (io_service::*)(transport_handle_t)>(&io_service::close),
static_cast<void (io_service::*)(int)>(&io_service::close))
.addOverloadedFunctions(
"write",
[](io_service* service, transport_handle_t transport, cxx17::string_view s) {
return service->write(transport,
std::vector<char>(s.data(), s.data() + s.length()));
return service->write(transport, std::vector<char>(s.data(), s.data() + s.length()));
},
[](io_service* service, transport_handle_t transport, yasio::obstream* obs) {
return service->write(transport, std::move(obs->buffer()));
})
[](io_service* service, transport_handle_t transport, yasio::obstream* obs) { return service->write(transport, std::move(obs->buffer())); })
.addOverloadedFunctions(
"write_to",
[](io_service* service, transport_handle_t transport, cxx17::string_view s,
cxx17::string_view ip, u_short port) {
return service->write_to(transport,
std::vector<char>(s.data(), s.data() + s.length()),
ip::endpoint{ip.data(), port});
[](io_service* service, transport_handle_t transport, cxx17::string_view s, cxx17::string_view ip, u_short port) {
return service->write_to(transport, std::vector<char>(s.data(), s.data() + s.length()), ip::endpoint{ip.data(), port});
},
[](io_service* service, transport_handle_t transport, yasio::obstream* obs,
cxx17::string_view ip, u_short port) {
return service->write_to(transport, std::move(obs->buffer()),
ip::endpoint{ip.data(), port});
[](io_service* service, transport_handle_t transport, yasio::obstream* obs, cxx17::string_view ip, u_short port) {
return service->write_to(transport, std::move(obs->buffer()), ip::endpoint{ip.data(), port});
})
.addStaticFunction(
"set_option",
[](io_service* service, int opt, kaguya::VariadicArgType args) {
switch (opt)
{
case YOPT_C_LOCAL_HOST:
case YOPT_C_REMOTE_HOST:
service->set_option(opt, static_cast<int>(args[0]),
static_cast<const char*>(args[1]));
break;
.addStaticFunction("set_option",
[](io_service* service, int opt, kaguya::VariadicArgType args) {
switch (opt)
{
case YOPT_C_LOCAL_HOST:
case YOPT_C_REMOTE_HOST:
service->set_option(opt, static_cast<int>(args[0]), static_cast<const char*>(args[1]));
break;
# if YASIO_VERSION_NUM >= 0x033100
case YOPT_C_LFBFD_IBTS:
case YOPT_C_LFBFD_IBTS:
# endif
case YOPT_C_LOCAL_PORT:
case YOPT_C_REMOTE_PORT:
case YOPT_C_KCP_CONV:
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]));
break;
case YOPT_C_ENABLE_MCAST:
case YOPT_C_LOCAL_ENDPOINT:
case YOPT_C_REMOTE_ENDPOINT:
service->set_option(opt, static_cast<int>(args[0]),
static_cast<const char*>(args[1]),
static_cast<int>(args[2]));
break;
case YOPT_C_MOD_FLAGS:
service->set_option(opt, static_cast<int>(args[0]),
static_cast<int>(args[1]),
static_cast<int>(args[2]));
break;
case YOPT_S_TCP_KEEPALIVE:
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]),
static_cast<int>(args[2]));
break;
case YOPT_C_LFBFD_PARAMS:
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]),
static_cast<int>(args[2]), static_cast<int>(args[3]),
static_cast<int>(args[4]));
break;
case YOPT_S_EVENT_CB:
(void)0;
{
kaguya::LuaFunction fn = args[0];
io_event_cb_t fnwrap = [=](event_ptr e) mutable -> void { fn(e.get()); };
service->set_option(opt, std::addressof(fnwrap));
}
break;
default:
service->set_option(opt, static_cast<int>(args[0]));
}
})
case YOPT_C_LOCAL_PORT:
case YOPT_C_REMOTE_PORT:
case YOPT_C_KCP_CONV:
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]));
break;
case YOPT_C_ENABLE_MCAST:
case YOPT_C_LOCAL_ENDPOINT:
case YOPT_C_REMOTE_ENDPOINT:
service->set_option(opt, static_cast<int>(args[0]), static_cast<const char*>(args[1]), static_cast<int>(args[2]));
break;
case YOPT_C_MOD_FLAGS:
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]), static_cast<int>(args[2]));
break;
case YOPT_S_TCP_KEEPALIVE:
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]), static_cast<int>(args[2]));
break;
case YOPT_C_LFBFD_PARAMS:
service->set_option(opt, static_cast<int>(args[0]), static_cast<int>(args[1]), static_cast<int>(args[2]),
static_cast<int>(args[3]), static_cast<int>(args[4]));
break;
case YOPT_S_EVENT_CB:
(void)0;
{
kaguya::LuaFunction fn = args[0];
io_event_cb_t fnwrap = [=](event_ptr e) mutable -> void { fn(e.get()); };
service->set_option(opt, std::addressof(fnwrap));
}
break;
default:
service->set_option(opt, static_cast<int>(args[0]));
}
})
.addStaticFunction("native_ptr", [](io_service* service) { return (void*)service; }));
// ##-- yasio::obstream
lyasio["obstream"].setClass(
kaguya::UserdataMetatable<yasio::obstream>()
.setConstructors<yasio::obstream(), yasio::obstream(int)>()
.addFunction("push32", &yasio::obstream::push32)
.addOverloadedFunctions(
"pop32", static_cast<void (yasio::obstream ::*)()>(&yasio::obstream::pop32),
static_cast<void (yasio::obstream ::*)(uint32_t)>(&yasio::obstream::pop32))
.addFunction("push16", &yasio::obstream::push16)
.addOverloadedFunctions(
"pop16", static_cast<void (yasio::obstream ::*)()>(&yasio::obstream::pop16),
static_cast<void (yasio::obstream ::*)(uint16_t)>(&yasio::obstream::pop16))
.addFunction("push8", &yasio::obstream::push8)
.addOverloadedFunctions(
"pop8", static_cast<void (yasio::obstream ::*)()>(&yasio::obstream::pop8),
static_cast<void (yasio::obstream ::*)(uint8_t)>(&yasio::obstream::pop8))
.addFunction("write_ix", &yasio::obstream::write_ix<int64_t>)
.addFunction("write_bool", &yasio::obstream::write<bool>)
.addFunction("write_i8", &yasio::obstream::write<int8_t>)
.addFunction("write_i16", &yasio::obstream::write<int16_t>)
.addFunction("write_i32", &yasio::obstream::write<int32_t>)
.addFunction("write_i64", &yasio::obstream::write<int64_t>)
.addFunction("write_u8", &yasio::obstream::write<uint8_t>)
.addFunction("write_u16", &yasio::obstream::write<uint16_t>)
.addFunction("write_u32", &yasio::obstream::write<uint32_t>)
.addFunction("write_u64", &yasio::obstream::write<uint64_t>)
.addFunction("write_f", &yasio::obstream::write<float>)
.addFunction("write_lf", &yasio::obstream::write<double>)
.addStaticFunction(
"write_v",
[](yasio::obstream* obs, cxx17::string_view sv, kaguya::VariadicArgType args) {
int lfl = -1;
if (args.size() > 0)
lfl = static_cast<int>(args[0]);
return lyasio::obstream_write_v(obs, sv, lfl);
})
.addFunction("write_bytes", static_cast<void (yasio::obstream::*)(cxx17::string_view)>(
&yasio::obstream::write_bytes))
.addFunction("length", &yasio::obstream::length)
.addStaticFunction("to_string", [](yasio::obstream* obs) {
return cxx17::string_view(obs->data(), obs->length());
}));
// ##-- obstream
lyasio::register_obstream<obstream>(yasio_lib, "obstream", kaguya_obstream_class(obstream));
lyasio::register_obstream<fast_obstream>(yasio_lib, "fast_obstream", kaguya_obstream_class(fast_obstream));
// ##-- yasio::ibstream_view
lyasio["ibstream_view"].setClass(
kaguya::UserdataMetatable<yasio::ibstream_view>()
.setConstructors<yasio::ibstream_view(), yasio::ibstream_view(const void*, int),
yasio::ibstream_view(const yasio::obstream*)>()
.addFunction("read_ix", &yasio::ibstream_view::read_ix<int64_t>)
.addFunction("read_bool", &yasio::ibstream_view::read<bool>)
.addFunction("read_i8", &yasio::ibstream_view::read<int8_t>)
.addFunction("read_i16", &yasio::ibstream_view::read<int16_t>)
.addFunction("read_i32", &yasio::ibstream_view::read<int32_t>)
.addFunction("read_i64", &yasio::ibstream_view::read<int64_t>)
.addFunction("read_u8", &yasio::ibstream_view::read<uint8_t>)
.addFunction("read_u16", &yasio::ibstream_view::read<uint16_t>)
.addFunction("read_u32", &yasio::ibstream_view::read<uint32_t>)
.addFunction("read_u64", &yasio::ibstream_view::read<uint64_t>)
.addFunction("read_f", &yasio::ibstream_view::read<float>)
.addFunction("read_lf", &yasio::ibstream_view::read<double>)
.addStaticFunction("read_v",
[](yasio::ibstream* ibs, kaguya::VariadicArgType args) {
int length_field_bits = -1;
if (args.size() > 0)
length_field_bits = static_cast<int>(args[0]);
return lyasio::ibstream_read_v(ibs, length_field_bits);
})
.addFunction("read_bytes", static_cast<cxx17::string_view (yasio::ibstream_view::*)(int)>(
&yasio::ibstream_view::read_bytes))
.addFunction("seek", &yasio::ibstream_view::seek)
.addFunction("length", &yasio::ibstream_view::length)
.addStaticFunction("to_string", [](yasio::ibstream_view* ibs) {
return cxx17::string_view(ibs->data(), ibs->length());
}));
// ##-- yasio::ibstream
lyasio::register_ibstream<ibstream, ibstream_view, obstream>(yasio_lib, "ibstream", "ibstream_view", kaguya_ibstream_class(ibstream, ibstream_view, obstream),
kaguya_ibstream_view_class(ibstream_view, obstream));
lyasio::register_ibstream<fast_ibstream, fast_ibstream_view, fast_obstream>(yasio_lib, "fast_ibstream", "fast_ibstream_view",
kaguya_ibstream_class(fast_ibstream, fast_ibstream_view, fast_obstream),
kaguya_ibstream_view_class(fast_ibstream_view, fast_obstream));
// ##-- ibstream
lyasio["ibstream"].setClass(kaguya::UserdataMetatable<yasio::ibstream, yasio::ibstream_view>()
.setConstructors<yasio::ibstream(std::vector<char>),
yasio::ibstream(const yasio::obstream*)>());
lyasio.setField("highp_clock", &highp_clock<steady_clock_t>);
lyasio.setField("highp_time", &highp_clock<system_clock_t>);
yasio_lib.setField("highp_clock", &highp_clock<steady_clock_t>);
yasio_lib.setField("highp_time", &highp_clock<system_clock_t>);
// ##-- yasio enums
# define YASIO_EXPORT_ENUM(v) lyasio[# v] = v
# define YASIO_EXPORT_ENUM(v) yasio_lib[# v] = v
YASIO_EXPORT_ENUM(YCK_TCP_CLIENT);
YASIO_EXPORT_ENUM(YCK_TCP_SERVER);
YASIO_EXPORT_ENUM(YCK_UDP_CLIENT);
@ -699,7 +636,7 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L)
YASIO_EXPORT_ENUM(SEEK_SET);
YASIO_EXPORT_ENUM(SEEK_END);
return lyasio.push(); /* return 'yasio' table */
return yasio_lib.push(); /* return 'yasio' table */
}
} /* extern "C" */

90
external/yasio/bindings/yasio_sol.hpp vendored Normal file
View File

@ -0,0 +1,90 @@
//////////////////////////////////////////////////////////////////////////////////////////
// A cross platform socket APIs, support ios & android & wp8 & window store
// universal app
//////////////////////////////////////////////////////////////////////////////////////////
/*
The MIT License (MIT)
Copyright (c) 2012-2020 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.
*/
#ifndef YASIO__SOL_HPP
#define YASIO__SOL_HPP
#include "yasio/detail/fp16.hpp"
#if YASIO__HAS_CXX14
# if !YASIO__HAS_CXX20
# include "sol/sol.hpp"
# else
# include "sol3/sol.hpp"
# endif
# if !YASIO__HAS_CXX17
namespace sol
{
namespace stack
{
template <> struct pusher<cxx17::string_view> {
static int push(lua_State* L, const cxx17::string_view& str)
{
lua_pushlstring(L, !str.empty() ? str.c_str() : "", str.length());
return 1;
}
};
template <> struct getter<cxx17::string_view> {
static cxx17::string_view get(lua_State* L, int index, record& tracking)
{
tracking.use(1); // THIS IS THE ONLY BIT THAT CHANGES
size_t len = 0;
const char* s = lua_tolstring(L, index, &len);
return cxx17::string_view(s, len);
}
};
} // namespace stack
template <> struct lua_type_of<cxx17::string_view> : std::integral_constant<type, type::string> {};
} // namespace sol
# endif
namespace sol
{
namespace stack
{
# if defined(YASIO_HAVE_HALF_FLOAT)
template <> struct pusher<fp16_t> {
static int push(lua_State* L, const fp16_t& value)
{
lua_pushnumber(L, static_cast<float>(value));
return 1;
}
};
template <> struct getter<fp16_t> {
static fp16_t get(lua_State* L, int index, record& tracking)
{
tracking.use(1); // THIS IS THE ONLY BIT THAT CHANGES
return fp16_t{static_cast<float>(lua_tonumber(L, index))};
}
};
# endif
} // namespace stack
# if defined(YASIO_HAVE_HALF_FLOAT)
template <> struct lua_type_of<fp16_t> : std::integral_constant<type, type::number> {};
# endif
} // namespace sol
#endif
#endif

View File

@ -40,14 +40,7 @@ SOFTWARE.
# include <arpa/inet.h>
#endif
#include "yasio/detail/config.hpp"
// IEEE 754 16-bit half-precision floating-point
#if defined(YASIO_HAVE_HALF_FLOAT)
# include "half/half.hpp"
typedef half_float::half fp16_t;
# define YASIO__SWAP_SHORT(s) ((((s) >> 8) & 0x00ff) | (((s) << 8) & 0xff00))
#endif
#include "yasio/detail/fp16.hpp"
#if !defined(_MSC_VER) || (defined(_MSC_VER) && _MSC_VER < 1800) || (NTDDI_VERSION <= 0x06010000 && !defined(WINRT))

35
external/yasio/detail/fp16.hpp vendored Normal file
View File

@ -0,0 +1,35 @@
//////////////////////////////////////////////////////////////////////////////////////////
// A multi-platform support c++11 library with focus on asio (asynchronous socket I/O)
// for any client application, support windows & linux & apple & android & win10-universal.
//////////////////////////////////////////////////////////////////////////////////////////
/*
The MIT License (MIT)
Copyright (c) 2012-2020 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.
*/
#ifndef YASIO__FP16_HPP
#define YASIO__FP16_HPP
#include "yasio/detail/config.hpp"
#if defined(YASIO_HAVE_HALF_FLOAT)
// Includes IEEE 754 16-bit half-precision floating-point library
# include "half/half.hpp"
typedef half_float::half fp16_t;
# define YASIO__SWAP_SHORT(s) ((((s) >> 8) & 0x00ff) | (((s) << 8) & 0xff00))
#endif
#endif

View File

@ -114,13 +114,13 @@ template <typename _Stream> struct read_ix_helper<_Stream, int64_t> {
};
} // namespace detail
template <typename _ConvertTraits> class basic_ibstream_view {
template <typename _Traits> class basic_ibstream_view {
public:
using traits_type = _ConvertTraits;
using my_type = basic_ibstream_view<_ConvertTraits>;
using convert_traits_type = _Traits;
using this_type = basic_ibstream_view<_Traits>;
basic_ibstream_view() { this->reset("", 0); }
basic_ibstream_view(const void* data, size_t size) { this->reset(data, size); }
basic_ibstream_view(const obstream* obs) { this->reset(obs->data(), obs->length()); }
basic_ibstream_view(const basic_obstream<_Traits>* obs) { this->reset(obs->data(), obs->length()); }
basic_ibstream_view(const basic_ibstream_view&) = delete;
basic_ibstream_view(basic_ibstream_view&&) = delete;
@ -138,7 +138,7 @@ public:
/* read 7bit encoded variant integer value
** @dotnet BinaryReader.Read7BitEncodedInt(64)
*/
template <typename _Intty> _Intty read_ix() { return detail::read_ix_helper<my_type, _Intty>::read_ix(this); }
template <typename _Intty> _Intty read_ix() { return detail::read_ix_helper<this_type, _Intty>::read_ix(this); }
/* read blob data with '7bit encoded int' length field */
cxx17::string_view read_v()
@ -205,7 +205,7 @@ public:
{
_Nty value;
::memcpy(&value, ptr, sizeof(value));
return traits_type::template from<_Nty>(value);
return convert_traits_type::template from<_Nty>(value);
}
template <typename _LenT> inline cxx17::string_view read_v_fx()
@ -236,14 +236,11 @@ protected:
};
/// --------------------- CLASS ibstream ---------------------
template <typename _ConvertTraits> class basic_ibstream : public basic_ibstream_view<_ConvertTraits> {
template <typename _Traits> class basic_ibstream : public basic_ibstream_view<_Traits> {
public:
basic_ibstream() {}
basic_ibstream(std::vector<char> blob) : basic_ibstream_view<_ConvertTraits>(), blob_(std::move(blob))
{
this->reset(blob_.data(), static_cast<int>(blob_.size()));
}
basic_ibstream(const obstream* obs) : basic_ibstream_view<_ConvertTraits>(), blob_(obs->buffer())
basic_ibstream(std::vector<char> blob) : basic_ibstream_view<_Traits>(), blob_(std::move(blob)) { this->reset(blob_.data(), static_cast<int>(blob_.size())); }
basic_ibstream(const basic_obstream<_Traits>* obs) : basic_ibstream_view<_Traits>(), blob_(obs->buffer())
{
this->reset(blob_.data(), static_cast<int>(blob_.size()));
}

View File

@ -35,9 +35,35 @@ SOFTWARE.
#include "yasio/detail/endian_portable.hpp"
namespace yasio
{
template <typename _ConvertTraits> class basic_obstream {
namespace detail
{
template <typename _Stream, typename _Intty> inline void write_ix_impl(_Stream* stream, _Intty value)
{
// Write out an int 7 bits at a time. The high bit of the byte,
// when on, tells reader to continue reading more bytes.
auto v = (typename std::make_unsigned<_Intty>::type)value; // support negative numbers
while (v >= 0x80)
{
stream->write_byte((uint8_t)((uint32_t)v | 0x80));
v >>= 7;
}
stream->write_byte((uint8_t)v);
}
template <typename _Stream, typename _Intty> struct write_ix_helper {};
template <typename _Stream> struct write_ix_helper<_Stream, int32_t> {
static void write_ix(_Stream* stream, int32_t value) { write_ix_impl<_Stream, int32_t>(stream, value); }
};
template <typename _Stream> struct write_ix_helper<_Stream, int64_t> {
static void write_ix(_Stream* stream, int64_t value) { write_ix_impl<_Stream, int64_t>(stream, value); }
};
} // namespace detail
template <typename _Traits> class basic_obstream {
public:
using traits_type = _ConvertTraits;
using convert_traits_type = _Traits;
using this_type = basic_obstream<_Traits>;
basic_obstream(size_t capacity = 128) { buffer_.reserve(capacity); }
basic_obstream(const basic_obstream& rhs) : buffer_(rhs.buffer_) {}
@ -147,30 +173,16 @@ public:
template <typename _Nty> inline void write(_Nty value)
{
auto nv = traits_type::template to<_Nty>(value);
auto nv = convert_traits_type::template to<_Nty>(value);
write_bytes(&nv, sizeof(nv));
}
/* write 7bit encoded variant integer value
** @dotnet BinaryWriter.Write7BitEncodedInt(64)
*/
template <typename _Intty> inline void write_ix(_Intty value)
{
// Write out an int 7 bits at a time. The high bit of the byte,
// when on, tells reader to continue reading more bytes.
auto v = (typename std::make_unsigned<_Intty>::type)value; // support negative numbers
while (v >= 0x80)
{
write_byte((uint8_t)((uint32_t)v | 0x80));
v >>= 7;
}
write_byte((uint8_t)v);
}
template <typename _Intty> void write_ix(_Intty value) { detail::write_ix_helper<this_type, _Intty>::write_ix(this, value); }
template <typename _Nty> inline void pwrite(ptrdiff_t offset, const _Nty value) { swrite(this->data() + offset, value); }
template <typename _Nty> static void swrite(void* ptr, const _Nty value)
{
auto nv = traits_type::template to<_Nty>(value);
auto nv = convert_traits_type::template to<_Nty>(value);
::memcpy(ptr, &nv, sizeof(nv));
}
@ -205,6 +217,19 @@ private:
write_bytes(value.data(), size);
}
template <typename _Intty> inline void write_ix_impl(_Intty value)
{
// Write out an int 7 bits at a time. The high bit of the byte,
// when on, tells reader to continue reading more bytes.
auto v = (typename std::make_unsigned<_Intty>::type)value; // support negative numbers
while (v >= 0x80)
{
write_byte((uint8_t)((uint32_t)v | 0x80));
v >>= 7;
}
write_byte((uint8_t)v);
}
protected:
std::vector<char> buffer_;
std::stack<size_t> offset_stack_;

View File

@ -1193,7 +1193,11 @@ int io_service::write_to(transport_handle_t transport, std::vector<char> buffer,
void io_service::handle_event(event_ptr event)
{
if (options_.deferred_event_)
{
if (options_.on_defer_event_ && !options_.on_defer_event_(event))
return;
events_.emplace(std::move(event));
}
else
options_.on_event_(std::move(event));
}
@ -2199,6 +2203,9 @@ void io_service::set_option_internal(int opt, va_list ap) // lgtm [cpp/poorly-do
case YOPT_S_EVENT_CB:
options_.on_event_ = *va_arg(ap, event_cb_t*);
break;
case YOPT_S_DEFER_EVENT_CB:
options_.on_defer_event_ = *va_arg(ap, defer_event_cb_t*);
break;
case YOPT_C_LFBFD_FN: {
auto channel = channel_at(static_cast<size_t>(va_arg(ap, int)));
if (channel)

View File

@ -81,7 +81,7 @@ namespace inet
enum
{ // lgtm [cpp/irregular-enum-init]
// Set with deferred dispatch event.
// Set whether deferred dispatch event.
// params: deferred_event:int(1)
YOPT_S_DEFERRED_EVENT = 1,
@ -102,8 +102,14 @@ enum
// Set event callback
// params: func:event_cb_t*
// remarks: this callback will be invoke at io_service::dispatch caller thread
YOPT_S_EVENT_CB,
// Sets callback before defer dispatch event.
// params: func:defer_event_cb_t*
// remarks: this callback invoke at io_service thread
YOPT_S_DEFER_EVENT_CB,
// Set tcp keepalive in seconds, probes is tries.
// params: idle:int(7200), interal:int(75), probes:int(10)
YOPT_S_TCP_KEEPALIVE,
@ -304,6 +310,7 @@ typedef std::shared_ptr<highp_timer> highp_timer_ptr;
typedef std::function<bool()> timer_cb_t;
typedef std::function<void()> light_timer_cb_t;
typedef std::function<void(event_ptr&&)> event_cb_t;
typedef std::function<bool(event_ptr&)> defer_event_cb_t;
typedef std::function<void(int, size_t)> completion_cb_t;
typedef std::function<int(void* ptr, int len)> decode_len_fn_t;
typedef std::function<int(std::vector<ip::endpoint>&, const char*, unsigned short)> resolv_fn_t;
@ -1096,6 +1103,7 @@ private:
bool dns_dirty_ = true; // only for c-ares
bool deferred_event_ = true;
defer_event_cb_t on_defer_event_;
// tcp keepalive settings
struct __unnamed01 {