From f65746b4c35967a8a274076cc226c63e50ac0540 Mon Sep 17 00:00:00 2001 From: xiyoo0812 Date: Wed, 22 May 2024 10:33:33 +0000 Subject: [PATCH] =?UTF-8?q?=E8=BF=91=E6=9C=9F=E4=BC=98=E5=8C=96=E5=90=88?= =?UTF-8?q?=E9=9B=86=201=E3=80=81bson=E5=8A=9F=E8=83=BD=E4=BC=98=E5=8C=96?= =?UTF-8?q?=202=E3=80=81bitarray=E6=94=B9=E6=88=90bitset=203=E3=80=81GM?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E9=87=8D=E6=9E=84=204=E3=80=81sqlite?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E4=BC=98=E5=8C=96=205=E3=80=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=AB=AF=E5=8F=A3=E5=92=8C=E6=96=87=E4=BB=B6=E5=8D=A0?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/lbson/src/bson.h | 10 +- extend/lbson/src/lbson.cpp | 2 +- extend/lcodec/lcodec.vcxproj | 2 +- extend/lcodec/lcodec.vcxproj.filters | 2 +- extend/lcodec/src/bitarray.h | 262 --------------------------- extend/lcodec/src/bitset.h | 51 ++++++ extend/lcodec/src/lcodec.cpp | 69 ++++--- extend/lcodec/src/lcodec.h | 2 +- extend/lsqlite/src/lsqlite.h | 2 +- script/constant.lua | 6 +- script/driver/lmdb.lua | 11 ++ script/driver/sqlite.lua | 17 ++ script/driver/unqlite.lua | 11 ++ script/network/http_server.lua | 11 ++ script/network/net_server.lua | 11 ++ script/network/rpc_server.lua | 12 ++ script/queue/mongo_mq.lua | 4 +- server/center/center_gm.lua | 6 +- server/center/gm_mgr.lua | 55 +----- server/test.lua | 4 +- server/test/bitarray_test.lua | 30 --- server/test/bitset_test.lua | 19 ++ server/test/mongo_test.lua | 2 +- 23 files changed, 203 insertions(+), 398 deletions(-) delete mode 100644 extend/lcodec/src/bitarray.h create mode 100644 extend/lcodec/src/bitset.h delete mode 100644 server/test/bitarray_test.lua create mode 100644 server/test/bitset_test.lua diff --git a/extend/lbson/src/bson.h b/extend/lbson/src/bson.h index 7a8b10b9..4e0ca7bf 100644 --- a/extend/lbson/src/bson.h +++ b/extend/lbson/src/bson.h @@ -183,6 +183,12 @@ namespace lbson { return sprintf(str, "%zd", i); } + void pack_date(lua_State* L) { + lua_getfield(L, -1, "date"); + m_buffer.write(lua_tointeger(L, -1) * 1000); + lua_pop(L, 1); + } + void pack_int64(lua_State* L) { lua_getfield(L, -1, "value"); m_buffer.write(lua_tointeger(L, -1)); @@ -355,6 +361,8 @@ namespace lbson { pack_binary(L); break; case bson_type::BSON_DATE: + pack_date(L); + break; case bson_type::BSON_INT64: case bson_type::BSON_TIMESTAMP: pack_int64(L); @@ -520,7 +528,7 @@ namespace lbson { lua_pushinteger(L, read_val(L, slice)); break; case bson_type::BSON_DATE: - lua_pushinteger(L, read_val(L, slice)); + lua_pushinteger(L, read_val(L, slice) / 1000); break; case bson_type::BSON_INT64: case bson_type::BSON_TIMESTAMP: diff --git a/extend/lbson/src/lbson.cpp b/extend/lbson/src/lbson.cpp index e889ca51..89af3594 100644 --- a/extend/lbson/src/lbson.cpp +++ b/extend/lbson/src/lbson.cpp @@ -28,7 +28,7 @@ namespace lbson { return thread_bson.int64(L, value); } static int date(lua_State* L, int64_t value) { - return thread_bson.date(L, value); + return thread_bson.date(L, value * 1000); } static void init_static_bson() { diff --git a/extend/lcodec/lcodec.vcxproj b/extend/lcodec/lcodec.vcxproj index 057950c3..ce3bcfab 100644 --- a/extend/lcodec/lcodec.vcxproj +++ b/extend/lcodec/lcodec.vcxproj @@ -7,7 +7,7 @@ - + diff --git a/extend/lcodec/lcodec.vcxproj.filters b/extend/lcodec/lcodec.vcxproj.filters index bd7a67ab..b7e46004 100644 --- a/extend/lcodec/lcodec.vcxproj.filters +++ b/extend/lcodec/lcodec.vcxproj.filters @@ -1,7 +1,7 @@  - + src diff --git a/extend/lcodec/src/bitarray.h b/extend/lcodec/src/bitarray.h deleted file mode 100644 index 8e076570..00000000 --- a/extend/lcodec/src/bitarray.h +++ /dev/null @@ -1,262 +0,0 @@ -#pragma once - -#include -#include - -namespace lcodec { - typedef unsigned int BWORD; - - /* number of bits in a word */ - #define BITS_PER_BWORD (CHAR_BIT * sizeof(BWORD)) - /* gets the word that contains the bit corresponding to a given index i */ - #define I_BWORD(i) ((BWORD)(i) / BITS_PER_BWORD) - /* computes a mask to access the correct bit inside this word */ - #define I_BIT(i) ((BWORD)1 << ((BWORD)(i) % BITS_PER_BWORD)) - /* computes how many words to store n bits */ - #define BWORDS_FOR_BITS(n) (I_BWORD((n) - 1) + 1) - - class bitarray - { - public: - ~bitarray() { - if (values_) { - free(values_); - values_ = nullptr; - } - size_ = 0; - } - - size_t general(size_t nbits) { - values_ = (BWORD*)calloc(BWORDS_FOR_BITS(nbits), sizeof(BWORD)); - if (values_ != nullptr) - return size_ = nbits; - return 0; - } - - /* set ith bit to 1 if b is truthy, else 0 */ - void set_bit(size_t i, size_t b) { - raw_set_bit(check_index(i), b); - } - - /* get ith bit (1 or 0) */ - size_t get_bit(size_t i) { - return raw_get_bit(check_index(i)); - } - - /* 1 -> 0 and 0 -> 1 */ - void flip_bit(size_t i) { - size_t idx = check_index(i); - if (idx < size_) { - BWORD mask; - BWORD* word = get_bit_access(idx, &mask); - *word = (*word & mask) ? (*word & ~mask) : (*word | mask); - } - } - - void flip() { - size_t nwords = BWORDS_FOR_BITS(size_); - for (size_t i = 0; i < nwords; ++i) { - values_[i] = ~values_[i]; - } - for (size_t i = size_; i < nwords * BITS_PER_BWORD; ++i) { - raw_set_bit(i, 0); - } - } - - void fill(bool b) { - BWORD bb = b ? (BWORD)-1 : 0; - size_t nwords = BWORDS_FOR_BITS(size_); - for (size_t i = 0; i < nwords; ++i) { - values_[i] = bb; - } - for (size_t i = size_; i < nwords * BITS_PER_BWORD; ++i) { - raw_set_bit(i, 0); - } - } - - /* resize the array. if new size is bigger, fill the new bit positions with 0. - also set any unused bits to 0 (ie the gap between size and the actual end - of WORDs). returns the new size, or 0 is returned if failed (array unchanged)*/ - size_t resize(size_t nbits) { - if (nbits == size_) - return nbits; - size_t oldwords = BWORDS_FOR_BITS(size_); - size_t newwords = BWORDS_FOR_BITS(nbits); - if (oldwords != newwords) { - BWORD* tmp = (BWORD* )realloc(values_, newwords * sizeof(BWORD)); - if (tmp == nullptr) - return 0; - values_ = tmp; - } - size_ = nbits; - size_t oldbits = size_; - if (nbits < oldbits) { - for (size_t i = nbits; i < newwords * BITS_PER_BWORD; ++i) - raw_set_bit(i, 0); - } else { - /* gap between oldbits and oldwords*BITS_PER_BWORD is guaranteed to be 0 */ - for (size_t i = oldwords; i < newwords; ++i) - values_[i] = 0; - } - return nbits; - } - - void reverse() { - for (size_t i = 0, j = size_ - 1; i < j; ++i, --j) { - int tmp = raw_get_bit(i); - raw_set_bit(i, raw_get_bit(j)); - raw_set_bit(j, tmp); - } - } - - /* copy values from ba to tg */ - void concat(bitarray* tg) { - resize(size_ + tg->size_); - for (size_t i = 0; i < BWORDS_FOR_BITS(size_); ++i) - values_[size_] = tg->values_[i]; - } - - /* copy values from ba to tg */ - bitarray* clone() { - bitarray* ba = new bitarray(); - if (!ba->general(size_)) { - delete ba; - return nullptr; - } - for (size_t i = 0; i < BWORDS_FOR_BITS(size_); ++i) { - ba->values_[i] = values_[i]; - } - return ba; - } - - bitarray* slice(size_t from, size_t to) { - size_t ifrom = check_index(from); - size_t ito = check_index(to, true); - size_t len = ito - ifrom + 1; - bitarray* ba = new bitarray(); - if (!ba->general(len)) { - delete ba; - return nullptr; - } - for(size_t i = 0; i < len; ++i) { - ba->raw_set_bit(i, raw_get_bit(ifrom + i)); - } - return ba; - } - - void from_string(std::string str, size_t i) { - size_t idx = check_index(i); - size_t slen = str.size(); - resize(i + slen); - for (size_t j = 0; j < slen; ++j) - raw_set_bit(idx + j, str[j] != '0'); - } - - template - void from_number(T src, size_t i) { - size_t idx = check_index(i); - size_t tgt = sizeof(T) * CHAR_BIT; - resize(i + tgt); - for (size_t j = idx, k = 0; k < tgt; ++j, ++k) { - T maskt = (T)1 << (T)(tgt - k - 1); - size_t b = !!(src & maskt); - raw_set_bit(j, b); - } - } - - template - T to_number(size_t i) { - T res = 0; - size_t idx = check_index(i); - size_t tgt = sizeof(T) * CHAR_BIT; - for (size_t j = idx, k = 0; k < tgt; ++j, ++k) { - BWORD mask; - BWORD* word = get_bit_access(j, &mask); - T maskt = (T)1 << (T)(tgt - k - 1); - res = (*word & mask) ? (res | maskt) : (res & ~maskt); - } - return res; - } - - std::string to_string(size_t i) { - std::string str = "bitarray<"; - str.append(std::to_string(size_)); - str.append(">["); - for (size_t i = 0; i < size_; ++i) { - str.append(raw_get_bit(i) ? "1," : "0,"); - } - return str; - } - - bool equal(bitarray *r) { - if (size_ != r->size_) - return false; - for (size_t i = 0; i < BWORDS_FOR_BITS(size_); ++i) - if (values_[i] != r->values_[i]) - return false; - return true; - } - - void lshift(size_t s) { - size_t sz = size_; - for (size_t i = 0; i + s < sz; ++i) - raw_set_bit(i, raw_get_bit(i + s)); - for (size_t i = sz > s ? sz - s : 0; i < sz; ++i) - raw_set_bit(i, 0); - } - - void rshift(size_t s) { - size_t sz = size_; - for (size_t i = 0; i + s < sz; ++i) - raw_set_bit(sz - i - 1, raw_get_bit(sz - i - 1 - s)); - for (size_t i = sz > s ? sz - s : 0; i < sz; ++i) - raw_set_bit(sz - i - 1, 0); - } - - size_t length() { - return size_; - } - - private: - size_t check_index(size_t i, bool tail = false) { - if (tail) { - return (i > 0 && i <= size_) ? i - 1 : size_ - 1; - } - return i > 0 ? i - 1 : 0; - } - - /* set ith bit to 1 if b is truthy, else 0 */ - void raw_set_bit(size_t i, size_t b) { - if (i < size_) { - BWORD mask; - BWORD* word = get_bit_access(i, &mask); - if (b) - *word |= mask; /* set bit */ - else - *word &= ~mask; /* reset bit */ - } - } - - /* get ith bit (1 or 0) */ - size_t raw_get_bit(size_t i) { - if (i < size_) { - BWORD mask; - BWORD* word = get_bit_access(i, &mask); - return (*word & mask) ? 1 : 0; - } - return -1; - } - - /* given an index, returns the word address and the mask to access the bit */ - BWORD* get_bit_access(size_t i, BWORD* mask) { - if (mask != nullptr) - *mask = I_BIT(i); - return &values_[I_BWORD(i)]; - } - - private: - size_t size_; - BWORD* values_; /* uses little endian to store bits */ - }; - -} diff --git a/extend/lcodec/src/bitset.h b/extend/lcodec/src/bitset.h new file mode 100644 index 00000000..3bdaae7b --- /dev/null +++ b/extend/lcodec/src/bitset.h @@ -0,0 +1,51 @@ +#pragma once + +#include +#include +#include + +namespace lcodec { + + template + std::string lua_bitset_new(std::string val) { + std::bitset bit(val); + return bit.to_string(); + } + + template + bool lua_bitset_get(std::string val, size_t pos) { + std::bitset bit(val); + return bit[pos - 1]; + } + + template + std::string lua_bitset_set(std::string val, size_t pos, bool bval) { + std::bitset bit(val); + return bit.set(pos - 1, bval).to_string(); + } + + template + std::string lua_bitset_flip(std::string val, size_t pos) { + std::bitset bit(val); + return bit.flip(pos - 1).to_string(); + } + + template + std::string lua_bitset_reset(std::string val, size_t pos) { + std::bitset bit(val); + if (pos == 0) { + return bit.reset().to_string(); + } + return bit.reset(pos - 1).to_string(); + } + + template + bool lua_bitset_check(std::string val, size_t len) { + if (len > N) return false; + std::bitset bit(val); + for (size_t i = 0; i < len; ++i) { + if (!bit[i]) return false; + } + return true; + } +} diff --git a/extend/lcodec/src/lcodec.cpp b/extend/lcodec/src/lcodec.cpp index 72c2ac60..d488d730 100644 --- a/extend/lcodec/src/lcodec.cpp +++ b/extend/lcodec/src/lcodec.cpp @@ -41,20 +41,10 @@ namespace lcodec { codec->set_buff(&thread_buff); return codec; } - - static bitarray* lbarray(lua_State* L, size_t nbits) { - bitarray* barray = new bitarray(); - if (!barray->general(nbits)) { - delete barray; - return nullptr; - } - return barray; - } luakit::lua_table open_lcodec(lua_State* L) { luakit::kit_state kit_state(L); auto llcodec = kit_state.new_table("codec"); - llcodec.set_function("bitarray", lbarray); llcodec.set_function("guid_new", guid_new); llcodec.set_function("guid_string", guid_string); llcodec.set_function("guid_tostring", guid_tostring); @@ -76,34 +66,37 @@ namespace lcodec { llcodec.set_function("rediscodec", rds_codec); llcodec.set_function("wsscodec", wss_codec); llcodec.set_function("url_encode", url_encode); - llcodec.set_function("url_decode", url_decode); - - kit_state.new_class( - "flip", &bitarray::flip, - "fill", &bitarray::fill, - "equal", &bitarray::equal, - "clone", &bitarray::clone, - "slice", &bitarray::slice, - "concat", &bitarray::concat, - "lshift", &bitarray::lshift, - "rshift", &bitarray::rshift, - "length", &bitarray::length, - "resize", &bitarray::resize, - "reverse", &bitarray::reverse, - "set_bit", &bitarray::set_bit, - "get_bit", &bitarray::get_bit, - "flip_bit", &bitarray::flip_bit, - "to_string", &bitarray::to_string, - "from_string", &bitarray::from_string, - "to_uint8", &bitarray::to_number, - "to_uint16", &bitarray::to_number, - "to_uint32", &bitarray::to_number, - "to_uint64", &bitarray::to_number, - "from_uint8", &bitarray::from_number, - "from_uint16", &bitarray::from_number, - "from_uint32", &bitarray::from_number, - "from_uint64", &bitarray::from_number - ); + llcodec.set_function("url_decode", url_decode); + llcodec.set_function("bit32_new", lua_bitset_new<32>); + llcodec.set_function("bit64_new", lua_bitset_new<64>); + llcodec.set_function("bit128_new", lua_bitset_new<128>); + llcodec.set_function("bit256_new", lua_bitset_new<256>); + llcodec.set_function("bit512_new", lua_bitset_new<512>); + llcodec.set_function("bit32_get", lua_bitset_get<32>); + llcodec.set_function("bit64_get", lua_bitset_get<64>); + llcodec.set_function("bit128_get", lua_bitset_get<128>); + llcodec.set_function("bit256_get", lua_bitset_get<256>); + llcodec.set_function("bit512_get", lua_bitset_get<512>); + llcodec.set_function("bit32_set", lua_bitset_set<32>); + llcodec.set_function("bit64_set", lua_bitset_set<64>); + llcodec.set_function("bit128_set", lua_bitset_set<128>); + llcodec.set_function("bit256_set", lua_bitset_set<256>); + llcodec.set_function("bit512_set", lua_bitset_set<512>); + llcodec.set_function("bit32_flip", lua_bitset_flip<32>); + llcodec.set_function("bit64_flip", lua_bitset_flip<64>); + llcodec.set_function("bit128_flip", lua_bitset_flip<128>); + llcodec.set_function("bit256_flip", lua_bitset_flip<256>); + llcodec.set_function("bit512_flip", lua_bitset_flip<512>); + llcodec.set_function("bit32_reset", lua_bitset_reset<32>); + llcodec.set_function("bit64_reset", lua_bitset_reset<64>); + llcodec.set_function("bit128_reset", lua_bitset_reset<128>); + llcodec.set_function("bit256_reset", lua_bitset_reset<256>); + llcodec.set_function("bit512_reset", lua_bitset_reset<512>); + llcodec.set_function("bit32_check", lua_bitset_check<32>); + llcodec.set_function("bit64_check", lua_bitset_check<64>); + llcodec.set_function("bit128_check", lua_bitset_check<128>); + llcodec.set_function("bit256_check", lua_bitset_check<256>); + llcodec.set_function("bit512_check", lua_bitset_check<512>); return llcodec; } } diff --git a/extend/lcodec/src/lcodec.h b/extend/lcodec/src/lcodec.h index eafa4c3c..aa277d4f 100644 --- a/extend/lcodec/src/lcodec.h +++ b/extend/lcodec/src/lcodec.h @@ -10,4 +10,4 @@ #include "redis.h" #include "mysql.h" #include "websocket.h" -#include "bitarray.h" +#include "bitset.h" diff --git a/extend/lsqlite/src/lsqlite.h b/extend/lsqlite/src/lsqlite.h index 9191f2a2..226dd35f 100644 --- a/extend/lsqlite/src/lsqlite.h +++ b/extend/lsqlite/src/lsqlite.h @@ -151,7 +151,7 @@ namespace lsqlite { ~sqlite_driver() { close(); } void close() { - if (m_sdb) sqlite3_close(m_sdb); + if (m_sdb) sqlite3_close_v2(m_sdb); m_sdb = nullptr; } diff --git a/script/constant.lua b/script/constant.lua index ab625577..b2a15a1b 100644 --- a/script/constant.lua +++ b/script/constant.lua @@ -91,10 +91,8 @@ GMType.GLOBAL = 0 -- 全局相关 GMType.PLAYER = 1 -- 玩家相关, ID为玩家的ID GMType.SERVICE = 2 -- 服务相关, 转发所有服务 GMType.SYSTEM = 3 -- 业务相关, ID为队伍ID,房间ID等 -GMType.OFFLINE = 4 -- 玩家相关, ID为玩家的ID,需要处理离线 -GMType.LOCAL = 5 -- 本地事件转发 -GMType.HASHKEY = 6 -- 服务相关, ID按hash分发 - +GMType.LOCAL = 4 -- 本地事件转发 +GMType.HASHKEY = 5 -- 服务相关, ID按hash分发 -- robot类型 local RobotType = enum("RobotType", 0) diff --git a/script/driver/lmdb.lua b/script/driver/lmdb.lua index bfa84487..2d00ef3b 100644 --- a/script/driver/lmdb.lua +++ b/script/driver/lmdb.lua @@ -2,6 +2,8 @@ local log_debug = logger.debug local sformat = string.format +local update_mgr = quanta.get("update_mgr") + local MDB_SUCCESS = lmdb.MDB_CODE.MDB_SUCCESS local MDB_NOTFOUND = lmdb.MDB_CODE.MDB_NOTFOUND @@ -24,6 +26,15 @@ prop:reader("jcodec", nil) function Lmdb:__init() stdfs.mkdir(LMDB_PATH) + update_mgr:attach_quit(self) +end + +function Lmdb:on_quit() + if self.driver then + log_debug("[Lmdb][on_quit]") + self.driver.close() + self.driver = nil + end end function Lmdb:open(name, dbname) diff --git a/script/driver/sqlite.lua b/script/driver/sqlite.lua index a7437d57..5aa30130 100644 --- a/script/driver/sqlite.lua +++ b/script/driver/sqlite.lua @@ -3,6 +3,8 @@ local log_err = logger.err local log_debug = logger.debug local sformat = string.format +local update_mgr = quanta.get("update_mgr") + local SQLITE_OK = sqlite.SQLITE_CODE.SQLITE_OK local SQLITE_DONE = sqlite.SQLITE_CODE.SQLITE_DONE local SQLITE_NFOUND = sqlite.SQLITE_CODE.SQLITE_NOTFOUND @@ -19,6 +21,21 @@ prop:reader("prepares", {}) function Sqlite:__init() stdfs.mkdir(SQDB_PATH) + update_mgr:attach_quit(self) +end + +function Sqlite:on_quit() + if self.driver then + log_debug("[Sqlite][on_quit]") + for _, stmts in pairs(self.prepares) do + for _, stmt in pairs(stmts) do + stmt.close() + end + end + self.prepares = {} + self.driver.close() + self.driver = nil + end end function Sqlite:open(dbname) diff --git a/script/driver/unqlite.lua b/script/driver/unqlite.lua index 77f37cc2..4e5169c2 100644 --- a/script/driver/unqlite.lua +++ b/script/driver/unqlite.lua @@ -2,6 +2,8 @@ local log_debug = logger.debug local sformat = string.format +local update_mgr = quanta.get("update_mgr") + local UNQLITE_OK = unqlite.UNQLITE_CODE.UNQLITE_OK local UNQLITE_NOTFOUND = unqlite.UNQLITE_CODE.UNQLITE_NOTFOUND @@ -17,6 +19,15 @@ prop:reader("jcodec", nil) function Unqlite:__init() stdfs.mkdir(UNQLITE_PATH) + update_mgr:attach_quit(self) +end + +function Unqlite:on_quit() + if self.driver then + log_debug("[Unqlite][on_quit]") + self.driver.close() + self.driver = nil + end end function Unqlite:open(name) diff --git a/script/network/http_server.lua b/script/network/http_server.lua index a5096965..148d4c69 100644 --- a/script/network/http_server.lua +++ b/script/network/http_server.lua @@ -14,6 +14,8 @@ local jsoncodec = json.jsoncodec local httpdcodec = codec.httpdcodec local derive_port = luabus.derive_port +local update_mgr = quanta.get("update_mgr") + local HttpServer = class() local prop = property(HttpServer) prop:reader("ip", nil) --http server地址 @@ -29,6 +31,15 @@ function HttpServer:__init(http_addr) self.hcodec = httpdcodec(self.jcodec) self.handlers = { GET = {}, POST = {}, PUT = {}, DELETE = {} } self:setup(http_addr) + --注册退出 + update_mgr:attach_quit(self) +end + +function HttpServer:on_quit() + if self.listener then + log_debug("[HttpServer][on_quit]") + self.listener:close() + end end function HttpServer:setup(http_addr) diff --git a/script/network/net_server.lua b/script/network/net_server.lua index 9f70032d..064c340e 100644 --- a/script/network/net_server.lua +++ b/script/network/net_server.lua @@ -3,6 +3,7 @@ local log_err = logger.err local log_info = logger.info local log_warn = logger.warn +local log_debug = logger.debug local signalquit = signal.quit local qdefer = quanta.defer local qxpcall = quanta.xpcall @@ -11,6 +12,7 @@ local derive_port = luabus.derive_port local proto_pb = luabus.eproto_type.pb local event_mgr = quanta.get("event_mgr") +local update_mgr = quanta.get("update_mgr") local thread_mgr = quanta.get("thread_mgr") local socket_mgr = quanta.get("socket_mgr") local protobuf_mgr = quanta.get("protobuf_mgr") @@ -42,6 +44,15 @@ prop:accessor("msgtype", 0) --消息类型 function NetServer:__init(session_type) self.session_type = session_type self.codec = protobuf.pbcodec() + --注册退出 + update_mgr:attach_quit(self) +end + +function NetServer:on_quit() + if self.listener then + log_debug("[NetServer][on_quit]") + self.listener:close() + end end --induce:根据 order 推导port diff --git a/script/network/rpc_server.lua b/script/network/rpc_server.lua index fab7b01e..812b4e5b 100644 --- a/script/network/rpc_server.lua +++ b/script/network/rpc_server.lua @@ -6,12 +6,14 @@ local signalquit = signal.quit local log_err = logger.err local log_warn = logger.warn local log_info = logger.info +local log_debug = logger.debug local qdefer = quanta.defer local qxpcall = quanta.xpcall local hash_code = codec.hash_code local derive_port = luabus.derive_port local event_mgr = quanta.get("event_mgr") +local update_mgr = quanta.get("update_mgr") local thread_mgr = quanta.get("thread_mgr") local socket_mgr = quanta.get("socket_mgr") @@ -53,8 +55,18 @@ function RpcServer:__init(holder, ip, port, induce) self.listener = listener self.ip, self.port = ip, real_port log_info("[RpcServer][setup] now listen {}:{} success!", ip, real_port) + --监听rpc event_mgr:add_listener(self, "rpc_heartbeat") event_mgr:add_listener(self, "rpc_register") + --注册退出 + update_mgr:attach_quit(self) +end + +function RpcServer:on_quit() + if self.listener then + log_debug("[RpcServer][on_quit]") + self.listener:close() + end end --rpc事件 diff --git a/script/queue/mongo_mq.lua b/script/queue/mongo_mq.lua index 81dd9392..479d52cb 100644 --- a/script/queue/mongo_mq.lua +++ b/script/queue/mongo_mq.lua @@ -59,8 +59,8 @@ end function MongoMQ:send_message(target_id, event, args, ttl) local doc = { args = args, event = event, target_id = target_id, time = quanta.now_ms, uuid = new_guid() } if ttl then - --设置过期ttl字段, 单位毫秒 - doc.ttl = bdate(quanta.now_ms + ttl * 1000) + --设置过期ttl字段 + doc.ttl = bdate(quanta.now + ttl) end local ok = mongo_agent:insert({ self.coll_name, doc }, target_id) if not ok then diff --git a/server/center/center_gm.lua b/server/center/center_gm.lua index 5501cb08..3408a5b2 100644 --- a/server/center/center_gm.lua +++ b/server/center/center_gm.lua @@ -126,14 +126,16 @@ function CenterGM:show_snapshot(service_name, index) local ok, codeoe, res = router_mgr:call_router_id(quanta_id, "rpc_show_snapshot") if not ok then log_err("[CenterGM][show_snapshot] exec service={}-{} failed! codeoe={},res={}", service_name, index, codeoe, res) + return codeoe end - return codeoe, res + return res end local ok, codeoe, res = router_mgr:call_target(quanta_id, "rpc_show_snapshot") if not ok then log_err("[CenterGM][show_snapshot] exec service={}-{} failed! codeoe={},res={}", service_name, index, codeoe, res) + return codeoe end - return codeoe, res + return res end -- export diff --git a/server/center/gm_mgr.lua b/server/center/gm_mgr.lua index 1cf59d5e..8679fb9e 100644 --- a/server/center/gm_mgr.lua +++ b/server/center/gm_mgr.lua @@ -8,13 +8,11 @@ local log_err = logger.err local log_debug = logger.debug local sformat = string.format local tunpack = table.unpack -local tinsert = table.insert local make_sid = service.make_sid local guid_index = codec.guid_index local online = quanta.get("online") local cmdline = quanta.get("cmdline") -local monitor = quanta.get("monitor") local event_mgr = quanta.get("event_mgr") local update_mgr = quanta.get("update_mgr") local router_mgr = quanta.get("router_mgr") @@ -22,18 +20,15 @@ local router_mgr = quanta.get("router_mgr") local GLOBAL = quanta.enum("GMType", "GLOBAL") local SYSTEM = quanta.enum("GMType", "SYSTEM") local SERVICE = quanta.enum("GMType", "SERVICE") -local OFFLINE = quanta.enum("GMType", "OFFLINE") local LOCAL = quanta.enum("GMType", "LOCAL") local HASHKEY = quanta.enum("GMType", "HASHKEY") local PLAYER = quanta.enum("GMType", "PLAYER") local SUCCESS = quanta.enum("KernCode", "SUCCESS") -local PLAYER_NOT_EXIST = quanta.enum("KernCode", "PLAYER_NOT_EXIST") local GM_Mgr = singleton() local prop = property(GM_Mgr) prop:reader("http_server", nil) prop:reader("services", {}) -prop:reader("monitors", {}) prop:reader("gm_page", "") prop:reader("gm_status", false) @@ -45,16 +40,12 @@ function GM_Mgr:__init() --创建HTTP服务器 local server = HttpServer(environ.get("QUANTA_GM_HTTP")) - service.modify_host(server:get_port()) self.http_server = server --是否开启GM功能 if environ.status("QUANTA_GM_SERVER") then self.gm_status = true self:register_webgm() end - --关注monitor - monitor:watch_service_ready(self, "monitor") - monitor:watch_service_close(self, "monitor") --定时更新 update_mgr:attach_second5(self) self:on_second5() @@ -80,16 +71,9 @@ function GM_Mgr:on_second5() self.gm_page = import("center/gm_page.lua") end --- 事件请求 -function GM_Mgr:on_register_command(command_list, service_id) - self:rpc_register_command(command_list, service_id) - return SUCCESS -end - function GM_Mgr:register_webgm() self:register_get("/", "on_gm_page", self) self:register_get("/gmlist", "on_gmlist", self) - self:register_get("/monitors", "on_monitors", self) self:register_post("/command", "on_command", self) self:register_post("/message", "on_message", self) end @@ -167,16 +151,6 @@ function GM_Mgr:on_gmlist(url, body, params) return { text = "GM指令", nodes = cmdline:get_displays() } end ---monitor拉取 -function GM_Mgr:on_monitors(url, body, params) - log_debug("[GM_Mgr][on_monitors] body: {}", params) - local nodes = {} - for _, addr in pairs(self.monitors) do - tinsert(nodes, { text = addr, tag = "log" }) - end - return { text = "在线日志", nodes = nodes } -end - --后台GM调用,字符串格式 function GM_Mgr:on_command(url, body) log_debug("[GM_Mgr][on_command] body: {}", body) @@ -193,12 +167,11 @@ end --参数分发预处理 function GM_Mgr:dispatch_pre_command(fmtargs) local result = event_mgr:notify_listener("on_center_command", fmtargs.name, fmtargs.args) - local _, status_ok, args = tunpack(result) + local status_ok, args = tunpack(result) --无额外处理 if not status_ok then return self:dispatch_command(fmtargs.args, fmtargs.type, fmtargs.service) end - return self:dispatch_command(args, fmtargs.type, fmtargs.service) end @@ -228,7 +201,6 @@ function GM_Mgr:dispatch_command(cmd_args, gm_type, service_id) [SYSTEM] = GM_Mgr.exec_system_cmd, [PLAYER] = GM_Mgr.exec_player_cmd, [SERVICE] = GM_Mgr.exec_service_cmd, - [OFFLINE] = GM_Mgr.exec_offline_cmd, [LOCAL] = GM_Mgr.exec_local_cmd, [HASHKEY] = GM_Mgr.exec_hash_cmd, } @@ -279,32 +251,13 @@ end --local command function GM_Mgr:exec_local_cmd(service_id, cmd_name, ...) - local ok, code, res = tunpack(event_mgr:notify_listener(cmd_name, ...)) + local ok, res = tunpack(event_mgr:notify_listener(cmd_name, ...)) if not ok then - return { code = code, msg = "fail" } + return { code = 1, msg = res } end return { code = 0, msg = res } end ---兼容在线和离线的玩家指令 -function GM_Mgr:exec_offline_cmd(service_id, cmd_name, player_id, ...) - log_debug("[GM_Mgr][exec_offline_cmd] cmd_name:{} player_id:{}", cmd_name, player_id) - local ok, codeoe, res = online:call_lobby(player_id, "rpc_command_execute", cmd_name, player_id, ...) - if not ok then - log_err("[GM_Mgr][exec_offline_cmd] rpc_command_execute failed! cmd_name={} player_id={}", cmd_name, player_id) - return { code = 1, msg = codeoe } - end - if codeoe == PLAYER_NOT_EXIST then - ok, codeoe, res = router_mgr:call_lobby_hash(player_id, "rpc_command_execute", cmd_name, player_id, ...) - if not ok then - log_err("[GM_Mgr][exec_offline_cmd] rpc_command_execute failed! player_id:{}, cmd_name={}", player_id, cmd_name) - return { code = 1, msg = codeoe } - end - return { code = codeoe, msg = res } - end - return { code = codeoe, msg = res } -end - --player command function GM_Mgr:exec_player_cmd(service_id, cmd_name, player_id, ...) if player_id == 0 then @@ -325,4 +278,4 @@ end quanta.gm_mgr = GM_Mgr() -return GM_Mgr \ No newline at end of file +return GM_Mgr diff --git a/server/test.lua b/server/test.lua index da7f8892..9d8bed96 100644 --- a/server/test.lua +++ b/server/test.lua @@ -26,12 +26,12 @@ quanta.startup(function()--初始化test import("test/worker_test.lua") import("test/lock_test.lua") import("test/detour_test.lua") - import("test/bitarray_test.lua") + import("test/bitset_test.lua") import("test/lmdb_test.lua") import("test/unqlite_test.lua") import("test/sqlite_test.lua") import("test/ssl_test.lua") import("test/xml_test.lua") ]] - import("test/mongo_test.lua") + import("test/bitset_test.lua") end) diff --git a/server/test/bitarray_test.lua b/server/test/bitarray_test.lua deleted file mode 100644 index 8a0311ac..00000000 --- a/server/test/bitarray_test.lua +++ /dev/null @@ -1,30 +0,0 @@ ---bitarray_test.lua - -local log_debug = logger.debug - -local array = codec.bitarray(32) -log_debug("array1: {}", array.to_string()) -array.fill(1) -log_debug("array11: {}", array.to_string()) -array.flip(1) -log_debug("array12: {}", array.to_string()) -array.flip_bit(3) -log_debug("array13: {}-{}", array.to_string(), array.get_bit(3)) -array.from_uint32(65535) -log_debug("array2: {}-{}", array.to_string(), array.to_uint32()) -array.rshift(1) -log_debug("array3: {}-{}", array.to_string(), array.to_uint32()) -array.lshift(1) -log_debug("array4: {}-{}", array.to_string(), array.to_uint32()) -array.set_bit(32, 1) -local a2 = array.clone() -log_debug("array5: {}", a2.to_string()) -local a3 = a2.slice(1, 16) -log_debug("array51: {}", a3.to_string()) -local a4 = a2.slice(17) -log_debug("array52: {}", a4.to_string()) -a4.concat(a3) -log_debug("array6: {}", a4.to_string()) -a4.reverse() -log_debug("array7: {}", a4.to_string()) -log_debug("array8: {}-{}", a4.equal(a2), a3.equal(a2)) diff --git a/server/test/bitset_test.lua b/server/test/bitset_test.lua new file mode 100644 index 00000000..93cda0b5 --- /dev/null +++ b/server/test/bitset_test.lua @@ -0,0 +1,19 @@ +--bitset_test.lua + +local log_debug = logger.debug + +local bval = codec.bit32_new() +log_debug("bit32_new: {}", bval) +local fval = codec.bit32_flip(bval, 3) +log_debug("bit32_flip: {}-{}", bval, fval) +log_debug("bit32_get: {}-{}", codec.bit32_get(fval, 3), codec.bit32_get(fval, 2)) + +local bval2 = codec.bit32_new("010000101") +log_debug("bit32_new: {}", bval2) +local sval = codec.bit32_set(bval2, 4, true) +log_debug("bit32_set: {}-{}", bval2, sval) +local sval2 = codec.bit32_set(sval, 2, true) +log_debug("bit32_set: {}-{}", bval2, sval2) +log_debug("bit32_check 4: {}", codec.bit32_check(sval2, 4)) +log_debug("bit32_check 5: {}", codec.bit32_check(sval2, 5)) + diff --git a/server/test/mongo_test.lua b/server/test/mongo_test.lua index 56a55f7c..9da73028 100644 --- a/server/test/mongo_test.lua +++ b/server/test/mongo_test.lua @@ -34,7 +34,7 @@ timer_mgr:once(2000, function() log_debug("db listIndexes code: {}, err = {}", icode, ierr) icode, ierr = mongo_mgr:drop_indexes("test_mongo_2", "test_uid") log_debug("db drop_indexes code: {}, err = {}", icode, ierr) - + icode, ierr = mongo_mgr:create_indexes("test_mongo_3", {{key={"pid",1,"uid",1},name="test_uid", unique = true}}) log_debug("db create_indexes code: {}, err = {}", icode, ierr) icode, ierr = mongo_mgr:create_indexes("test_mongo_3", { { key = { ttl = 1 }, expireAfterSeconds = 0, name = "ttl", unique = false } })