aboutsummaryrefslogtreecommitdiff
path: root/src/lua/net/luaopen_Socket.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-08-12 19:52:54 +0800
committerchai <chaifix@163.com>2018-08-12 19:52:54 +0800
commit7b34bd98bb00796febd5351b9d2e75fd2c247432 (patch)
treedd2da6fa01094f864d8deb358d7f9a8fe1b32b1c /src/lua/net/luaopen_Socket.cpp
parent5fe41eca99adf4bf0fb5832033a96f98b530d4f1 (diff)
*update
Diffstat (limited to 'src/lua/net/luaopen_Socket.cpp')
-rw-r--r--src/lua/net/luaopen_Socket.cpp119
1 files changed, 54 insertions, 65 deletions
diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp
index 7dbfb33..395729a 100644
--- a/src/lua/net/luaopen_Socket.cpp
+++ b/src/lua/net/luaopen_Socket.cpp
@@ -1,120 +1,109 @@
#include "lua/luax.h"
#include "../luaopen_types.h"
#include "libjin/jin.h"
+#include "lua_net_Buffer.h"
namespace jin
{
namespace lua
{
- /**
- * л
- * int
- * float
- * bool
- *
- * һmessageһtable
- */
- class DenseBuffer
- {
- public:
- DenseBuffer(int len)
- {
- this->len = len;
- buffer = new char[len];
- memset(buffer, 0, len);
- }
- ~DenseBuffer()
- {
- delete[] buffer;
- }
- char* operator&()
- {
- return buffer;
- }
- int size()
- {
- return len;
- }
-
- private:
- int len;
- char* buffer;
- };
-
+
using namespace jin::net;
- static inline Socket* checkSocket(lua_State* L)
+ const int BUFFER_SIZE = 1024;
+
+ static inline Socket* checkSocket(lua_State* L, int pos = 1)
{
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_SOCKET);
+ Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET);
if (proxy != 0 && proxy != nullptr)
return (Socket*)proxy->object;
return nullptr;
}
- /**
- +---------------+
- | message table | -1
- +---------------+
- | ... | -2
- +---------------+
- | ... | ...
- */
- static char* serialize(lua_State* L)
+ static inline net::Buffer* checkNetBuffer(lua_State* L, int pos = 1)
{
- if (!luax_istable(L, -1))
- luax_typerror(L, -1, "table");
- DenseBuffer* buffer;
-
+ Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER);
+ if (proxy != 0 && proxy != nullptr)
+ return (net::Buffer*)proxy->object;
+ return nullptr;
}
+ // return net.Buffer
static int l_accept(lua_State* L)
{
Socket* socket = checkSocket(L);
-
+ Socket* client = socket->accept();
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy));
+ proxy->bind(client, JIN_NETWORK_SOCKET);
+ return 1;
}
+ // return net.Buffer
static int l_receive(lua_State* L)
{
Socket* socket = checkSocket(L);
-
+ char buffer[BUFFER_SIZE] = {0};
+ 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));
+ proxy->bind(netBuffer, JIN_NETWORK_BUFFER);
+ return 1;
}
- static int l_receive(lua_State* L)
+ // Socket:receiveFrom(address, port)
+ static int l_receiveFrom(lua_State* L)
{
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(netBuffer, JIN_NETWORK_BUFFER);
+ return 1;
}
-
+
+ // Socket:send(net.Buffer) -> data_length
static int l_send(lua_State* L)
{
Socket* socket = checkSocket(L);
- DenseBuffer buffer(1024);
- socket->send(&buffer, buffer.size());
+ net::Buffer* buffer = checkNetBuffer(L, 2);
+ int len = socket->send(buffer->buffer, buffer->size);
+ luax_pushinteger(L, len);
+ return 1;
}
- // thread:sendTo(address, port, table)
+ // Socket:sendTo(address, port, net.Buffer)
static int l_sendTo(lua_State* L)
{
Socket* socket = checkSocket(L);
-
+ int address = luax_checkinteger(L, 2);
+ int port = luax_checkinteger(L, 3);
+ net::Buffer* buffer = checkNetBuffer(L, 4);
+ socket->sendTo(buffer->buffer, buffer->size, address, port);
+ return 0;
}
static int l_close(lua_State* L)
{
Socket* socket = checkSocket(L);
-
+ socket->close();
+ return 0;
}
static int l_configBlocking(lua_State* L)
{
Socket* socket = checkSocket(L);
-
+ bool blocking = luax_checkbool(L, 2);
+ socket->configureBlocking(blocking);
+ return 0;
}
static const luaL_Reg socket_function[] = {
{ "accept", l_accept },
{ "receive", l_receive },
- { "receiveFrom", l_receive },
+ { "receiveFrom", l_receiveFrom },
{ "send", l_send },
{ "sendTo", l_sendTo },
{ "close", l_close },
@@ -124,9 +113,9 @@ namespace lua
int luaopen_Socket(lua_State* L)
{
- luax_newtype(L, TYPE_SOURCE, socket_function);
+ luax_newtype(L, JIN_NETWORK_SOCKET, socket_function);
return 0;
}
-}
-} \ No newline at end of file
+} // lua
+} // jin \ No newline at end of file