Skip to content

Commit

Permalink
修复Lua解释器加锁;加强shell (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
Notify-ctrl authored Dec 24, 2024
1 parent 7d0ab6d commit f942f19
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/core/c-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ Lua::~Lua() {
lua_close(L);
}

// 只要不上锁,就必须上锁;只要另一线程仍在活动,就必须上锁
bool Lua::needLock() {
auto thr = QThread::currentThread();
bool ret = false;
if (current_thread != thr) {
bool locked = true;
if (interpreter_lock.tryLock(0)) {
locked = false;
interpreter_lock.unlock();
}
if (!locked || current_thread != thr) {
current_thread = thr;
ret = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ void Room::removeRejectId(int id) {
void Room::quitRoom(ServerPlayer *player, const QString &) {
removePlayer(player);
if (isOutdated()) {
player->kicked();
emit player->kicked();
}
}

Expand Down Expand Up @@ -631,7 +631,7 @@ void Room::startGame(ServerPlayer *player, const QString &) {
if (isOutdated()) {
for (auto p : getPlayers()) {
p->doNotify("ErrorMsg", "room is outdated");
p->kicked();
emit p->kicked();
}
} else {
manuallyStart();
Expand Down
64 changes: 64 additions & 0 deletions src/server/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ void Shell::helpCommand(QStringList &) {
"At least 1 <name> required.",
"unbanuuid");
HELP_MSG("%s: reset <name>'s password to 1234.", "resetpassword/rp");
HELP_MSG("%s: View status of server.", "stat/gc");
HELP_MSG("%s: View detail information (Lua) of room by an id.", "dumproom");
HELP_MSG("%s: Kick all players in a room, then abandon it.", "killroom");
qInfo();
qInfo("===== Package commands =====");
HELP_MSG("%s: Install a new package from <url>.", "install");
Expand Down Expand Up @@ -466,6 +469,65 @@ void Shell::statCommand(QStringList &) {
((double)server->db->getMemUsage()) / 1048576);
}

void Shell::dumpRoomCommand(QStringList &list) {
if (list.isEmpty() || list[0].isEmpty()) {
static const char *code =
"local _, reqRoom = debug.getupvalue(ResumeRoom, 1)\n"
"for id, room in pairs(reqRoom.runningRooms) do\n"
" fk.qInfo(string.format('<Lua> Room #%d', id))\n"
"end\n";
for (auto thr : ServerInstance->findChildren<RoomThread *>()) {
qInfo("== Lua room info of RoomThread %p ==", thr);
thr->getLua()->eval(code);
}
return;
}

auto pid = list[0];
bool ok;
int id = pid.toInt(&ok);
if (!ok) return;

auto room = ServerInstance->findRoom(id);
if (!room) {
qInfo("No such room.");
} else {
static auto code = QStringLiteral(
"(function(id)\n"
" local _, reqRoom = debug.getupvalue(ResumeRoom, 1)\n"
" local room = reqRoom.runningRooms[id]\n"
" if not room then fk.qInfo('<Lua> No such room.'); return end\n"
" fk.qInfo(string.format('== <Lua> Data of room %d ==', id))\n"
" fk.qInfo(json.encode(room:toJsonObject(room.players[1])))\n"
"end)\n");
auto L = qobject_cast<RoomThread *>(room->parent())->getLua();
L->eval(code + QStringLiteral("(%1)").arg(id));
}
}

void Shell::killRoomCommand(QStringList &list) {
if (list.isEmpty() || list[0].isEmpty()) {
qWarning("Need room id to do this.");
return;
}

auto pid = list[0];
bool ok;
int id = pid.toInt(&ok);
if (!ok) return;

auto room = ServerInstance->findRoom(id);
if (!room) {
qInfo("No such room.");
} else {
qInfo("Killing room %d", id);
for (auto player : room->getPlayers()) {
if (player->getId() > 0) emit player->kicked();
}
emit room->abandoned();
}
}

#ifdef FK_USE_READLINE
static void sigintHandler(int) {
rl_reset_line_state();
Expand Down Expand Up @@ -520,6 +582,8 @@ Shell::Shell() {
{"rp", &Shell::resetPasswordCommand},
{"stat", &Shell::statCommand},
{"gc", &Shell::statCommand},
{"dumproom", &Shell::dumpRoomCommand},
{"killroom", &Shell::killRoomCommand},
// special command
{"quit", &Shell::helpCommand},
{"crash", &Shell::helpCommand},
Expand Down
2 changes: 2 additions & 0 deletions src/server/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Shell: public QThread {
void reloadConfCommand(QStringList &);
void resetPasswordCommand(QStringList &);
void statCommand(QStringList &);
void dumpRoomCommand(QStringList &);
void killRoomCommand(QStringList &);

#ifdef FK_USE_READLINE
private:
Expand Down

0 comments on commit f942f19

Please sign in to comment.