diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/3rdparty/luax/luax.h | 5 | ||||
-rw-r--r-- | src/libjin/Common/Subsystem.hpp | 2 | ||||
-rw-r--r-- | src/libjin/Net/Socket.h | 1 | ||||
-rw-r--r-- | src/lua/audio/luaopen_Source.cpp | 1 | ||||
-rw-r--r-- | src/lua/bit/luaopen_bit.cpp | 91 | ||||
-rw-r--r-- | src/lua/embed/net.lua.h | 61 | ||||
-rw-r--r-- | src/lua/luaopen_jin.cpp | 2 | ||||
-rw-r--r-- | src/lua/luaopen_types.h | 3 | ||||
-rw-r--r-- | src/lua/math/luaopen_math.cpp | 82 | ||||
-rw-r--r-- | src/lua/net/luaopen_Socket.cpp | 132 | ||||
-rw-r--r-- | src/lua/net/luaopen_net.cpp | 97 |
11 files changed, 312 insertions, 165 deletions
diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h index 4940f9f..94cc2e3 100644 --- a/src/3rdparty/luax/luax.h +++ b/src/3rdparty/luax/luax.h @@ -97,6 +97,7 @@ inline bool luax_checkbool(lua_State *L, int numArg) */ #define luax_pushnumber lua_pushnumber #define luax_pushstring lua_pushstring +#define luax_pushlstring lua_pushlstring #define luax_pushinteger lua_pushinteger #define luax_pushboolean lua_pushboolean @@ -333,6 +334,6 @@ inline int luax_register_searcher(lua_State * L, lua_CFunction f, int pos) return 0; } -#endif +#endif // #if LUA_VERSION_NUM == 501 -#endif
\ No newline at end of file +#endif // __LUAX_H
\ No newline at end of file diff --git a/src/libjin/Common/Subsystem.hpp b/src/libjin/Common/Subsystem.hpp index f4e270b..c3af3dc 100644 --- a/src/libjin/Common/Subsystem.hpp +++ b/src/libjin/Common/Subsystem.hpp @@ -14,7 +14,7 @@ namespace jin struct Setting {}; typedef Setting SettingBase; - bool init(const SettingBase* setting) + bool init(const SettingBase* setting = nullptr) { static bool success = initSystem(setting); return success; diff --git a/src/libjin/Net/Socket.h b/src/libjin/Net/Socket.h index 0eb27e0..fae6bd2 100644 --- a/src/libjin/Net/Socket.h +++ b/src/libjin/Net/Socket.h @@ -33,7 +33,6 @@ namespace net ~Socket(); void configureBlocking(bool bocking); - Socket* accept(); int receive(char* buffer, int size); int send(char* buffer, int size); diff --git a/src/lua/audio/luaopen_Source.cpp b/src/lua/audio/luaopen_Source.cpp index d147e5e..cff3f7f 100644 --- a/src/lua/audio/luaopen_Source.cpp +++ b/src/lua/audio/luaopen_Source.cpp @@ -109,7 +109,6 @@ namespace lua int luaopen_Source(lua_State* L) { luax_newtype(L, TYPE_SOURCE, f); - return 0; } diff --git a/src/lua/bit/luaopen_bit.cpp b/src/lua/bit/luaopen_bit.cpp new file mode 100644 index 0000000..fc3c188 --- /dev/null +++ b/src/lua/bit/luaopen_bit.cpp @@ -0,0 +1,91 @@ +#include "lua/luax.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + + static int l_and(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushinteger(L, a & b); + return 1; + } + + static int l_or(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushinteger(L, a | b); + return 1; + } + + static int l_xor(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushinteger(L, a ^ b); + return 1; + } + + static int l_not(lua_State* L) + { + int n = luax_checkinteger(L, 1); + luax_pushinteger(L, ~n); + return 1; + } + + static int l_lshift(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushinteger(L, a << b); + return 1; + } + + static int l_rshift(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushinteger(L, a >> b); + return 1; + } + + static int l_include(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushboolean(L, (a & b) == b); + return 1; + } + + // return buffer, size + static int l_write(lua_State* L) + { + + } + + static const luaL_Reg f[] = { + { "AND", l_and }, + { "OR" , l_or }, + { "XOR", l_xor }, + { "NOT", l_not }, + { "LS", l_lshift }, + { "RS", l_rshift }, + { "INC", l_include }, + + { "write", l_write}, + + { 0, 0 } + }; + + int luaopen_bit(lua_State* L) + { + luax_newlib(L, f); + return 1; + } + +} +}
\ No newline at end of file diff --git a/src/lua/embed/net.lua.h b/src/lua/embed/net.lua.h new file mode 100644 index 0000000..73b60c3 --- /dev/null +++ b/src/lua/embed/net.lua.h @@ -0,0 +1,61 @@ +/* net.lua */ +static const char* net_lua = R"( +jin.net = jin.net or {} + +--[[ +socketͨŵ +* INT +* FLOAT +* BOOL +* STRING +STRINGжҽ磬0β +һЭ鶨ӣðЭͳһһluaļ +-- s2c_package.lua +local INT = jin.net.dataType.INT +local FLOAT = jin.net.dataType.FLOAT +local BOOL = jin.net.dataType.BOOL +local STRING = jin.net.dataType.STRING +Skill = { + id = INT, + damage = FLOAT, + range = FLOAT, + description = STRING +} +ʱ +local data, size = jin.net.serialize(Message.Skill, message) +Socket:send(data, size) +յʱ +local data, size = Socket:receive() +local message = jin.net.deserialize(Message.Skill, data, size) +]] + +jin.net.INT = 1 +jin.net.FLOAT = 2 +jin.net.BOOL = 3 +jin.net.STRING = 4 + +jin.net.deserialize = function(prototype, data, size) + local message = {} + local i = 1 + for k, t in pairs(prototype) do + message[k] = data[i] + i = i + 1 + end + return message +end + +jin.net.serialize = function(prototype, message) + local data = "" + local size = 0 + for i, v in pairs(message) do + data, size = jin.bit.write(data, size, v) + end + return data, size +end + +-- Э +jin.net.decode = function() + +end + +)";
\ No newline at end of file diff --git a/src/lua/luaopen_jin.cpp b/src/lua/luaopen_jin.cpp index 4113ffe..0a258ec 100644 --- a/src/lua/luaopen_jin.cpp +++ b/src/lua/luaopen_jin.cpp @@ -19,6 +19,7 @@ namespace lua extern int luaopen_joypad(lua_State* L); extern int luaopen_math(lua_State* L); extern int luaopen_thread(lua_State* L); + extern int luaopen_bit(lua_State* L); static int l_getversion(lua_State* L) { @@ -72,6 +73,7 @@ namespace lua {"joypad", luaopen_joypad}, {"math", luaopen_math}, {"thread", luaopen_thread}, + {"bit", luaopen_bit}, {0, 0} }; diff --git a/src/lua/luaopen_types.h b/src/lua/luaopen_types.h index 6c0b4a8..894af48 100644 --- a/src/lua/luaopen_types.h +++ b/src/lua/luaopen_types.h @@ -13,6 +13,9 @@ // thread module #define TYPE_THREAD "Thread" +// network module +#define TYPE_SOCKET "Socket" + namespace jin { namespace lua diff --git a/src/lua/math/luaopen_math.cpp b/src/lua/math/luaopen_math.cpp index 98abeab..e4f68df 100644 --- a/src/lua/math/luaopen_math.cpp +++ b/src/lua/math/luaopen_math.cpp @@ -3,78 +3,18 @@ namespace jin { - namespace lua - { - static int l_and(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushinteger(L, a & b); - return 1; - } - - static int l_or(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushinteger(L, a | b); - return 1; - } - - static int l_xor(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushinteger(L, a ^ b); - return 1; - } - - static int l_not(lua_State* L) - { - int n = luax_checkinteger(L, 1); - luax_pushinteger(L, ~n); - return 1; - } - - static int l_lshift(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushinteger(L, a << b); - return 1; - } - - static int l_rshift(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushinteger(L, a >> b); - return 1; - } - - static int l_include(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushboolean(L, (a & b) == b); - return 1; - } +namespace lua +{ - static const luaL_Reg f[] = { - { "AND", l_and }, - { "OR" , l_or }, - { "XOR", l_xor }, - { "NOT", l_not }, - { "LSHIFT", l_lshift }, - { "RSHIFT", l_rshift }, - { "INC", l_include}, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { 0, 0 } + }; - int luaopen_math(lua_State* L) - { - luax_newlib(L, f); - return 1; - } + int luaopen_math(lua_State* L) + { + luax_newlib(L, f); + return 1; } + +} }
\ No newline at end of file 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 |