aboutsummaryrefslogtreecommitdiff
path: root/src/lua/net/luaopen_Socket.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-08-08 21:31:35 +0800
committerchai <chaifix@163.com>2018-08-08 21:31:35 +0800
commit3a7b295e0fe166d6e4a44cfe17ad8c4f441e6084 (patch)
treec67c820e51b9ccd19893a590c7aaffcce384d547 /src/lua/net/luaopen_Socket.cpp
parentaa7549126a4e821cecf75fbc32a433a7f8122d2b (diff)
*update
Diffstat (limited to 'src/lua/net/luaopen_Socket.cpp')
-rw-r--r--src/lua/net/luaopen_Socket.cpp132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp
new file mode 100644
index 0000000..7dbfb33
--- /dev/null
+++ b/src/lua/net/luaopen_Socket.cpp
@@ -0,0 +1,132 @@
+#include "lua/luax.h"
+#include "../luaopen_types.h"
+#include "libjin/jin.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)
+ {
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_SOCKET);
+ if (proxy != 0 && proxy != nullptr)
+ return (Socket*)proxy->object;
+ return nullptr;
+ }
+
+ /**
+ +---------------+
+ | message table | -1
+ +---------------+
+ | ... | -2
+ +---------------+
+ | ... | ...
+ */
+ static char* serialize(lua_State* L)
+ {
+ if (!luax_istable(L, -1))
+ luax_typerror(L, -1, "table");
+ DenseBuffer* buffer;
+
+ }
+
+ static int l_accept(lua_State* L)
+ {
+ Socket* socket = checkSocket(L);
+
+ }
+
+ static int l_receive(lua_State* L)
+ {
+ Socket* socket = checkSocket(L);
+
+ }
+
+ static int l_receive(lua_State* L)
+ {
+ Socket* socket = checkSocket(L);
+
+ }
+
+ static int l_send(lua_State* L)
+ {
+ Socket* socket = checkSocket(L);
+ DenseBuffer buffer(1024);
+ socket->send(&buffer, buffer.size());
+ }
+
+ // thread:sendTo(address, port, table)
+ static int l_sendTo(lua_State* L)
+ {
+ Socket* socket = checkSocket(L);
+
+ }
+
+ static int l_close(lua_State* L)
+ {
+ Socket* socket = checkSocket(L);
+
+ }
+
+ static int l_configBlocking(lua_State* L)
+ {
+ Socket* socket = checkSocket(L);
+
+ }
+
+ static const luaL_Reg socket_function[] = {
+ { "accept", l_accept },
+ { "receive", l_receive },
+ { "receiveFrom", l_receive },
+ { "send", l_send },
+ { "sendTo", l_sendTo },
+ { "close", l_close },
+ { "configBlocking", l_configBlocking },
+ { 0, 0 }
+ };
+
+ int luaopen_Socket(lua_State* L)
+ {
+ luax_newtype(L, TYPE_SOURCE, socket_function);
+ return 0;
+ }
+
+}
+} \ No newline at end of file