Skip to content

Commit 98f2dad

Browse files
committed
Export transport local_endpoint and remote_endpoint to lua
1 parent e1e4709 commit 98f2dad

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

examples/lua/scripts/example.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ client:start(function(event)
105105
print('yasio - ' .. result)
106106
elseif(t == yasio.YEK_CONNECT_RESPONSE) then -- connect responseType
107107
if(event:status() == 0) then
108-
print("yasio - connect server succeed.")
108+
local t = event:transport()
109+
local local_ep = t:local_endpoint()
110+
local remote_ep = t:remote_endpoint()
111+
print(string.format("yasio - connect server succeed, local_ep=(%s:%u) remote_ep=(%s:%u) ",
112+
local_ep:ip(), local_ep:port(),
113+
remote_ep:ip(), remote_ep:port()))
109114
-- local transport = event:transport()
110115
-- local requestData = "GET /index.htm HTTP/1.1\r\nHost: www.ip138.com\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36\r\nAccept: */*;q=0.8\r\nConnection: Close\r\n\r\n"
111116
-- client:write(transport, obs)

yasio/bindings/lyasio.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L)
158158
# else
159159
auto yasio_lib = state_view.create_named_table("yasio");
160160
# endif
161+
yasio_lib.new_usertype<ip::endpoint>(
162+
"endpoint", "ip", [](const ip::endpoint& ep) { return ep.ip(); }, "port", [](const ip::endpoint& ep) { return ep.port(); });
163+
yasio_lib.new_usertype<io_transport>(
164+
"io_transport", "remote_endpoint", [](io_transport* transport) { return transport->remote_endpoint(); }, "local_endpoint",
165+
[](io_transport* transport) { return transport->local_endpoint(); });
161166
yasio_lib.new_usertype<io_event>(
162167
"io_event", "kind", &io_event::kind, "status", &io_event::status, "passive", [](io_event* e) { return !!e->passive(); }, "packet",
163168
[](io_event* ev, sol::variadic_args args, sol::this_state s) {
@@ -172,9 +177,9 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L)
172177
case lyasio::BUFFER_RAW:
173178
return sol::make_object(L, cxx17::string_view{packet_data(pkt), packet_len(pkt)});
174179
case lyasio::BUFFER_FAST:
175-
return sol::make_object(L, cxx14::make_unique<yasio::fast_ibstream>(forward_packet((packet_t &&) pkt)));
180+
return sol::make_object(L, cxx14::make_unique<yasio::fast_ibstream>(forward_packet((packet_t&&)pkt)));
176181
default:
177-
return sol::make_object(L, cxx14::make_unique<yasio::ibstream>(forward_packet((packet_t &&) pkt)));
182+
return sol::make_object(L, cxx14::make_unique<yasio::ibstream>(forward_packet((packet_t&&)pkt)));
178183
}
179184
},
180185
"cindex", &io_event::cindex, "transport", &io_event::transport
@@ -596,7 +601,7 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L)
596601
auto& pkt = ev->packet();
597602
if (is_packet_empty(pkt))
598603
return std::unique_ptr<yasio::ibstream>{};
599-
return cxx14::make_unique<yasio::ibstream>(forward_packet((packet_t &&) pkt));
604+
return cxx14::make_unique<yasio::ibstream>(forward_packet((packet_t&&)pkt));
600605
})
601606
.addStaticFunction("raw_packet",
602607
[](io_event* ev) {
@@ -610,15 +615,15 @@ YASIO_LUA_API int luaopen_yasio(lua_State* L)
610615
auto& pkt = ev->packet();
611616
if (is_packet_empty(pkt))
612617
return std::unique_ptr<yasio::fast_ibstream>{};
613-
return cxx14::make_unique<yasio::fast_ibstream>(forward_packet((packet_t &&) pkt));
618+
return cxx14::make_unique<yasio::fast_ibstream>(forward_packet((packet_t&&)pkt));
614619
})
615620
.addFunction("cindex", &io_event::cindex)
616621
.addFunction("transport", &io_event::transport)
617622
.addFunction("timestamp", &io_event::timestamp));
618623
# if !YASIO_LUA_ENABLE_GLOBAL
619624
state["yasio"] = yasio_lib;
620625
# endif
621-
bool succeed = state.dostring(R"(
626+
bool succeed = state.dostring(R"(
622627
local yasio = yasio;
623628
yasio.io_event.packet = function(self, buffer_type)
624629
if buffer_type == yasio.BUFFER_RAW then

0 commit comments

Comments
 (0)