From 6551adeca70d4299a99d45245d4e13dbfdfa87e5 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 23 Oct 2018 15:56:01 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9lua=E7=BB=91=E5=AE=9A=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/modules/net/je_lua_net.cpp | 86 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/lua/modules/net/je_lua_net.cpp (limited to 'src/lua/modules/net/je_lua_net.cpp') 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..c477477 --- /dev/null +++ b/src/lua/modules/net/je_lua_net.cpp @@ -0,0 +1,86 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "libjin/jin.h" +#include "lua/common/common.h" +#include "je_lua_buffer.h" + +namespace JinEngine +{ +namespace Lua +{ + + using namespace JinEngine::Lua::Net; + using namespace JinEngine::Net; + + static int l_initNetwork(lua_State* L) + { + JinEngine::Net::NetManager::get()->init(); + return 1; + } + + static 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 = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + proxy->bind(new Ref(socket, JIN_NETWORK_SOCKET)); + return 1; + } + + static int l_Buffer(lua_State* L) + { + int size = luax_checkinteger(L, 1); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + Net::Buffer* buffer = new Net::Buffer(size); + proxy->bind(new Ref(buffer, JIN_NETWORK_BUFFER)); + return 1; + } + + static const luaL_Reg f[] = { + { "init", l_initNetwork }, + { "newSocket", l_Socket }, + { "newBuffer", l_Buffer }, + { 0, 0 } + }; + + extern int luaopen_Socket(lua_State* L); + extern int luaopen_Buffer(lua_State* L); + + int luaopen_net(lua_State* L) + { + luaopen_Socket(L); + luaopen_Buffer(L); + + luax_newlib(L, f); + + return 1; + } + +} // namespace Lua +} // namespace JinEngine \ No newline at end of file -- cgit v1.1-26-g67d0