From 96c52c47ca48c56f1ec9a67dd1338db300c630fc Mon Sep 17 00:00:00 2001 From: xiyoo0812 Date: Mon, 4 Sep 2023 06:50:14 +0000 Subject: [PATCH] =?UTF-8?q?!218=20=E6=A1=86=E6=9E=B6=E7=BC=96=E8=A7=A3?= =?UTF-8?q?=E7=A0=81=E8=B0=83=E6=95=B4=201=E3=80=81=E7=BC=96=E8=A7=A3?= =?UTF-8?q?=E7=A0=81=E8=B0=83=E7=94=A8=E4=BB=8Elua=E7=A7=BB=E5=88=B0C++=20?= =?UTF-8?q?2=E3=80=81http/redis/mongo/json/lua=E7=BC=96=E8=A7=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=203=E3=80=81redis=E6=94=AF=E6=8C=81=E9=9B=86?= =?UTF-8?q?=E7=BE=A4=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 1 - core/luabus/src/lua_socket_node.cpp | 98 +++--- core/luabus/src/lua_socket_node.h | 9 +- core/luabus/src/luabus.cpp | 4 +- core/luabus/src/socket_mgr.cpp | 2 +- core/luabus/src/socket_mgr.h | 15 +- core/luabus/src/socket_stream.cpp | 27 +- core/luabus/src/socket_stream.h | 4 +- extend/lbson/src/bson.h | 41 ++- extend/lcodec/lcodec.vcxproj | 3 +- extend/lcodec/lcodec.vcxproj.filters | 9 +- extend/lcodec/src/crc.h | 344 ------------------- extend/lcodec/src/http.h | 212 ++++++++++++ extend/lcodec/src/lcodec.cpp | 63 ++-- extend/lcodec/src/lcodec.h | 3 +- extend/lcodec/src/redis.h | 206 ++++++++++++ extend/lcrypt/lcrypt.vcxproj | 2 + extend/lcrypt/lcrypt.vcxproj.filters | 6 + extend/lcrypt/src/crc.c | 343 +++++++++++++++++++ extend/lcrypt/src/crc.h | 18 + extend/lcrypt/src/lcrypt.c | 40 +++ extend/lcrypt/src/lcrypt.h | 1 + extend/lcurl/src/lcurl.cpp | 4 +- extend/lcurl/src/lcurl.h | 4 +- extend/lhttp/lhttp.lmak | 38 --- extend/lhttp/lhttp.mak | 133 -------- extend/lhttp/lhttp.vcxproj | 78 ----- extend/lhttp/lhttp.vcxproj.filters | 21 -- extend/lhttp/src/http.h | 219 ------------- extend/lhttp/src/lhttp.cpp | 47 --- extend/ljson/src/ljson.cpp | 16 +- extend/ljson/src/ljson.h | 31 ++ extend/luakit/include/lua_base.h | 4 +- extend/luakit/include/lua_buff.h | 8 +- extend/luakit/include/lua_codec.h | 68 ++-- extend/luakit/include/lua_function.h | 49 ++- extend/luakit/include/lua_kit.h | 63 ++-- extend/luakit/include/lua_slice.h | 8 +- extend/luakit/include/lua_stack.h | 2 - extend/luakit/include/lua_table.h | 12 +- quanta.sln | 8 - script/agent/redis_agent.lua | 2 +- script/basic/environ.lua | 4 +- script/basic/library.lua | 2 - script/basic/string.lua | 18 - script/driver/mongo.lua | 16 +- script/driver/redis.lua | 472 ++++++++------------------- script/driver/redisps.lua | 13 +- script/driver/socket.lua | 77 +---- script/network/http_server.lua | 69 ++-- script/queue/redis_mq.lua | 7 +- script/store/redis_mgr.lua | 17 +- server/business/entity/entity.lua | 9 + server/cache/document.lua | 27 +- server/center/gm_mgr.lua | 19 +- server/center/gm_page.lua | 2 +- server/monitor/monitor_mgr.lua | 10 +- server/monitor/redis_discovery.lua | 10 +- server/robot/accord_mgr.lua | 86 +++-- server/router/transfer_mgr.lua | 1 - server/test.lua | 2 +- server/test/http_test.lua | 18 +- server/test/redis_cluster.lua | 22 ++ server/test/redis_test.lua | 76 +++-- server/test/url_test.lua | 34 -- 65 files changed, 1508 insertions(+), 1769 deletions(-) delete mode 100644 extend/lcodec/src/crc.h create mode 100644 extend/lcodec/src/http.h create mode 100644 extend/lcodec/src/redis.h create mode 100644 extend/lcrypt/src/crc.c create mode 100644 extend/lcrypt/src/crc.h delete mode 100644 extend/lhttp/lhttp.lmak delete mode 100644 extend/lhttp/lhttp.mak delete mode 100644 extend/lhttp/lhttp.vcxproj delete mode 100644 extend/lhttp/lhttp.vcxproj.filters delete mode 100644 extend/lhttp/src/http.h delete mode 100644 extend/lhttp/src/lhttp.cpp create mode 100644 server/test/redis_cluster.lua delete mode 100644 server/test/url_test.lua diff --git a/Makefile b/Makefile index de4a71b0..763b9abc 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,6 @@ luaext: cd extend/lcrypt; make SOLUTION_DIR=$(CUR_DIR) -f lcrypt.mak; cd extend/lcurl; make SOLUTION_DIR=$(CUR_DIR) -f lcurl.mak; cd extend/ldetour; make SOLUTION_DIR=$(CUR_DIR) -f ldetour.mak; - cd extend/lhttp; make SOLUTION_DIR=$(CUR_DIR) -f lhttp.mak; cd extend/ljson; make SOLUTION_DIR=$(CUR_DIR) -f ljson.mak; cd extend/lstdfs; make SOLUTION_DIR=$(CUR_DIR) -f lstdfs.mak; cd extend/ltimer; make SOLUTION_DIR=$(CUR_DIR) -f ltimer.mak; diff --git a/core/luabus/src/lua_socket_node.cpp b/core/luabus/src/lua_socket_node.cpp index 22425d0b..e06f7961 100644 --- a/core/luabus/src/lua_socket_node.cpp +++ b/core/luabus/src/lua_socket_node.cpp @@ -24,7 +24,7 @@ lua_socket_node::lua_socket_node(uint32_t token, lua_State* L, std::shared_ptrset_package_callback(token, [=](slice* data){ - on_recv(data); + return on_recv(data); }); } @@ -48,12 +48,6 @@ int lua_socket_node::call_data(lua_State* L) { return 1; } -int lua_socket_node::call_text(const char* data, uint32_t data_len) { - if (data_len > SOCKET_PACKET_MAX) return 0; - m_mgr->send(m_token, data, data_len); - return data_len; -} - int lua_socket_node::call_head(uint16_t cmd_id, uint8_t flag, uint8_t type, uint8_t crc8, uint32_t session_id, const char* data, uint32_t data_len) { size_t length = data_len + sizeof(socket_header); if (length <= USHRT_MAX) { @@ -205,20 +199,18 @@ int lua_socket_node::transfer_hash(lua_State* L, uint32_t session_id, uint16_t s return 0; } -void lua_socket_node::on_recv(slice* slice) { +int lua_socket_node::on_recv(slice* slice) { if (eproto_type::proto_head == m_proto_type) { - on_call_head(slice); - return; - } - if (eproto_type::proto_common == m_proto_type) { - on_call_common(slice); - return; + return on_call_head(slice); } if (eproto_type::proto_text == m_proto_type) { - on_call_text(slice); - return; + return on_call_text(slice); + } + if (eproto_type::proto_rpc != m_proto_type) { + return on_call_data(slice); } + size_t data_len; size_t header_len = sizeof(router_header); auto hdata = slice->peek(header_len); router_header* header = (router_header*)hdata; @@ -226,14 +218,7 @@ void lua_socket_node::on_recv(slice* slice) { if (msg == rpc_type::transfer_call) { header_len = sizeof(transfer_header); } - - size_t data_len; slice->erase(header_len); - auto data = (char*)slice->data(&data_len); - if (data_len == 0) { - return; - } - switch (msg) { case rpc_type::remote_call: on_call(header, slice); @@ -241,29 +226,39 @@ void lua_socket_node::on_recv(slice* slice) { case rpc_type::transfer_call: on_transfer((transfer_header*)header, slice); break; - case rpc_type::forward_target: - if (!m_router->do_forward_target(header, data, data_len)) - on_forward_error(header, slice); + case rpc_type::forward_target:{ + auto data = (char*)slice->data(&data_len); + if (!m_router->do_forward_target(header, data, data_len)) { + on_forward_error(header, slice); + } + } break; - case rpc_type::forward_master: - if (!m_router->do_forward_master(header, data, data_len)) - on_forward_error(header, slice); + case rpc_type::forward_master: { + auto data = (char*)slice->data(&data_len); + if (!m_router->do_forward_master(header, data, data_len)) { + on_forward_error(header, slice); + } + } break; - case rpc_type::forward_hash: - if (!m_router->do_forward_hash(header, data, data_len)) - on_forward_error(header, slice); + case rpc_type::forward_hash: { + auto data = (char*)slice->data(&data_len); + if (!m_router->do_forward_hash(header, data, data_len)) { + on_forward_error(header, slice); + } + } break; case rpc_type::forward_broadcast: { size_t broadcast_num = 0; - if (m_router->do_forward_broadcast(header, m_token, data, data_len, broadcast_num)) + auto data = (char*)slice->data(&data_len); + if (m_router->do_forward_broadcast(header, m_token, data, data_len, broadcast_num)) { on_forward_broadcast(header, broadcast_num); - else + } else { on_forward_error(header, slice); + } } break; - default: - break; } + return header->len; } void lua_socket_node::on_forward_error(router_header* header, slice* slice) { @@ -279,13 +274,6 @@ void lua_socket_node::on_forward_broadcast(router_header* header, size_t broadca } } -void lua_socket_node::on_call(router_header* header, slice* slice) { - m_codec->set_slice(slice); - uint8_t flag = header->context & 0xff; - uint32_t session_id = header->session_id; - m_luakit->object_call(this, "on_call", nullptr, m_codec, std::tie(), header->len, session_id, flag); -} - void lua_socket_node::on_transfer(transfer_header* header, slice* slice) { uint8_t service_id = header->service_id; uint32_t target_id = header->target_id; @@ -293,7 +281,7 @@ void lua_socket_node::on_transfer(transfer_header* header, slice* slice) { m_luakit->object_call(this, "on_transfer", nullptr, std::tie(), header->len, session_id, service_id, target_id, slice); } -void lua_socket_node::on_call_head(slice* slice) { +int lua_socket_node::on_call_head(slice* slice) { size_t header_len = sizeof(socket_header); auto data = slice->peek(header_len); socket_header* header = (socket_header*)data; @@ -306,15 +294,27 @@ void lua_socket_node::on_call_head(slice* slice) { slice->erase(header_len); std::string body((char*)slice->head(), slice->size()); m_luakit->object_call(this, "on_call_head", nullptr, std::tie(), header->len, cmd_id, flag, type, crc8, session_id, body); + return header->len; } -void lua_socket_node::on_call_text(slice* slice) { - std::string body((char*)slice->head(), slice->size()); - m_luakit->object_call(this, "on_call_text", nullptr, std::tie(), body.size(), body); +void lua_socket_node::on_call(router_header* header, slice* slice) { + m_codec->set_slice(slice); + uint8_t flag = header->context & 0xff; + uint32_t session_id = header->session_id; + m_luakit->object_call(this, "on_call", nullptr, m_codec, std::tie(), header->len, session_id, flag); } -void lua_socket_node::on_call_common(slice* slice) { +int lua_socket_node::on_call_data(slice* slice) { m_codec->set_slice(slice); - m_luakit->object_call(this, "on_call_common", nullptr, m_codec, std::tie(), slice->size()); + size_t buf_size = slice->size(); + m_luakit->object_call(this, "on_call_data", nullptr, m_codec, std::tie(), buf_size); + return buf_size; } +int lua_socket_node::on_call_text(slice* slice) { + bool success = true; + m_codec->set_slice(slice); + size_t buf_size = slice->size(); + m_luakit->object_call(this, "on_call_data", [&](std::string_view) { success = false; }, m_codec, std::tie(), buf_size); + return success ? (buf_size - slice->size()) : -1; +} diff --git a/core/luabus/src/lua_socket_node.h b/core/luabus/src/lua_socket_node.h index 52acc2ed..35df5957 100644 --- a/core/luabus/src/lua_socket_node.h +++ b/core/luabus/src/lua_socket_node.h @@ -21,7 +21,6 @@ class lua_socket_node void set_codec(codec_base* codec) { m_codec = codec; } int call_data(lua_State* L); - int call_text(const char* data, uint32_t data_len); int call(lua_State* L, uint32_t session_id, uint8_t flag); int call_head(uint16_t cmd_id, uint8_t flag, uint8_t type, uint8_t crc8, uint32_t session_id, const char* data, uint32_t data_len); int forward_target(lua_State* L, uint32_t session_id, uint8_t flag, uint32_t target_id); @@ -60,10 +59,10 @@ class lua_socket_node uint16_t m_sindex = 1; private: - void on_recv(slice* slice); - void on_call_head(slice* slice); - void on_call_text(slice* slice); - void on_call_common(slice* slice); + int on_recv(slice* slice); + int on_call_head(slice* slice); + int on_call_text(slice* slice); + int on_call_data(slice* slice); void on_call(router_header* header, slice* slice); void on_transfer(transfer_header* header, slice* slice); void on_forward_broadcast(router_header* header, size_t target_size); diff --git a/core/luabus/src/luabus.cpp b/core/luabus/src/luabus.cpp index dd9b2354..6895e8aa 100644 --- a/core/luabus/src/luabus.cpp +++ b/core/luabus/src/luabus.cpp @@ -45,7 +45,8 @@ namespace luabus { "rpc", eproto_type::proto_rpc, "head", eproto_type::proto_head, "text", eproto_type::proto_text, - "common", eproto_type::proto_common + "mongo", eproto_type::proto_mongo, + "mysql", eproto_type::proto_mysql ); kit_state.new_class( "send", &socket_udp::send, @@ -78,7 +79,6 @@ namespace luabus { "set_codec", &lua_socket_node::set_codec, "call_head", &lua_socket_node::call_head, "call_data", &lua_socket_node::call_data, - "call_text", &lua_socket_node::call_text, "set_nodelay", &lua_socket_node::set_nodelay, "set_timeout", &lua_socket_node::set_timeout, "forward_hash", &lua_socket_node::forward_hash, diff --git a/core/luabus/src/socket_mgr.cpp b/core/luabus/src/socket_mgr.cpp index 2fa2e8bc..bd32f423 100644 --- a/core/luabus/src/socket_mgr.cpp +++ b/core/luabus/src/socket_mgr.cpp @@ -300,7 +300,7 @@ void socket_mgr::set_connect_callback(uint32_t token, const std::function& cb) { +void socket_mgr::set_package_callback(uint32_t token, const std::function& cb) { auto node = get_object(token); if (node) { node->set_package_callback(cb); diff --git a/core/luabus/src/socket_mgr.h b/core/luabus/src/socket_mgr.h index 6d2ff6fb..596cdca8 100644 --- a/core/luabus/src/socket_mgr.h +++ b/core/luabus/src/socket_mgr.h @@ -22,11 +22,12 @@ enum class elink_status : int // 协议类型 enum class eproto_type : int { - proto_rpc = 0, // rpc协议 - proto_head = 1, // head协议,根据协议头解析 - proto_text = 2, // text协议,根据文本协议 - proto_common = 3, // 通用协议,协议前4个字节为长度 - proto_max = 4, // max + proto_rpc = 0, // rpc协议,根据协议头解析 + proto_head = 1, // head协议,根据协议头解析 + proto_text = 2, // text协议,文本协议 + proto_mongo = 3, // mongo协议,协议前4个字节为长度 + proto_mysql = 4, // mysql协议,协议前3个字节为长度 + proto_max = 5, // max }; struct sendv_item @@ -51,7 +52,7 @@ struct socket_object virtual void set_accept_callback(const std::function& cb) { } virtual void set_connect_callback(const std::function& cb) { } virtual void set_error_callback(const std::function& cb) { } - virtual void set_package_callback(const std::function& cb) { } + virtual void set_package_callback(const std::function& cb) { } #ifdef _MSC_VER virtual void on_complete(WSAOVERLAPPED* ovl) = 0; @@ -94,7 +95,7 @@ class socket_mgr void set_accept_callback(uint32_t token, const std::function& cb); void set_connect_callback(uint32_t token, const std::function& cb); - void set_package_callback(uint32_t token, const std::function& cb); + void set_package_callback(uint32_t token, const std::function& cb); void set_error_callback(uint32_t token, const std::function& cb); bool watch_listen(socket_t fd, socket_object* object); diff --git a/core/luabus/src/socket_stream.cpp b/core/luabus/src/socket_stream.cpp index a09438ad..3897471a 100644 --- a/core/luabus/src/socket_stream.cpp +++ b/core/luabus/src/socket_stream.cpp @@ -410,7 +410,7 @@ void socket_stream::do_send(size_t max_len, bool is_eof) { return; } total_send += send_len; - m_send_buffer->pop_space((size_t)send_len); + m_send_buffer->pop_size((size_t)send_len); } if (is_eof || max_len == 0) { on_error("connection-lost"); @@ -496,14 +496,22 @@ void socket_stream::dispatch_package() { } package_size = header->len; } - else if (eproto_type::proto_common == m_proto_type) { + else if (eproto_type::proto_mongo == m_proto_type) { uint32_t* length = (uint32_t*)m_recv_buffer->peek_data(sizeof(uint32_t)); if (!length) { break; } - //头长度只包含内容,不包括长度 + //package_size = length + contents package_size = *length; } + else if (eproto_type::proto_mysql == m_proto_type) { + uint32_t* length = (uint32_t*)m_recv_buffer->peek_data(sizeof(uint32_t)); + if (!length) { + break; + } + //package_size = length + serialize_id + contents + package_size = ((*length) >> 8) + sizeof(uint32_t); + } else if (eproto_type::proto_text == m_proto_type) { package_size = m_recv_buffer->size(); if (data_len == 0) break; @@ -518,9 +526,18 @@ void socket_stream::dispatch_package() { } // 数据包还没有收完整 if (data_len < package_size) break; - m_package_cb(m_recv_buffer->get_slice(package_size)); + int read_size = m_package_cb(m_recv_buffer->get_slice(package_size)); + // 数据包还没有收完整 + if (read_size == 0) { + break; + } + // 数据包解析失败 + if (read_size < 0) { + on_error("package-read-err"); + break; + } // 接收缓冲读游标调整 - m_recv_buffer->pop_size(package_size); + m_recv_buffer->pop_size(read_size); m_last_recv_time = ltimer::steady_ms(); // 防止单个连接处理太久,不能大于100ms if (m_last_recv_time - now > 100) break; diff --git a/core/luabus/src/socket_stream.h b/core/luabus/src/socket_stream.h index 6e379d78..848134ab 100644 --- a/core/luabus/src/socket_stream.h +++ b/core/luabus/src/socket_stream.h @@ -19,7 +19,7 @@ struct socket_stream : public socket_object void try_connect(); void close() override; void set_accept_callback(const std::function& cb) override { m_accept_cb = cb; } - void set_package_callback(const std::function& cb) override { m_package_cb = cb; } + void set_package_callback(const std::function& cb) override { m_package_cb = cb; } void set_error_callback(const std::function& cb) override { m_error_cb = cb; } void set_connect_callback(const std::function& cb) override { m_connect_cb = cb; } void set_timeout(int duration) override { m_timeout = duration; } @@ -72,7 +72,7 @@ struct socket_stream : public socket_object int m_ovl_ref = 0; #endif - std::function m_package_cb = nullptr; + std::function m_package_cb = nullptr; std::function m_error_cb = nullptr; std::function m_accept_cb = nullptr; std::function m_connect_cb = nullptr; diff --git a/extend/lbson/src/bson.h b/extend/lbson/src/bson.h index 9ba5b622..b325af44 100644 --- a/extend/lbson/src/bson.h +++ b/extend/lbson/src/bson.h @@ -83,7 +83,11 @@ namespace lbson { int decode_slice(lua_State* L, slice* slice) { lua_settop(L, 0); - unpack_dict(L, slice, false); + try { + unpack_dict(L, slice, false); + } catch (const exception& e){ + luaL_error(L, e.what()); + } return lua_gettop(L); } @@ -130,11 +134,11 @@ namespace lbson { void write_binary(bson_value* value) { m_buffer.write(value->str.size() + 1); m_buffer.write(value->stype); - m_buffer.write(value->str.c_str(), value->str.size()); + m_buffer.write(value->str); } void write_cstring(const char* buf, size_t len) { - m_buffer.write(buf, len); + m_buffer.push_data((uint8_t*)buf, len); m_buffer.write('\0'); } @@ -242,7 +246,7 @@ namespace lbson { case bson_type::BSON_JSCODE: case bson_type::BSON_DOCUMENT: case bson_type::BSON_OBJECTID: - m_buffer.write(value->str.c_str(), value->str.size()); + m_buffer.write(value->str); break; case bson_type::BSON_REGEX: write_cstring(value->str.c_str(), value->str.size()); @@ -320,7 +324,7 @@ namespace lbson { const char* read_bytes(lua_State* L, slice* slice, size_t sz) { const char* dst = (const char*)slice->peek(sz); if (!dst) { - luaL_error(L, "Invalid bson string , length = %d", sz); + throw length_error("invalid bson string , length = " + sz); } slice->erase(sz); return dst; @@ -329,7 +333,7 @@ namespace lbson { const char* read_string(lua_State* L, slice* slice, size_t& sz) { sz = (size_t)read_val(L, slice); if (sz <= 0) { - luaL_error(L, "Invalid bson string , length = %d", sz); + throw length_error("invalid bson string , length = " + sz); } sz = sz - 1; const char* dst = ""; @@ -345,14 +349,14 @@ namespace lbson { const char* dst = (const char*)slice->data(&sz); for (l = 0; l < sz; ++l) { if (l == sz - 1) { - luaL_error(L, "Invalid bson block : cstring"); + throw invalid_argument("invalid bson block : cstring"); } if (dst[l] == '\0') { slice->erase(l + 1); return dst; } } - luaL_error(L, "Invalid bson block : cstring"); + throw invalid_argument("invalid bson block : cstring"); return ""; } @@ -371,7 +375,7 @@ namespace lbson { void unpack_dict(lua_State* L, slice* slice, bool isarray) { uint32_t sz = read_val(L, slice); if (slice->size() < sz - 4) { - luaL_error(L, "decode can't unpack one value"); + throw length_error("decode can't unpack one value"); } lua_createtable(L, 0, 8); while (!slice->empty()) { @@ -427,7 +431,7 @@ namespace lbson { lua_push_object(L, new bson_value(bt, 0)); break; default: - luaL_error(L, "Invalid bson type : %d", bt); + throw invalid_argument("invalid bson type:" + (int)bt); } lua_rawset(L, -3); } @@ -454,27 +458,30 @@ namespace lbson { } virtual size_t decode(lua_State* L) { - if (!m_slice) { - luaL_error(L, "source slice is null"); - } + if (!m_slice) return 0; //skip length + request_id m_slice->erase(8); uint32_t session_id = m_bson->read_val(L, m_slice); uint32_t opcode = m_bson->read_val(L, m_slice); if (opcode != OP_MSG_CODE) { - luaL_error(L, "Unsupported opcode: %d", opcode); + throw invalid_argument("unsupported opcode:" + opcode); } uint32_t flags = m_bson->read_val(L, m_slice); if (flags > 0 && ((flags & OP_CHECKSUM) != 0 || ((flags ^ OP_MORE_COME) != 0))) { - luaL_error(L, "Unsupported flags: %d", opcode); + throw invalid_argument("unsupported flags:" + flags); } uint32_t payload = m_bson->read_val(L, m_slice); if (payload != 0) { - luaL_error(L, "Unsupported payload: %d", opcode); + throw invalid_argument("unsupported payload:" + payload); } int otop = lua_gettop(L); lua_pushinteger(L, session_id); - m_bson->unpack_dict(L, m_slice, false); + try { + m_bson->unpack_dict(L, m_slice, false); + } catch (const exception& e){ + lua_settop(L, otop); + throw e; + } return lua_gettop(L) - otop; } diff --git a/extend/lcodec/lcodec.vcxproj b/extend/lcodec/lcodec.vcxproj index 82a24e80..8a378106 100644 --- a/extend/lcodec/lcodec.vcxproj +++ b/extend/lcodec/lcodec.vcxproj @@ -8,11 +8,12 @@ - + + diff --git a/extend/lcodec/lcodec.vcxproj.filters b/extend/lcodec/lcodec.vcxproj.filters index 220a9eef..d203b5db 100644 --- a/extend/lcodec/lcodec.vcxproj.filters +++ b/extend/lcodec/lcodec.vcxproj.filters @@ -4,21 +4,24 @@ inc - - inc - inc inc + + inc + inc inc + + inc + diff --git a/extend/lcodec/src/crc.h b/extend/lcodec/src/crc.h deleted file mode 100644 index ac84cc31..00000000 --- a/extend/lcodec/src/crc.h +++ /dev/null @@ -1,344 +0,0 @@ -#pragma once - -#include - -namespace lcodec { - static const uint8_t crc8tab_lsb[256] = { - 0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c, - 0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38, 0x3c, - 0x23, 0x27, 0x2b, 0x2f, 0x33, 0x37, 0x3b, 0x3f, - 0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17, 0x1b, 0x1f, - 0x25, 0x21, 0x2d, 0x29, 0x35, 0x31, 0x3d, 0x39, - 0x05, 0x01, 0x0d, 0x09, 0x15, 0x11, 0x1d, 0x19, - 0x06, 0x02, 0x0e, 0x0a, 0x16, 0x12, 0x1e, 0x1a, - 0x26, 0x22, 0x2e, 0x2a, 0x36, 0x32, 0x3e, 0x3a, - 0x29, 0x2d, 0x21, 0x25, 0x39, 0x3d, 0x31, 0x35, - 0x09, 0x0d, 0x01, 0x05, 0x19, 0x1d, 0x11, 0x15, - 0x0a, 0x0e, 0x02, 0x06, 0x1a, 0x1e, 0x12, 0x16, - 0x2a, 0x2e, 0x22, 0x26, 0x3a, 0x3e, 0x32, 0x36, - 0x0c, 0x08, 0x04, 0x00, 0x1c, 0x18, 0x14, 0x10, - 0x2c, 0x28, 0x24, 0x20, 0x3c, 0x38, 0x34, 0x30, - 0x2f, 0x2b, 0x27, 0x23, 0x3f, 0x3b, 0x37, 0x33, - 0x0f, 0x0b, 0x07, 0x03, 0x1f, 0x1b, 0x17, 0x13, - 0x31, 0x35, 0x39, 0x3d, 0x21, 0x25, 0x29, 0x2d, - 0x11, 0x15, 0x19, 0x1d, 0x01, 0x05, 0x09, 0x0d, - 0x12, 0x16, 0x1a, 0x1e, 0x02, 0x06, 0x0a, 0x0e, - 0x32, 0x36, 0x3a, 0x3e, 0x22, 0x26, 0x2a, 0x2e, - 0x14, 0x10, 0x1c, 0x18, 0x04, 0x00, 0x0c, 0x08, - 0x34, 0x30, 0x3c, 0x38, 0x24, 0x20, 0x2c, 0x28, - 0x37, 0x33, 0x3f, 0x3b, 0x27, 0x23, 0x2f, 0x2b, - 0x17, 0x13, 0x1f, 0x1b, 0x07, 0x03, 0x0f, 0x0b, - 0x18, 0x1c, 0x10, 0x14, 0x08, 0x0c, 0x00, 0x04, - 0x38, 0x3c, 0x30, 0x34, 0x28, 0x2c, 0x20, 0x24, - 0x3b, 0x3f, 0x33, 0x37, 0x2b, 0x2f, 0x23, 0x27, - 0x1b, 0x1f, 0x13, 0x17, 0x0b, 0x0f, 0x03, 0x07, - 0x3d, 0x39, 0x35, 0x31, 0x2d, 0x29, 0x25, 0x21, - 0x1d, 0x19, 0x15, 0x11, 0x0d, 0x09, 0x05, 0x01, - 0x1e, 0x1a, 0x16, 0x12, 0x0e, 0x0a, 0x06, 0x02 - }; - - static const uint8_t crc8tab_msb[256] = { - 0x00, 0x31, 0x62, 0x53, 0xc4, 0xf5, 0xa6, 0x97, - 0xb9, 0x88, 0xdb, 0xea, 0x7d, 0x4c, 0x1f, 0x2e, - 0x43, 0x72, 0x21, 0x10, 0x87, 0xb6, 0xe5, 0xd4, - 0xfa, 0xcb, 0x98, 0xa9, 0x3e, 0x0f, 0x5c, 0x6d, - 0x86, 0xb7, 0xe4, 0xd5, 0x42, 0x73, 0x20, 0x11, - 0x3f, 0x0e, 0x5d, 0x6c, 0xfb, 0xca, 0x99, 0xa8, - 0xc5, 0xf4, 0xa7, 0x96, 0x01, 0x30, 0x63, 0x52, - 0x7c, 0x4d, 0x1e, 0x2f, 0xb8, 0x89, 0xda, 0xeb, - 0x3d, 0x0c, 0x5f, 0x6e, 0xf9, 0xc8, 0x9b, 0xaa, - 0x84, 0xb5, 0xe6, 0xd7, 0x40, 0x71, 0x22, 0x13, - 0x7e, 0x4f, 0x1c, 0x2d, 0xba, 0x8b, 0xd8, 0xe9, - 0xc7, 0xf6, 0xa5, 0x94, 0x03, 0x32, 0x61, 0x50, - 0xbb, 0x8a, 0xd9, 0xe8, 0x7f, 0x4e, 0x1d, 0x2c, - 0x02, 0x33, 0x60, 0x51, 0xc6, 0xf7, 0xa4, 0x95, - 0xf8, 0xc9, 0x9a, 0xab, 0x3c, 0x0d, 0x5e, 0x6f, - 0x41, 0x70, 0x23, 0x12, 0x85, 0xb4, 0xe7, 0xd6, - 0x7a, 0x4b, 0x18, 0x29, 0xbe, 0x8f, 0xdc, 0xed, - 0xc3, 0xf2, 0xa1, 0x90, 0x07, 0x36, 0x65, 0x54, - 0x39, 0x08, 0x5b, 0x6a, 0xfd, 0xcc, 0x9f, 0xae, - 0x80, 0xb1, 0xe2, 0xd3, 0x44, 0x75, 0x26, 0x17, - 0xfc, 0xcd, 0x9e, 0xaf, 0x38, 0x09, 0x5a, 0x6b, - 0x45, 0x74, 0x27, 0x16, 0x81, 0xb0, 0xe3, 0xd2, - 0xbf, 0x8e, 0xdd, 0xec, 0x7b, 0x4a, 0x19, 0x28, - 0x06, 0x37, 0x64, 0x55, 0xc2, 0xf3, 0xa0, 0x91, - 0x47, 0x76, 0x25, 0x14, 0x83, 0xb2, 0xe1, 0xd0, - 0xfe, 0xcf, 0x9c, 0xad, 0x3a, 0x0b, 0x58, 0x69, - 0x04, 0x35, 0x66, 0x57, 0xc0, 0xf1, 0xa2, 0x93, - 0xbd, 0x8c, 0xdf, 0xee, 0x79, 0x48, 0x1b, 0x2a, - 0xc1, 0xf0, 0xa3, 0x92, 0x05, 0x34, 0x67, 0x56, - 0x78, 0x49, 0x1a, 0x2b, 0xbc, 0x8d, 0xde, 0xef, - 0x82, 0xb3, 0xe0, 0xd1, 0x46, 0x77, 0x24, 0x15, - 0x3b, 0x0a, 0x59, 0x68, 0xff, 0xce, 0x9d, 0xac - }; - - static const uint16_t crc16tab[256]= { - 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7, - 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef, - 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6, - 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de, - 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485, - 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d, - 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4, - 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc, - 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823, - 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b, - 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12, - 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a, - 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41, - 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49, - 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70, - 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78, - 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f, - 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067, - 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e, - 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256, - 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d, - 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405, - 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c, - 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634, - 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab, - 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3, - 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a, - 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92, - 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9, - 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1, - 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8, - 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0 - }; - - static uint32_t crc32_tab[] = { - 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, - 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, - 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, - 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, - 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, - 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, - 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, - 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL, - 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, - 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, - 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, - 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, - 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L, - 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, - 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, - 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L, - 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, - 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, - 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, - 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, - 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL, - 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, - 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, - 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL, - 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, - 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L, - 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, - 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, - 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L, - 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, - 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, - 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L, - 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, - 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, - 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, - 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, - 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL, - 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, - 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, - 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L, - 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, - 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, - 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, - 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, - 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L, - 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, - 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, - 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, - 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, - 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L, - 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, - 0x2d02ef8dL - }; - - static const uint64_t crc64_tab[256] = { - 0x0000000000000000ULL, 0x7ad870c830358979ULL, - 0xf5b0e190606b12f2ULL, 0x8f689158505e9b8bULL, - 0xc038e5739841b68fULL, 0xbae095bba8743ff6ULL, - 0x358804e3f82aa47dULL, 0x4f50742bc81f2d04ULL, - 0xab28ecb46814fe75ULL, 0xd1f09c7c5821770cULL, - 0x5e980d24087fec87ULL, 0x24407dec384a65feULL, - 0x6b1009c7f05548faULL, 0x11c8790fc060c183ULL, - 0x9ea0e857903e5a08ULL, 0xe478989fa00bd371ULL, - 0x7d08ff3b88be6f81ULL, 0x07d08ff3b88be6f8ULL, - 0x88b81eabe8d57d73ULL, 0xf2606e63d8e0f40aULL, - 0xbd301a4810ffd90eULL, 0xc7e86a8020ca5077ULL, - 0x4880fbd87094cbfcULL, 0x32588b1040a14285ULL, - 0xd620138fe0aa91f4ULL, 0xacf86347d09f188dULL, - 0x2390f21f80c18306ULL, 0x594882d7b0f40a7fULL, - 0x1618f6fc78eb277bULL, 0x6cc0863448deae02ULL, - 0xe3a8176c18803589ULL, 0x997067a428b5bcf0ULL, - 0xfa11fe77117cdf02ULL, 0x80c98ebf2149567bULL, - 0x0fa11fe77117cdf0ULL, 0x75796f2f41224489ULL, - 0x3a291b04893d698dULL, 0x40f16bccb908e0f4ULL, - 0xcf99fa94e9567b7fULL, 0xb5418a5cd963f206ULL, - 0x513912c379682177ULL, 0x2be1620b495da80eULL, - 0xa489f35319033385ULL, 0xde51839b2936bafcULL, - 0x9101f7b0e12997f8ULL, 0xebd98778d11c1e81ULL, - 0x64b116208142850aULL, 0x1e6966e8b1770c73ULL, - 0x8719014c99c2b083ULL, 0xfdc17184a9f739faULL, - 0x72a9e0dcf9a9a271ULL, 0x08719014c99c2b08ULL, - 0x4721e43f0183060cULL, 0x3df994f731b68f75ULL, - 0xb29105af61e814feULL, 0xc849756751dd9d87ULL, - 0x2c31edf8f1d64ef6ULL, 0x56e99d30c1e3c78fULL, - 0xd9810c6891bd5c04ULL, 0xa3597ca0a188d57dULL, - 0xec09088b6997f879ULL, 0x96d1784359a27100ULL, - 0x19b9e91b09fcea8bULL, 0x636199d339c963f2ULL, - 0xdf7adabd7a6e2d6fULL, 0xa5a2aa754a5ba416ULL, - 0x2aca3b2d1a053f9dULL, 0x50124be52a30b6e4ULL, - 0x1f423fcee22f9be0ULL, 0x659a4f06d21a1299ULL, - 0xeaf2de5e82448912ULL, 0x902aae96b271006bULL, - 0x74523609127ad31aULL, 0x0e8a46c1224f5a63ULL, - 0x81e2d7997211c1e8ULL, 0xfb3aa75142244891ULL, - 0xb46ad37a8a3b6595ULL, 0xceb2a3b2ba0eececULL, - 0x41da32eaea507767ULL, 0x3b024222da65fe1eULL, - 0xa2722586f2d042eeULL, 0xd8aa554ec2e5cb97ULL, - 0x57c2c41692bb501cULL, 0x2d1ab4dea28ed965ULL, - 0x624ac0f56a91f461ULL, 0x1892b03d5aa47d18ULL, - 0x97fa21650afae693ULL, 0xed2251ad3acf6feaULL, - 0x095ac9329ac4bc9bULL, 0x7382b9faaaf135e2ULL, - 0xfcea28a2faafae69ULL, 0x8632586aca9a2710ULL, - 0xc9622c4102850a14ULL, 0xb3ba5c8932b0836dULL, - 0x3cd2cdd162ee18e6ULL, 0x460abd1952db919fULL, - 0x256b24ca6b12f26dULL, 0x5fb354025b277b14ULL, - 0xd0dbc55a0b79e09fULL, 0xaa03b5923b4c69e6ULL, - 0xe553c1b9f35344e2ULL, 0x9f8bb171c366cd9bULL, - 0x10e3202993385610ULL, 0x6a3b50e1a30ddf69ULL, - 0x8e43c87e03060c18ULL, 0xf49bb8b633338561ULL, - 0x7bf329ee636d1eeaULL, 0x012b592653589793ULL, - 0x4e7b2d0d9b47ba97ULL, 0x34a35dc5ab7233eeULL, - 0xbbcbcc9dfb2ca865ULL, 0xc113bc55cb19211cULL, - 0x5863dbf1e3ac9decULL, 0x22bbab39d3991495ULL, - 0xadd33a6183c78f1eULL, 0xd70b4aa9b3f20667ULL, - 0x985b3e827bed2b63ULL, 0xe2834e4a4bd8a21aULL, - 0x6debdf121b863991ULL, 0x1733afda2bb3b0e8ULL, - 0xf34b37458bb86399ULL, 0x8993478dbb8deae0ULL, - 0x06fbd6d5ebd3716bULL, 0x7c23a61ddbe6f812ULL, - 0x3373d23613f9d516ULL, 0x49aba2fe23cc5c6fULL, - 0xc6c333a67392c7e4ULL, 0xbc1b436e43a74e9dULL, - 0x95ac9329ac4bc9b5ULL, 0xef74e3e19c7e40ccULL, - 0x601c72b9cc20db47ULL, 0x1ac40271fc15523eULL, - 0x5594765a340a7f3aULL, 0x2f4c0692043ff643ULL, - 0xa02497ca54616dc8ULL, 0xdafce7026454e4b1ULL, - 0x3e847f9dc45f37c0ULL, 0x445c0f55f46abeb9ULL, - 0xcb349e0da4342532ULL, 0xb1eceec59401ac4bULL, - 0xfebc9aee5c1e814fULL, 0x8464ea266c2b0836ULL, - 0x0b0c7b7e3c7593bdULL, 0x71d40bb60c401ac4ULL, - 0xe8a46c1224f5a634ULL, 0x927c1cda14c02f4dULL, - 0x1d148d82449eb4c6ULL, 0x67ccfd4a74ab3dbfULL, - 0x289c8961bcb410bbULL, 0x5244f9a98c8199c2ULL, - 0xdd2c68f1dcdf0249ULL, 0xa7f41839ecea8b30ULL, - 0x438c80a64ce15841ULL, 0x3954f06e7cd4d138ULL, - 0xb63c61362c8a4ab3ULL, 0xcce411fe1cbfc3caULL, - 0x83b465d5d4a0eeceULL, 0xf96c151de49567b7ULL, - 0x76048445b4cbfc3cULL, 0x0cdcf48d84fe7545ULL, - 0x6fbd6d5ebd3716b7ULL, 0x15651d968d029fceULL, - 0x9a0d8ccedd5c0445ULL, 0xe0d5fc06ed698d3cULL, - 0xaf85882d2576a038ULL, 0xd55df8e515432941ULL, - 0x5a3569bd451db2caULL, 0x20ed197575283bb3ULL, - 0xc49581ead523e8c2ULL, 0xbe4df122e51661bbULL, - 0x3125607ab548fa30ULL, 0x4bfd10b2857d7349ULL, - 0x04ad64994d625e4dULL, 0x7e7514517d57d734ULL, - 0xf11d85092d094cbfULL, 0x8bc5f5c11d3cc5c6ULL, - 0x12b5926535897936ULL, 0x686de2ad05bcf04fULL, - 0xe70573f555e26bc4ULL, 0x9ddd033d65d7e2bdULL, - 0xd28d7716adc8cfb9ULL, 0xa85507de9dfd46c0ULL, - 0x273d9686cda3dd4bULL, 0x5de5e64efd965432ULL, - 0xb99d7ed15d9d8743ULL, 0xc3450e196da80e3aULL, - 0x4c2d9f413df695b1ULL, 0x36f5ef890dc31cc8ULL, - 0x79a59ba2c5dc31ccULL, 0x037deb6af5e9b8b5ULL, - 0x8c157a32a5b7233eULL, 0xf6cd0afa9582aa47ULL, - 0x4ad64994d625e4daULL, 0x300e395ce6106da3ULL, - 0xbf66a804b64ef628ULL, 0xc5bed8cc867b7f51ULL, - 0x8aeeace74e645255ULL, 0xf036dc2f7e51db2cULL, - 0x7f5e4d772e0f40a7ULL, 0x05863dbf1e3ac9deULL, - 0xe1fea520be311aafULL, 0x9b26d5e88e0493d6ULL, - 0x144e44b0de5a085dULL, 0x6e963478ee6f8124ULL, - 0x21c640532670ac20ULL, 0x5b1e309b16452559ULL, - 0xd476a1c3461bbed2ULL, 0xaeaed10b762e37abULL, - 0x37deb6af5e9b8b5bULL, 0x4d06c6676eae0222ULL, - 0xc26e573f3ef099a9ULL, 0xb8b627f70ec510d0ULL, - 0xf7e653dcc6da3dd4ULL, 0x8d3e2314f6efb4adULL, - 0x0256b24ca6b12f26ULL, 0x788ec2849684a65fULL, - 0x9cf65a1b368f752eULL, 0xe62e2ad306bafc57ULL, - 0x6946bb8b56e467dcULL, 0x139ecb4366d1eea5ULL, - 0x5ccebf68aecec3a1ULL, 0x2616cfa09efb4ad8ULL, - 0xa97e5ef8cea5d153ULL, 0xd3a62e30fe90582aULL, - 0xb0c7b7e3c7593bd8ULL, 0xca1fc72bf76cb2a1ULL, - 0x45775673a732292aULL, 0x3faf26bb9707a053ULL, - 0x70ff52905f188d57ULL, 0x0a2722586f2d042eULL, - 0x854fb3003f739fa5ULL, 0xff97c3c80f4616dcULL, - 0x1bef5b57af4dc5adULL, 0x61372b9f9f784cd4ULL, - 0xee5fbac7cf26d75fULL, 0x9487ca0fff135e26ULL, - 0xdbd7be24370c7322ULL, 0xa10fceec0739fa5bULL, - 0x2e675fb4576761d0ULL, 0x54bf2f7c6752e8a9ULL, - 0xcdcf48d84fe75459ULL, 0xb71738107fd2dd20ULL, - 0x387fa9482f8c46abULL, 0x42a7d9801fb9cfd2ULL, - 0x0df7adabd7a6e2d6ULL, 0x772fdd63e7936bafULL, - 0xf8474c3bb7cdf024ULL, 0x829f3cf387f8795dULL, - 0x66e7a46c27f3aa2cULL, 0x1c3fd4a417c62355ULL, - 0x935745fc4798b8deULL, 0xe98f353477ad31a7ULL, - 0xa6df411fbfb21ca3ULL, 0xdc0731d78f8795daULL, - 0x536fa08fdfd90e51ULL, 0x29b7d047efec8728ULL, - }; - - // LSB-first - uint8_t crc8_lsb(const char *buf, int len) { - int counter; - uint8_t crc = 0; - for (counter = 0; counter < len; counter++) - crc = crc8tab_lsb[crc ^ *buf++]; - return crc; - } - - // MSB-first - uint8_t crc8_msb(const char *buf, int len) { - int counter; - uint8_t crc = 0; - for (counter = 0; counter < len; counter++) - crc = crc8tab_msb[crc ^ *buf++]; - return crc; - } - - uint16_t crc16(const char *buf, int len) { - int counter; - uint16_t crc = 0; - for (counter = 0; counter < len; counter++) - crc = (crc<<8) ^ crc16tab[((crc>>8) ^ *buf++)&0x00FF]; - return crc; - } - - /* crc32 hash */ - uint32_t crc32(const char* s, int len) { - int i; - uint32_t crc32val = 0; - crc32val ^= 0xFFFFFFFF; - for (i = 0; i < len; i++) { - crc32val = crc32_tab[(crc32val ^ s[i]) & 0xFF] ^ ((crc32val >> 8) & 0x00FFFFFF); - } - return crc32val ^ 0xFFFFFFFF; - } - - /* crc64 hash */ - uint64_t crc64(const char *s, int l) { - int j = 0; - uint64_t crc = 0; - - for (j = 0; j < l; j++) { - uint8_t byte = s[j]; - crc = crc64_tab[(uint8_t)crc ^ byte] ^ (crc >> 8); - } - return crc; - } -} diff --git a/extend/lcodec/src/http.h b/extend/lcodec/src/http.h new file mode 100644 index 00000000..8d66f0e5 --- /dev/null +++ b/extend/lcodec/src/http.h @@ -0,0 +1,212 @@ +#pragma once +#include +#include +#include + +#include "lua_kit.h" + +#ifdef _MSC_VER +#define strncasecmp _strnicmp +#endif + +using namespace std; +using namespace luakit; + +namespace lcodec { + + inline size_t LCRLF = 2; + inline size_t LCRLF2 = 4; + inline const char* CRLF = "\r\n"; + inline const char* CRLF2 = "\r\n\r\n"; + + #define SC_UNKNOWN 0 + #define SC_OK 200 + #define SC_NOCONTENT 204 + #define SC_PARTIAL 206 + #define SC_OBJMOVED 302 + #define SC_BADREQUEST 400 + #define SC_FORBIDDEN 403 + #define SC_NOTFOUND 404 + #define SC_BADMETHOD 405 + #define SC_SERVERERROR 500 + #define SC_SERVERBUSY 503 + + class httpcodec : public codec_base { + public: + virtual uint8_t* encode(lua_State* L, int index, size_t* len) { + m_buf->clean(); + //status (http begining) + format_http(lua_tointeger(L, index)); + //headers + lua_pushnil(L); + while (lua_next(L, index + 1) != 0) { + format_http_header(lua_tostring(L, -2), lua_tostring(L, -1)); + lua_pop(L, 1); + } + //body + uint8_t* body = nullptr; + if (lua_type(L, index + 2) == LUA_TTABLE) { + body = m_jcodec->encode(L, index + 2, len); + } else { + body = (uint8_t*)lua_tolstring(L, index + 2, len); + } + format_http_header("Content-Length", std::to_string(*len)); + m_buf->push_data((const uint8_t*)CRLF, LCRLF); + m_buf->push_data(body, *len); + return m_buf->data(len); + } + + virtual size_t decode(lua_State* L) { + if (!m_slice) return 0; + int top = lua_gettop(L); + size_t osize = m_slice->size(); + string_view buf = m_slice->contents(); + parse_http_packet(L, buf); + m_slice->erase(osize - buf.size()); + return lua_gettop(L) - top; + } + + void set_codec(codec_base* codec) { + m_jcodec = codec; + } + + void set_buff(luabuf* buf) { + m_buf = buf; + } + + protected: + void format_http(size_t status) { + switch (status) { + case SC_OK: m_buf->write("HTTP/1.1 200 OK\r\n"); break; + case SC_NOCONTENT: m_buf->write("HTTP/1.1 204 No Content\r\n"); break; + case SC_PARTIAL: m_buf->write("HTTP/1.1 206 Partial Content\r\n"); break; + case SC_BADREQUEST: m_buf->write("HTTP/1.1 400 Bad Request\r\n"); break; + case SC_OBJMOVED: m_buf->write("HTTP/1.1 302 Moved Temporarily\r\n"); break; + case SC_NOTFOUND: m_buf->write("HTTP/1.1 404 Not Found\r\n"); break; + case SC_BADMETHOD: m_buf->write("HTTP/1.1 405 Method Not Allowed\r\n"); break; + default: m_buf->write("HTTP/1.1 500 Internal Server Error\r\n"); break; + } + } + + void format_http_header(string_view key, string_view val) { + m_buf->push_data((uint8_t*)key.data(), key.size()); + m_buf->push_data((uint8_t*)": ", LCRLF); + m_buf->push_data((uint8_t*)val.data(), val.size()); + m_buf->push_data((const uint8_t*)CRLF, LCRLF); + } + + void split(string_view str, string_view delim, vector& res) { + size_t cur = 0; + size_t step = delim.size(); + size_t pos = str.find(delim); + while (pos != string_view::npos) { + res.push_back(str.substr(cur, pos - cur)); + cur = pos + step; + pos = str.find(delim, cur); + } + if (str.size() > cur) { + res.push_back(str.substr(cur)); + } + } + + void http_parse_url(lua_State* L, string_view url) { + string_view sparams; + size_t pos = url.find("?"); + if (pos != string_view::npos) { + sparams = url.substr(pos + 1); + url = url.substr(0, pos); + } + if (url.size() > 1 && url.back() == '/') { + url.remove_suffix(1); + } + //url + lua_pushlstring(L, url.data(), url.size()); + //params + lua_createtable(L, 0, 4); + if (!sparams.empty()) { + vector params; + split(sparams, "&", params); + for (string_view param : params) { + size_t pos = param.find("="); + if (pos != string_view::npos) { + string_view key = param.substr(0, pos); + param.remove_prefix(pos + 1); + lua_pushlstring(L, key.data(), key.size()); + lua_pushlstring(L, param.data(), param.size()); + lua_settable(L, -3); + } + } + } + } + + void http_parse_body(lua_State* L, string_view header, string_view& buf) { + m_buf->clean(); + bool jsonable = false; + slice* mslice = nullptr; + vector headers; + split(header, CRLF, headers); + for (auto header : headers) { + size_t pos = header.find(":"); + if (pos != string_view::npos) { + string_view key = header.substr(0, pos); + header.remove_prefix(pos + 1); + if (!strncasecmp(key.data(), "Content-Length", key.size())) { + mslice = m_buf->get_slice(); + size_t content_size = atol(header.data()); + mslice->attach((uint8_t*)buf.data(), content_size); + buf.remove_prefix(content_size); + } + else if (!strncasecmp(key.data(), "Transfer-Encoding", key.size()) && !strncasecmp(header.data(), "chunked", header.size())) { + size_t pos = buf.find(CRLF2); + string_view chunk_data = buf.substr(0, pos); + buf.remove_prefix(pos + LCRLF2); + vector chunks; + split(chunk_data, CRLF, chunks); + for (size_t i = 0; i < chunks.size(); i++) { + if (i % 2 != 0) { + m_buf->push_data((const uint8_t*)chunks[i].data(), chunks[i].size()); + } + } + } + else if (!strncasecmp(key.data(), "Content-Type", key.size()) && !strncasecmp(header.data(), "application/json", header.size())) { + jsonable = true; + } + } + } + if (jsonable) { + if (!mslice) mslice = m_buf->get_slice(); + m_jcodec->set_slice(mslice); + m_jcodec->decode(L); + return; + } + lua_pushlstring(L, (char*)m_buf->head(), m_buf->size()); + } + + void parse_http_packet(lua_State* L, string_view& buf) { + size_t pos = buf.find(CRLF2); + if (pos == string_view::npos) throw length_error("http text not full"); + string_view header = buf.substr(0, pos); + buf.remove_prefix(pos + LCRLF2); + auto begining = read_line(header); + vector parts; + split(begining, " ", parts); + //method + lua_pushlstring(L, parts[0].data(), parts[0].size()); + //url + params + http_parse_url(L, parts[1]); + //header + body + http_parse_body(L, header, buf); + } + + string_view read_line(string_view& buf) { + size_t pos = buf.find(CRLF); + auto ss = buf.substr(0, pos); + buf.remove_prefix(pos + LCRLF); + return ss; + } + + protected: + luabuf* m_buf = nullptr; + codec_base* m_jcodec = nullptr; + }; +} diff --git a/extend/lcodec/src/lcodec.cpp b/extend/lcodec/src/lcodec.cpp index 0267093a..72da346d 100644 --- a/extend/lcodec/src/lcodec.cpp +++ b/extend/lcodec/src/lcodec.cpp @@ -5,8 +5,22 @@ namespace lcodec { thread_local ketama thread_ketama; + thread_local rdscodec thread_rds; + thread_local httpcodec thread_http; thread_local luakit::luabuf thread_buff; - + + static rdscodec* rds_codec(codec_base* codec) { + thread_rds.set_codec(codec); + thread_rds.set_buff(&thread_buff); + return &thread_rds; + } + + static httpcodec* http_codec(codec_base* codec) { + thread_http.set_codec(codec); + thread_http.set_buff(&thread_buff); + return &thread_http; + } + static int serialize(lua_State* L) { return luakit::serialize(L, &thread_buff); } @@ -41,41 +55,6 @@ namespace lcodec { return barray; } - static int lcrc8(lua_State* L) { - size_t len; - const char* key = lua_tolstring(L, 1, &len); - lua_pushinteger(L, crc8_lsb(key, len)); - return 1; - } - - static int lcrc8_msb(lua_State* L) { - size_t len; - const char* key = lua_tolstring(L, 1, &len); - lua_pushinteger(L, crc8_msb(key, len)); - return 1; - } - - static int lcrc16(lua_State* L) { - size_t len; - const char* key = lua_tolstring(L, 1, &len); - lua_pushinteger(L, crc16(key, len)); - return 1; - } - - static int lcrc32(lua_State* L) { - size_t len; - const char* key = lua_tolstring(L, 1, &len); - lua_pushinteger(L, crc32(key, len)); - return 1; - } - - static int lcrc64(lua_State* L) { - size_t len; - const char* key = lua_tolstring(L, 1, &len); - lua_pushinteger(L, (int64_t)crc64(key, len)); - return 1; - } - luakit::lua_table open_lcodec(lua_State* L) { luakit::kit_state kit_state(L); auto llcodec = kit_state.new_table(); @@ -103,11 +82,9 @@ namespace lcodec { llcodec.set_function("ketama_remove", ketama_remove); llcodec.set_function("ketama_next", ketama_next); llcodec.set_function("ketama_map", ketama_map); - llcodec.set_function("crc8_msb", lcrc8_msb); - llcodec.set_function("crc64", lcrc64); - llcodec.set_function("crc32", lcrc32); - llcodec.set_function("crc16", lcrc16); - llcodec.set_function("crc8", lcrc8); + llcodec.set_function("rediscodec", rds_codec); + llcodec.set_function("httpcodec", http_codec); + kit_state.new_class( "flip", &bitarray::flip, "fill", &bitarray::fill, @@ -140,7 +117,7 @@ namespace lcodec { extern "C" { LUALIB_API int luaopen_lcodec(lua_State* L) { - auto lluabus = lcodec::open_lcodec(L); - return lluabus.push_stack(); + auto llcodec = lcodec::open_lcodec(L); + return llcodec.push_stack(); } } diff --git a/extend/lcodec/src/lcodec.h b/extend/lcodec/src/lcodec.h index 4c025583..dc05b05a 100644 --- a/extend/lcodec/src/lcodec.h +++ b/extend/lcodec/src/lcodec.h @@ -3,8 +3,9 @@ #include "lua_kit.h" -#include "crc.h" #include "guid.h" +#include "http.h" #include "hash.h" +#include "redis.h" #include "ketama.h" #include "bitarray.h" diff --git a/extend/lcodec/src/redis.h b/extend/lcodec/src/redis.h new file mode 100644 index 00000000..ca556980 --- /dev/null +++ b/extend/lcodec/src/redis.h @@ -0,0 +1,206 @@ +#pragma once +#include + +#ifdef _MSC_VER +#define strncasecmp _strnicmp +#endif + +#include "lua_kit.h" + +using namespace std; +using namespace luakit; + +namespace lcodec { + inline size_t CRLF_LEN = 2; + inline size_t HEAD_SIZE = 24; + inline const char* RDS_CRLF = "\r\n"; + + class rdscodec : public codec_base { + public: + virtual uint8_t* encode(lua_State* L, int index, size_t* len) { + m_buf->clean(); + int n = lua_gettop(L); + char* head = (char*)m_buf->peek_space(HEAD_SIZE); + m_buf->pop_space(sprintf(head, "*%d\r\n", n)); + for (int i = 1; i <= n; ++i) { + encode_bulk_string(L, i); + } + return m_buf->data(len); + } + + virtual size_t decode(lua_State* L) { + if (!m_slice) return 0; + int top = lua_gettop(L); + size_t osize = m_slice->size(); + string_view buf = m_slice->contents(); + parse_redis_packet(L, buf); + m_slice->erase(osize - buf.size()); + return lua_gettop(L) - top; + } + + void set_codec(codec_base* codec) { + m_jcodec = codec; + } + + void set_buff(luabuf* buf) { + m_buf = buf; + } + + protected: + void parse_redis_success(lua_State* L, string_view line) { + lua_pushboolean(L, true); + lua_pushlstring(L, line.data(), line.size()); + } + + void parse_redis_error(lua_State* L, string_view line) { + lua_pushboolean(L, false); + lua_pushlstring(L, line.data(), line.size()); + } + + void parse_redis_integer(lua_State* L, string_view line, bool rootable = false) { + if (rootable) lua_pushboolean(L, true); + lua_pushinteger(L, atoll(line.data())); + } + + void parse_redis_string(lua_State* L, string_view line, string_view& buf, bool rootable = false) { + int64_t length = atoll(line.data()); + if (length >= 0) { + string_view nline; + if (!read_line(buf, nline)) + throw length_error("redis text not full"); + if (!strncasecmp(nline.data(), "[js]", 4)) { + nline.remove_prefix(4); + m_jcodec->decode(L, (uint8_t*)nline.data(), nline.size()); + } else { + lua_pushlstring(L, nline.data(), nline.size()); + } + } + else { + lua_pushnil(L); + } + if (rootable) { + lua_pushboolean(L, true); + lua_insert(L, -2); + } + } + + void parse_redis_array(lua_State* L, string_view line, string_view& buf, bool rootable = false) { + int64_t length = atoll(line.data()); + if (length >= 0) { + lua_createtable(L, 0, 4); + for (int i = 1; i <= length; ++i) { + string_view line; + if (!read_line(buf, line)) throw length_error("redis text not full"); + switch (line[0]) { + case ':': + parse_redis_integer(L, line.substr(1)); + break; + case '$': + parse_redis_string(L, line.substr(1), buf); + break; + case '*': + parse_redis_array(L, line.substr(1), buf); + break; + default: + throw invalid_argument("invalid redis format"); + break; + } + lua_seti(L, -2, i); + } + } + else { + lua_pushnil(L); + } + if (rootable) { + lua_pushboolean(L, true); + lua_insert(L, -2); + } + } + + void parse_redis_packet(lua_State* L, string_view& buf) { + string_view line; + if (!read_line(buf, line)) throw length_error("redis text not full"); + switch (line[0]) { + case '+': + parse_redis_success(L, line.substr(1)); + break; + case '-': + parse_redis_error(L, line.substr(1)); + break; + case ':': + parse_redis_integer(L, line.substr(1), true); + break; + case '$': + parse_redis_string(L, line.substr(1), buf, true); + break; + case '*': + parse_redis_array(L, line.substr(1), buf, true); + break; + default: + throw invalid_argument("invalid redis format"); + break; + } + } + + bool read_line(string_view& buf, string_view& line) { + size_t pos = buf.find(RDS_CRLF); + if (pos != string_view::npos) { + line = buf.substr(0, pos); + buf.remove_prefix(pos + CRLF_LEN); + return true; + } + return false; + } + + void number_encode(double value) { + auto svalue = std::to_string(value); + char* head = (char*)m_buf->peek_space(HEAD_SIZE + svalue.size()); + m_buf->pop_space(sprintf(head, "$%zd\r\n%s\r\n", svalue.size(), svalue.c_str())); + } + + void integer_encode(int64_t integer) { + auto svalue = std::to_string(integer); + char* head = (char*)m_buf->peek_space(HEAD_SIZE + svalue.size()); + m_buf->pop_space(sprintf(head, "$%zd\r\n%s\r\n", svalue.size(), svalue.c_str())); + } + + void string_encode(lua_State* L, int idx) { + size_t len; + const char* data = lua_tolstring(L, idx, &len); + char* head = (char*)m_buf->peek_space(HEAD_SIZE + len); + m_buf->pop_space(sprintf(head, "$%zd\r\n%s\r\n", len, data)); + } + + void table_encode(lua_State* L, int idx) { + size_t len; + char* body = (char*)m_jcodec->encode(L, idx, &len); + char* head = (char*)m_buf->peek_space(HEAD_SIZE + len); + m_buf->pop_space(sprintf(head, "$%zd\r\n[js]%s\r\n", len + 4, body)); + } + + void encode_bulk_string(lua_State* L, int idx) { + int type = lua_type(L, idx); + switch (type) { + case LUA_TSTRING: + string_encode(L, idx); + break; + case LUA_TBOOLEAN: + integer_encode(lua_tointeger(L, idx)); + break; + case LUA_TNUMBER: + lua_isinteger(L, idx) ? integer_encode(lua_tointeger(L, idx)) : number_encode(lua_tonumber(L, idx)); + break; + case LUA_TTABLE: + table_encode(L, idx); + break; + default: + m_buf->write("$-1\r\n"); + break; + } + } + + protected: + luabuf* m_buf = nullptr; + codec_base* m_jcodec = nullptr; + }; +} diff --git a/extend/lcrypt/lcrypt.vcxproj b/extend/lcrypt/lcrypt.vcxproj index 91dfb6a6..1ea4e959 100644 --- a/extend/lcrypt/lcrypt.vcxproj +++ b/extend/lcrypt/lcrypt.vcxproj @@ -8,6 +8,7 @@ + @@ -19,6 +20,7 @@ + diff --git a/extend/lcrypt/lcrypt.vcxproj.filters b/extend/lcrypt/lcrypt.vcxproj.filters index aa3723d7..26a34e4f 100644 --- a/extend/lcrypt/lcrypt.vcxproj.filters +++ b/extend/lcrypt/lcrypt.vcxproj.filters @@ -4,6 +4,9 @@ inc + + inc + inc @@ -33,6 +36,9 @@ src + + src + src diff --git a/extend/lcrypt/src/crc.c b/extend/lcrypt/src/crc.c new file mode 100644 index 00000000..1cc808d7 --- /dev/null +++ b/extend/lcrypt/src/crc.c @@ -0,0 +1,343 @@ +#include "crc.h" + +#include + +static const uint8_t crc8tab_lsb[256] = { + 0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c, + 0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38, 0x3c, + 0x23, 0x27, 0x2b, 0x2f, 0x33, 0x37, 0x3b, 0x3f, + 0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17, 0x1b, 0x1f, + 0x25, 0x21, 0x2d, 0x29, 0x35, 0x31, 0x3d, 0x39, + 0x05, 0x01, 0x0d, 0x09, 0x15, 0x11, 0x1d, 0x19, + 0x06, 0x02, 0x0e, 0x0a, 0x16, 0x12, 0x1e, 0x1a, + 0x26, 0x22, 0x2e, 0x2a, 0x36, 0x32, 0x3e, 0x3a, + 0x29, 0x2d, 0x21, 0x25, 0x39, 0x3d, 0x31, 0x35, + 0x09, 0x0d, 0x01, 0x05, 0x19, 0x1d, 0x11, 0x15, + 0x0a, 0x0e, 0x02, 0x06, 0x1a, 0x1e, 0x12, 0x16, + 0x2a, 0x2e, 0x22, 0x26, 0x3a, 0x3e, 0x32, 0x36, + 0x0c, 0x08, 0x04, 0x00, 0x1c, 0x18, 0x14, 0x10, + 0x2c, 0x28, 0x24, 0x20, 0x3c, 0x38, 0x34, 0x30, + 0x2f, 0x2b, 0x27, 0x23, 0x3f, 0x3b, 0x37, 0x33, + 0x0f, 0x0b, 0x07, 0x03, 0x1f, 0x1b, 0x17, 0x13, + 0x31, 0x35, 0x39, 0x3d, 0x21, 0x25, 0x29, 0x2d, + 0x11, 0x15, 0x19, 0x1d, 0x01, 0x05, 0x09, 0x0d, + 0x12, 0x16, 0x1a, 0x1e, 0x02, 0x06, 0x0a, 0x0e, + 0x32, 0x36, 0x3a, 0x3e, 0x22, 0x26, 0x2a, 0x2e, + 0x14, 0x10, 0x1c, 0x18, 0x04, 0x00, 0x0c, 0x08, + 0x34, 0x30, 0x3c, 0x38, 0x24, 0x20, 0x2c, 0x28, + 0x37, 0x33, 0x3f, 0x3b, 0x27, 0x23, 0x2f, 0x2b, + 0x17, 0x13, 0x1f, 0x1b, 0x07, 0x03, 0x0f, 0x0b, + 0x18, 0x1c, 0x10, 0x14, 0x08, 0x0c, 0x00, 0x04, + 0x38, 0x3c, 0x30, 0x34, 0x28, 0x2c, 0x20, 0x24, + 0x3b, 0x3f, 0x33, 0x37, 0x2b, 0x2f, 0x23, 0x27, + 0x1b, 0x1f, 0x13, 0x17, 0x0b, 0x0f, 0x03, 0x07, + 0x3d, 0x39, 0x35, 0x31, 0x2d, 0x29, 0x25, 0x21, + 0x1d, 0x19, 0x15, 0x11, 0x0d, 0x09, 0x05, 0x01, + 0x1e, 0x1a, 0x16, 0x12, 0x0e, 0x0a, 0x06, 0x02 +}; + +static const uint8_t crc8tab_msb[256] = { + 0x00, 0x31, 0x62, 0x53, 0xc4, 0xf5, 0xa6, 0x97, + 0xb9, 0x88, 0xdb, 0xea, 0x7d, 0x4c, 0x1f, 0x2e, + 0x43, 0x72, 0x21, 0x10, 0x87, 0xb6, 0xe5, 0xd4, + 0xfa, 0xcb, 0x98, 0xa9, 0x3e, 0x0f, 0x5c, 0x6d, + 0x86, 0xb7, 0xe4, 0xd5, 0x42, 0x73, 0x20, 0x11, + 0x3f, 0x0e, 0x5d, 0x6c, 0xfb, 0xca, 0x99, 0xa8, + 0xc5, 0xf4, 0xa7, 0x96, 0x01, 0x30, 0x63, 0x52, + 0x7c, 0x4d, 0x1e, 0x2f, 0xb8, 0x89, 0xda, 0xeb, + 0x3d, 0x0c, 0x5f, 0x6e, 0xf9, 0xc8, 0x9b, 0xaa, + 0x84, 0xb5, 0xe6, 0xd7, 0x40, 0x71, 0x22, 0x13, + 0x7e, 0x4f, 0x1c, 0x2d, 0xba, 0x8b, 0xd8, 0xe9, + 0xc7, 0xf6, 0xa5, 0x94, 0x03, 0x32, 0x61, 0x50, + 0xbb, 0x8a, 0xd9, 0xe8, 0x7f, 0x4e, 0x1d, 0x2c, + 0x02, 0x33, 0x60, 0x51, 0xc6, 0xf7, 0xa4, 0x95, + 0xf8, 0xc9, 0x9a, 0xab, 0x3c, 0x0d, 0x5e, 0x6f, + 0x41, 0x70, 0x23, 0x12, 0x85, 0xb4, 0xe7, 0xd6, + 0x7a, 0x4b, 0x18, 0x29, 0xbe, 0x8f, 0xdc, 0xed, + 0xc3, 0xf2, 0xa1, 0x90, 0x07, 0x36, 0x65, 0x54, + 0x39, 0x08, 0x5b, 0x6a, 0xfd, 0xcc, 0x9f, 0xae, + 0x80, 0xb1, 0xe2, 0xd3, 0x44, 0x75, 0x26, 0x17, + 0xfc, 0xcd, 0x9e, 0xaf, 0x38, 0x09, 0x5a, 0x6b, + 0x45, 0x74, 0x27, 0x16, 0x81, 0xb0, 0xe3, 0xd2, + 0xbf, 0x8e, 0xdd, 0xec, 0x7b, 0x4a, 0x19, 0x28, + 0x06, 0x37, 0x64, 0x55, 0xc2, 0xf3, 0xa0, 0x91, + 0x47, 0x76, 0x25, 0x14, 0x83, 0xb2, 0xe1, 0xd0, + 0xfe, 0xcf, 0x9c, 0xad, 0x3a, 0x0b, 0x58, 0x69, + 0x04, 0x35, 0x66, 0x57, 0xc0, 0xf1, 0xa2, 0x93, + 0xbd, 0x8c, 0xdf, 0xee, 0x79, 0x48, 0x1b, 0x2a, + 0xc1, 0xf0, 0xa3, 0x92, 0x05, 0x34, 0x67, 0x56, + 0x78, 0x49, 0x1a, 0x2b, 0xbc, 0x8d, 0xde, 0xef, + 0x82, 0xb3, 0xe0, 0xd1, 0x46, 0x77, 0x24, 0x15, + 0x3b, 0x0a, 0x59, 0x68, 0xff, 0xce, 0x9d, 0xac +}; + +static const uint16_t crc16tab[256]= { + 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7, + 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef, + 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6, + 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de, + 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485, + 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d, + 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4, + 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc, + 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823, + 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b, + 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12, + 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a, + 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41, + 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49, + 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70, + 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78, + 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f, + 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067, + 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e, + 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256, + 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d, + 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405, + 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c, + 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634, + 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab, + 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3, + 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a, + 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92, + 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9, + 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1, + 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8, + 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0 +}; + +static uint32_t crc32_tab[] = { + 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, + 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, + 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, + 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, + 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, + 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, + 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, + 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL, + 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, + 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, + 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, + 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, + 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L, + 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, + 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, + 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L, + 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, + 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, + 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, + 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, + 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL, + 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, + 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, + 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL, + 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, + 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L, + 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, + 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, + 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L, + 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, + 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, + 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L, + 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, + 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, + 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, + 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, + 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL, + 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, + 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, + 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L, + 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, + 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, + 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, + 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, + 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L, + 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, + 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, + 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, + 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, + 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L, + 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, + 0x2d02ef8dL +}; + +static const uint64_t crc64_tab[256] = { + 0x0000000000000000ULL, 0x7ad870c830358979ULL, + 0xf5b0e190606b12f2ULL, 0x8f689158505e9b8bULL, + 0xc038e5739841b68fULL, 0xbae095bba8743ff6ULL, + 0x358804e3f82aa47dULL, 0x4f50742bc81f2d04ULL, + 0xab28ecb46814fe75ULL, 0xd1f09c7c5821770cULL, + 0x5e980d24087fec87ULL, 0x24407dec384a65feULL, + 0x6b1009c7f05548faULL, 0x11c8790fc060c183ULL, + 0x9ea0e857903e5a08ULL, 0xe478989fa00bd371ULL, + 0x7d08ff3b88be6f81ULL, 0x07d08ff3b88be6f8ULL, + 0x88b81eabe8d57d73ULL, 0xf2606e63d8e0f40aULL, + 0xbd301a4810ffd90eULL, 0xc7e86a8020ca5077ULL, + 0x4880fbd87094cbfcULL, 0x32588b1040a14285ULL, + 0xd620138fe0aa91f4ULL, 0xacf86347d09f188dULL, + 0x2390f21f80c18306ULL, 0x594882d7b0f40a7fULL, + 0x1618f6fc78eb277bULL, 0x6cc0863448deae02ULL, + 0xe3a8176c18803589ULL, 0x997067a428b5bcf0ULL, + 0xfa11fe77117cdf02ULL, 0x80c98ebf2149567bULL, + 0x0fa11fe77117cdf0ULL, 0x75796f2f41224489ULL, + 0x3a291b04893d698dULL, 0x40f16bccb908e0f4ULL, + 0xcf99fa94e9567b7fULL, 0xb5418a5cd963f206ULL, + 0x513912c379682177ULL, 0x2be1620b495da80eULL, + 0xa489f35319033385ULL, 0xde51839b2936bafcULL, + 0x9101f7b0e12997f8ULL, 0xebd98778d11c1e81ULL, + 0x64b116208142850aULL, 0x1e6966e8b1770c73ULL, + 0x8719014c99c2b083ULL, 0xfdc17184a9f739faULL, + 0x72a9e0dcf9a9a271ULL, 0x08719014c99c2b08ULL, + 0x4721e43f0183060cULL, 0x3df994f731b68f75ULL, + 0xb29105af61e814feULL, 0xc849756751dd9d87ULL, + 0x2c31edf8f1d64ef6ULL, 0x56e99d30c1e3c78fULL, + 0xd9810c6891bd5c04ULL, 0xa3597ca0a188d57dULL, + 0xec09088b6997f879ULL, 0x96d1784359a27100ULL, + 0x19b9e91b09fcea8bULL, 0x636199d339c963f2ULL, + 0xdf7adabd7a6e2d6fULL, 0xa5a2aa754a5ba416ULL, + 0x2aca3b2d1a053f9dULL, 0x50124be52a30b6e4ULL, + 0x1f423fcee22f9be0ULL, 0x659a4f06d21a1299ULL, + 0xeaf2de5e82448912ULL, 0x902aae96b271006bULL, + 0x74523609127ad31aULL, 0x0e8a46c1224f5a63ULL, + 0x81e2d7997211c1e8ULL, 0xfb3aa75142244891ULL, + 0xb46ad37a8a3b6595ULL, 0xceb2a3b2ba0eececULL, + 0x41da32eaea507767ULL, 0x3b024222da65fe1eULL, + 0xa2722586f2d042eeULL, 0xd8aa554ec2e5cb97ULL, + 0x57c2c41692bb501cULL, 0x2d1ab4dea28ed965ULL, + 0x624ac0f56a91f461ULL, 0x1892b03d5aa47d18ULL, + 0x97fa21650afae693ULL, 0xed2251ad3acf6feaULL, + 0x095ac9329ac4bc9bULL, 0x7382b9faaaf135e2ULL, + 0xfcea28a2faafae69ULL, 0x8632586aca9a2710ULL, + 0xc9622c4102850a14ULL, 0xb3ba5c8932b0836dULL, + 0x3cd2cdd162ee18e6ULL, 0x460abd1952db919fULL, + 0x256b24ca6b12f26dULL, 0x5fb354025b277b14ULL, + 0xd0dbc55a0b79e09fULL, 0xaa03b5923b4c69e6ULL, + 0xe553c1b9f35344e2ULL, 0x9f8bb171c366cd9bULL, + 0x10e3202993385610ULL, 0x6a3b50e1a30ddf69ULL, + 0x8e43c87e03060c18ULL, 0xf49bb8b633338561ULL, + 0x7bf329ee636d1eeaULL, 0x012b592653589793ULL, + 0x4e7b2d0d9b47ba97ULL, 0x34a35dc5ab7233eeULL, + 0xbbcbcc9dfb2ca865ULL, 0xc113bc55cb19211cULL, + 0x5863dbf1e3ac9decULL, 0x22bbab39d3991495ULL, + 0xadd33a6183c78f1eULL, 0xd70b4aa9b3f20667ULL, + 0x985b3e827bed2b63ULL, 0xe2834e4a4bd8a21aULL, + 0x6debdf121b863991ULL, 0x1733afda2bb3b0e8ULL, + 0xf34b37458bb86399ULL, 0x8993478dbb8deae0ULL, + 0x06fbd6d5ebd3716bULL, 0x7c23a61ddbe6f812ULL, + 0x3373d23613f9d516ULL, 0x49aba2fe23cc5c6fULL, + 0xc6c333a67392c7e4ULL, 0xbc1b436e43a74e9dULL, + 0x95ac9329ac4bc9b5ULL, 0xef74e3e19c7e40ccULL, + 0x601c72b9cc20db47ULL, 0x1ac40271fc15523eULL, + 0x5594765a340a7f3aULL, 0x2f4c0692043ff643ULL, + 0xa02497ca54616dc8ULL, 0xdafce7026454e4b1ULL, + 0x3e847f9dc45f37c0ULL, 0x445c0f55f46abeb9ULL, + 0xcb349e0da4342532ULL, 0xb1eceec59401ac4bULL, + 0xfebc9aee5c1e814fULL, 0x8464ea266c2b0836ULL, + 0x0b0c7b7e3c7593bdULL, 0x71d40bb60c401ac4ULL, + 0xe8a46c1224f5a634ULL, 0x927c1cda14c02f4dULL, + 0x1d148d82449eb4c6ULL, 0x67ccfd4a74ab3dbfULL, + 0x289c8961bcb410bbULL, 0x5244f9a98c8199c2ULL, + 0xdd2c68f1dcdf0249ULL, 0xa7f41839ecea8b30ULL, + 0x438c80a64ce15841ULL, 0x3954f06e7cd4d138ULL, + 0xb63c61362c8a4ab3ULL, 0xcce411fe1cbfc3caULL, + 0x83b465d5d4a0eeceULL, 0xf96c151de49567b7ULL, + 0x76048445b4cbfc3cULL, 0x0cdcf48d84fe7545ULL, + 0x6fbd6d5ebd3716b7ULL, 0x15651d968d029fceULL, + 0x9a0d8ccedd5c0445ULL, 0xe0d5fc06ed698d3cULL, + 0xaf85882d2576a038ULL, 0xd55df8e515432941ULL, + 0x5a3569bd451db2caULL, 0x20ed197575283bb3ULL, + 0xc49581ead523e8c2ULL, 0xbe4df122e51661bbULL, + 0x3125607ab548fa30ULL, 0x4bfd10b2857d7349ULL, + 0x04ad64994d625e4dULL, 0x7e7514517d57d734ULL, + 0xf11d85092d094cbfULL, 0x8bc5f5c11d3cc5c6ULL, + 0x12b5926535897936ULL, 0x686de2ad05bcf04fULL, + 0xe70573f555e26bc4ULL, 0x9ddd033d65d7e2bdULL, + 0xd28d7716adc8cfb9ULL, 0xa85507de9dfd46c0ULL, + 0x273d9686cda3dd4bULL, 0x5de5e64efd965432ULL, + 0xb99d7ed15d9d8743ULL, 0xc3450e196da80e3aULL, + 0x4c2d9f413df695b1ULL, 0x36f5ef890dc31cc8ULL, + 0x79a59ba2c5dc31ccULL, 0x037deb6af5e9b8b5ULL, + 0x8c157a32a5b7233eULL, 0xf6cd0afa9582aa47ULL, + 0x4ad64994d625e4daULL, 0x300e395ce6106da3ULL, + 0xbf66a804b64ef628ULL, 0xc5bed8cc867b7f51ULL, + 0x8aeeace74e645255ULL, 0xf036dc2f7e51db2cULL, + 0x7f5e4d772e0f40a7ULL, 0x05863dbf1e3ac9deULL, + 0xe1fea520be311aafULL, 0x9b26d5e88e0493d6ULL, + 0x144e44b0de5a085dULL, 0x6e963478ee6f8124ULL, + 0x21c640532670ac20ULL, 0x5b1e309b16452559ULL, + 0xd476a1c3461bbed2ULL, 0xaeaed10b762e37abULL, + 0x37deb6af5e9b8b5bULL, 0x4d06c6676eae0222ULL, + 0xc26e573f3ef099a9ULL, 0xb8b627f70ec510d0ULL, + 0xf7e653dcc6da3dd4ULL, 0x8d3e2314f6efb4adULL, + 0x0256b24ca6b12f26ULL, 0x788ec2849684a65fULL, + 0x9cf65a1b368f752eULL, 0xe62e2ad306bafc57ULL, + 0x6946bb8b56e467dcULL, 0x139ecb4366d1eea5ULL, + 0x5ccebf68aecec3a1ULL, 0x2616cfa09efb4ad8ULL, + 0xa97e5ef8cea5d153ULL, 0xd3a62e30fe90582aULL, + 0xb0c7b7e3c7593bd8ULL, 0xca1fc72bf76cb2a1ULL, + 0x45775673a732292aULL, 0x3faf26bb9707a053ULL, + 0x70ff52905f188d57ULL, 0x0a2722586f2d042eULL, + 0x854fb3003f739fa5ULL, 0xff97c3c80f4616dcULL, + 0x1bef5b57af4dc5adULL, 0x61372b9f9f784cd4ULL, + 0xee5fbac7cf26d75fULL, 0x9487ca0fff135e26ULL, + 0xdbd7be24370c7322ULL, 0xa10fceec0739fa5bULL, + 0x2e675fb4576761d0ULL, 0x54bf2f7c6752e8a9ULL, + 0xcdcf48d84fe75459ULL, 0xb71738107fd2dd20ULL, + 0x387fa9482f8c46abULL, 0x42a7d9801fb9cfd2ULL, + 0x0df7adabd7a6e2d6ULL, 0x772fdd63e7936bafULL, + 0xf8474c3bb7cdf024ULL, 0x829f3cf387f8795dULL, + 0x66e7a46c27f3aa2cULL, 0x1c3fd4a417c62355ULL, + 0x935745fc4798b8deULL, 0xe98f353477ad31a7ULL, + 0xa6df411fbfb21ca3ULL, 0xdc0731d78f8795daULL, + 0x536fa08fdfd90e51ULL, 0x29b7d047efec8728ULL, +}; + +// LSB-first +uint8_t crc8_lsb(const char *buf, int len) { + int counter; + uint8_t crc = 0; + for (counter = 0; counter < len; counter++) + crc = crc8tab_lsb[crc ^ *buf++]; + return crc; +} + +// MSB-first +uint8_t crc8_msb(const char *buf, int len) { + int counter; + uint8_t crc = 0; + for (counter = 0; counter < len; counter++) + crc = crc8tab_msb[crc ^ *buf++]; + return crc; +} + +/* crc16 hash */ +uint16_t crc16(const char *buf, int len) { + int counter; + uint16_t crc = 0; + for (counter = 0; counter < len; counter++) + crc = (crc<<8) ^ crc16tab[((crc>>8) ^ *buf++)&0x00FF]; + return crc; +} + +/* crc32 hash */ +uint32_t crc32(const char* s, int len) { + int i; + uint32_t crc32val = 0; + crc32val ^= 0xFFFFFFFF; + for (i = 0; i < len; i++) { + crc32val = crc32_tab[(crc32val ^ s[i]) & 0xFF] ^ ((crc32val >> 8) & 0x00FFFFFF); + } + return crc32val ^ 0xFFFFFFFF; +} + +/* crc64 hash */ +uint64_t crc64(const char *s, int l) { + int j = 0; + uint64_t crc = 0; + + for (j = 0; j < l; j++) { + uint8_t byte = s[j]; + crc = crc64_tab[(uint8_t)crc ^ byte] ^ (crc >> 8); + } + return crc; +} diff --git a/extend/lcrypt/src/crc.h b/extend/lcrypt/src/crc.h new file mode 100644 index 00000000..7ab2befd --- /dev/null +++ b/extend/lcrypt/src/crc.h @@ -0,0 +1,18 @@ +#pragma once + +#include "lcrypt.h" + +// LSB-first +LCRYPT_API uint8_t crc8_lsb(const char* buf, int len); + +// MSB-first +LCRYPT_API uint8_t crc8_msb(const char* buf, int len); + +/* crc16 hash */ +LCRYPT_API uint16_t crc16(const char* buf, int len); + +/* crc32 hash */ +LCRYPT_API uint32_t crc32(const char* s, int len); + +/* crc64 hash */ +LCRYPT_API uint64_t crc64(const char* s, int l); diff --git a/extend/lcrypt/src/lcrypt.c b/extend/lcrypt/src/lcrypt.c index 98737e5b..f49e84e5 100644 --- a/extend/lcrypt/src/lcrypt.c +++ b/extend/lcrypt/src/lcrypt.c @@ -455,6 +455,41 @@ static int lrsa_private_decrypt(lua_State* L) { return 2; } +static int lcrc8(lua_State* L) { + size_t len; + const char* key = lua_tolstring(L, 1, &len); + lua_pushinteger(L, crc8_lsb(key, len)); + return 1; +} + +static int lcrc8_msb(lua_State* L) { + size_t len; + const char* key = lua_tolstring(L, 1, &len); + lua_pushinteger(L, crc8_msb(key, len)); + return 1; +} + +static int lcrc16(lua_State* L) { + size_t len; + const char* key = lua_tolstring(L, 1, &len); + lua_pushinteger(L, crc16(key, len)); + return 1; +} + +static int lcrc32(lua_State* L) { + size_t len; + const char* key = lua_tolstring(L, 1, &len); + lua_pushinteger(L, crc32(key, len)); + return 1; +} + +static int lcrc64(lua_State* L) { + size_t len; + const char* key = lua_tolstring(L, 1, &len); + lua_pushinteger(L, (int64_t)crc64(key, len)); + return 1; +} + static const luaL_Reg lcrypt_funcs[] = { { "md5", lmd5 }, { "sha1", lsha1 }, @@ -462,6 +497,11 @@ static const luaL_Reg lcrypt_funcs[] = { { "sha256", lsha256 }, { "sha384", lsha384 }, { "sha512", lsha512 }, + { "crc8", lcrc8 }, + { "crc64", lcrc64 }, + { "crc32", lcrc32 }, + { "crc16", lcrc16 }, + { "crc8_msb", lcrc8_msb }, { "hmac_sha1", lhmac_sha1 }, { "hmac_sha224", lhmac_sha224 }, { "hmac_sha256", lhmac_sha256 }, diff --git a/extend/lcrypt/src/lcrypt.h b/extend/lcrypt/src/lcrypt.h index a036b233..d5cf6778 100644 --- a/extend/lcrypt/src/lcrypt.h +++ b/extend/lcrypt/src/lcrypt.h @@ -19,6 +19,7 @@ extern "C" { #define LZ_MAX_SIZE_CHUNK 65536 #include "lz4.h" +#include "crc.h" #include "md5.h" #include "rsa.h" #include "sha1.h" diff --git a/extend/lcurl/src/lcurl.cpp b/extend/lcurl/src/lcurl.cpp index 4708bfcf..50800cbd 100644 --- a/extend/lcurl/src/lcurl.cpp +++ b/extend/lcurl/src/lcurl.cpp @@ -29,8 +29,8 @@ namespace lcurl { curlm_mgr* curl_mgr = new curlm_mgr(curlm, curle); luacurl.set("curlm_mgr", curl_mgr); //函数导出 - luacurl.set_function("url_encode", [&](lua_State* L, string str){ - char* output = curl_easy_escape(curle, str.c_str(), str.size()); + luacurl.set_function("url_encode", [&](lua_State* L, string_view str){ + char* output = curl_easy_escape(curle, str.data(), str.size()); if (output) { lua_pushstring(L, output); curl_free(output); diff --git a/extend/lcurl/src/lcurl.h b/extend/lcurl/src/lcurl.h index a8a7ba3b..9c77b62f 100644 --- a/extend/lcurl/src/lcurl.h +++ b/extend/lcurl/src/lcurl.h @@ -70,13 +70,13 @@ namespace lcurl { } private: - bool request(string_view data, bool body_field = false) { + bool request(string_view& data, bool body_field = false) { if (header) { curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); } int len = data.size(); if (body_field || len > 0) { - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.data()); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len); } if (curl_multi_add_handle(curlm, curl) == CURLM_OK) { diff --git a/extend/lhttp/lhttp.lmak b/extend/lhttp/lhttp.lmak deleted file mode 100644 index d597e012..00000000 --- a/extend/lhttp/lhttp.lmak +++ /dev/null @@ -1,38 +0,0 @@ ---工程名字 -PROJECT_NAME = "lhttp" - ---目标名字 -TARGET_NAME = "lhttp" - -----工程类型: static/dynamic/exe -PROJECT_TYPE = "dynamic" - ---需要的include目录 -INCLUDES = { - "../lua/lua", - "../fmt/include", - "../luakit/include" -} - ---需要定义的选项 -DEFINES = { - "FMT_HEADER_ONLY" -} - ---WINDOWS需要定义的选项 -WINDOWS_DEFINES = { - "LUA_BUILD_AS_DLL" -} - ---需要连接的库文件 -LIBS = { - "lua" -} - ---依赖项目 -DEPS = { - "lualib" -} - ---分组定义 -GROUP = "luaext" diff --git a/extend/lhttp/lhttp.mak b/extend/lhttp/lhttp.mak deleted file mode 100644 index 82289158..00000000 --- a/extend/lhttp/lhttp.mak +++ /dev/null @@ -1,133 +0,0 @@ -#工程名字 -PROJECT_NAME = lhttp - -#目标名字 -TARGET_NAME = lhttp - -#系统环境 -UNAME_S = $(shell uname -s) - -#伪目标 -.PHONY: clean all target pre_build post_build -all : pre_build target post_build - -#CFLAG -MYCFLAGS = - -#需要定义的FLAG -MYCFLAGS += -Wsign-compare -MYCFLAGS += -Wno-sign-compare -MYCFLAGS += -Wno-unused-variable -MYCFLAGS += -Wno-unused-parameter -MYCFLAGS += -Wno-unused-but-set-parameter -MYCFLAGS += -Wno-unknown-pragmas - -#c标准库版本 -#gnu99/gnu11/gnu17 -STDC = -std=gnu99 - -#c++标准库版本 -#c++11/c++14/c++17/c++20 -STDCPP = -std=c++17 - -#需要的include目录 -MYCFLAGS += -I../lua/lua -MYCFLAGS += -I../fmt/include -MYCFLAGS += -I../luakit/include - -#需要定义的选项 -MYCFLAGS += -DFMT_HEADER_ONLY - -#LDFLAGS -LDFLAGS = - - -#源文件路径 -SRC_DIR = src - -#需要排除的源文件,目录基于$(SRC_DIR) -EXCLUDE = - -#需要连接的库文件 -LIBS = -#是否启用mimalloc库 -LIBS += -lmimalloc -MYCFLAGS += -I$(SOLUTION_DIR)extend/mimalloc/mimalloc/include -include ../../mimalloc-ex.h -#自定义库 -LIBS += -llua -#系统库 -LIBS += -lm -ldl -lstdc++ -lpthread - -#定义基础的编译选项 -ifndef CC -CC = gcc -endif -ifndef CX -CX = c++ -endif -CFLAGS = -g -O2 -Wall -Wno-deprecated -Wextra $(STDC) $(MYCFLAGS) -CXXFLAGS = -g -O2 -Wall -Wno-deprecated -Wextra $(STDCPP) $(MYCFLAGS) - -#项目目录 -ifndef SOLUTION_DIR -SOLUTION_DIR=./ -endif - -#临时文件目录 -INT_DIR = $(SOLUTION_DIR)temp/$(PROJECT_NAME) - -#目标文件前缀,定义则.so和.a加lib前缀,否则不加 -PROJECT_PREFIX = - -#目标定义 -MYCFLAGS += -fPIC -TARGET_DIR = $(SOLUTION_DIR)bin -TARGET_DYNAMIC = $(TARGET_DIR)/$(PROJECT_PREFIX)$(TARGET_NAME).so -#soname -ifeq ($(UNAME_S), Linux) -LDFLAGS += -Wl,-soname,$(PROJECT_PREFIX)$(TARGET_NAME).so -endif -#install_name -ifeq ($(UNAME_S), Darwin) -LDFLAGS += -Wl,-install_name,$(PROJECT_PREFIX)$(TARGET_NAME).so -endif - -#link添加.so目录 -LDFLAGS += -L$(SOLUTION_DIR)bin -LDFLAGS += -L$(SOLUTION_DIR)library - -#自动生成目标 -OBJS = -#根目录 -OBJS += $(patsubst $(SRC_DIR)/%.c, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.c))) -OBJS += $(patsubst $(SRC_DIR)/%.m, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.m))) -OBJS += $(patsubst $(SRC_DIR)/%.cc, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.cc))) -OBJS += $(patsubst $(SRC_DIR)/%.cpp, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.cpp))) - -# 编译所有源文件 -$(INT_DIR)/%.o : $(SRC_DIR)/%.c - $(CC) $(CFLAGS) -c $< -o $@ -$(INT_DIR)/%.o : $(SRC_DIR)/%.m - $(CC) $(CFLAGS) -c $< -o $@ -$(INT_DIR)/%.o : $(SRC_DIR)/%.cc - $(CX) $(CXXFLAGS) -c $< -o $@ -$(INT_DIR)/%.o : $(SRC_DIR)/%.cpp - $(CX) $(CXXFLAGS) -c $< -o $@ - -$(TARGET_DYNAMIC) : $(OBJS) - $(CC) -o $@ -shared $(OBJS) $(LDFLAGS) $(LIBS) - -#target伪目标 -target : $(TARGET_DYNAMIC) - -#clean伪目标 -clean : - rm -rf $(INT_DIR) - -#预编译 -pre_build: - mkdir -p $(INT_DIR) - mkdir -p $(TARGET_DIR) - -#后编译 -post_build: diff --git a/extend/lhttp/lhttp.vcxproj b/extend/lhttp/lhttp.vcxproj deleted file mode 100644 index 263ec758..00000000 --- a/extend/lhttp/lhttp.vcxproj +++ /dev/null @@ -1,78 +0,0 @@ - - - - - Develop - x64 - - - - - - - - - - {11E2E293-CF1A-0E75-B860-D3F9B4ED0407} - lhttp - Win32Proj - 10.0 - lhttp - - - - DynamicLibrary - v143 - MultiByte - - - - - - - - - - <_ProjectFileVersion>11.0.50727.1 - - - lhttp - $(SolutionDir)temp\bin\$(Platform)\ - $(SolutionDir)temp\$(ProjectName)\$(Platform)\ - - - - Disabled - ..\lua\lua;..\fmt\include;..\luakit\include;$(SolutionDir)extend\mimalloc\mimalloc\include;%(AdditionalIncludeDirectories) - WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;FMT_HEADER_ONLY;LUA_BUILD_AS_DLL;%(PreprocessorDefinitions) - Default - MultiThreadedDLL - - Level3 - ProgramDatabase - Default - ..\..\mimalloc-ex.h - stdcpp17 - true - - - $(OutDir)$(TargetName)$(TargetExt) - $(SolutionDir)library\$(Platform);;%(AdditionalLibraryDirectories) - true - Console - $(SolutionDir)library\$(Platform)\$(TargetName).lib - $(SolutionDir)temp\$(ProjectName)\$(Platform)\$(TargetName).pdb - lua.lib;mimalloc.lib;%(AdditionalDependencies) - - - - - - - copy /y $(TargetPath) $(SolutionDir)bin - - - - - - \ No newline at end of file diff --git a/extend/lhttp/lhttp.vcxproj.filters b/extend/lhttp/lhttp.vcxproj.filters deleted file mode 100644 index bd614008..00000000 --- a/extend/lhttp/lhttp.vcxproj.filters +++ /dev/null @@ -1,21 +0,0 @@ - - - - - inc - - - - - src - - - - - {CF9F3FDE-7326-F1D8-E642-05F0E07A3695} - - - {25D902C2-4283-AB8C-FBAC-54DFA101AD31} - - - \ No newline at end of file diff --git a/extend/lhttp/src/http.h b/extend/lhttp/src/http.h deleted file mode 100644 index e76f55d4..00000000 --- a/extend/lhttp/src/http.h +++ /dev/null @@ -1,219 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "fmt/core.h" -#include "lua_kit.h" - -#ifdef _MSC_VER -#define strcasecmp _stricmp -#endif - -using namespace std; - -namespace lhttp { - - const string CRLF = "\r\n"; - const string CRLF2 = "\r\n\r\n"; - const string RESPONSE = "HTTP/1.1 {}\r\nDate: {}\r\n{}\r\n{}"; - - #define SC_UNKNOWN 0 - #define SC_OK 200 - #define SC_NOCONTENT 204 - #define SC_PARTIAL 206 - #define SC_OBJMOVED 302 - #define SC_BADREQUEST 400 - #define SC_FORBIDDEN 403 - #define SC_NOTFOUND 404 - #define SC_BADMETHOD 405 - #define SC_SERVERERROR 500 - #define SC_SERVERBUSY 503 - - class http_request - { - public: - http_request() {} - - string get_header(const string key) { - auto it = headers.find(key); - if (it != headers.end()) { - return it->second; - } - return ""; - } - - string get_param(const string key) { - auto it = params.find(key); - if (it != params.end()) { - return it->second; - } - return ""; - } - - luakit::reference get_params(lua_State* L) { - luakit::kit_state kit_state(L); - return kit_state.new_reference(params); - } - - luakit::reference get_headers(lua_State* L) { - luakit::kit_state kit_state(L); - return kit_state.new_reference(headers); - } - - bool parse(const string buf) { - size_t size = buf.size(); - if (size == 0) return false; - size_t pos = buf.find(CRLF2); - if (pos == string::npos) return false; - string header = buf.substr(0, pos); - size_t offset = pos + CRLF2.length(); - vector lines; - split(header, CRLF, lines); - size_t count = lines.size(); - if (count == 0) return false; - vector parts; - split(lines[0], " ", parts); - if (parts.size() < 3) return false; - method = parts[0]; - version = parts[2]; - parse_url(parts[1]); - for (size_t i = 1; i < count; ++i) { - parse_header(lines[i]); - } - if (size >= offset) { - parse_content(buf.substr(offset)); - } - return body.size() == content_size; - } - - private: - void split(const string& str, const string& delim, vector& res) { - size_t cur = 0; - size_t step = delim.size(); - size_t pos = str.find(delim); - while (pos != string::npos) { - res.push_back(str.substr(cur, pos - cur)); - cur = pos + step; - pos = str.find(delim, cur); - } - if (str.size() > cur) { - res.push_back(str.substr(cur)); - } - } - - void parse_url(const string& str) { - params.clear(); - size_t pos = str.find("?"); - if (pos != string::npos) { - url = str.substr(0, pos); - string args = str.substr(pos + 1); - vector parts; - split(args, "&", parts); - for (const string& part : parts) { - size_t pos = part.find("="); - if (pos != string::npos) { - params.insert(make_pair(part.substr(0, pos), part.substr(pos + 1))); - } - } - } else { - url = str; - } - if (url.size() > 1 && url.back() == '/') { - url.pop_back(); - } - } - - void parse_header(const string& str) { - size_t pos = str.find(":"); - if (pos != string::npos) { - string key = str.substr(0, pos); - string value = str.substr(pos + 1); - value.erase(0, value.find_first_not_of(" ")); - headers.insert(make_pair(key, value)); - if (!strcasecmp(key.c_str(), "Content-Length")) { - content_size = atoi(value.c_str()); - } - else if (!strcasecmp(key.c_str(), "Transfer-Encoding") && !strcasecmp(value.c_str(), "chunked")) { - chunked = true; - } - } - } - - void parse_content(const string& str) { - if (!chunked) { - body = str; - return; - } - vector lines; - split(str, CRLF, lines); - for (size_t i = 0; i < lines.size(); i++) { - if (i % 2 != 0) { - body.append(lines[i]); - } - } - content_size = body.size(); - } - - public: - bool chunked = false; - size_t content_size = 0; - map params; - map headers; - string url, body, method, version; - }; - - class http_response - { - public: - http_response() {} - - void set_header(const string key, const string value) { - headers.insert(make_pair(key, value)); - } - - string serialize() { - set_header("Content-Length", fmt::format("{}", content.size())); - return fmt::format(RESPONSE, format_status(), format_date(), format_header(), content); - } - - private: - string format_date() { - char date[32]; - time_t rawtime; - time(&rawtime); - struct tm* timeinfo; - timeinfo = gmtime(&rawtime); - strftime(date, 32, "%a, %d %b %Y %T GMT", timeinfo); - return date; - } - - string format_header() { - string str; - for (auto it : headers) { - str = fmt::format("{}{}: {}\r\n", str, it.first, it.second); - } - return str; - } - - string format_status() { - switch (status) { - case SC_OK: return "200 OK"; - case SC_NOCONTENT: return "204 No Content"; - case SC_PARTIAL: return "206 Partial Content"; - case SC_BADREQUEST: return "400 Bad Request"; - case SC_OBJMOVED: return "302 Moved Temporarily"; - case SC_NOTFOUND: return "404 Not Found"; - case SC_BADMETHOD: return "405 Method Not Allowed"; - default: return "500 Internal Server Error"; - } - } - - public: - string content; - size_t status = 200; - map headers; - }; -} diff --git a/extend/lhttp/src/lhttp.cpp b/extend/lhttp/src/lhttp.cpp deleted file mode 100644 index 7f5376c5..00000000 --- a/extend/lhttp/src/lhttp.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#define LUA_LIB -#include "http.h" - -namespace lhttp { - - static http_request* create_request(lua_State* L) { - return new http_request(); - } - - static http_response* create_response(lua_State* L) { - return new http_response(); - } - - luakit::lua_table open_lhttp(lua_State* L) { - luakit::kit_state kit_state(L); - auto lhttp = kit_state.new_table(); - lhttp.set_function("create_request", create_request); - lhttp.set_function("create_response", create_response); - kit_state.new_class( - "url", &http_request::url, - "body", &http_request::body, - "method", &http_request::method, - "chunked", &http_request::chunked, - "version", &http_request::version, - "content_size", &http_request::content_size, - "get_headers", &http_request::get_headers, - "get_params", &http_request::get_params, - "get_header", &http_request::get_header, - "get_param", &http_request::get_param, - "parse", &http_request::parse - ); - kit_state.new_class( - "status", &http_response::status, - "content", &http_response::content, - "serialize", &http_response::serialize, - "set_header", &http_response::set_header - ); - return lhttp; - } -} - -extern "C" { - LUALIB_API int luaopen_lhttp(lua_State* L) { - auto lhttp = lhttp::open_lhttp(L); - return lhttp.push_stack(); - } -} diff --git a/extend/ljson/src/ljson.cpp b/extend/ljson/src/ljson.cpp index adafdab8..691b0a26 100644 --- a/extend/ljson/src/ljson.cpp +++ b/extend/ljson/src/ljson.cpp @@ -3,13 +3,21 @@ #include "ljson.h" namespace ljson { - thread_local yyjson lyyjson; + thread_local yyjson thread_json; + thread_local jsoncodec thread_codec; + + static jsoncodec* json_codec() { + thread_codec.set_json(&thread_json); + return &thread_codec; + } + luakit::lua_table open_ljson(lua_State* L) { luakit::kit_state kit_state(L); auto ljson = kit_state.new_table(); - ljson.set_function("pretty", [](lua_State* L){ return lyyjson.pretty(L); }); - ljson.set_function("encode", [](lua_State* L){ return lyyjson.encode(L); }); - ljson.set_function("decode", [](lua_State* L){ return lyyjson.decode(L); }); + ljson.set_function("jsoncodec", json_codec); + ljson.set_function("pretty", [](lua_State* L) { return thread_json.pretty(L); }); + ljson.set_function("encode", [](lua_State* L) { return thread_json.encode(L); }); + ljson.set_function("decode", [](lua_State* L) { return thread_json.decode(L); }); return ljson; } } diff --git a/extend/ljson/src/ljson.h b/extend/ljson/src/ljson.h index 04cb3761..fc2d8d5a 100644 --- a/extend/ljson/src/ljson.h +++ b/extend/ljson/src/ljson.h @@ -9,8 +9,10 @@ using namespace luakit; namespace ljson { const uint8_t max_encode_depth = 16; + class jsoncodec; class yyjson { public: + friend jsoncodec; int encode(lua_State* L) { bool empty_as_array = luaL_opt(L, lua_toboolean, 2, false); return encode_impl(L, YYJSON_WRITE_ALLOW_INVALID_UNICODE, empty_as_array); @@ -208,4 +210,33 @@ namespace ljson { } } }; + + class jsoncodec : public codec_base { + public: + virtual uint8_t* encode(lua_State* L, int index, size_t* len) { + yyjson_write_err err; + yyjson_mut_doc* doc = yyjson_mut_doc_new(nullptr); + yyjson_mut_val* val = m_json->encode_one(L, doc, false, index, 0); + uint8_t* json = (uint8_t*)yyjson_mut_val_write_opts(val, YYJSON_WRITE_ALLOW_INVALID_UNICODE, nullptr, len, &err); + return json; + } + + virtual size_t decode(lua_State* L) { + if (!m_slice) return 0; + yyjson_read_err err; + yyjson_doc* doc = yyjson_read_opts((char*)m_slice->head(), m_slice->size(), YYJSON_READ_ALLOW_INVALID_UNICODE, nullptr, &err); + if (!doc) throw invalid_argument(err.msg); + int otop = lua_gettop(L); + m_json->decode_one(L, yyjson_doc_get_root(doc), true); + yyjson_doc_free(doc); + return lua_gettop(L) - otop; + } + + void set_json(yyjson* json) { + m_json = json; + } + + protected: + yyjson* m_json; + }; } diff --git a/extend/luakit/include/lua_base.h b/extend/luakit/include/lua_base.h index 33d0ade3..da63f1b1 100644 --- a/extend/luakit/include/lua_base.h +++ b/extend/luakit/include/lua_base.h @@ -23,8 +23,8 @@ namespace luakit { #define MAX_LUA_META_KEY 128 - //异常处理器 - using exception_handler = std::function; + //错误函数 + using error_fn = std::function; template const char* lua_get_meta_name() { diff --git a/extend/luakit/include/lua_buff.h b/extend/luakit/include/lua_buff.h index 305eb1bd..28274a11 100644 --- a/extend/luakit/include/lua_buff.h +++ b/extend/luakit/include/lua_buff.h @@ -141,8 +141,12 @@ namespace luakit { return std::string_view((const char*)m_head, len); } - size_t write(const char* src, size_t len) { - return push_data((const uint8_t*)src, len); + size_t write(const char* src) { + return push_data((const uint8_t*)src, strlen(src)); + } + + size_t write(const std::string& src) { + return push_data((const uint8_t*)src.c_str(), src.size()); } template diff --git a/extend/luakit/include/lua_codec.h b/extend/luakit/include/lua_codec.h index 309e3982..4787705a 100644 --- a/extend/luakit/include/lua_codec.h +++ b/extend/luakit/include/lua_codec.h @@ -4,6 +4,8 @@ #pragma warning(disable: 4267) #endif +#include + #include "lua_buff.h" namespace luakit { @@ -40,7 +42,7 @@ namespace luakit { T value_decode(lua_State* L, slice* slice) { T* value = slice->read(); if (value == nullptr) { - luaL_error(L, "decode can't unpack one value"); + throw std::length_error("decode can't unpack one value"); } return *value; } @@ -161,9 +163,8 @@ namespace luakit { return; } auto str = (const char*)slice->peek(sz); - if (str == nullptr) { - luaL_error(L, "decode string is out of range"); - return; + if (str == nullptr || sz > USHRT_MAX) { + throw std::length_error("decode string is out of range"); } slice->erase(sz); lua_pushlstring(L, str, sz); @@ -227,13 +228,17 @@ namespace luakit { } inline int decode_slice(lua_State* L, slice* slice) { - lua_settop(L, 0); - while (1) { - uint8_t* type = slice->read(); - if (type == nullptr) break; - decode_value(L, slice, *type); + int top = lua_gettop(L); + try { + while (1) { + uint8_t* type = slice->read(); + if (type == nullptr) break; + decode_value(L, slice, *type); + } + } catch (const std::exception& e){ + luaL_error(L, e.what()); } - return lua_gettop(L); + return lua_gettop(L) - top; } inline int decode(lua_State* L, luabuf* buff) { @@ -279,18 +284,30 @@ namespace luakit { inline void serialize_table(lua_State* L, luabuf* buff, int index, int depth, int line) { index = lua_absindex(L, index); - bool barray = is_array(L, index, lua_rawlen(L, index)); + size_t rawlen = lua_rawlen(L, index); + bool barray = is_array(L, index, rawlen); int size = 0; - lua_pushnil(L); serialize_value(buff, "{"); serialize_crcn(buff, depth, line); - while (lua_next(L, index) != 0) { - if (size++ > 0) { - serialize_value(buff, ","); - serialize_crcn(buff, depth, line); + if (barray) { + for (int i = 1; i <= rawlen; ++i){ + if (size++ > 0) { + serialize_value(buff, ","); + serialize_crcn(buff, depth, line); + } + lua_geti(L, index, i); + serialize_one(L, buff, -1, depth, line); + lua_pop(L, 1); } - if (!barray) { + } + else { + lua_pushnil(L); + while (lua_next(L, index) != 0) { + if (size++ > 0) { + serialize_value(buff, ","); + serialize_crcn(buff, depth, line); + } if (lua_isnumber(L, -2)) { lua_pushvalue(L, -2); serialize_quote(buff, lua_tostring(L, -1), "[", "]="); @@ -304,9 +321,9 @@ namespace luakit { serialize_one(L, buff, -2, depth, line); serialize_value(buff, "="); } + serialize_one(L, buff, -1, depth, line); + lua_pop(L, 1); } - serialize_one(L, buff, -1, depth, line); - lua_pop(L, 1); } serialize_crcn(buff, depth - 1, line); serialize_value(buff, "}"); @@ -370,8 +387,13 @@ namespace luakit { class codec_base { public: void __gc() {} - virtual uint8_t* encode(lua_State* L, int index, size_t* len) = 0; virtual size_t decode(lua_State* L) = 0; + virtual uint8_t* encode(lua_State* L, int index, size_t* len) = 0; + size_t decode(lua_State* L, uint8_t* data, size_t len) { + slice mslice(data, len); + m_slice = &mslice; + return decode(L); + } void set_slice(slice* slice) { m_slice = slice; } protected: slice* m_slice = nullptr; @@ -390,13 +412,15 @@ namespace luakit { virtual size_t decode(lua_State* L) { if (!m_slice) return 0; - int old_top = lua_gettop(L); + int top = lua_gettop(L); while (1) { uint8_t* type = m_slice->read(); if (type == nullptr) break; decode_value(L, m_slice, *type); } - return lua_gettop(L) - old_top; + size_t argnum = lua_gettop(L) - top; + m_slice = nullptr; + return argnum; } protected: diff --git a/extend/luakit/include/lua_function.h b/extend/luakit/include/lua_function.h index 5c6968ed..23d45f22 100644 --- a/extend/luakit/include/lua_function.h +++ b/extend/luakit/include/lua_function.h @@ -243,7 +243,7 @@ namespace luakit { //call function //------------------------------------------------------------------------------- - inline bool lua_call_function(lua_State* L, exception_handler handler, int arg_count, int ret_count) { + inline bool lua_call_function(lua_State* L, error_fn efn, int arg_count, int ret_count) { int func_idx = lua_gettop(L) - arg_count; if (func_idx <= 0 || !lua_isfunction(L, func_idx)) return false; @@ -254,8 +254,8 @@ namespace luakit { lua_insert(L, func_idx); if (lua_pcall(L, arg_count, ret_count, func_idx)) { - if (handler != nullptr) { - handler(lua_tostring(L, -1)); + if (efn != nullptr) { + efn(lua_tostring(L, -1)); } lua_pop(L, 2); return false; @@ -265,9 +265,9 @@ namespace luakit { } template - bool lua_call_function(lua_State* L, exception_handler handler, std::tuple&& rets, arg_types... args) { + bool lua_call_function(lua_State* L, error_fn efn, std::tuple&& rets, arg_types... args) { native_to_lua_mutil(L, std::forward(args)...); - if (!lua_call_function(L, handler, sizeof...(arg_types), sizeof...(ret_types))) + if (!lua_call_function(L, efn, sizeof...(arg_types), sizeof...(ret_types))) return false; lua_to_native_mutil(L, rets, std::make_index_sequence()); lua_pop(L, (int)sizeof...(ret_types)); @@ -275,10 +275,18 @@ namespace luakit { } template - bool lua_call_function(lua_State* L, exception_handler handler, codec_base* codec, std::tuple&& rets, arg_types... args) { + bool lua_call_function(lua_State* L, error_fn efn, codec_base* codec, std::tuple&& rets, arg_types... args) { + int arg_num = sizeof...(arg_types); native_to_lua_mutil(L, std::forward(args)...); - int arg_num = codec->decode(L); - if (!lua_call_function(L, handler, arg_num + sizeof...(arg_types), sizeof...(ret_types))) + try { + arg_num += codec->decode(L); + } catch(const std::length_error&) { + return false; + } catch(const std::exception& e) { + if (efn) efn(e.what()); + return false; + } + if (!lua_call_function(L, efn, arg_num, sizeof...(ret_types))) return false; lua_to_native_mutil(L, rets, std::make_index_sequence()); lua_pop(L, (int)sizeof...(ret_types)); @@ -286,33 +294,38 @@ namespace luakit { } template - bool call_global_function (lua_State* L, const char* function, exception_handler handler, std::tuple&& rets, arg_types... args) { + bool call_global_function (lua_State* L, const char* function, error_fn efn, std::tuple&& rets, arg_types... args) { + lua_guard g(L); if (!get_global_function(L, function)) return false; - return lua_call_function(L, handler, std::forward>(rets), std::forward(args)...); + return lua_call_function(L, efn, std::forward>(rets), std::forward(args)...); } template - bool call_table_function(lua_State* L, const char* table, const char* function, exception_handler handler, std::tuple&& rets, arg_types... args) { + bool call_table_function(lua_State* L, const char* table, const char* function, error_fn efn, std::tuple&& rets, arg_types... args) { + lua_guard g(L); if (!get_table_function(L, table, function)) return false; - return lua_call_function(L, handler, std::forward>(rets), std::forward(args)...); + return lua_call_function(L, efn, std::forward>(rets), std::forward(args)...); } template - bool call_table_function(lua_State* L, const char* table, const char* function, exception_handler handler, codec_base* codec, std::tuple&& rets, arg_types... args) { + bool call_table_function(lua_State* L, const char* table, const char* function, error_fn efn, codec_base* codec, std::tuple&& rets, arg_types... args) { + lua_guard g(L); if (!get_table_function(L, table, function)) return false; - return lua_call_function(L, handler, codec, std::forward>(rets), std::forward(args)...); + return lua_call_function(L, efn, codec, std::forward>(rets), std::forward(args)...); } template - bool call_object_function(lua_State* L, T* o, const char* function, exception_handler handler, std::tuple&& rets, arg_types... args) { + bool call_object_function(lua_State* L, T* o, const char* function, error_fn efn, std::tuple&& rets, arg_types... args) { + lua_guard g(L); if (!get_object_function(L, o, function)) return false; - return lua_call_function(L, handler, std::forward>(rets), std::forward(args)...); + return lua_call_function(L, efn, std::forward>(rets), std::forward(args)...); } template - bool call_object_function(lua_State* L, T* o, const char* function, exception_handler handler, codec_base* codec, std::tuple&& rets, arg_types... args) { + bool call_object_function(lua_State* L, T* o, const char* function, error_fn efn, codec_base* codec, std::tuple&& rets, arg_types... args) { + lua_guard g(L); if (!get_object_function(L, o, function)) return false; - return lua_call_function(L, handler, codec, std::forward>(rets), std::forward(args)...); + return lua_call_function(L, efn, codec, std::forward>(rets), std::forward(args)...); } template diff --git a/extend/luakit/include/lua_kit.h b/extend/luakit/include/lua_kit.h index 046c75c1..4ec1dce1 100644 --- a/extend/luakit/include/lua_kit.h +++ b/extend/luakit/include/lua_kit.h @@ -18,8 +18,7 @@ namespace luakit { "size", &slice::size, "recv", &slice::recv, "peek", &slice::check, - "string", &slice::string, - "contents", &slice::contents + "string", &slice::string ); } kit_state(lua_State* L) : m_L(L) {} @@ -53,75 +52,75 @@ namespace luakit { } template - bool call(const char* function, exception_handler handler, std::tuple&& rets, arg_types... args) { - return call_global_function(m_L, function, handler, std::forward>(rets), std::forward(args)...); + bool call(const char* function, error_fn efn, std::tuple&& rets, arg_types... args) { + return call_global_function(m_L, function, efn, std::forward>(rets), std::forward(args)...); } - bool call(const char* function, exception_handler handler = nullptr) { - return call_global_function(m_L, function, handler, std::tie()); + bool call(const char* function, error_fn efn = nullptr) { + return call_global_function(m_L, function, efn, std::tie()); } - bool call(exception_handler handler = nullptr) { - return lua_call_function(m_L, handler, std::tie()); + bool call(error_fn efn = nullptr) { + return lua_call_function(m_L, efn, std::tie()); } template - bool table_call(const char* table, const char* function, exception_handler handler, std::tuple&& rets, arg_types... args) { - return call_table_function(m_L, table, function, handler, std::forward>(rets), std::forward(args)...); + bool table_call(const char* table, const char* function, error_fn efn, std::tuple&& rets, arg_types... args) { + return call_table_function(m_L, table, function, efn, std::forward>(rets), std::forward(args)...); } template - bool table_call(const char* table, const char* function, exception_handler handler, codec_base* codec, std::tuple&& rets, arg_types... args) { - return call_table_function(m_L, table, function, handler, codec, std::forward>(rets), std::forward(args)...); + bool table_call(const char* table, const char* function, error_fn efn, codec_base* codec, std::tuple&& rets, arg_types... args) { + return call_table_function(m_L, table, function, efn, codec, std::forward>(rets), std::forward(args)...); } - bool table_call(const char* table, const char* function, exception_handler handler = nullptr) { - return call_table_function(m_L, table, function, handler, std::tie()); + bool table_call(const char* table, const char* function, error_fn efn = nullptr) { + return call_table_function(m_L, table, function, efn, std::tie()); } template - bool object_call(T* obj, const char* function, exception_handler handler, std::tuple&& rets, arg_types... args) { - return call_object_function(m_L, obj, function, handler, std::forward>(rets), std::forward(args)...); + bool object_call(T* obj, const char* function, error_fn efn, std::tuple&& rets, arg_types... args) { + return call_object_function(m_L, obj, function, efn, std::forward>(rets), std::forward(args)...); } template - bool object_call(T* obj, const char* function, exception_handler handler, codec_base* codec, std::tuple&& rets, arg_types... args) { - return call_object_function(m_L, obj, function, handler, codec, std::forward>(rets), std::forward(args)...); + bool object_call(T* obj, const char* function, error_fn efn, codec_base* codec, std::tuple&& rets, arg_types... args) { + return call_object_function(m_L, obj, function, efn, codec, std::forward>(rets), std::forward(args)...); } template - bool object_call(T* obj, const char* function, exception_handler handler = nullptr) { - return call_object_function(function, obj, handler, std::tie()); + bool object_call(T* obj, const char* function, error_fn efn = nullptr) { + return call_object_function(function, obj, efn, std::tie()); } - bool run_file(const std::string& filename, exception_handler handler = nullptr) { - return run_file(filename.c_str(), handler); + bool run_file(const std::string& filename, error_fn efn = nullptr) { + return run_file(filename.c_str(), efn); } - bool run_file(const char* filename, exception_handler handler = nullptr) { + bool run_file(const char* filename, error_fn efn = nullptr) { lua_guard g(m_L); if (luaL_loadfile(m_L, filename)) { - if (handler) { - handler(lua_tostring(m_L, -1)); + if (efn) { + efn(lua_tostring(m_L, -1)); } return false; } - return lua_call_function(m_L, handler, 0, 0); + return lua_call_function(m_L, efn, 0, 0); } - bool run_script(const std::string& script, exception_handler handler = nullptr) { - return run_script(script.c_str(), handler); + bool run_script(const std::string& script, error_fn efn= nullptr) { + return run_script(script.c_str(), efn); } - bool run_script(const char* script, exception_handler handler = nullptr) { + bool run_script(const char* script, error_fn efn= nullptr) { lua_guard g(m_L); if (luaL_loadstring(m_L, script)) { - if (handler) { - handler(lua_tostring(m_L, -1)); + if (efn) { + efn(lua_tostring(m_L, -1)); } return false; } - return lua_call_function(m_L, handler, 0, 0); + return lua_call_function(m_L, efn, 0, 0); } lua_table new_table(const char* name = nullptr) { diff --git a/extend/luakit/include/lua_slice.h b/extend/luakit/include/lua_slice.h index 6f8eb0ba..0c43d1a1 100644 --- a/extend/luakit/include/lua_slice.h +++ b/extend/luakit/include/lua_slice.h @@ -93,11 +93,9 @@ namespace luakit { return m_head; } - int contents(lua_State* L) { + std::string_view contents() { size_t len = (size_t)(m_tail - m_head); - lua_pushlightuserdata(L, (void*)m_head); - lua_pushinteger(L, len); - return 2; + return std::string_view((const char*)m_head, len); } int string(lua_State* L) { @@ -105,7 +103,7 @@ namespace luakit { lua_pushlstring(L, (const char*)m_head, len); return 1; } - + protected: uint8_t* m_head = nullptr; uint8_t* m_tail = nullptr; diff --git a/extend/luakit/include/lua_stack.h b/extend/luakit/include/lua_stack.h index b01b12b4..a82218ad 100644 --- a/extend/luakit/include/lua_stack.h +++ b/extend/luakit/include/lua_stack.h @@ -311,8 +311,6 @@ namespace luakit { if (lua_istable(L, idx)) { lua_getfield(L, idx, "__pointer__"); T obj = (T)lua_touserdata(L, -1); - const char* meta_name2 = lua_get_meta_name(); - size_t pkey2 = lua_get_object_key(obj); lua_pop(L, 1); return obj; } diff --git a/extend/luakit/include/lua_table.h b/extend/luakit/include/lua_table.h index 7a5b6c5e..cc8b21d9 100644 --- a/extend/luakit/include/lua_table.h +++ b/extend/luakit/include/lua_table.h @@ -48,18 +48,18 @@ namespace luakit { } template - bool call(const char* function, exception_handler handler, std::tuple&& rets, arg_types... args) { + bool call(const char* function, error_fn efn, std::tuple&& rets, arg_types... args) { if (!get_function(function)) return false; - return lua_call_function(m_L, handler, std::forward>(rets), std::forward(args)...); + return lua_call_function(m_L, efn, std::forward>(rets), std::forward(args)...); } - bool call(const char* function, exception_handler handler = nullptr) { + bool call(const char* function, error_fn efn = nullptr) { if (!get_function(function)) return false; - return lua_call_function(m_L, handler, std::tie()); + return lua_call_function(m_L, efn, std::tie()); } - bool call(exception_handler handler = nullptr) { - return lua_call_function(m_L, handler, std::tie()); + bool call(error_fn efn = nullptr) { + return lua_call_function(m_L, efn, std::tie()); } void create_with() {} diff --git a/quanta.sln b/quanta.sln index 9bd1e05b..d9ebe9f5 100644 --- a/quanta.sln +++ b/quanta.sln @@ -44,11 +44,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ldetour", "extend\ldetour\l {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} = {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} EndProjectSection EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lhttp", "extend\lhttp\lhttp.vcxproj", "{11E2E293-CF1A-0E75-B860-D3F9B4ED0407}" - ProjectSection(ProjectDependencies) = postProject - {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} = {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} - EndProjectSection -EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ljson", "extend\ljson\ljson.vcxproj", "{D2FD53CE-5F95-C3EC-13A1-11D91B9A412C}" ProjectSection(ProjectDependencies) = postProject {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} = {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} @@ -124,8 +119,6 @@ Global {6B01DD6C-28A1-231F-FE9B-1D3F40E3CA3B}.Develop|x64.Build.0 = Develop|x64 {086B9D7D-2973-47BD-8CF0-82BB5A9BFA55}.Develop|x64.ActiveCfg = Develop|x64 {086B9D7D-2973-47BD-8CF0-82BB5A9BFA55}.Develop|x64.Build.0 = Develop|x64 - {11E2E293-CF1A-0E75-B860-D3F9B4ED0407}.Develop|x64.ActiveCfg = Develop|x64 - {11E2E293-CF1A-0E75-B860-D3F9B4ED0407}.Develop|x64.Build.0 = Develop|x64 {D2FD53CE-5F95-C3EC-13A1-11D91B9A412C}.Develop|x64.ActiveCfg = Develop|x64 {D2FD53CE-5F95-C3EC-13A1-11D91B9A412C}.Develop|x64.Build.0 = Develop|x64 {8667E717-B97C-935C-4FDF-2878AF7DAB1A}.Develop|x64.ActiveCfg = Develop|x64 @@ -160,7 +153,6 @@ Global {6779B38A-0EC6-088E-3974-E2A164AE1520} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907} {6B01DD6C-28A1-231F-FE9B-1D3F40E3CA3B} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907} {086B9D7D-2973-47BD-8CF0-82BB5A9BFA55} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907} - {11E2E293-CF1A-0E75-B860-D3F9B4ED0407} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907} {D2FD53CE-5F95-C3EC-13A1-11D91B9A412C} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907} {8667E717-B97C-935C-4FDF-2878AF7DAB1A} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907} {2DE9C09F-02F5-DF2B-524C-FFBEBC8DE5EE} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907} diff --git a/script/agent/redis_agent.lua b/script/agent/redis_agent.lua index 2fe181e0..97846cb7 100644 --- a/script/agent/redis_agent.lua +++ b/script/agent/redis_agent.lua @@ -14,7 +14,7 @@ end --db_query: { cmd, ...} function RedisAgent:execute(db_query, hash_key, db_id) local key = hash_key or mrandom() - return router_mgr:call_redis_hash(key, "rpc_redis_execute", db_id or MAIN_DBID, key, tunpack(db_query)) + return router_mgr:call_redis_hash(key, "rpc_redis_execute", db_id or MAIN_DBID, tunpack(db_query)) end ------------------------------------------------------------------ diff --git a/script/basic/environ.lua b/script/basic/environ.lua index f76858ed..9ef9e2fe 100644 --- a/script/basic/environ.lua +++ b/script/basic/environ.lua @@ -61,9 +61,9 @@ local function parse_hosts(value) local hosts = {} local strs = ssplit(value, ",") for _, str in pairs(strs) do - local k, v = usplit(str, ":") + local k, v = saddr(str) if k then - hosts[k] = v + hosts[#hosts + 1] = { k, v } end end return hosts diff --git a/script/basic/library.lua b/script/basic/library.lua index fc15c15f..2b284ec1 100644 --- a/script/basic/library.lua +++ b/script/basic/library.lua @@ -26,8 +26,6 @@ local librarys = { if qgetenv("QUANTA_MODE") then --aoi解析 librarys.aoi = require("laoi") - --http解析 - librarys.http = require("lhttp") --Curl库 librarys.curl = require("lcurl") --网络库 diff --git a/script/basic/string.lua b/script/basic/string.lua index f1ba73fc..18e84bb6 100644 --- a/script/basic/string.lua +++ b/script/basic/string.lua @@ -52,24 +52,6 @@ function qstring.split(str, token) return t end -function qstring.split_pos(str, token) - if #str > 0 then - local elem = {} - local pos, t = 0, { cur = 0 } - for st, sp in function() return sfind(str, token, pos, true) end do - if st > 1 then - elem[#elem + 1] = {ssub(str, pos, st - 1), sp } - end - pos = sp + 1 - end - if pos <= #str then - elem[#elem + 1] = { ssub(str, pos), #str } - end - t.elem = elem - return t - end -end - function qstring.ends_with(str, ending) return str:sub(-#ending) == ending end diff --git a/script/driver/mongo.lua b/script/driver/mongo.lua index dfd81256..f132df0b 100644 --- a/script/driver/mongo.lua +++ b/script/driver/mongo.lua @@ -102,14 +102,14 @@ function MongoDB:setup_pool(hosts) log_err("[MongoDB][setup_pool] mongo config err: hosts is empty") return end - local count = POOL_COUNT - while count > 0 do - for ip, port in pairs(hosts) do - local socket = Socket(self, ip, port) + local count = 1 + for _, host in pairs(hosts) do + for c = 1, POOL_COUNT do + local socket = Socket(self, host[1], host[2]) self.connections[count] = socket socket.sessions = {} socket:set_id(count) - count = count - 1 + count = count + 1 end end self.timer_id = timer_mgr:register(0, SECOND_MS, -1, function() @@ -157,11 +157,12 @@ end function MongoDB:login(socket) local id, ip, port = socket.id, socket.ip, socket.port - local ok, err = socket:connect(ip, port, eproto_type.common, self.codec) + local ok, err = socket:connect(ip, port, eproto_type.mongo) if not ok then log_err("[MongoDB][login] connect db(%s:%s:%s:%s) failed: %s!", ip, port, self.name, id, err) return false end + socket:set_codec(self.codec) if self.user and self.passwd then local aok, aerr = self:auth(socket, self.user, self.passwd) if not aok then @@ -300,9 +301,6 @@ function MongoDB:op_msg(sock, session_id, cmd, ...) if not sock:send_data(session_id, cmd, ...) then return false, "send failed" end - if session_id <= 0 then - return true - end sock.sessions[session_id] = cmd self.req_counter:count_increase() local _ = qdefer(function() diff --git a/script/driver/redis.lua b/script/driver/redis.lua index 8361d5ed..e24b6a07 100644 --- a/script/driver/redis.lua +++ b/script/driver/redis.lua @@ -4,16 +4,14 @@ local QueueFIFO = import("container/queue_fifo.lua") local tonumber = tonumber local log_err = logger.err -local log_warn = logger.warn local log_info = logger.info -local ssub = string.sub -local slower = string.lower +local log_debug = logger.debug local sformat = string.format +local slower = string.lower local tinsert = table.insert local mrandom = qmath.random -local tjoin = qtable.join +local crc16 = crypt.crc16 local tdelete = qtable.delete -local is_array = qtable.is_array local qhash = codec.hash_code local makechan = quanta.make_channel @@ -22,120 +20,12 @@ local thread_mgr = quanta.get("thread_mgr") local event_mgr = quanta.get("event_mgr") local update_mgr = quanta.get("update_mgr") -local LineTitle = "\r\n" local SUCCESS = quanta.enum("KernCode", "SUCCESS") local SECOND_MS = quanta.enum("PeriodTime", "SECOND_MS") local SECOND_10_MS = quanta.enum("PeriodTime", "SECOND_10_MS") local DB_TIMEOUT = quanta.enum("NetwkTime", "DB_CALL_TIMEOUT") -local POOL_COUNT = environ.number("QUANTA_DB_POOL_COUNT", 5) - -local function _next_line(resp) - local nindex = resp.cur + 1 - local elem = resp.elem[nindex] - if elem then - resp.cur = nindex - return elem[1] - end -end - -local function _parse_offset(resp) - return resp.elem[resp.cur][2] -end - -local _redis_proto_parser = {} -_redis_proto_parser["+"] = function(body) - --simple string - return true, true, body -end - -_redis_proto_parser["-"] = function(body) - -- error reply - return true, false, body -end - -_redis_proto_parser[":"] = function(body) - -- integer reply - return true, true, tonumber(body) -end - -local function _parse_packet(packet, istext) - local line = _next_line(packet) - if not line then - return false, "packet isn't complete" - end - if istext then - return true, true, line - end - local prefix, body = ssub(line, 1, 1), ssub(line, 2) - local prefix_func = _redis_proto_parser[prefix] - if prefix_func then - return prefix_func(body, packet) - end - return false, "packet format err" -end - -_redis_proto_parser["$"] = function(body, packet) - -- bulk string - if tonumber(body) < 0 then - return true, true - end - return _parse_packet(packet, true) -end - -_redis_proto_parser["*"] = function(body, packet) - -- array - local length = tonumber(body) - if length < 0 then - return true, true - end - local array = {} - for i = 1, length do - local ok, err, value = _parse_packet(packet) - if not ok then - return false, err - end - array[i] = value - end - return true, true, array -end - -local function _compose_bulk_string(value) - if not value then - return "\r\n$-1" - end - if type(value) ~= "string" then - value = tostring(value) - end - return sformat("\r\n$%d\r\n%s", #value, value) -end - -local function _format_args(...) - local args = {} - for _, arg in pairs({ ... }) do - if type(arg) ~= "table" then - tinsert(args, arg) - else - if is_array(arg) then - tjoin(arg, args) - else - for key, value in pairs(arg) do - tinsert(args, key) - tinsert(args, value) - end - end - end - end - return args -end - -local function _compose_args(cmd, ...) - local args = _format_args(...) - local buff = sformat("*%d%s", #args + 1, _compose_bulk_string(cmd)) - for _, arg in ipairs(args) do - buff = sformat("%s%s", buff, _compose_bulk_string(arg)) - end - return sformat("%s\r\n", buff) -end +local POOL_COUNT = environ.number("QUANTA_DB_POOL_COUNT", 3) +local REDIS_SLOT = 16384 local function _tomap(value) if (type(value) == 'table') then @@ -149,166 +39,77 @@ local function _tomap(value) end local function _toboolean(value) - value = tostring(value) - if value == '1' or value == 'true' or value == 'TRUE' then + if value == 1 or value == 'true' or value == 'TRUE' then return true end return false end local function _toscan(value) + if (type(value) == 'table') then + return tonumber(value[1]), value[2] + end + return 0, {} +end + +local function _tohscan(value) if (type(value) == 'table') then return tonumber(value[1]), _tomap(value[2]) end return 0, {} end -local redis_commands = { - del = { cmd = "DEL" }, - set = { cmd = "SET" }, - keys = { cmd = "KEYS" }, - type = { cmd = "TYPE" }, - incr = { cmd = "INCR" }, - incrby = { cmd = "INCRBY", }, - rename = { cmd = "RENAME" }, - ttl = { cmd = "TTL" }, - dbsize = { cmd = "DBSIZE" }, - pttl = { cmd = "PTTL" }, -- >= 2.6 - setex = { cmd = "SETEX" }, -- >= 2.0 - psetex = { cmd = "PSETEX" }, -- >= 2.6 - get = { cmd = "GET" }, - mget = { cmd = "MGET" }, - getset = { cmd = "GETSET" }, - decr = { cmd = "DECR" }, - decrby = { cmd = "DECRBY" }, - append = { cmd = "APPEND" }, -- >= 2.0 - substr = { cmd = "SUBSTR" }, -- >= 2.0 - strlen = { cmd = "STRLEN" }, -- >= 2.2 - setrange = { cmd = "SETRANGE"}, -- >= 2.2 - getrange = { cmd = "GETRANGE"}, -- >= 2.2 - setbit = { cmd = "SETBIT" }, -- >= 2.2 - getbit = { cmd = "GETBIT" }, -- >= 2.2 - bitop = { cmd = "BITOP" }, -- >= 2.6 - bitcount = { cmd = "BITCOUNT"}, -- >= 2.6 - rpush = { cmd = "RPUSH" }, - lpush = { cmd = "LPUSH" }, - llen = { cmd = "LLEN" }, - lrange = { cmd = "LRANGE" }, - ltrim = { cmd = "LTRIM" }, - lindex = { cmd = "LINDEX" }, - lset = { cmd = "LSET" }, - lrem = { cmd = "LREM" }, - lpop = { cmd = "LPOP" }, - rpop = { cmd = "RPOP" }, - blpop = { cmd = "BLPOP" }, -- >= 2.0 - brpop = { cmd = "BRPOP" }, -- >= 2.0 - rpushx = { cmd = "RPUSHX" }, -- >= 2.2 - lpushx = { cmd = "LPUSHX" }, -- >= 2.2 - linsert = { cmd = "LINSERT" }, -- >= 2.2 - sadd = { cmd = "SADD" }, - srem = { cmd = "SREM" }, - spop = { cmd = "SPOP" }, - scard = { cmd = "SCARD" }, - sinter = { cmd = "SINTER" }, - sunion = { cmd = "SUNION" }, - sdiff = { cmd = "SDIFF" }, - zadd = { cmd = "ZADD" }, - zrem = { cmd = "ZREM" }, - zcount = { cmd = "ZCOUNT" }, - zcard = { cmd = "ZCARD" }, - zscore = { cmd = "ZSCORE" }, - zrank = { cmd = "ZRANK" }, -- >= 2.0 - zrevrank = { cmd = "ZREVRANK"}, -- >= 2.0 - hget = { cmd = "HGET" }, -- >= 2.0 - hdel = { cmd = "HDEL" }, -- >= 2.0 - hlen = { cmd = "HLEN" }, -- >= 2.0 - hkeys = { cmd = "HKEYS" }, -- >= 2.0 - hvals = { cmd = "HVALS" }, -- >= 2.0 - hincrby = { cmd = "HINCRBY" }, -- >= 2.0 - echo = { cmd = "ECHO" }, - select = { cmd = "SELECT" }, - multi = { cmd = "MULTI" }, -- >= 2.0 - exec = { cmd = "EXEC" }, -- >= 2.0 - discard = { cmd = "DISCARD" }, -- >= 2.0 - watch = { cmd = "WATCH" }, -- >= 2.2 - unwatch = { cmd = "UNWATCH" }, -- >= 2.2 - eval = { cmd = "EVAL" }, -- >= 2.6 - evalsha = { cmd = "EVALSHA" }, -- >= 2.6 - script = { cmd = "SCRIPT" }, -- >= 2.6 - time = { cmd = "TIME" }, -- >= 2.6 - client = { cmd = "CLIENT" }, -- >= 2.4 - slaveof = { cmd = "SLAVEOF" }, - save = { cmd = "SAVE" }, - bgsave = { cmd = "BGSAVE" }, - lastsave = { cmd = "LASTSAVE"}, - flushdb = { cmd = "FLUSHDB" }, - flushall = { cmd = "FLUSHALL"}, - monitor = { cmd = "MONITOR" }, - hmset = { cmd = "HMSET" }, -- >= 2.0 - hmget = { cmd = "HMGET" }, -- >= 2.0 - sort = { cmd = "SORT" }, - mset = { cmd = "MSET" }, - publish = { cmd = "PUBLISH" }, -- >= 2.0 - sinterstore = { cmd = "SINTERSTORE" }, - sunionstore = { cmd = "SUNIONSTORE" }, - sdiffstore = { cmd = "SDIFFSTORE" }, - smembers = { cmd = "SMEMBERS" }, - srandmember = { cmd = "SRANDMEMBER" }, - rpoplpush = { cmd = "RPOPLPUSH" }, - randomkey = { cmd = "RANDOMKEY" }, - brpoplpush = { cmd = "BRPOPLPUSH" }, -- >= 2.2 - bgrewriteaof= { cmd = "BGREWRITEAOF"}, - zrange = { cmd = "ZRANGE", }, - zrevrange = { cmd = "ZREVRANGE" }, - zrangebyscore = { cmd = "ZRANGEBYSCORE" }, - zrevrangebyscore= { cmd = "ZREVRANGEBYSCORE" }, -- >= 2.2 - zunionstore = { cmd = "ZUNIONSTORE" }, -- >= 2.0 - zinterstore = { cmd = "ZINTERSTORE" }, -- >= 2.0 - zremrangebyscore= { cmd = "ZREMRANGEBYSCORE" }, - zremrangebyrank = { cmd = "ZREMRANGEBYRANK" }, -- >= 2.0 - scan = { cmd = "SCAN", convertor = _toscan }, -- >= 2.8 - hscan = { cmd = "HSCAN", convertor = _toscan }, -- >= 2.8 - sscan = { cmd = "SSCAN", convertor = _toscan }, -- >= 2.8 - zscan = { cmd = "ZSCAN", convertor = _toscan }, -- >= 2.8 - zincrby = { cmd = "ZINCRBY", convertor = tonumber }, - incrbyfloat = { cmd = "INCRBYFLOAT", convertor = tonumber }, - hincrbyfloat = { cmd = "HINCRBYFLOAT", convertor = tonumber }, -- >= 2.6 - setnx = { cmd = "SETNX", convertor = _toboolean }, - exists = { cmd = "EXISTS", convertor = _toboolean }, - renamenx = { cmd = "RENAMENX", convertor = _toboolean }, - expire = { cmd = "EXPIRE", convertor = _toboolean }, - pexpire = { cmd = "PEXPIRE", convertor = _toboolean }, -- >= 2.6 - expireat = { cmd = "EXPIREAT", convertor = _toboolean }, - pexpireat = { cmd = "PEXPIREAT", convertor = _toboolean }, -- >= 2.6 - move = { cmd = "MOVE", convertor = _toboolean }, - persist = { cmd = "PERSIST", convertor = _toboolean }, -- >= 2.2 - smove = { cmd = "SMOVE", convertor = _toboolean }, - sismember = { cmd = "SISMEMBER", convertor = _toboolean }, - hset = { cmd = "HSET", convertor = _toboolean }, -- >= 2.0 - hsetnx = { cmd = "HSETNX", convertor = _toboolean }, -- >= 2.0 - hexists = { cmd = "HEXISTS", convertor = _toboolean }, -- >= 2.0 - msetnx = { cmd = "MSETNX", convertor = _toboolean }, - hgetall = { cmd = "HGETALL", convertor = _tomap }, -- >= 2.0 - config = { cmd = "CONFIG", convertor = _tomap }, -- >= 2.0 +local rconvertors = { + scan = _toscan, + sscan = _toscan, + hscan = _tohscan, + zscan = _tohscan, + zincrby = tonumber, + incrbyfloat = tonumber, + hincrbyfloat = tonumber, + setnx = _toboolean, + exists = _toboolean, + renamenx = _toboolean, + expire = _toboolean, + pexpire = _toboolean, + expireat = _toboolean, + pexpireat = _toboolean, + move = _toboolean, + persist = _toboolean, + smove = _toboolean, + sismember = _toboolean, + hset = _toboolean, + hsetnx = _toboolean, + hexists = _toboolean, + msetnx = _toboolean, + hgetall = _tomap, + config = _tomap, } local RedisDB = class() local prop = property(RedisDB) prop:reader("id", nil) --id +prop:reader("codec", nil) --codec prop:reader("passwd", nil) --passwd -prop:reader("executer", nil) --执行者 prop:reader("timer_id", nil) --timer_id prop:reader("connections", {}) --connections +prop:reader("clusters", {}) --clusters prop:reader("alives", {}) --alives +prop:reader("slots", {}) --slots prop:reader("req_counter", nil) prop:reader("res_counter", nil) prop:reader("subscrible", false) +prop:reader("cluster", false) --cluster function RedisDB:__init(conf, id) self.id = id self.passwd = conf.passwd + self:set_options(conf.opts) --attach_hour update_mgr:attach_hour(self) + --codec + local jcodec = json.jsoncodec() + self.codec = codec.rediscodec(jcodec) --setup self:setup(conf) end @@ -333,47 +134,52 @@ function RedisDB:setup(conf) self.timer_id = timer_mgr:register(0, SECOND_MS, -1, function() self:check_alive() end) - self:setup_command() self.req_counter = quanta.make_sampling(sformat("redis %s req", self.id)) self.res_counter = quanta.make_sampling(sformat("redis %s res", self.id)) end -function RedisDB:setup_command() - for cmd, param in pairs(redis_commands) do - RedisDB[cmd] = function(this, ...) - return this:commit(self.executer, param, ...) - end - end -end - function RedisDB:setup_pool(hosts) if not next(hosts) then log_err("[RedisDB][setup_pool] redis config err: hosts is empty") return end - local count = POOL_COUNT - while count > 0 do - for ip, port in pairs(hosts) do - local socket = Socket(self, ip, port) + local count = 1 + for _, host in pairs(hosts) do + for c = 1, POOL_COUNT do + local socket = Socket(self, host[1], host[2]) self.connections[count] = socket - socket.task_queue = QueueFIFO() + socket.cmd_queue = QueueFIFO() socket:set_id(count) - count = count - 1 + count = count + 1 end end end -function RedisDB:set_executer(id) +function RedisDB:choose_node(key) + if self.cluster and type(key) == "string" then + local index = crc16(key) % REDIS_SLOT + for _, cluster in pairs(self.clusters) do + if index >= cluster.min and index <= cluster.max then + local count = #cluster.alives + if count > 0 then + return cluster.alives[(index % count) + 1] + end + end + end + return + end local count = #self.alives if count > 0 then - local index = qhash(id or mrandom(), count) - self.executer = self.alives[index] - return true + local index = qhash(key or mrandom(), count) + return self.alives[index] end - return false end function RedisDB:set_options(opts) + if opts.cluster then + log_debug("[RedisDB][set_options] cluster status open") + self.cluster = true + end end function RedisDB:available() @@ -382,8 +188,34 @@ end function RedisDB:on_hour() for _, sock in pairs(self.alives) do - self.executer = sock - self:send({ cmd = "PING" }) + self:direct_send(sock, "PING") + end +end + +function RedisDB:check_clusters() + if self.cluster then + local _, socket = next(self.alives) + if not socket then + return + end + local ok, res = self:commit(socket, "cluster", "slots") + if not ok then + log_err("[RedisDB][check_clusters] load cluster slots failed! because: %s", res) + return + end + for i, info in pairs(res) do + for j = 3, #info do + self.slots[info[j][2]] = i + end + self.clusters[i] = { min = info[1], max = info[2], alives = {} } + end + for _, sock in pairs(self.alives) do + local index = self.slots[sock.port] or 0 + local cluster = self.clusters[index] + if cluster then + tinsert(cluster.alives, sock) + end + end end end @@ -398,8 +230,8 @@ function RedisDB:check_alive() end if channel:execute(true) then timer_mgr:set_period(self.timer_id, SECOND_10_MS) + self:check_clusters() end - self:set_executer() end) end end @@ -410,6 +242,7 @@ function RedisDB:login(socket) log_err("[RedisDB][login] connect db(%s:%s:%s) failed!", ip, port, id) return false end + socket:set_codec(self.codec) if self.passwd and #self.passwd > 0 then local ok, res = self:auth(socket) if not ok or res ~= "OK" then @@ -428,110 +261,95 @@ function RedisDB:login(socket) return true, SUCCESS end -function RedisDB:on_socket_alive() +function RedisDB:auth(sock) + return self:commit(sock, "AUTH", self.passwd) end -function RedisDB:auth(sock) - return self:commit(sock, { cmd = "AUTH" }, self.passwd) +function RedisDB:on_socket_alive() end function RedisDB:delive(sock) + if self.cluster then + local index = self.slots[sock.port] or 0 + local cluster = self.clusters[index] + if cluster then + tdelete(cluster.alives, sock) + end + end tdelete(self.alives, sock) self.connections[sock.id] = sock end function RedisDB:on_socket_error(sock, token, err) - --清空状态 - if sock == self.executer then - self.executer = nil - self:set_executer() - end --设置重连 self:delive(sock) timer_mgr:set_period(self.timer_id, SECOND_MS) event_mgr:fire_second(function() self:check_alive() end) - local task_queue = sock.task_queue - local session_id = task_queue:pop() + local cmd_queue = sock.cmd_queue + local session_id = cmd_queue:pop() while session_id do if session_id > 0 then thread_mgr:response(session_id, false, err) end - session_id = task_queue:pop() + session_id = cmd_queue:pop() end end -function RedisDB:on_socket_recv(sock, token) - while true do - local packet = sock:peek_lines(LineTitle) - if not packet then - break - end - local ok, succ, rdsucc, res = pcall(_parse_packet, packet) - if not ok then - log_err("[RedisDB][on_socket_recv] exec parse failed: %s", succ) - break - end - if not succ then - log_warn("[RedisDB][on_socket_recv] parse failed: %s", rdsucc) - break - end - sock:pop(_parse_offset(packet)) - if self.subscrible then - self:do_socket_recv(res) - end - local session_id = sock.task_queue:pop() - if session_id and session_id > 0 then - self.res_counter:count_increase() - thread_mgr:response(session_id, rdsucc, res) - end +function RedisDB:on_socket_recv(sock, succ, res) + if self.subscrible then + self:do_socket_recv(res) + end + local session_id = sock.cmd_queue:pop() + if session_id and session_id > 0 then + self.res_counter:count_increase() + thread_mgr:response(session_id, succ, res) end end -function RedisDB:do_socket_recv(res) -end - -function RedisDB:wait_response(socket, session_id, packet, param) - if not socket then - return false, "db not connected" - end - if not socket:send(packet) then +function RedisDB:wait_response(socket, session_id, cmd, ...) + if not socket:send_data(cmd, ...) then return false, "send request failed" end self.req_counter:count_increase() - socket.task_queue:push(session_id) - local ok, res = thread_mgr:yield(session_id, sformat("redis_comit:%s", param.cmd), DB_TIMEOUT) + socket.cmd_queue:push(session_id) + local ok, res = thread_mgr:yield(session_id, sformat("redis_comit:%s", cmd), DB_TIMEOUT) if not ok then - log_err("[RedisDB][wait_response] exec cmd %s failed: %s", param.cmd, res) + log_err("[RedisDB][wait_response] exec cmd %s failed: %s", cmd, res) return ok, res end - local convertor = param.convertor + local convertor = rconvertors[slower(cmd)] if convertor then return ok, convertor(res) end return ok, res end -function RedisDB:commit(sock, param, ...) +function RedisDB:commit(sock, cmd, ...) local session_id = thread_mgr:build_session_id() - local packet = _compose_args(param.cmd, ...) - return self:wait_response(sock, session_id, packet, param) + return self:wait_response(sock, session_id, cmd, ...) end -function RedisDB:send(param, ...) - local sock = self.executer - if sock and sock:send(_compose_args(param.cmd, ...)) then - sock.task_queue:push(0) +function RedisDB:send(cmd, key, ...) + local sock = self:choose_node(key) + if sock and sock:send_data(cmd, key, ...) then + sock.cmd_queue:push(0) end end -function RedisDB:execute(cmd, ...) - local lcmd = slower(cmd) - if RedisDB[lcmd] then - return self[lcmd](self, ...) +function RedisDB:direct_send(sock, cmd, ...) + if sock:send_data(cmd, ...) then + sock.cmd_queue:push(0) + end +end + +function RedisDB:execute(cmd, key, ...) + local sock = self:choose_node(key) + if not sock then + return false, "db not connected" end - return self:commit(self.executer,{ cmd = cmd }, ...) + return self:commit(sock, cmd, key, ...) end return RedisDB diff --git a/script/driver/redisps.lua b/script/driver/redisps.lua index 51bf0415..0dedf2aa 100644 --- a/script/driver/redisps.lua +++ b/script/driver/redisps.lua @@ -58,10 +58,10 @@ function PSRedis:setup_pool(hosts) log_err("[PSRedis][setup_pool] redis config err: hosts is empty") return end - for ip, port in pairs(hosts) do - local socket = Socket(self, ip, port) + for _, host in pairs(hosts) do + local socket = Socket(self, host[1], host[2]) self.connections[1] = socket - socket.task_queue = QueueFIFO() + socket.cmd_queue = QueueFIFO() socket:set_id(1) break end @@ -69,17 +69,14 @@ end function PSRedis:on_socket_alive() for channel in pairs(self.subscribes) do - self:subscribe(channel) + self:execute("subscribe", channel) end for channel in pairs(self.psubscribes) do - self:psubscribes(channel) + self:execute("psubscribes", channel) end event_mgr:notify_trigger("on_subscribe_alive") end -function PSRedis:on_hour() -end - function PSRedis:do_socket_recv(res) if type(res) == "table" then local ttype, channel, data, data2 = tunpack(res) diff --git a/script/driver/socket.lua b/script/driver/socket.lua index 8ef81991..3367082b 100644 --- a/script/driver/socket.lua +++ b/script/driver/socket.lua @@ -1,10 +1,7 @@ --socket.lua -local ssub = string.sub local log_err = logger.err local log_info = logger.info -local ends_with = qstring.ends_with -local split_pos = qstring.split_pos local qxpcall = quanta.xpcall local eproto_type = luabus.eproto_type @@ -25,7 +22,6 @@ prop:reader("alive", false) prop:reader("proto_type", eproto_type.text) prop:reader("session", nil) --连接成功对象 prop:reader("listener", nil) -prop:reader("recvbuf", "") prop:accessor("id", 0) function Socket:__init(host, ip, port) @@ -67,7 +63,13 @@ function Socket:listen(ip, port, ptype) return true end -function Socket:connect(ip, port, ptype, codec) +function Socket:set_codec(codec) + if self.session then + self.session.set_codec(codec) + end +end + +function Socket:connect(ip, port, ptype) if self.session then if self.alive then return true @@ -88,23 +90,14 @@ function Socket:connect(ip, port, ptype, codec) session.on_connect = function(res) local success = res == "ok" self.alive = success - if success then - if codec then - session.set_codec(codec) - end - else + if not success then self.token = nil self.session = nil end thread_mgr:response(block_id, success, res) end - session.on_call_text = function(recv_len, buff) - self:on_socket_recv(token, buff) - end - session.on_call_common = function(recv_len, session_id, ...) - thread_mgr:fork(function(...) - self.host:on_socket_recv(self, session_id, ...) - end, ...) + session.on_call_data = function(recv_len, ...) + self:on_socket_recv(token, ...) end session.on_error = function(stoken, err) self:on_socket_error(stoken, err) @@ -121,11 +114,10 @@ function Socket:on_socket_accept(session) socket:accept(session, session.ip, self.port) end -function Socket:on_socket_recv(token, buff) - thread_mgr:fork(function() - self.recvbuf = self.recvbuf .. buff - self.host:on_socket_recv(self, token) - end) +function Socket:on_socket_recv(token, ...) + thread_mgr:fork(function(...) + self.host:on_socket_recv(self, ...) + end, ...) end function Socket:on_socket_error(token, err) @@ -143,8 +135,8 @@ end function Socket:accept(session, ip, port) local token = session.token session.set_timeout(NETWORK_TIMEOUT) - session.on_call_text = function(recv_len, buff) - self:on_socket_recv(token, buff) + session.on_call_data = function(recv_len, ...) + self:on_socket_recv(token, ...) end session.on_error = function(stoken, err) self:on_socket_error(stoken, err) @@ -156,42 +148,9 @@ function Socket:accept(session, ip, port) self.host:on_socket_accept(self, token) end -function Socket:peek(len, offset) - offset = offset or 0 - if offset + len <= #self.recvbuf then - return ssub(self.recvbuf, offset + 1, offset + len) - end -end - -function Socket:peek_lines(split_char) - if #self.recvbuf >= #split_char then - if ends_with(self.recvbuf, split_char) then - return split_pos(self.recvbuf, split_char) - end - end -end - -function Socket:pop(len) - if len > 0 then - if #self.recvbuf > len then - self.recvbuf = ssub(self.recvbuf, len + 1) - else - self.recvbuf = "" - end - end -end - -function Socket:send(text) - if self.alive and text then - local send_len = self.session.call_text(text, #text) - return send_len > 0 - end - return false, "socket not alive" -end - -function Socket:send_data(session_id, ...) +function Socket:send_data(...) if self.alive then - local send_len = self.session.call_data(session_id, ...) + local send_len = self.session.call_data(...) return send_len > 0 end return false, "socket not alive" diff --git a/script/network/http_server.lua b/script/network/http_server.lua index cc30748e..536c7f93 100644 --- a/script/network/http_server.lua +++ b/script/network/http_server.lua @@ -3,32 +3,29 @@ local Socket = import("driver/socket.lua") local type = type local pcall = pcall -local pairs = pairs -local tostring = tostring local log_err = logger.err local log_warn = logger.warn local log_info = logger.info local log_debug = logger.debug local tunpack = table.unpack local signalquit = signal.quit -local json_encode = json.encode local saddr = qstring.addr -local thread_mgr = quanta.get("thread_mgr") - local HttpServer = class() local prop = property(HttpServer) -prop:reader("listener", nil) --网络连接对象 prop:reader("ip", nil) --http server地址 prop:reader("port", 8080) --http server端口 +prop:reader("codec", nil) --codec +prop:reader("listener", nil) --网络连接对象 prop:reader("clients", {}) --clients -prop:reader("requests", {}) --requests prop:reader("get_handlers", {}) --get_handlers prop:reader("put_handlers", {}) --put_handlers prop:reader("del_handlers", {}) --del_handlers prop:reader("post_handlers", {}) --post_handlers function HttpServer:__init(http_addr) + local jcodec = json.jsoncodec() + self.codec = codec.httpcodec(jcodec) self:setup(http_addr) end @@ -46,7 +43,6 @@ end function HttpServer:close(token, socket) self.clients[token] = nil - self.requests[token] = nil socket:close() end @@ -58,40 +54,28 @@ function HttpServer:on_socket_error(socket, token, err) end log_debug("[HttpServer][on_socket_error] client(token:%s) close!", token) self.clients[token] = nil - self.requests[token] = nil end function HttpServer:on_socket_accept(socket, token) --log_debug("[HttpServer][on_socket_accept] client(token:%s) connected!", token) self.clients[token] = socket + socket:set_codec(self.codec) end -function HttpServer:on_socket_recv(socket, token) - local request = self.requests[token] - if not request then - request = http.create_request() - self.requests[token] = request +function HttpServer:on_socket_recv(socket, method, url, params, body) + log_debug("[HttpServer][on_socket_recv] recv: %s, %s, %s, %s!", method, url, params, body) + if method == "GET" then + return self:on_http_request(self.get_handlers, socket, url, params) end - local buf = socket:get_recvbuf() - if not request.parse(buf) then - return + if method == "POST" then + return self:on_http_request(self.post_handlers, socket, url, body, params) + end + if method == "PUT" then + return self:on_http_request(self.put_handlers, socket, url, body, params) + end + if method == "DELETE" then + return self:on_http_request(self.del_handlers, socket, url, params) end - socket:pop(#buf) - thread_mgr:fork(function() - local method = request.method - if method == "GET" then - return self:on_http_request(self.get_handlers, socket, request.url, request.get_params(), request) - end - if method == "POST" then - return self:on_http_request(self.post_handlers, socket, request.url, request.body, request) - end - if method == "PUT" then - return self:on_http_request(self.put_handlers, socket, request.url, request.body, request) - end - if method == "DELETE" then - return self:on_http_request(self.del_handlers, socket, request.url, request.get_params(), request) - end - end) end --注册get回调 @@ -156,23 +140,14 @@ function HttpServer:response(socket, status, response, headers) if not token or not response then return end - local new_resp = http.create_response() - for key, value in pairs(headers or {}) do - new_resp.set_header(key, value) + if not headers then + headers = { ["Content-Type"] = "application/json" } end - if type(response) == "table" then - new_resp.set_header("Content-Type", "application/json") - new_resp.content = json_encode(response) - elseif type(response) == "string" then - new_resp.content = response + if type(response) == "string" then local html = response:find(" table: %s", res, self.coll_name) return code end if next(res) then for key, value in pairs(res) do + local field_data = value.val self.count = self.count - 1 - if value == "nil" then + if field_data == "nil" then self:unset_field(key) else - self:set_field(key, json_decode(value, 1)) + self:set_field(key, field_data) end end local conf = self.prototype @@ -98,7 +97,7 @@ function Document:destory() log_err("[Document][destory] del failed: %s=> table: %s", res, self.coll_name) return false, code end - local rcode, rres = redis_mgr:execute(MAIN_DBID, self.primary_id, "DEL", self.hotkey) + local rcode, rres = redis_mgr:execute(MAIN_DBID, "DEL", self.hotkey) if qfailed(rcode) then log_err("[Document][destory] del failed: %s=> hotkey: %s", rres, self.hotkey) return false, code @@ -116,11 +115,10 @@ end --保存数据库 function Document:update() - local pid = self.primary_id local channel = makechan("doc update") --清理缓存 channel:push(function() - local rcode, rres = redis_mgr:execute(MAIN_DBID, pid, "DEL", self.hotkey) + local rcode, rres = redis_mgr:execute(MAIN_DBID, "DEL", self.hotkey) if qfailed(rcode) then log_err("[Document][update] del failed: %s=> hotkey: %s", rres, self.hotkey) return false, rcode @@ -129,8 +127,8 @@ function Document:update() return true, SUCCESS end) channel:push(function() - local selector = { [self.primary_key] = pid } - local code, res = mongo_mgr:update(MAIN_DBID, pid, self.coll_name, self.datas, selector, true) + local selector = { [self.primary_key] = self.primary_id } + local code, res = mongo_mgr:update(MAIN_DBID, self.primary_id, self.coll_name, self.datas, selector, true) if qfailed(code) then log_err("[Document][update] update failed: %s=> table: %s", res, self.coll_name) return false, code @@ -165,9 +163,6 @@ function Document:update_redis(fields) cursor = cursor[name] end event_mgr:fire_frame(function() - if cursor then - cursor = json_encode(cursor) - end self:cmomit_redis(key, cursor or "nil") end) end @@ -243,7 +238,7 @@ end --记录缓存 function Document:cmomit_redis(field, value) - local code, res = redis_mgr:execute(MAIN_DBID, self.primary_id, "HSET", self.hotkey, field, value) + local code, res = redis_mgr:execute(MAIN_DBID, "HSET", self.hotkey, field, { val = value }) if qfailed(code) then log_err("[Document][cmomit_redis] failed: %s=> hotkey: %s", res, self.hotkey) self:flush() diff --git a/server/center/gm_mgr.lua b/server/center/gm_mgr.lua index 7db11df7..66f0e179 100644 --- a/server/center/gm_mgr.lua +++ b/server/center/gm_mgr.lua @@ -10,7 +10,6 @@ local sformat = string.format local tunpack = table.unpack local tinsert = table.insert local make_sid = service.make_sid -local jdecode = json.decode local guid_index = codec.guid_index local online = quanta.get("online") @@ -160,18 +159,18 @@ end --http 回调 ---------------------------------------------------------------------- --gm_page -function GM_Mgr:on_gm_page(url, body, request) +function GM_Mgr:on_gm_page(url, params) return self.gm_page, {["Access-Control-Allow-Origin"] = "*"} end --gm列表 -function GM_Mgr:on_gmlist(url, body, request) +function GM_Mgr:on_gmlist(url, params) return { text = "GM指令", nodes = cmdline:get_displays() } end --monitor拉取 -function GM_Mgr:on_monitors(url, body, request) - log_debug("[GM_Mgr][on_monitors] body: %s", body) +function GM_Mgr:on_monitors(url, params) + log_debug("[GM_Mgr][on_monitors] body: %s", params) local nodes = {} for _, addr in pairs(self.monitors) do tinsert(nodes, { text = addr, tag = "log" }) @@ -180,17 +179,15 @@ function GM_Mgr:on_monitors(url, body, request) end --后台GM调用,字符串格式 -function GM_Mgr:on_command(url, body, request) +function GM_Mgr:on_command(url, body) log_debug("[GM_Mgr][on_command] body: %s", body) - local cmd_req = jdecode(body) - return self:exec_command(cmd_req.data) + return self:exec_command(body.data) end --后台GM调用,table格式 -function GM_Mgr:on_message(url, body, request) +function GM_Mgr:on_message(url, body) log_debug("[GM_Mgr][on_message] body: %s", body) - local cmd_req = jdecode(body) - return self:exec_message(cmd_req.data) + return self:exec_message(body.data) end ------------------------------------------------------------------------- diff --git a/server/center/gm_page.lua b/server/center/gm_page.lua index 43910266..8677739c 100644 --- a/server/center/gm_page.lua +++ b/server/center/gm_page.lua @@ -237,7 +237,7 @@ return [[ url: url, type: "POST", dataType: "json", - contentType: "utf-8", + contentType: "application/json", data: result.data, success: function (res) { var result = res.msg diff --git a/server/monitor/monitor_mgr.lua b/server/monitor/monitor_mgr.lua index b3f2e251..38079cf5 100644 --- a/server/monitor/monitor_mgr.lua +++ b/server/monitor/monitor_mgr.lua @@ -8,7 +8,6 @@ local env_addr = environ.addr local log_warn = logger.warn local log_info = logger.info local log_debug = logger.debug -local jdecode = json.decode local signal_quit = signal.quit local timer_mgr = quanta.get("timer_mgr") @@ -114,15 +113,14 @@ function MonitorMgr:broadcast(service_id, rpc, ...) end -- command处理 -function MonitorMgr:on_monitor_command(url, body, request) +function MonitorMgr:on_monitor_command(url, body) log_debug("[MonitorMgr][on_monitor_command]: %s", body) --执行函数 local function handler_cmd(jbody) - local data_req = jdecode(jbody) - if data_req.token then - return self:call(data_req.token, data_req.rpc, data_req.data) + if body.token then + return self:call(body.token, body.rpc, body.data) end - return self:broadcast(data_req.service_id, data_req.rpc, data_req.data) + return self:broadcast(body.service_id, body.rpc, body.data) end --开始执行 local ok, res = pcall(handler_cmd, body) diff --git a/server/monitor/redis_discovery.lua b/server/monitor/redis_discovery.lua index d974e44e..e84de456 100644 --- a/server/monitor/redis_discovery.lua +++ b/server/monitor/redis_discovery.lua @@ -84,7 +84,7 @@ end function RedisDiscovery:on_subscribe_alive() log_debug("[RedisDiscovery][on_subscribe_alive]") - self.subscriber:psubscribe(CHANNEL_PT) + self.subscriber:execute("psubscribe", CHANNEL_PT) self:load_services() end @@ -156,8 +156,8 @@ function RedisDiscovery:query_instances() local now = quanta.now repeat local ok, next_cur, datas = self.redis:execute("HSCAN", SERVICE_KEY, cur, "count", 200) - if not ok or not cur or not datas then - log_err("[RedisDiscovery][query_instances] query failed: cur:%s, datas:%s", cur, datas) + if not ok or not next_cur or not datas then + log_err("[RedisDiscovery][query_instances] query failed: cur:%s, datas:%s", next_cur, datas) return end for node_data, score in pairs(datas) do @@ -173,13 +173,13 @@ end -- 注册实例 function RedisDiscovery:regi_instance(node_data) self.redis:execute("HSET", SERVICE_KEY, node_data, quanta.now) - self.subscriber:publish(CHANNEL_UP, node_data) + self.redis:execute("publish", CHANNEL_UP, node_data) end -- 删除实例 function RedisDiscovery:del_instance(node_data) self.redis:execute("HDEL", SERVICE_KEY, node_data) - self.subscriber:publish(CHANNEL_DN, node_data) + self.redis:execute("publish", CHANNEL_DN, node_data) end return RedisDiscovery diff --git a/server/robot/accord_mgr.lua b/server/robot/accord_mgr.lua index 87704b60..ebfa5209 100644 --- a/server/robot/accord_mgr.lua +++ b/server/robot/accord_mgr.lua @@ -1,5 +1,5 @@ -- accord_mgr.lua -local log_warn = logger.warn +local log_warn = logger.warn local log_debug = logger.debug local jdecode = json.decode @@ -149,7 +149,7 @@ end -- http 回调 ---------------------------------------------------------------------- -- accord_html -function AccordMgr:on_accord_page(url, body, request) +function AccordMgr:on_accord_page(url, body) if self.load_db_status then return self.accord_html, { ["Access-Control-Allow-Origin"] = "*" @@ -169,45 +169,41 @@ function AccordMgr:on_accord_page(url, body, request) end -- accord_css -function AccordMgr:on_accord_css(url, body, request) +function AccordMgr:on_accord_css(url, body) return self.accord_css, { ["Access-Control-Allow-Origin"] = "*" } end -- 拉取日志 -function AccordMgr:on_message(url, body, request) - local open_id = request.get_param("open_id") +function AccordMgr:on_message(url, params) --log_debug("[AccordMgr][on_message] open_id: %s", open_id) - return robot_mgr:get_accord_message(open_id) + return robot_mgr:get_accord_message(params.open_id) end -- monitor拉取 -function AccordMgr:on_create(url, body, request) - local jbody = jdecode(body) +function AccordMgr:on_create(url, body) log_debug("[AccordMgr][on_create] body: %s", body) - return robot_mgr:create_robot(jbody.ip, jbody.port, jbody.open_id, jbody.passwd) + return robot_mgr:create_robot(body.ip, body.port, body.open_id, body.passwd) end -- 后台GM调用,字符串格式 -function AccordMgr:on_destory(url, body, request) - local jbody = jdecode(body) +function AccordMgr:on_destory(url, body) log_debug("[AccordMgr][on_destory] body: %s", body) - return robot_mgr:destory_robot(jbody.open_id) + return robot_mgr:destory_robot(body.open_id) end -- 服务器列表 -function AccordMgr:on_server_list(url, body, request) +function AccordMgr:on_server_list(url, body) log_debug("[AccordMgr][on_server_list] body: %s", body) return { code = 0, server_list = self.server_list} end -- 编辑服务器 -function AccordMgr:on_server_edit(url, body, request) +function AccordMgr:on_server_edit(url, body) log_debug("[AccordMgr][on_server_edit] body: %s", body) - local jbody = jdecode(body) - if jbody and jbody.data then - local data = jbody.data + if body.data then + local data = body.data if self.server_list[data.name] then accord_dao:save_server(data) else @@ -220,11 +216,10 @@ function AccordMgr:on_server_edit(url, body, request) end -- 删除服务器 -function AccordMgr:on_server_del(url, body, request) +function AccordMgr:on_server_del(url, body) log_debug("[AccordMgr][on_server_del] body: %s", body) - local jbody = jdecode(body) - if jbody and jbody.data then - local data = jbody.data + if body.data then + local data = body.data self.server_list[data.name] = nil accord_dao:del_server(data.name) return { code = 0} @@ -233,17 +228,16 @@ function AccordMgr:on_server_del(url, body, request) end -- 协议列表 -function AccordMgr:on_accord_list(url, body, request) +function AccordMgr:on_accord_list(url, body) log_debug("[AccordMgr][on_accord_list] body: %s", body) return { code = 0, accord_list = self.accord_list} end -- 编辑协议 -function AccordMgr:on_accord_edit(url, body, request) +function AccordMgr:on_accord_edit(url, body) log_debug("[AccordMgr][on_accord_edit] body: %s", body) - local jbody = jdecode(body) - if jbody and jbody.data then - local data = jbody.data + if body.data then + local data = body.data local accords = data.accords local low_name = data.low_name local clone = data.clone @@ -264,11 +258,10 @@ function AccordMgr:on_accord_edit(url, body, request) end -- 删除协议 -function AccordMgr:on_accord_del(url, body, request) +function AccordMgr:on_accord_del(url, body) log_debug("[AccordMgr][on_accord_del] body: %s", body) - local jbody = jdecode(body) - if jbody and jbody.data then - local data = jbody.data + if body.data then + local data = body.data self.accord_list[data.name] = nil accord_dao:del_accord_conf(data.name) return { code = 0} @@ -277,11 +270,10 @@ function AccordMgr:on_accord_del(url, body, request) end -- 客户端上传用例 -function AccordMgr:on_upload(url, body, request) - local jbody = jdecode(body) +function AccordMgr:on_upload(url, body) log_debug("[AccordMgr][on_upload] body: %s", body) - if jbody and jbody.data then - local data = jdecode(jbody.data); + if body.data then + local data = jdecode(body.data); local add = false if not self.accord_list[data.name] then add = true @@ -298,11 +290,10 @@ function AccordMgr:on_upload(url, body, request) end -- 编辑协议选项 -function AccordMgr:on_proto_edit(url, body, request) +function AccordMgr:on_proto_edit(url, body) log_debug("[AccordMgr][on_proto_edit] body: %s", body) - local jbody = jdecode(body) - if jbody and jbody.data then - local data = jbody.data + if body.data then + local data = body.data local accord = self.accord_list[data.name] if not accord then return { code = -1, msg = "不存在的协议配置,请重新创建!"} @@ -315,11 +306,10 @@ function AccordMgr:on_proto_edit(url, body, request) end -- 删除协议选项 -function AccordMgr:on_proto_del(url, body, request) +function AccordMgr:on_proto_del(url, body) log_debug("[AccordMgr][on_proto_del] body: %s", body) - local jbody = jdecode(body) - if jbody and jbody.data then - local data = jbody.data + if body.data then + local data = body.data local accord = self.accord_list[data.accord_name] if not accord then return { code = -1, msg = "不存在的协议配置!"} @@ -331,19 +321,17 @@ function AccordMgr:on_proto_del(url, body, request) end -- 后台GM调用,table格式 -function AccordMgr:on_run(url, body, request) - local jbody = jdecode(body) - if jbody.cmd_id ~= 1001 then +function AccordMgr:on_run(url, body) + if body.cmd_id ~= 1001 then log_debug("[AccordMgr][on_run] body: %s", body) end - return robot_mgr:run_accord_message(jbody.open_id, jbody.cmd_id, jbody.data) + return robot_mgr:run_accord_message(body.open_id, body.cmd_id, body.data) end -- 后台GM调用,table格式 -function AccordMgr:on_runall(url, body, request) - local jbody = jdecode(body) +function AccordMgr:on_runall(url, body) log_debug("[AccordMgr][on_runall] body: %s", body) - return robot_mgr:run_accord_messages(jbody.open_id, jbody.data) + return robot_mgr:run_accord_messages(body.open_id, body.data) end quanta.accord_mgr = AccordMgr() diff --git a/server/router/transfer_mgr.lua b/server/router/transfer_mgr.lua index e29b390e..f1ed3cad 100644 --- a/server/router/transfer_mgr.lua +++ b/server/router/transfer_mgr.lua @@ -49,7 +49,6 @@ function TransferMgr:rpc_login_service(client, player_id, serv_name, serv_id) local routers = self:update_service(player_id, serv_name, serv_id) if routers then log_info("[TransferMgr][rpc_login_service]: %s, service: %s-%s", player_id, serv_name, serv_id) - log_warn("[TransferMgr][rpc_login_service]: %s, routers: %s", player_id, routers) return SUCCESS end log_warn("[TransferMgr][rpc_login_service]: %s, service: %s-%s failed!", player_id, serv_name, serv_id) diff --git a/server/test.lua b/server/test.lua index 92afb8e8..82093c58 100644 --- a/server/test.lua +++ b/server/test.lua @@ -33,5 +33,5 @@ quanta.startup(function()--初始化test import("test/detour_test.lua") import("test/bitarray_test.lua") ]] - import("test/stdfs_test.lua") + import("test/redis_cluster.lua") end) diff --git a/server/test/http_test.lua b/server/test/http_test.lua index 45b9947c..9dbad731 100644 --- a/server/test/http_test.lua +++ b/server/test/http_test.lua @@ -9,18 +9,28 @@ local http_client = quanta.get("http_client") local data = {aaa = 123} if quanta.index == 1 then - local on_post = function(path, body, request) - log_debug("on_post: %s, %s, %s", path, body, request.get_headers()) + local on_post = function(path, body, params) + log_debug("on_post: %s, %s, %s", path, body, params) return data end - local on_get = function(path, query, request) - log_debug("on_get: %s, %s, %s", path, query, request.get_headers()) + local on_get = function(path, params) + log_debug("on_get: %s, %s", path, params) + return data + end + local on_put = function(path, body, params) + log_debug("on_put: %s, %s, %s", path, body, params) + return data + end + local on_del = function(path, params) + log_debug("on_del: %s, %s", path, params) return data end local HttpServer = import("network/http_server.lua") local server = HttpServer("0.0.0.0:8888") server:register_get("*", on_get) server:register_post("*", on_post) + server:register_put("*", on_put) + server:register_del("*", on_del) quanta.server = server elseif quanta.index == 2 then for i = 1, 1 do diff --git a/server/test/redis_cluster.lua b/server/test/redis_cluster.lua new file mode 100644 index 00000000..5b0eb750 --- /dev/null +++ b/server/test/redis_cluster.lua @@ -0,0 +1,22 @@ +-- redis_cluster.lua +local log_debug = logger.debug + +local timer_mgr = quanta.get("timer_mgr") + +local RedisMgr = import("store/redis_mgr.lua") +local redis_mgr = RedisMgr() + +local MAIN_DBID = environ.number("QUANTA_DB_MAIN_ID") + +timer_mgr:once(2000, function() + local code, res = redis_mgr:execute(MAIN_DBID, "cluster", "nodes") + log_debug("db get code: %s, res(%s) = %s", code, type(res), res) + code, res = redis_mgr:execute(MAIN_DBID, "cluster", "slots") + log_debug("db get code: %s, res(%s) = %s", code, type(res), res) + code, res = redis_mgr:execute(MAIN_DBID, "set", "aaa", {abc = 3, ddd = 4}) + log_debug("db set code: %s, res = %s", code, res) + code, res = redis_mgr:execute(MAIN_DBID, "get", "aaa") + log_debug("db get code: %s, res = %s", code, res) + local code1, res1, res2 = redis_mgr:execute(MAIN_DBID, "scan", "0") + log_debug("db scan code: %s, res = %s, res2=%s", code1, res1, res2) +end) \ No newline at end of file diff --git a/server/test/redis_test.lua b/server/test/redis_test.lua index f81e0b24..8c55de03 100644 --- a/server/test/redis_test.lua +++ b/server/test/redis_test.lua @@ -8,63 +8,73 @@ local redis_mgr = RedisMgr() local MAIN_DBID = environ.number("QUANTA_DB_MAIN_ID") -timer_mgr:once(3000, function() - local code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "get", "aaa") +timer_mgr:once(2000, function() + local code, res = redis_mgr:execute(MAIN_DBID, "get", "aaa") log_debug("db get code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "set", "aaa", 123) + code, res = redis_mgr:execute(MAIN_DBID, "set", "aaa", {abc = 3, ddd = 4}) log_debug("db set code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "get", "aaa") + code, res = redis_mgr:execute(MAIN_DBID, "get", "aaa") log_debug("db get code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "del", "aaa") - log_debug("db del code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "get", "aaa") + + code, res = redis_mgr:execute(MAIN_DBID, "get", "aaa") log_debug("db get code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "hget", "bb", "k1") + code, res = redis_mgr:execute(MAIN_DBID, "hget", "bb", "k1") log_debug("db hget code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "hset", "bb", "k1", 2) + code, res = redis_mgr:execute(MAIN_DBID, "hset", "bb", "k1", 123) log_debug("db hset code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "hget", "bb", "k1") + code, res = redis_mgr:execute(MAIN_DBID, "hmset", "bb", "k2", 234, "k3", 345) + log_debug("db hmset code: %s, res = %s", code, res) + code, res = redis_mgr:execute(MAIN_DBID, "hget", "bb", "k1") log_debug("db hget code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "hdel", "bb", "k1") + code, res = redis_mgr:execute(MAIN_DBID, "hdel", "bb", "k1") log_debug("db hdel code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "hget", "bb", "k1") + code, res = redis_mgr:execute(MAIN_DBID, "hmget", "bb", "k1", "k2", "k3") log_debug("db hget code: %s, res = %s", code, res) - --code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "subscribe", "test") - --log_debug("db subscribe code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "publish", "test", 123) - log_debug("db publish code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "sadd", "set1","one") + local scode, cur, vals = redis_mgr:execute(MAIN_DBID, "hscan", "bb", 0) + log_debug("db hscan code: %s, cur = %s, vals=%s", scode, cur, vals) + code, res = redis_mgr:execute(MAIN_DBID, "sadd", "sset", "k1", "k2", "k3") log_debug("db sadd code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "scard", "set1") + code, res = redis_mgr:execute(MAIN_DBID, "scard", "sset") log_debug("db scard code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "sismember", "set1","one") + code, res = redis_mgr:execute(MAIN_DBID, "sismember", "sset", "one") log_debug("db sismember code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "sismember", "set1","two") + code, res = redis_mgr:execute(MAIN_DBID, "sismember", "sset", "k2") log_debug("db sismember code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "smembers", "set1") + code, res = redis_mgr:execute(MAIN_DBID, "smembers", "sset") log_debug("db smembers code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "spop", "set1") + scode, cur, vals = redis_mgr:execute(MAIN_DBID, "sscan", "sset", 0) + log_debug("db sscan code: %s, cur = %s, vals=%s", scode, cur, vals) + code, res = redis_mgr:execute(MAIN_DBID, "spop", "sset") log_debug("db spop code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "zadd", "zset1",1,"one") + code, res = redis_mgr:execute(MAIN_DBID, "zadd", "zset1",1,"one") log_debug("db zadd code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "zadd", "zset1",1,"uno") + code, res = redis_mgr:execute(MAIN_DBID, "zadd", "zset1",1,"uno") log_debug("db zadd code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "zadd", "zset1",2,"two") + code, res = redis_mgr:execute(MAIN_DBID, "zadd", "zset1",2,"two") log_debug("db zadd code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "zadd", "zset1",3,"three") + code, res = redis_mgr:execute(MAIN_DBID, "zadd", "zset1",3,"three") log_debug("db zadd code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "zcard", "zset1") + code, res = redis_mgr:execute(MAIN_DBID, "zcard", "zset1") log_debug("db zcard code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "zcount", "zset1",1,2) + code, res = redis_mgr:execute(MAIN_DBID, "zcount", "zset1",1,2) log_debug("db zcount code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "zrange", "zset1",0,2) + code, res = redis_mgr:execute(MAIN_DBID, "zrange", "zset1",0,2) log_debug("db zrange code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "zrank", "zset1","two") + code, res = redis_mgr:execute(MAIN_DBID, "zrank", "zset1","two") log_debug("db zrank code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "zscore", "zset1","two") + code, res = redis_mgr:execute(MAIN_DBID, "zscore", "zset1","two") log_debug("db zscore code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "zrange", "zset1", 0, 2, "WITHSCORES") + code, res = redis_mgr:execute(MAIN_DBID, "zrange", "zset1", 0, 2, "WITHSCORES") log_debug("db zrange withscores code: %s, res = %s", code, res) - code, res = redis_mgr:execute(MAIN_DBID, MAIN_DBID, "zrangebyscore", "zset1", 0, 2) + code, res = redis_mgr:execute(MAIN_DBID, "zrangebyscore", "zset1", 0, 2) log_debug("db zrange zrangebyscore code: %s, res = %s", code, res) + scode, cur, vals = redis_mgr:execute(MAIN_DBID, "zscan", "zset1", 0) + log_debug("db zscan code: %s, cur = %s, vals=%s", scode, cur, vals) + --[[ + --code, res = redis_mgr:execute(MAIN_DBID, "subscribe", "test") + --log_debug("db subscribe code: %s, res = %s", code, res) + code, res = redis_mgr:execute(MAIN_DBID, "publish", "test", 123) + log_debug("db publish code: %s, res = %s", code, res) + log_debug("db spop code: %s, res = %s", code, res) + ]] end) \ No newline at end of file diff --git a/server/test/url_test.lua b/server/test/url_test.lua deleted file mode 100644 index adf80bba..00000000 --- a/server/test/url_test.lua +++ /dev/null @@ -1,34 +0,0 @@ --- url_test.lua - -local log_debug = logger.debug -local json_encode = json.encode - -local text = "GET /uri.cgi?aaa=ds HTTP/1.1\r\nUser-Agent: Mozilla/5.0\r\nHost: 127.0.0.1\r\n\r\n{'AAA':123}" - -local function dump_req(name, req) - log_debug("%s: method:%s, url:%s, ver:%s, body:%s", name, req.method, req.url, req.version, req.body) - log_debug("%s: headers:%s", name, req.get_headers()) - log_debug("%s: params:%s", name, req.get_params()) -end - -local function dump_req_header(name, req, key) - log_debug("%s: key:%s, value:%s", name, key, req.get_header(key)) -end - -local function dump_req_param(name, req, key) - log_debug("%s: key:%s, param:%s", name, key, req.get_param(key)) -end - -local req1 = http.create_request() -if req1.parse(text) then - dump_req("req1", req1) - dump_req_header("req1", req1, "User-Agent") - dump_req_header("req1", req1, "Accept") - dump_req_header("req1", req1, "Host") - dump_req_param("req1", req1, "aaa") -end - -local resp1 = http.create_response() -resp1.set_header("Content-Type", "text/plain") -resp1.content = json_encode({a=2,b=3,c={d=4}}) -log_debug("resp : %s", resp1.serialize())