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/socket.cpp | 129 ----------------------------------------- 1 file changed, 129 deletions(-) delete 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 deleted file mode 100644 index d6de730..0000000 --- a/src/lua/modules/net/socket.cpp +++ /dev/null @@ -1,129 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" -#include "Buffer.h" - -namespace JinEngine -{ - namespace Lua - { - - using namespace JinEngine::Net; - using namespace Lua::Net; - - typedef Ref& SocketRef; - - const int BUFFER_SIZE = 1024; - - static inline SocketRef 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) - { - SocketRef 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) - { - SocketRef 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) - { - SocketRef 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) - { - SocketRef 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) - { - SocketRef 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) - { - SocketRef socket = checkSocket(L); - socket->close(); - return 0; - } - - static int l_configBlocking(lua_State* L) - { - SocketRef 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; - } - - } // namespace Lua -} // namespace JinEngine \ No newline at end of file -- cgit v1.1-26-g67d0