aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/net/socket.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-10-23 12:23:58 +0800
committerchai <chaifix@163.com>2018-10-23 12:23:58 +0800
commit40fc27154fe754181934dc7ee31375e6bdfb33fc (patch)
tree897ad98d759bc308ef66561181ba88b85f2ccd47 /src/lua/modules/net/socket.cpp
parent1480c9445100075c9e1a894eb07c0ef727b509a1 (diff)
*merge from minimal
Diffstat (limited to 'src/lua/modules/net/socket.cpp')
-rw-r--r--src/lua/modules/net/socket.cpp212
1 files changed, 107 insertions, 105 deletions
diff --git a/src/lua/modules/net/socket.cpp b/src/lua/modules/net/socket.cpp
index 6d3fdfb..d6de730 100644
--- a/src/lua/modules/net/socket.cpp
+++ b/src/lua/modules/net/socket.cpp
@@ -4,124 +4,126 @@
#include "libjin/jin.h"
#include "Buffer.h"
-namespace jin
-{
-namespace lua
+namespace JinEngine
{
+ namespace Lua
+ {
- using namespace jin::net;
- using namespace lua::net;
+ using namespace JinEngine::Net;
+ using namespace Lua::Net;
- const int BUFFER_SIZE = 1024;
+ typedef Ref<Socket>& SocketRef;
- static inline Ref<Socket>& checkSocket(lua_State* L, int pos = 1)
- {
- Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET);
- return proxy->getRef<Socket>();
- }
+ const int BUFFER_SIZE = 1024;
- static inline Ref<Buffer>& checkNetBuffer(lua_State* L, int pos = 1)
- {
- Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER);
- return proxy->getRef<Buffer>();
- }
+ static inline SocketRef checkSocket(lua_State* L, int pos = 1)
+ {
+ Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET);
+ return proxy->getRef<Socket>();
+ }
- // return net.Socket
- static int l_accept(lua_State* L)
- {
- Ref<Socket>& socket = checkSocket(L);
- Socket* client = socket->accept();
- Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy));
- proxy->bind(new Ref<Socket>(client, JIN_NETWORK_SOCKET));
- return 1;
- }
+ static inline Ref<Buffer>& checkNetBuffer(lua_State* L, int pos = 1)
+ {
+ Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER);
+ return proxy->getRef<Buffer>();
+ }
+
+ // 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<Socket>(client, JIN_NETWORK_SOCKET));
+ return 1;
+ }
- // return net.Buffer
- static int l_receive(lua_State* L)
- {
- Ref<Socket>& 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<Buffer>(netBuffer, JIN_NETWORK_BUFFER));
- 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<Buffer>(netBuffer, JIN_NETWORK_BUFFER));
+ return 1;
+ }
- // Socket:receiveFrom(address, port)
- static int l_receiveFrom(lua_State* L)
- {
- Ref<Socket>& 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<Buffer>(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<Buffer>(netBuffer, JIN_NETWORK_BUFFER));
+ return 1;
+ }
- // Socket:send(net.Buffer) -> data_length
- static int l_send(lua_State* L)
- {
- Ref<Socket>& socket = checkSocket(L);
- Ref<Buffer>& ref = checkNetBuffer(L, 2);
- int len = socket->send(ref->buffer, ref->size);
- luax_pushinteger(L, len);
- return 1;
- }
+ // Socket:send(net.Buffer) -> data_length
+ static int l_send(lua_State* L)
+ {
+ SocketRef socket = checkSocket(L);
+ Ref<Buffer>& 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>& socket = checkSocket(L);
- int address = luax_checkinteger(L, 2);
- int port = luax_checkinteger(L, 3);
- Ref<Buffer>& buffer = checkNetBuffer(L, 4);
- socket->sendTo(buffer->buffer, buffer->size, address, port);
- return 0;
- }
+ // 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>& buffer = checkNetBuffer(L, 4);
+ socket->sendTo(buffer->buffer, buffer->size, address, port);
+ return 0;
+ }
- static int l_close(lua_State* L)
- {
- Ref<Socket>& socket = checkSocket(L);
- socket->close();
- return 0;
- }
+ static int l_close(lua_State* L)
+ {
+ SocketRef socket = checkSocket(L);
+ socket->close();
+ return 0;
+ }
- static int l_configBlocking(lua_State* L)
- {
- Ref<Socket>& socket = checkSocket(L);
- bool blocking = luax_checkbool(L, 2);
- socket->configureBlocking(blocking);
- 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 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 }
- };
+ 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;
- }
+ int luaopen_Socket(lua_State* L)
+ {
+ luax_newtype(L, JIN_NETWORK_SOCKET, socket_function);
+ return 0;
+ }
-} // lua
-} // jin \ No newline at end of file
+ } // namespace Lua
+} // namespace JinEngine \ No newline at end of file