aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/net/je_lua_net.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/modules/net/je_lua_net.cpp')
-rw-r--r--src/lua/modules/net/je_lua_net.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/lua/modules/net/je_lua_net.cpp b/src/lua/modules/net/je_lua_net.cpp
new file mode 100644
index 0000000..b081733
--- /dev/null
+++ b/src/lua/modules/net/je_lua_net.cpp
@@ -0,0 +1,84 @@
+#include "lua/modules/luax.h"
+
+#include "libjin/jin.h"
+#include "lua/common/je_lua_common.h"
+
+#include "je_lua_buffer.h"
+#include "je_lua_socket.h"
+
+namespace JinEngine
+{
+namespace Lua
+{
+
+ using namespace JinEngine::Lua::Net;
+ using namespace JinEngine::Net;
+
+ LUA_IMPLEMENT int l_initNetwork(lua_State* L)
+ {
+ JinEngine::Net::NetManager::get()->init();
+ return 1;
+ }
+
+ LUA_IMPLEMENT int l_Socket(lua_State* L)
+ {
+ SocketInformation info = { 0 };
+ {
+ const char* socketType = luax_checkstring(L, 1);
+ if (strcmp(socketType, "TCP") == 0)
+ info.type = SocketType::TCP;
+ else if (strcmp(socketType, "UDP") == 0)
+ info.type = SocketType::UDP;
+ else
+ {
+ luax_error(L, "jin.net.Socket() first paramter wrong, must be TCP or UDP");
+ return 0;
+ }
+ // type, port
+ if (luax_gettop(L) == 2)
+ {
+ info.port = luax_checkinteger(L, 2);
+ }
+ // type, address, port
+ else if (luax_gettop(L) == 3)
+ {
+ if (luax_isstringstrict(L, 2))
+ info.address = tk_strtohl(luax_checkstring(L, 2));
+ else if (luax_isintegerstrict(L, 2))
+ info.address = luax_checkinteger(L, 2);
+ info.port = luax_checkinteger(L, 3);
+ }
+ }
+ Socket* socket = new Socket(info);
+ Proxy* proxy = luax_newinstance(L, Jin_Lua_Socket);
+ proxy->bind(new Shared<Socket>(socket, Jin_Lua_Socket));
+ return 1;
+ }
+
+ LUA_IMPLEMENT int l_Buffer(lua_State* L)
+ {
+ int size = luax_checkinteger(L, 1);
+ Proxy* proxy = luax_newinstance(L, Jin_Lua_Buffer);
+ Net::Buffer* buffer = new Net::Buffer(size);
+ proxy->bind(new Shared<Buffer>(buffer, Jin_Lua_Buffer));
+ return 1;
+ }
+
+ LUA_EXPORT int luaopen_net(lua_State* L)
+ {
+ 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;
+ }
+
+} // namespace Lua
+} // namespace JinEngine \ No newline at end of file