From 36621e6be9604517c900adc0d97665e975c2b325 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 22 Aug 2018 13:58:13 +0800 Subject: *update --- src/lua/modules/net/socket.cpp | 127 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/lua/modules/net/socket.cpp (limited to 'src/lua/modules/net/socket.cpp') diff --git a/src/lua/modules/net/socket.cpp b/src/lua/modules/net/socket.cpp new file mode 100644 index 0000000..6d3fdfb --- /dev/null +++ b/src/lua/modules/net/socket.cpp @@ -0,0 +1,127 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "lua/common/common.h" +#include "libjin/jin.h" +#include "Buffer.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::net; + using namespace lua::net; + + const int BUFFER_SIZE = 1024; + + static inline Ref& checkSocket(lua_State* L, int pos = 1) + { + Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET); + return proxy->getRef(); + } + + static inline Ref& checkNetBuffer(lua_State* L, int pos = 1) + { + Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER); + return proxy->getRef(); + } + + // return net.Socket + static int l_accept(lua_State* L) + { + Ref& socket = checkSocket(L); + Socket* client = socket->accept(); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + proxy->bind(new Ref(client, JIN_NETWORK_SOCKET)); + return 1; + } + + // return net.Buffer + static int l_receive(lua_State* L) + { + Ref& socket = checkSocket(L); + char buffer[BUFFER_SIZE] = {0}; + int size = socket->receive(buffer, BUFFER_SIZE); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + net::Buffer* netBuffer = new net::Buffer(buffer, size); + proxy->bind(new Ref(netBuffer, JIN_NETWORK_BUFFER)); + return 1; + } + + // Socket:receiveFrom(address, port) + static int l_receiveFrom(lua_State* L) + { + Ref& socket = checkSocket(L); + int address = luax_checkinteger(L, 2); + int port = luax_checkinteger(L, 3); + char buffer[BUFFER_SIZE]; + int size = socket->receiveFrom(buffer, BUFFER_SIZE, address, port); + net::Buffer* netBuffer = new net::Buffer(buffer, size); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + proxy->bind(new Ref(netBuffer, JIN_NETWORK_BUFFER)); + return 1; + } + + // Socket:send(net.Buffer) -> data_length + static int l_send(lua_State* L) + { + Ref& socket = checkSocket(L); + Ref& ref = checkNetBuffer(L, 2); + int len = socket->send(ref->buffer, ref->size); + luax_pushinteger(L, len); + return 1; + } + + // Socket:sendTo(address, port, net.Buffer) + static int l_sendTo(lua_State* L) + { + Ref& socket = checkSocket(L); + int address = luax_checkinteger(L, 2); + int port = luax_checkinteger(L, 3); + Ref& buffer = checkNetBuffer(L, 4); + socket->sendTo(buffer->buffer, buffer->size, address, port); + return 0; + } + + static int l_close(lua_State* L) + { + Ref& socket = checkSocket(L); + socket->close(); + return 0; + } + + static int l_configBlocking(lua_State* L) + { + Ref& socket = checkSocket(L); + bool blocking = luax_checkbool(L, 2); + socket->configureBlocking(blocking); + return 0; + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_SOCKET); + proxy->release(); + return 0; + } + + static 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 } + }; + + int luaopen_Socket(lua_State* L) + { + luax_newtype(L, JIN_NETWORK_SOCKET, socket_function); + return 0; + } + +} // lua +} // jin \ No newline at end of file -- cgit v1.1-26-g67d0