From 831e814ce9bdb84e86c06c4a52008f6bdaaa00d6 Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 16 Nov 2018 00:24:51 +0800 Subject: =?UTF-8?q?*=E5=90=88=E5=B9=B6master=E5=88=B0minimal=E5=88=86?= =?UTF-8?q?=E6=94=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/modules/net/je_lua_socket.cpp | 129 ++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/lua/modules/net/je_lua_socket.cpp (limited to 'src/lua/modules/net/je_lua_socket.cpp') diff --git a/src/lua/modules/net/je_lua_socket.cpp b/src/lua/modules/net/je_lua_socket.cpp new file mode 100644 index 0000000..db170e4 --- /dev/null +++ b/src/lua/modules/net/je_lua_socket.cpp @@ -0,0 +1,129 @@ +#include "lua/modules/luax.h" + +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" +#include "je_lua_buffer.h" + +using namespace JinEngine::Net; +using namespace JinEngine::Lua::Net; + +namespace JinEngine +{ + namespace Lua + { + + const char* Jin_Lua_Socket = "Socket"; + + typedef Shared& SharedSocket; + + const int BUFFER_SIZE = 1024; + + LUA_IMPLEMENT inline SharedSocket checkSocket(lua_State* L, int pos = 1) + { + Proxy* proxy = (Proxy*)luax_checktype(L, pos, Jin_Lua_Socket); + return proxy->getShared(); + } + + LUA_IMPLEMENT inline Shared& checkNetBuffer(lua_State* L, int pos = 1) + { + Proxy* proxy = (Proxy*)luax_checktype(L, pos, Jin_Lua_Buffer); + return proxy->getShared(); + } + + // return net.Socket + LUA_IMPLEMENT int l_accept(lua_State* L) + { + SharedSocket socket = checkSocket(L); + Socket* client = socket->accept(); + Proxy* proxy = luax_newinstance(L, Jin_Lua_Socket); + proxy->bind(new Shared(client, Jin_Lua_Socket)); + return 1; + } + + // return net.Buffer + LUA_IMPLEMENT int l_receive(lua_State* L) + { + SharedSocket socket = checkSocket(L); + char buffer[BUFFER_SIZE] = {0}; + int size = socket->receive(buffer, BUFFER_SIZE); + Proxy* proxy = luax_newinstance(L, Jin_Lua_Buffer); + Net::Buffer* netBuffer = new Net::Buffer(buffer, size); + proxy->bind(new Shared(netBuffer, Jin_Lua_Buffer)); + return 1; + } + + // Socket:receiveFrom(address, port) + LUA_IMPLEMENT int l_receiveFrom(lua_State* L) + { + SharedSocket 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 = luax_newinstance(L, Jin_Lua_Buffer); + proxy->bind(new Shared(netBuffer, Jin_Lua_Buffer)); + return 1; + } + + // Socket:send(net.Buffer) -> data_length + LUA_IMPLEMENT int l_send(lua_State* L) + { + SharedSocket socket = checkSocket(L); + Shared& shared = checkNetBuffer(L, 2); + int len = socket->send(shared->buffer, shared->size); + luax_pushinteger(L, len); + return 1; + } + + // Socket:sendTo(address, port, net.Buffer) + LUA_IMPLEMENT int l_sendTo(lua_State* L) + { + SharedSocket socket = checkSocket(L); + int address = luax_checkinteger(L, 2); + int port = luax_checkinteger(L, 3); + Shared& buffer = checkNetBuffer(L, 4); + socket->sendTo(buffer->buffer, buffer->size, address, port); + return 0; + } + + LUA_IMPLEMENT int l_close(lua_State* L) + { + SharedSocket socket = checkSocket(L); + socket->close(); + return 0; + } + + LUA_IMPLEMENT int l_configBlocking(lua_State* L) + { + SharedSocket socket = checkSocket(L); + bool blocking = luax_checkbool(L, 2); + socket->configureBlocking(blocking); + return 0; + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, Jin_Lua_Socket); + proxy->release(); + return 0; + } + + LUA_EXPORT void luaopen_Socket(lua_State* L) + { + 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 } + }; + luax_newtype(L, Jin_Lua_Socket, socket_function); + } + + } // namespace Lua +} // namespace JinEngine \ No newline at end of file -- cgit v1.1-26-g67d0