diff options
author | chai <chaifix@163.com> | 2018-08-12 19:52:54 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-12 19:52:54 +0800 |
commit | 7b34bd98bb00796febd5351b9d2e75fd2c247432 (patch) | |
tree | dd2da6fa01094f864d8deb358d7f9a8fe1b32b1c /src/lua/net/luaopen_net.cpp | |
parent | 5fe41eca99adf4bf0fb5832033a96f98b530d4f1 (diff) |
*update
Diffstat (limited to 'src/lua/net/luaopen_net.cpp')
-rw-r--r-- | src/lua/net/luaopen_net.cpp | 70 |
1 files changed, 67 insertions, 3 deletions
diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp index 8d67487..4bbb7e8 100644 --- a/src/lua/net/luaopen_net.cpp +++ b/src/lua/net/luaopen_net.cpp @@ -1,31 +1,95 @@ #include "lua/luax.h" #include "libjin/jin.h" +#include "../luaopen_types.h" +#include "lua_net_Buffer.h" namespace jin { namespace lua { + using namespace jin::net; + static int l_initNetwork(lua_State* L) { jin::net::Net::get()->init(); return 1; } + + // jin.net.toSocket(lightuserdata) + static int l_toSocket(lua_State*L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_SOCKET); + //Proxy * socketProxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + + return 1; + } + + // jin.net.Socket() + static int l_Socket(lua_State* L) + { + const char* socketType = luax_checkstring(L, 1); + SocketInformation info = { 0 }; + 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_isstring(L, 2)) + info.address = tk_strtohl(luax_checkstring(L, 2)); + else if(luax_isinteger(L, 2)) + info.address = luax_checkinteger(L, 2); + info.port = luax_checkinteger(L, 3); + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + Socket* socket = new Socket(info); + proxy->bind(socket, JIN_NETWORK_SOCKET); + return 1; + } + + // jin.net.Buffer() + 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(); + proxy->bind(buffer, JIN_NETWORK_BUFFER); + return 1; + } static const luaL_Reg f[] = { - { "init", l_initNetwork}, + { "init", l_initNetwork }, + { "toSocket", l_toSocket }, + { "Socket", l_Socket }, + { "Buffer", l_Buffer }, { 0, 0 } }; extern int luaopen_Socket(lua_State* L); + extern int luaopen_Buffer(lua_State* L); // only tcp int luaopen_net(lua_State* L) { + luaopen_Socket(L); + luaopen_Buffer(L); + luax_newlib(L, f); return 1; } -} -}
\ No newline at end of file +} // lua +} // jin
\ No newline at end of file |