Skip to content

Commit

Permalink
avoid full CPU occupation when room is paused
Browse files Browse the repository at this point in the history
(last 2 commits by tangjs520)
  • Loading branch information
Paracel committed Aug 11, 2014
1 parent 071da88 commit 1e99ef2
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 50 deletions.
22 changes: 11 additions & 11 deletions builds/vs2010/sanguosha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2805,59 +2805,59 @@
<context>
<name>Room</name>
<message>
<location filename="../../src/server/room.cpp" line="2029"/>
<location filename="../../src/server/room.cpp" line="2034"/>
<source>disconnected</source>
<translation>断开</translation>
</message>
<message>
<location filename="../../src/server/room.cpp" line="2254"/>
<location filename="../../src/server/room.cpp" line="2262"/>
<source>Computer %1</source>
<translation>电脑 %1</translation>
</message>
<message>
<location filename="../../src/server/room.cpp" line="2052"/>
<location filename="../../src/server/room.cpp" line="2057"/>
<source>&lt;font color=#000000&gt;Player &lt;b&gt;%1&lt;/b&gt; left the game&lt;/font&gt;</source>
<translation>&lt;font color=#000000&gt;玩家 %1 离开了游戏&lt;/font&gt;</translation>
</message>
<message>
<location filename="../../src/server/room.cpp" line="2259"/>
<location filename="../../src/server/room.cpp" line="2267"/>
<source>Hello, I&apos;m a robot</source>
<translation>大家好,我是一个机器人</translation>
</message>
<message>
<location filename="../../src/server/room.cpp" line="2302"/>
<location filename="../../src/server/room.cpp" line="2310"/>
<source>&lt;font color=#EEB422&gt;Player &lt;b&gt;%1&lt;/b&gt; joined the game&lt;/font&gt;</source>
<translation>&lt;font color=#EEB422&gt;玩家 &lt;b&gt;%1&lt;/b&gt; 加入了游戏&lt;/font&gt;</translation>
</message>
<message>
<location filename="../../src/server/room.cpp" line="2990"/>
<location filename="../../src/server/room.cpp" line="2998"/>
<source>Unable to parse player</source>
<translation>不可分析的玩家</translation>
</message>
<message>
<location filename="../../src/server/room.cpp" line="2992"/>
<location filename="../../src/server/room.cpp" line="3000"/>
<source>Server is not waiting for reply from %1</source>
<translation>服务器无法获取 %1 的响应</translation>
</message>
<message>
<location filename="../../src/server/room.cpp" line="2994"/>
<location filename="../../src/server/room.cpp" line="3002"/>
<source>Reply command should be %1 instead of %2</source>
<translation>应答指令应为 %1 而不是 %2</translation>
</message>
<message>
<location filename="../../src/server/room.cpp" line="2997"/>
<location filename="../../src/server/room.cpp" line="3005"/>
<source>Reply serial should be %1 instead of %2</source>
<translation>玩家序号应为 %1 而不是 %2</translation>
</message>
<message>
<location filename="../../src/server/room.cpp" line="4311"/>
<location filename="../../src/server/room.cpp" line="4330"/>
<source>Card cannot be parsed:
%1</source>
<translation>卡牌字符串无法解析:
%1</translation>
</message>
<message>
<location filename="../../src/server/room.cpp" line="5523"/>
<location filename="../../src/server/room.cpp" line="5547"/>
<source>&lt;font color=#EEB422&gt;The network delay of player &lt;b&gt;%1&lt;/b&gt; is %2 milliseconds.&lt;/font&gt;</source>
<translation>&lt;font color=#EEB422&gt;玩家 &lt;b&gt;%1&lt;/b&gt; 的网络延迟为 %2 毫秒&lt;/font&gt;</translation>
</message>
Expand Down
65 changes: 35 additions & 30 deletions src/server/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ bool Room::notifyMoveFocus(const QList<ServerPlayer *> &players, CommandType com
}

bool Room::askForSkillInvoke(ServerPlayer *player, const QString &skill_name, const QVariant &data) {
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_INVOKE_SKILL);

bool invoked = false;
Expand Down Expand Up @@ -936,7 +936,7 @@ bool Room::askForSkillInvoke(ServerPlayer *player, const QString &skill_name, co
}

QString Room::askForChoice(ServerPlayer *player, const QString &skill_name, const QString &choices, const QVariant &data) {
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_MULTIPLE_CHOICE);

