diff options
Diffstat (limited to 'src/lua/net')
-rw-r--r-- | src/lua/net/lua_net_Buffer.h | 12 | ||||
-rw-r--r-- | src/lua/net/lua_net_Socket.h | 64 | ||||
-rw-r--r-- | src/lua/net/luaopen_Buffer.cpp | 8 | ||||
-rw-r--r-- | src/lua/net/luaopen_Socket.cpp | 16 | ||||
-rw-r--r-- | src/lua/net/luaopen_net.cpp | 14 |
5 files changed, 96 insertions, 18 deletions
diff --git a/src/lua/net/lua_net_Buffer.h b/src/lua/net/lua_net_Buffer.h index 7fba37f..6af3078 100644 --- a/src/lua/net/lua_net_Buffer.h +++ b/src/lua/net/lua_net_Buffer.h @@ -3,6 +3,7 @@ #include <cstring> #include <cstdlib> +#include "../luaopen_types.h" namespace jin { @@ -11,7 +12,7 @@ namespace lua namespace net { - class Buffer + class Buffer : public Object { public: Buffer(size_t s = 0) @@ -30,9 +31,12 @@ namespace net ~Buffer() { - delete[] buffer; - buffer = nullptr; - size = 0; + if (buffer != nullptr) + { + delete[] buffer; + buffer = nullptr; + size = 0; + } } void append(const void* data, size_t s) diff --git a/src/lua/net/lua_net_Socket.h b/src/lua/net/lua_net_Socket.h new file mode 100644 index 0000000..e02fb5b --- /dev/null +++ b/src/lua/net/lua_net_Socket.h @@ -0,0 +1,64 @@ +#ifndef __JIN_LUA_NET_SOCKET_H +#define __JIN_LUA_NET_SOCKET_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace net +{ + + class Socket : public jin::net::Socket + , public Object + { + public: + typedef jin::net::SocketInformation SocketInformation; + typedef jin::net::SocketType SocketType; + + Socket(jin::net::Socket base) + : jin::net::Socket(base) + { + } + + Socket(const SocketInformation& socketInformation) + : jin::net::Socket(socketInformation) + { + } + + Socket(SocketType type, unsigned short port) + : jin::net::Socket(type, port) + { + } + + Socket(SocketType type, unsigned int address, unsigned short port) + : jin::net::Socket(type, address) + { + } + + Socket(SocketType type, const char* address, unsigned short port) + : jin::net::Socket(type, address, port) + { + } + + Socket * accept() + { + jin::net::Socket* base = jin::net::Socket::accept(); + Socket* socket = new Socket(*base); + delete base; + return socket; + } + + private: + ~Socket() + { + } + + }; + +} +} +} + +#endif
\ No newline at end of file diff --git a/src/lua/net/luaopen_Buffer.cpp b/src/lua/net/luaopen_Buffer.cpp index 9278544..68e377b 100644 --- a/src/lua/net/luaopen_Buffer.cpp +++ b/src/lua/net/luaopen_Buffer.cpp @@ -108,7 +108,15 @@ namespace net return 2; } + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); + proxy->release<Buffer>(); + return 0; + } + static const luaL_Reg netbuffer_function[] = { + { "__gc", l_gc }, { "append", l_append }, { "grabString", l_grabString }, { "grabInteger", l_grabInteger }, diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp index 395729a..327cda1 100644 --- a/src/lua/net/luaopen_Socket.cpp +++ b/src/lua/net/luaopen_Socket.cpp @@ -2,13 +2,14 @@ #include "../luaopen_types.h" #include "libjin/jin.h" #include "lua_net_Buffer.h" +#include "lua_net_Socket.h" namespace jin { namespace lua { - - using namespace jin::net; + + using namespace jin::lua::net; const int BUFFER_SIZE = 1024; @@ -34,6 +35,7 @@ namespace lua Socket* socket = checkSocket(L); Socket* client = socket->accept(); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + client->retain(); proxy->bind(client, JIN_NETWORK_SOCKET); return 1; } @@ -46,6 +48,7 @@ namespace lua int size = socket->receive(buffer, BUFFER_SIZE); net::Buffer* netBuffer = new net::Buffer(buffer, size); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + netBuffer->retain(); proxy->bind(netBuffer, JIN_NETWORK_BUFFER); return 1; } @@ -60,6 +63,7 @@ namespace lua 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)); + netBuffer->retain(); proxy->bind(netBuffer, JIN_NETWORK_BUFFER); return 1; } @@ -100,7 +104,15 @@ namespace lua 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 }, diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp index 89a79f3..28313ce 100644 --- a/src/lua/net/luaopen_net.cpp +++ b/src/lua/net/luaopen_net.cpp @@ -2,13 +2,13 @@ #include "libjin/jin.h" #include "../luaopen_types.h" #include "lua_net_Buffer.h" +#include "lua_net_Socket.h" namespace jin { namespace lua { - - using namespace jin::net; + using namespace jin::lua::net; static int l_initNetwork(lua_State* L) { @@ -16,15 +16,6 @@ namespace lua return 1; } - // jin.net.toSocket(lightuserdata) - static int l_toSocket(lua_State*L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_SOCKET); - //Proxy * socketProxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); - - return 1; - } - // jin.net.Socket() static int l_Socket(lua_State* L) { @@ -71,7 +62,6 @@ namespace lua static const luaL_Reg f[] = { { "init", l_initNetwork }, - { "toSocket", l_toSocket }, { "Socket", l_Socket }, { "Buffer", l_Buffer }, { 0, 0 } |