diff options
Diffstat (limited to 'src/lua/net')
-rw-r--r-- | src/lua/net/Buffer.h (renamed from src/lua/net/lua_net_Buffer.h) | 4 | ||||
-rw-r--r-- | src/lua/net/Socket.h | 77 | ||||
-rw-r--r-- | src/lua/net/lua_net_Socket.h | 64 | ||||
-rw-r--r-- | src/lua/net/luaopen_Buffer.cpp | 4 | ||||
-rw-r--r-- | src/lua/net/luaopen_Socket.cpp | 9 | ||||
-rw-r--r-- | src/lua/net/luaopen_net.cpp | 4 |
6 files changed, 86 insertions, 76 deletions
diff --git a/src/lua/net/lua_net_Buffer.h b/src/lua/net/Buffer.h index 6af3078..035da5c 100644 --- a/src/lua/net/lua_net_Buffer.h +++ b/src/lua/net/Buffer.h @@ -16,15 +16,15 @@ namespace net { public: Buffer(size_t s = 0) + : size(s) { - size = s; buffer = new char[size]; memset(buffer, 0, size); } Buffer(const char* data, size_t s) + : size(s) { - size = s; buffer = new char[size]; memcpy(buffer, data, size); } diff --git a/src/lua/net/Socket.h b/src/lua/net/Socket.h new file mode 100644 index 0000000..5834092 --- /dev/null +++ b/src/lua/net/Socket.h @@ -0,0 +1,77 @@ +#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 +{ + + typedef jin::net::SocketInformation SocketInformation; + typedef jin::net::SocketType SocketType; + + class Socket : public Object + { + public: + Socket() {} + + Socket(SocketInformation info) + { + socket = new jin::net::Socket(info); + } + + void configureBlocking(bool blocking) + { + socket->configureBlocking(blocking); + } + + Socket* accept() + { + Socket* client = new Socket(); + client->socket = socket->accept(); + return client; + } + + int receive(char* buffer, int size) + { + return socket->receive(buffer, size); + } + + int send(char* buffer, int size) + { + return socket->send(buffer, size); + } + + void sendTo(char* buffer, int size, unsigned int address, unsigned int port) + { + socket->sendTo(buffer, size, address, port); + } + + int receiveFrom(char* buffer, int size, unsigned int address, unsigned int port) + { + return socket->receiveFrom(buffer, size, address, port); + } + + void close() + { + socket->close(); + } + + private: + jin::net::Socket* socket; + + ~Socket() + { + delete socket; + } + + }; + +} // net +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/net/lua_net_Socket.h b/src/lua/net/lua_net_Socket.h deleted file mode 100644 index e02fb5b..0000000 --- a/src/lua/net/lua_net_Socket.h +++ /dev/null @@ -1,64 +0,0 @@ -#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 68e377b..0ab47cb 100644 --- a/src/lua/net/luaopen_Buffer.cpp +++ b/src/lua/net/luaopen_Buffer.cpp @@ -1,7 +1,7 @@ #include "lua/luax.h" #include "../luaopen_types.h" #include "libjin/jin.h" -#include "lua_net_Buffer.h" +#include "Buffer.h" namespace jin { @@ -111,7 +111,7 @@ namespace net static int l_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); - proxy->release<Buffer>(); + proxy->release(); return 0; } diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp index 327cda1..abcd8f2 100644 --- a/src/lua/net/luaopen_Socket.cpp +++ b/src/lua/net/luaopen_Socket.cpp @@ -1,8 +1,8 @@ #include "lua/luax.h" #include "../luaopen_types.h" #include "libjin/jin.h" -#include "lua_net_Buffer.h" -#include "lua_net_Socket.h" +#include "Buffer.h" +#include "Socket.h" namespace jin { @@ -35,7 +35,6 @@ 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; } @@ -48,7 +47,6 @@ 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; } @@ -63,7 +61,6 @@ 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; } @@ -112,7 +109,7 @@ namespace lua } static const luaL_Reg socket_function[] = { - { "__gc", l_gc }, + { "__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 28313ce..13d24dd 100644 --- a/src/lua/net/luaopen_net.cpp +++ b/src/lua/net/luaopen_net.cpp @@ -1,8 +1,8 @@ #include "lua/luax.h" #include "libjin/jin.h" #include "../luaopen_types.h" -#include "lua_net_Buffer.h" -#include "lua_net_Socket.h" +#include "Buffer.h" +#include "Socket.h" namespace jin { |