QStringList validChoices = choices.split("+");
Expand Down Expand Up @@ -1014,7 +1014,7 @@ bool Room::askForNullification(const Card *trick, ServerPlayer *from, ServerPlay

bool Room::_askForNullification(const Card *trick, ServerPlayer *from, ServerPlayer *to,
bool positive, _NullificationAiHelper aiHelper) {
while (isPaused()) {}
tryPause();

_m_roomState.setCurrentCardUseReason(CardUseStruct::CARD_USE_REASON_RESPONSE_USE);
QString trick_name = trick->objectName();
Expand Down Expand Up @@ -1110,7 +1110,7 @@ bool Room::_askForNullification(const Card *trick, ServerPlayer *from, ServerPla

int Room::askForCardChosen(ServerPlayer *player, ServerPlayer *who, const QString &flags, const QString &reason,
bool handcard_visible, Card::HandlingMethod method, const QList<int> &disabled_ids) {
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_CHOOSE_CARD);

if (handcard_visible && !who->isKongcheng()) {
Expand Down Expand Up @@ -1184,7 +1184,7 @@ const Card *Room::askForCard(ServerPlayer *player, const QString &pattern, const
const QVariant &data, Card::HandlingMethod method, ServerPlayer *to,
bool isRetrial, const QString &skill_name, bool isProvision) {
Q_ASSERT(pattern != "slash" || method != Card::MethodUse); // use askForUseSlashTo instead
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_RESPONSE_CARD);

_m_roomState.setCurrentCardUsePattern(pattern);
Expand Down Expand Up @@ -1348,7 +1348,7 @@ const Card *Room::askForCard(ServerPlayer *player, const QString &pattern, const
const Card *Room::askForUseCard(ServerPlayer *player, const QString &pattern, const QString &prompt, int notice_index,
Card::HandlingMethod method, bool addHistory) {
Q_ASSERT(method != Card::MethodResponse);
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_RESPONSE_CARD);

_m_roomState.setCurrentCardUsePattern(pattern);
Expand Down Expand Up @@ -1432,7 +1432,7 @@ const Card *Room::askForUseSlashTo(ServerPlayer *slasher, ServerPlayer *victim,
}

int Room::askForAG(ServerPlayer *player, const QList<int> &card_ids, bool refusable, const QString &reason) {
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_AMAZING_GRACE);
Q_ASSERT(card_ids.length() > 0);

Expand Down Expand Up @@ -1463,7 +1463,7 @@ int Room::askForAG(ServerPlayer *player, const QList<int> &card_ids, bool refusa

const Card *Room::askForCardShow(ServerPlayer *player, ServerPlayer *requestor, const QString &reason) {
Q_ASSERT(!player->isKongcheng());
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_SHOW_CARD);
const Card *card = NULL;

Expand All @@ -1489,7 +1489,7 @@ const Card *Room::askForCardShow(ServerPlayer *player, ServerPlayer *requestor,
}

const Card *Room::askForSinglePeach(ServerPlayer *player, ServerPlayer *dying) {
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_ASK_PEACH);
_m_roomState.setCurrentCardUseReason(CardUseStruct::CARD_USE_REASON_RESPONSE_USE);

Expand Down Expand Up @@ -1707,9 +1707,11 @@ bool Room::canPause(ServerPlayer *player) const{
return true;
}

bool Room::isPaused() const{
if (!canPause(getOwner())) return false;
return game_paused;
void Room::tryPause() {
if (!canPause(getOwner())) return;
QMutexLocker locker(&m_mutex);
while (game_paused)
m_waitCond.wait(locker.mutex());
}

int Room::getLack() const{
Expand Down Expand Up @@ -1879,7 +1881,7 @@ void Room::setFixedDistance(Player *from, const Player *to, int distance) {
}

void Room::reverseFor3v3(const Card *card, ServerPlayer *player, QList<ServerPlayer *> &list) {
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_CHOOSE_DIRECTION);

bool isClockwise = false;
Expand Down Expand Up @@ -2111,13 +2113,16 @@ bool Room::trustCommand(ServerPlayer *player, const Json::Value &) {
bool Room::pauseCommand(ServerPlayer *player, const Json::Value &arg) {
if (!canPause(player)) return false;
bool pause = arg.asBool();
QMutexLocker locker(&m_mutex);
if (game_paused != pause) {
Json::Value arg(Json::arrayValue);
arg[0] = (int)S_GAME_EVENT_PAUSE;
arg[1] = pause;
doNotify(player, S_COMMAND_LOG_EVENT, arg);

game_paused = pause;
if (!game_paused)
m_waitCond.wakeAll();
}
return true;
}
Expand Down Expand Up @@ -4279,7 +4284,7 @@ void Room::setEmotion(ServerPlayer *target, const QString &emotion) {
#include <QElapsedTimer>

void Room::activate(ServerPlayer *player, CardUseStruct &card_use) {
while (isPaused()) {}
tryPause();

if (player->getPhase() != Player::Play) return;
if (player->hasFlag("Global_PlayPhaseTerminated")) {
Expand Down Expand Up @@ -4333,7 +4338,7 @@ void Room::activate(ServerPlayer *player, CardUseStruct &card_use) {
}

void Room::askForLuckCard() {
while (isPaused()) {}
tryPause();

QList<ServerPlayer *> players;
foreach (ServerPlayer *player, m_players) {
Expand Down Expand Up @@ -4442,7 +4447,7 @@ void Room::askForLuckCard() {
}

Card::Suit Room::askForSuit(ServerPlayer *player, const QString &reason) {
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_CHOOSE_SUIT);

AI *ai = player->getAI();
Expand All @@ -4469,7 +4474,7 @@ Card::Suit Room::askForSuit(ServerPlayer *player, const QString &reason) {
}

QString Room::askForKingdom(ServerPlayer *player) {
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_CHOOSE_KINGDOM);

QString result = "wei";
Expand Down Expand Up @@ -4499,7 +4504,7 @@ bool Room::askForDiscard(ServerPlayer *player, const QString &reason, int discar
bool optional, bool include_equip, const QString &prompt, const QString &pattern) {
if (!player->isAlive())
return false;
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_DISCARD_CARD);
min_num = qMin(min_num, discard_num);
ExpPattern exp_pattern(pattern);
Expand Down Expand Up @@ -4618,7 +4623,7 @@ const Card *Room::askForExchange(ServerPlayer *player, const QString &reason, in
bool include_equip, const QString &prompt, bool optional, const QString &pattern) {
if (!player->isAlive())
return NULL;
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_EXCHANGE_CARD);
min_num = qMin(min_num, discard_num);

Expand Down Expand Up @@ -4693,7 +4698,7 @@ ServerPlayer *Room::getLord() const{

void Room::askForGuanxing(ServerPlayer *zhuge, const QList<int> &cards, GuanxingType guanxing_type) {
QList<int> top_cards, bottom_cards;
while (isPaused()) {}
tryPause();
notifyMoveFocus(zhuge, S_COMMAND_SKILL_GUANXING);

AI *ai = zhuge->getAI();
Expand Down Expand Up @@ -4789,7 +4794,7 @@ void Room::returnToTopDrawPile(const QList<int> &cards) {

int Room::doGongxin(ServerPlayer *shenlvmeng, ServerPlayer *target, QList<int> enabled_ids, QString skill_name) {
Q_ASSERT(!target->isKongcheng());
while (isPaused()) {}
tryPause();
notifyMoveFocus(shenlvmeng, S_COMMAND_SKILL_GONGXIN);

LogMessage log;
Expand Down Expand Up @@ -4850,7 +4855,7 @@ const Card *Room::askForPindian(ServerPlayer *player, ServerPlayer *from, Server
if (!from->isAlive() || !to->isAlive())
return NULL;
Q_ASSERT(!player->isKongcheng());
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_PINDIAN);

if (player->getHandcardNum() == 1)
Expand Down Expand Up @@ -4883,7 +4888,7 @@ QList<const Card *> Room::askForPindianRace(ServerPlayer *from, ServerPlayer *to
if (!from->isAlive() || !to->isAlive())
return QList<const Card *>() << NULL << NULL;
Q_ASSERT(!from->isKongcheng() && !to->isKongcheng());
while (isPaused()) {}
tryPause();
Countdown countdown;
countdown.m_max = ServerInfo.getCommandTimeout(S_COMMAND_PINDIAN, S_CLIENT_INSTANCE);
countdown.m_type = Countdown::S_COUNTDOWN_USE_SPECIFIED;
Expand Down Expand Up @@ -4958,7 +4963,7 @@ ServerPlayer *Room::askForPlayerChosen(ServerPlayer *player, const QList<ServerP
return targets.first();
}

while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_CHOOSE_PLAYER);
AI *ai = player->getAI();
ServerPlayer *choice = NULL;
Expand Down Expand Up @@ -5016,7 +5021,7 @@ void Room::_setupChooseGeneralRequestArgs(ServerPlayer *player) {
}

QString Room::askForGeneral(ServerPlayer *player, const QStringList &generals, QString default_choice) {
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_CHOOSE_GENERAL);

if (generals.length() == 1)
Expand Down Expand Up @@ -5262,7 +5267,7 @@ void Room::sendCompulsoryTriggerLog(ServerPlayer *player, const QString &skill_n
void Room::showCard(ServerPlayer *player, int card_id, ServerPlayer *only_viewer) {
if (getCardOwner(card_id) != player) return;

while (isPaused()) {}
tryPause();
notifyMoveFocus(player);
Json::Value show_arg(Json::arrayValue);
show_arg[0] = toJsonString(player->objectName());
Expand Down Expand Up @@ -5292,7 +5297,7 @@ void Room::showCard(ServerPlayer *player, int card_id, ServerPlayer *only_viewer
void Room::showAllCards(ServerPlayer *player, ServerPlayer *to) {
if (player->isKongcheng())
return;
while (isPaused()) {}
tryPause();

Json::Value gongxinArgs(Json::arrayValue);
gongxinArgs[0] = toJsonString(player->objectName());
Expand Down Expand Up @@ -5420,7 +5425,7 @@ bool Room::askForYiji(ServerPlayer *guojia, QList<int> &cards, const QString &sk
else
reason.m_reason = CardMoveReason::S_REASON_GIVE;
}
while (isPaused()) {}
tryPause();
notifyMoveFocus(guojia, S_COMMAND_SKILL_YIJI);

ServerPlayer *target = NULL;
Expand Down Expand Up @@ -5507,7 +5512,7 @@ QString Room::generatePlayerName() {
}

QString Room::askForOrder(ServerPlayer *player, const QString &default_choice) {
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_CHOOSE_ORDER);

if (player->getAI())
Expand All @@ -5522,7 +5527,7 @@ QString Room::askForOrder(ServerPlayer *player, const QString &default_choice) {
}

QString Room::askForRole(ServerPlayer *player, const QStringList &roles, const QString &scheme) {
while (isPaused()) {}
tryPause();
notifyMoveFocus(player, S_COMMAND_CHOOSE_ROLE_3V3);

QStringList squeezed = roles.toSet().toList();
Expand Down
6 changes: 5 additions & 1 deletion src/server/room.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct LogMessage;
#include "room-state.h"
#include <qmutex.h>
#include <QStack>
#include <QWaitCondition>

class Room: public QThread {
Q_OBJECT
Expand All @@ -41,7 +42,7 @@ class Room: public QThread {
bool isFull() const;
bool isFinished() const;
bool canPause(ServerPlayer *p) const;
bool isPaused() const;
void tryPause();
int getLack() const;
QString getMode() const;
const Scenario *getScenario() const;
Expand Down Expand Up @@ -496,6 +497,9 @@ class Room: public QThread {
Json::Value m_fillAGarg;
Json::Value m_takeAGargs;

QWaitCondition m_waitCond;
mutable QMutex m_mutex;

static QString generatePlayerName();
void prepareForStart();
void assignGeneralsForPlayers(const QList<ServerPlayer *> &to_assign);
Expand Down
4 changes: 2 additions & 2 deletions src/server/roomthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ bool RoomThread::trigger(TriggerEvent triggerEvent, Room *room, ServerPlayer *ta
triggered.append(skill);
current_priority = skill->getPriority(triggerEvent);
if (skill->triggerable(target)) {
while (room->isPaused()) {}
room->tryPause();
broken = skill->trigger(triggerEvent, room, target, data);
if (broken) break;
i = 0;
Expand Down Expand Up @@ -672,7 +672,7 @@ bool RoomThread::trigger(TriggerEvent triggerEvent, Room *room, ServerPlayer *ta
throw throwed_event;
}

while (room->isPaused()) {}
room->tryPause();
return broken;
}

Expand Down
Loading

0 comments on commit 1e99ef2

Please sign in to comment.