diff options
Diffstat (limited to 'src/lua/net')
-rw-r--r-- | src/lua/net/luaopen_Socket.cpp | 132 | ||||
-rw-r--r-- | src/lua/net/luaopen_net.cpp | 97 |
2 files changed, 140 insertions, 89 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 diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp index 633ccdd..8d67487 100644 --- a/src/lua/net/luaopen_net.cpp +++ b/src/lua/net/luaopen_net.cpp @@ -1,104 +1,24 @@ -/** -* Notice: the net module is not finished yet. -*/ #include "lua/luax.h" -/* -#include "3rdparty/tekcos/tekcos.h" +#include "libjin/jin.h" namespace jin { namespace lua { - struct - { - tk_TCPsocket* sk; - }context; - - //* A table is needed. For example: - //* local conf = { - //* mode = "server", - //* ip = "", - //* port = 8000 - //* } - - static int l_open(lua_State* L) - { - // init context.sk - context.sk = 0; - if (!luax_istable(L, 1)) - { - luax_typerror(L, 1, "table is needed"); - return 0; - } - luax_getfield(L, 1, "mode"); - if (luax_isnil(L, -1)) - {// no mode field - luax_error(L, "mode field is needed, but get nil"); - return 0; - } - const char* mode = luax_checkstring(L, -1); - if (strcmp(mode, "server") == 0 || strcmp(mode, "client") == 0) - { - - if (strcmp(mode, "server") == 0) - {// a server, ignore ip field - - } - else - { - - } - } - else - { - luax_error(L, "\"server\" or \"client\" is needed, but get %s", mode); - return 0; - } - return 1; - } - - static int l_accept(lua_State* L) - { - return 1; - } - - static int l_send(lua_State* L) - { - return 1; - } - static int l_recv(lua_State* L) - { - return 1; - } - - static int l_close(lua_State* L) - { - return 1; - } - - static int l_nonblocking(lua_State* L) - { - return 1; - } - - // block mode by default - static int l_blocking(lua_State* L) + static int l_initNetwork(lua_State* L) { + jin::net::Net::get()->init(); return 1; } static const luaL_Reg f[] = { - {"open", l_open}, - {"accept", l_accept}, - {"send", l_send}, - {"recv", l_recv}, - {"close", l_close}, - {"blocking", l_blocking }, - {"nonblocking", l_nonblocking}, - {0, 0} + { "init", l_initNetwork}, + { 0, 0 } }; + extern int luaopen_Socket(lua_State* L); + // only tcp int luaopen_net(lua_State* L) { @@ -108,5 +28,4 @@ namespace lua } } -} -*/
\ No newline at end of file +}
\ No newline at end of file |