Skip to content

Commit 672b749

Browse files
committed
整理代码
1 parent 6b433e9 commit 672b749

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

binding/lua_socket.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ namespace bee::lua_socket {
2929
char* buf;
3030
size_t len;
3131
};
32+
#else
33+
struct luabuf {
34+
luabuf(lua_State* L, size_t len) {
35+
luaL_buffinit(L, &buffer);
36+
buf = luaL_prepbuffsize(&buffer, (size_t)len);
37+
}
38+
void pushresultsize(lua_State* L, int rc) {
39+
luaL_pushresultsize(&buffer, rc);
40+
}
41+
luaL_Buffer buffer;
42+
char* buf;
43+
};
3244
#endif
3345

3446
namespace endpoint {
@@ -189,14 +201,8 @@ namespace bee::lua_socket {
189201
}
190202
static int recv(lua_State* L, net::fd_t fd) {
191203
auto len = lua::optinteger<int, LUAL_BUFFERSIZE>(L, 2);
192-
#if LUA_VERSION_NUM >= 505
193204
luabuf b(L, (size_t)len);
194205
char* buf = b.buf;
195-
#else
196-
luaL_Buffer b;
197-
luaL_buffinit(L, &b);
198-
char* buf = luaL_prepbuffsize(&b, (size_t)len);
199-
#endif
200206
int rc;
201207
switch (net::socket::recv(fd, rc, buf, len)) {
202208
case net::socket::recv_status::close:
@@ -206,11 +212,7 @@ namespace bee::lua_socket {
206212
lua_pushboolean(L, 0);
207213
return 1;
208214
case net::socket::recv_status::success:
209-
#if LUA_VERSION_NUM >= 505
210215
b.pushresultsize(L, rc);
211-
#else
212-
luaL_pushresultsize(&b, rc);
213-
#endif
214216
return 1;
215217
case net::socket::recv_status::failed:
216218
return lua::return_net_error(L, "recv");
@@ -237,22 +239,12 @@ namespace bee::lua_socket {
237239
static int recvfrom(lua_State* L, net::fd_t fd) {
238240
auto len = lua::optinteger<int, LUAL_BUFFERSIZE>(L, 2);
239241
auto& ep = lua::newudata<net::endpoint>(L);
240-
#if LUA_VERSION_NUM >= 505
241242
luabuf b(L, (size_t)len);
242243
char* buf = b.buf;
243-
#else
244-
luaL_Buffer b;
245-
luaL_buffinit(L, &b);
246-
char* buf = luaL_prepbuffsize(&b, (size_t)len);
247-
#endif
248244
int rc;
249245
switch (net::socket::recvfrom(fd, rc, ep, buf, len)) {
250246
case net::socket::status::success:
251-
#if LUA_VERSION_NUM >= 505
252247
b.pushresultsize(L, rc);
253-
#else
254-
luaL_pushresultsize(&b, rc);
255-
#endif
256248
lua_insert(L, -2);
257249
return 2;
258250
case net::socket::status::wait:

0 commit comments

Comments
 (0)