diff options
author | chai <chaifix@163.com> | 2018-08-22 13:58:13 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-22 13:58:13 +0800 |
commit | 36621e6be9604517c900adc0d97665e975c2b325 (patch) | |
tree | d3db26c2de6b47aee96f6a24d2595ff80a8869b7 /src/lua/modules/net/socket.cpp | |
parent | 95df5cf921f57bfeac0d819994210dfcc2dcad14 (diff) |
*update
Diffstat (limited to 'src/lua/modules/net/socket.cpp')
-rw-r--r-- | src/lua/modules/net/socket.cpp | 127 |
1 files changed, 127 insertions, 0 deletions
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<Socket>& checkSocket(lua_State* L, int pos = 1) + { + Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET); + return proxy->getRef<Socket>(); + } + + static inline Ref<Buffer>& checkNetBuffer(lua_State* L, int pos = 1) + { + Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER); + return proxy->getRef<Buffer>(); + } + + // return net.Socket + static int l_accept(lua_State* L) + { + Ref<Socket>& socket = checkSocket(L); + Socket* client = socket->accept(); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + proxy->bind(new Ref<Socket>(client, JIN_NETWORK_SOCKET)); + return 1; + } + + // return net.Buffer + static int l_receive(lua_State* L) + { + Ref<Socket>& 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<Buffer>(netBuffer, JIN_NETWORK_BUFFER)); + return 1; + } + + // Socket:receiveFrom(address, port) + static int l_receiveFrom(lua_State* L) + { + Ref<Socket>& 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<Buffer>(netBuffer, JIN_NETWORK_BUFFER)); + return 1; + } + + // Socket:send(net.Buffer) -> data_length + static int l_send(lua_State* L) + { + Ref<Socket>& socket = checkSocket(L); + Ref<Buffer>& 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>& socket = checkSocket(L); + int address = luax_checkinteger(L, 2); + int port = luax_checkinteger(L, 3); + Ref<Buffer>& buffer = checkNetBuffer(L, 4); + socket->sendTo(buffer->buffer, buffer->size, address, port); + return 0; + } + + static int l_close(lua_State* L) + { + Ref<Socket>& socket = checkSocket(L); + socket->close(); + return 0; + } + + static int l_configBlocking(lua_State* L) + { + Ref<Socket>& 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 |