aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/net
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/modules/net')
-rw-r--r--src/lua/modules/net/je_lua_buffer.cpp199
-rw-r--r--src/lua/modules/net/je_lua_net.cpp15
-rw-r--r--src/lua/modules/net/je_lua_socket.cpp23
3 files changed, 115 insertions, 122 deletions
diff --git a/src/lua/modules/net/je_lua_buffer.cpp b/src/lua/modules/net/je_lua_buffer.cpp
index 974c935..751de05 100644
--- a/src/lua/modules/net/je_lua_buffer.cpp
+++ b/src/lua/modules/net/je_lua_buffer.cpp
@@ -8,117 +8,116 @@ namespace JinEngine
{
namespace Lua
{
- namespace Net
- {
+ typedef Shared<Net::Buffer>& SharedBuffer;
- typedef Shared<Buffer>& SharedBuffer;
+ static inline SharedBuffer checkNetBuffer(lua_State* L)
+ {
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER);
+ return proxy->getShared<Net::Buffer>();
+ }
- static inline SharedBuffer checkNetBuffer(lua_State* L)
+ // net.Buffer:append(value) -> value_length
+ LUA_IMPLEMENT int l_append(lua_State* L)
+ {
+ SharedBuffer shared = checkNetBuffer(L);
+ const int vp = 2;
+ if (luax_isintegerstrict(L, vp))
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER);
- return proxy->getShared<Buffer>();
- }
-
- // net.Buffer:append(value) -> value_length
- LUA_IMPLEMENT int l_append(lua_State* L)
+ int n = luax_checkinteger(L, vp);
+ int size = sizeof(n);
+ shared->append(&n, size);
+ luax_pushinteger(L, size);
+ return 1;
+ }
+ else if (luax_isfloatstrict(L, vp))
{
- SharedBuffer shared = checkNetBuffer(L);
- const int vp = 2;
- if (luax_isintegerstrict(L, vp))
- {
- int n = luax_checkinteger(L, vp);
- int size = sizeof(n);
- shared->append(&n, size);
- luax_pushinteger(L, size);
- return 1;
- }
- else if (luax_isfloatstrict(L, vp))
- {
- float n = luax_checknumber(L, vp);
- int size = sizeof(n);
- shared->append(&n, size);
- luax_pushinteger(L, size);
- return 1;
- }
- else if (luax_isbooleanstrict(L, vp))
- {
- bool n = luax_checkbool(L, vp);
- int size = sizeof(n);
- shared->append(&n, size);
- luax_pushinteger(L, size);
- return 1;
- }
- else if (luax_isstringstrict(L, vp))
- {
- const char* str = luax_checkstring(L, vp);
- int size = strlen(str) + 1;
- shared->append(str, size);
- luax_pushinteger(L, size);
- return 1;
- }
- else
- {
- luax_typerror(L, vp, "number, bool or string");
- return 0;
- }
+ float n = luax_checknumber(L, vp);
+ int size = sizeof(n);
+ shared->append(&n, size);
+ luax_pushinteger(L, size);
+ return 1;
}
-
- // net.Buffer:grabString(offset) -> string, length
- LUA_IMPLEMENT int l_grabString(lua_State* L)
+ else if (luax_isbooleanstrict(L, vp))
{
- SharedBuffer shared = checkNetBuffer(L);
- int offset = luax_checkinteger(L, 2);
- unsigned int len;
- char* data = shared->grabString(&len, offset);
- Array<char> str;
- str.bind(data, len);
- luax_pushstring(L, &str);
- luax_pushinteger(L, str.count());
- return 2;
+ bool n = luax_checkbool(L, vp);
+ int size = sizeof(n);
+ shared->append(&n, size);
+ luax_pushinteger(L, size);
+ return 1;
}
-
- // net.Buffer:grabInteger(offset) -> integer, length
- LUA_IMPLEMENT int l_grabInteger(lua_State* L)
+ else if (luax_isstringstrict(L, vp))
{
- SharedBuffer shared = checkNetBuffer(L);
- int offset = luax_checkinteger(L, 2);
- int len;
- int integer = shared->grabInteger(&len, offset);
- luax_pushinteger(L, integer);
- luax_pushinteger(L, len);
- return 2;
+ const char* str = luax_checkstring(L, vp);
+ int size = strlen(str) + 1;
+ shared->append(str, size);
+ luax_pushinteger(L, size);
+ return 1;
}
-
- LUA_IMPLEMENT int l_grabFloat(lua_State* L)
+ else
{
- SharedBuffer shared = checkNetBuffer(L);
- int offset = luax_checkinteger(L, 2);
- int len;
- float floatv = shared->grabFloat(&len, offset);
- luax_pushnumber(L, floatv);
- luax_pushinteger(L, len);
- return 2;
+ luax_typerror(L, vp, "number, bool or string");
+ return 0;
}
+ }
- LUA_IMPLEMENT int l_grabBoolean(lua_State* L)
- {
- SharedBuffer shared = checkNetBuffer(L);
- int offset = luax_checkinteger(L, 2);
- int len;
- bool boolean = shared->grabBoolean(&len, offset);
- luax_pushboolean(L, boolean);
- luax_pushinteger(L, len);
- return 2;
- }
+ // net.Buffer:grabString(offset) -> string, length
+ LUA_IMPLEMENT int l_grabString(lua_State* L)
+ {
+ SharedBuffer shared = checkNetBuffer(L);
+ int offset = luax_checkinteger(L, 2);
+ unsigned int len;
+ char* data = shared->grabString(&len, offset);
+ Array<char> str;
+ str.bind(data, len);
+ luax_pushstring(L, &str);
+ luax_pushinteger(L, str.count());
+ return 2;
+ }
- LUA_IMPLEMENT int l_gc(lua_State* L)
- {
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER);
- proxy->release();
- return 0;
- }
+ // net.Buffer:grabInteger(offset) -> integer, length
+ LUA_IMPLEMENT int l_grabInteger(lua_State* L)
+ {
+ SharedBuffer shared = checkNetBuffer(L);
+ int offset = luax_checkinteger(L, 2);
+ int len;
+ int integer = shared->grabInteger(&len, offset);
+ luax_pushinteger(L, integer);
+ luax_pushinteger(L, len);
+ return 2;
+ }
+
+ LUA_IMPLEMENT int l_grabFloat(lua_State* L)
+ {
+ SharedBuffer shared = checkNetBuffer(L);
+ int offset = luax_checkinteger(L, 2);
+ int len;
+ float floatv = shared->grabFloat(&len, offset);
+ luax_pushnumber(L, floatv);
+ luax_pushinteger(L, len);
+ return 2;
+ }
- LUA_IMPLEMENT const luaL_Reg netbuffer_function[] = {
+ LUA_IMPLEMENT int l_grabBoolean(lua_State* L)
+ {
+ SharedBuffer shared = checkNetBuffer(L);
+ int offset = luax_checkinteger(L, 2);
+ int len;
+ bool boolean = shared->grabBoolean(&len, offset);
+ luax_pushboolean(L, boolean);
+ luax_pushinteger(L, len);
+ return 2;
+ }
+
+ LUA_IMPLEMENT int l_gc(lua_State* L)
+ {
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER);
+ proxy->release();
+ return 0;
+ }
+
+ LUA_EXPORT int luaopen_Buffer(lua_State* L)
+ {
+ luaL_Reg netbuffer_function[] = {
{ "__gc", l_gc },
{ "append", l_append },
{ "grabString", l_grabString },
@@ -128,11 +127,7 @@ namespace JinEngine
{ 0, 0 }
};
- } // namespace Net
-
- LUA_EXPORT int luaopen_Buffer(lua_State* L)
- {
- luax_newtype(L, JIN_NETWORK_BUFFER, Net::netbuffer_function);
+ luax_newtype(L, JIN_NETWORK_BUFFER, netbuffer_function);
return 0;
}
diff --git a/src/lua/modules/net/je_lua_net.cpp b/src/lua/modules/net/je_lua_net.cpp
index 58ece43..4240903 100644
--- a/src/lua/modules/net/je_lua_net.cpp
+++ b/src/lua/modules/net/je_lua_net.cpp
@@ -62,13 +62,6 @@ namespace Lua
return 1;
}
- LUA_IMPLEMENT const luaL_Reg f[] = {
- { "init", l_initNetwork },
- { "newSocket", l_Socket },
- { "newBuffer", l_Buffer },
- { 0, 0 }
- };
-
LUA_PORT int luaopen_Socket(lua_State* L);
LUA_PORT int luaopen_Buffer(lua_State* L);
@@ -76,7 +69,13 @@ namespace Lua
{
luaopen_Socket(L);
luaopen_Buffer(L);
-
+
+ luaL_Reg f[] = {
+ { "init", l_initNetwork },
+ { "newSocket", l_Socket },
+ { "newBuffer", l_Buffer },
+ { 0, 0 }
+ };
luax_newlib(L, f);
return 1;
diff --git a/src/lua/modules/net/je_lua_socket.cpp b/src/lua/modules/net/je_lua_socket.cpp
index 4072921..a51f5f3 100644
--- a/src/lua/modules/net/je_lua_socket.cpp
+++ b/src/lua/modules/net/je_lua_socket.cpp
@@ -107,20 +107,19 @@ namespace JinEngine
return 0;
}
- LUA_IMPLEMENT const luaL_Reg socket_function[] = {
- { "__gc", l_gc },
- { "accept", l_accept },
- { "receive", l_receive },
- { "receiveFrom", l_receiveFrom },
- { "send", l_send },
- { "sendTo", l_sendTo },
- { "close", l_close },
- { "configBlocking", l_configBlocking },
- { 0, 0 }
- };
-
LUA_EXPORT int luaopen_Socket(lua_State* L)
{
+ luaL_Reg socket_function[] = {
+ { "__gc", l_gc },
+ { "accept", l_accept },
+ { "receive", l_receive },
+ { "receiveFrom", l_receiveFrom },
+ { "send", l_send },
+ { "sendTo", l_sendTo },
+ { "close", l_close },
+ { "configBlocking", l_configBlocking },
+ { 0, 0 }
+ };
luax_newtype(L, JIN_NETWORK_SOCKET, socket_function);
return 0;
}