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/Buffer.cpp | 136 +++++++++++++++++++++++++++++++++ src/lua/modules/net/luaopen_Buffer.cpp | 136 --------------------------------- src/lua/modules/net/luaopen_Socket.cpp | 127 ------------------------------ src/lua/modules/net/luaopen_net.cpp | 85 --------------------- src/lua/modules/net/net.cpp | 85 +++++++++++++++++++++ src/lua/modules/net/socket.cpp | 127 ++++++++++++++++++++++++++++++ 6 files changed, 348 insertions(+), 348 deletions(-) create mode 100644 src/lua/modules/net/Buffer.cpp delete mode 100644 src/lua/modules/net/luaopen_Buffer.cpp delete mode 100644 src/lua/modules/net/luaopen_Socket.cpp delete mode 100644 src/lua/modules/net/luaopen_net.cpp create mode 100644 src/lua/modules/net/net.cpp create mode 100644 src/lua/modules/net/socket.cpp (limited to 'src/lua/modules/net') diff --git a/src/lua/modules/net/Buffer.cpp b/src/lua/modules/net/Buffer.cpp new file mode 100644 index 0000000..3354518 --- /dev/null +++ b/src/lua/modules/net/Buffer.cpp @@ -0,0 +1,136 @@ +#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 +{ +namespace net +{ + + static inline Ref& checkNetBuffer(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); + return proxy->getRef(); + } + + // net.Buffer:append(value) -> value_length + static int l_append(lua_State* L) + { + Ref& ref = checkNetBuffer(L); + const int vp = 2; + if (luax_isintegerstrict(L, vp)) + { + int n = luax_checkinteger(L, vp); + int size = sizeof(n); + ref->append(&n, size); + luax_pushinteger(L, size); + return 1; + } + else if (luax_isfloatstrict(L, vp)) + { + float n = luax_checknumber(L, vp); + int size = sizeof(n); + ref->append(&n, size); + luax_pushinteger(L, size); + return 1; + } + else if (luax_isbooleanstrict(L, vp)) + { + bool n = luax_checkbool(L, vp); + int size = sizeof(n); + ref->append(&n, size); + luax_pushinteger(L, size); + return 1; + } + else if (luax_isstringstrict(L, vp)) + { + const char* str = luax_checkstring(L, vp); + int size = strlen(str) + 1; + ref->append(str, size); + luax_pushinteger(L, size); + return 1; + } + else + { + luax_typerror(L, vp, "number, bool or string"); + return 0; + } + } + + // net.Buffer:grabString(offset) -> string, length + static int l_grabString(lua_State* L) + { + Ref& ref = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + const char* str = ref->grabString(&len, offset); + luax_pushstring(L, str); + luax_pushinteger(L, len); + return 2; + } + + // net.Buffer:grabInteger(offset) -> integer, length + static int l_grabInteger(lua_State* L) + { + Ref& ref = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + int integer = ref->grabInteger(&len, offset); + luax_pushinteger(L, integer); + luax_pushinteger(L, len); + return 2; + } + + static int l_grabFloat(lua_State* L) + { + Ref& ref = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + float floatv = ref->grabFloat(&len, offset); + luax_pushnumber(L, floatv); + luax_pushinteger(L, len); + return 2; + } + + static int l_grabBoolean(lua_State* L) + { + Ref& ref = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + bool boolean = ref->grabBoolean(&len, offset); + luax_pushboolean(L, boolean); + luax_pushinteger(L, len); + return 2; + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); + proxy->release(); + return 0; + } + + static const luaL_Reg netbuffer_function[] = { + { "__gc", l_gc }, + { "append", l_append }, + { "grabString", l_grabString }, + { "grabInteger", l_grabInteger }, + { "grabBoolean", l_grabBoolean }, + { "grabFloat", l_grabFloat }, + { 0, 0 } + }; + +} // net + + int luaopen_Buffer(lua_State* L) + { + luax_newtype(L, JIN_NETWORK_BUFFER, net::netbuffer_function); + return 0; + } + +} // lua +} // jin \ No newline at end of file diff --git a/src/lua/modules/net/luaopen_Buffer.cpp b/src/lua/modules/net/luaopen_Buffer.cpp deleted file mode 100644 index 3354518..0000000 --- a/src/lua/modules/net/luaopen_Buffer.cpp +++ /dev/null @@ -1,136 +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 jin -{ -namespace lua -{ -namespace net -{ - - static inline Ref& checkNetBuffer(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); - return proxy->getRef(); - } - - // net.Buffer:append(value) -> value_length - static int l_append(lua_State* L) - { - Ref& ref = checkNetBuffer(L); - const int vp = 2; - if (luax_isintegerstrict(L, vp)) - { - int n = luax_checkinteger(L, vp); - int size = sizeof(n); - ref->append(&n, size); - luax_pushinteger(L, size); - return 1; - } - else if (luax_isfloatstrict(L, vp)) - { - float n = luax_checknumber(L, vp); - int size = sizeof(n); - ref->append(&n, size); - luax_pushinteger(L, size); - return 1; - } - else if (luax_isbooleanstrict(L, vp)) - { - bool n = luax_checkbool(L, vp); - int size = sizeof(n); - ref->append(&n, size); - luax_pushinteger(L, size); - return 1; - } - else if (luax_isstringstrict(L, vp)) - { - const char* str = luax_checkstring(L, vp); - int size = strlen(str) + 1; - ref->append(str, size); - luax_pushinteger(L, size); - return 1; - } - else - { - luax_typerror(L, vp, "number, bool or string"); - return 0; - } - } - - // net.Buffer:grabString(offset) -> string, length - static int l_grabString(lua_State* L) - { - Ref& ref = checkNetBuffer(L); - int offset = luax_checkinteger(L, 2); - int len; - const char* str = ref->grabString(&len, offset); - luax_pushstring(L, str); - luax_pushinteger(L, len); - return 2; - } - - // net.Buffer:grabInteger(offset) -> integer, length - static int l_grabInteger(lua_State* L) - { - Ref& ref = checkNetBuffer(L); - int offset = luax_checkinteger(L, 2); - int len; - int integer = ref->grabInteger(&len, offset); - luax_pushinteger(L, integer); - luax_pushinteger(L, len); - return 2; - } - - static int l_grabFloat(lua_State* L) - { - Ref& ref = checkNetBuffer(L); - int offset = luax_checkinteger(L, 2); - int len; - float floatv = ref->grabFloat(&len, offset); - luax_pushnumber(L, floatv); - luax_pushinteger(L, len); - return 2; - } - - static int l_grabBoolean(lua_State* L) - { - Ref& ref = checkNetBuffer(L); - int offset = luax_checkinteger(L, 2); - int len; - bool boolean = ref->grabBoolean(&len, offset); - luax_pushboolean(L, boolean); - luax_pushinteger(L, len); - return 2; - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); - proxy->release(); - return 0; - } - - static const luaL_Reg netbuffer_function[] = { - { "__gc", l_gc }, - { "append", l_append }, - { "grabString", l_grabString }, - { "grabInteger", l_grabInteger }, - { "grabBoolean", l_grabBoolean }, - { "grabFloat", l_grabFloat }, - { 0, 0 } - }; - -} // net - - int luaopen_Buffer(lua_State* L) - { - luax_newtype(L, JIN_NETWORK_BUFFER, net::netbuffer_function); - return 0; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/net/luaopen_Socket.cpp b/src/lua/modules/net/luaopen_Socket.cpp deleted file mode 100644 index 6d3fdfb..0000000 --- a/src/lua/modules/net/luaopen_Socket.cpp +++ /dev/null @@ -1,127 +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 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 diff --git a/src/lua/modules/net/luaopen_net.cpp b/src/lua/modules/net/luaopen_net.cpp deleted file mode 100644 index a984920..0000000 --- a/src/lua/modules/net/luaopen_net.cpp +++ /dev/null @@ -1,85 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "libjin/jin.h" -#include "lua/common/common.h" -#include "Buffer.h" - -namespace jin -{ -namespace lua -{ - using namespace jin::lua::net; - using namespace jin::net; - - static int l_initNetwork(lua_State* L) - { - jin::net::Net::get()->init(); - return 1; - } - - static int l_Socket(lua_State* L) - { - SocketInformation info = { 0 }; - { - const char* socketType = luax_checkstring(L, 1); - 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_isstringstrict(L, 2)) - info.address = tk_strtohl(luax_checkstring(L, 2)); - else if (luax_isintegerstrict(L, 2)) - info.address = luax_checkinteger(L, 2); - info.port = luax_checkinteger(L, 3); - } - } - Socket* socket = new Socket(info); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); - proxy->bind(new Ref(socket, JIN_NETWORK_SOCKET)); - return 1; - } - - 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(size); - proxy->bind(new Ref(buffer, JIN_NETWORK_BUFFER)); - return 1; - } - - static const luaL_Reg f[] = { - { "init", l_initNetwork }, - { "newSocket", l_Socket }, - { "newBuffer", l_Buffer }, - { 0, 0 } - }; - - extern int luaopen_Socket(lua_State* L); - extern int luaopen_Buffer(lua_State* L); - - int luaopen_net(lua_State* L) - { - luaopen_Socket(L); - luaopen_Buffer(L); - - luax_newlib(L, f); - - return 1; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/net/net.cpp b/src/lua/modules/net/net.cpp new file mode 100644 index 0000000..a984920 --- /dev/null +++ b/src/lua/modules/net/net.cpp @@ -0,0 +1,85 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "libjin/jin.h" +#include "lua/common/common.h" +#include "Buffer.h" + +namespace jin +{ +namespace lua +{ + using namespace jin::lua::net; + using namespace jin::net; + + static int l_initNetwork(lua_State* L) + { + jin::net::Net::get()->init(); + return 1; + } + + static int l_Socket(lua_State* L) + { + SocketInformation info = { 0 }; + { + const char* socketType = luax_checkstring(L, 1); + 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_isstringstrict(L, 2)) + info.address = tk_strtohl(luax_checkstring(L, 2)); + else if (luax_isintegerstrict(L, 2)) + info.address = luax_checkinteger(L, 2); + info.port = luax_checkinteger(L, 3); + } + } + Socket* socket = new Socket(info); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + proxy->bind(new Ref(socket, JIN_NETWORK_SOCKET)); + return 1; + } + + 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(size); + proxy->bind(new Ref(buffer, JIN_NETWORK_BUFFER)); + return 1; + } + + static const luaL_Reg f[] = { + { "init", l_initNetwork }, + { "newSocket", l_Socket }, + { "newBuffer", l_Buffer }, + { 0, 0 } + }; + + extern int luaopen_Socket(lua_State* L); + extern int luaopen_Buffer(lua_State* L); + + int luaopen_net(lua_State* L) + { + luaopen_Socket(L); + luaopen_Buffer(L); + + luax_newlib(L, f); + + return 1; + } + +} // lua +} // jin \ No newline at end of file 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