aboutsummaryrefslogtreecommitdiff
path: root/src/lua/net/luaopen_Socket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/net/luaopen_Socket.cpp')
-rw-r--r--src/lua/net/luaopen_Socket.cpp16
1 files changed, 14 insertions, 2 deletions
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 },