Skip to content

Commit b72786f

Browse files
committed
继续完善服务器安全关机机制
1 parent b9dd7b1 commit b72786f

9 files changed

Lines changed: 53 additions & 13 deletions

File tree

‎kbe/src/lib/network/event_dispatcher.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ void EventDispatcher::processUntilBreak()
279279
//-------------------------------------------------------------------------------------
280280
void EventDispatcher::processContinuously()
281281
{
282-
breakProcessing_ = false;
283-
while(!breakProcessing_)
282+
breakProcessing_ = EVENT_DISPATCHER_STATUS_RUNNING;
283+
while(breakProcessing_ != EVENT_DISPATCHER_STATUS_BREAK_PROCESSING)
284284
{
285285
this->processOnce(true);
286286
}
@@ -289,15 +289,15 @@ void EventDispatcher::processContinuously()
289289
//-------------------------------------------------------------------------------------
290290
int EventDispatcher::processOnce(bool shouldIdle)
291291
{
292-
breakProcessing_ = false;
292+
breakProcessing_ = EVENT_DISPATCHER_STATUS_RUNNING;
293293
this->processFrequentTasks();
294-
if(!breakProcessing_){
294+
if(breakProcessing_ != EVENT_DISPATCHER_STATUS_BREAK_PROCESSING){
295295
this->processTimers();
296296
}
297297

298298
this->processStats();
299299

300-
if(!breakProcessing_){
300+
if(breakProcessing_ != EVENT_DISPATCHER_STATUS_BREAK_PROCESSING){
301301
return this->processNetwork(shouldIdle);
302302
}
303303

‎kbe/src/lib/network/event_dispatcher.hpp‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,25 @@ class EventPoller;
3737
class EventDispatcher
3838
{
3939
public:
40+
enum EVENT_DISPATCHER_STATUS
41+
{
42+
EVENT_DISPATCHER_STATUS_RUNNING = 0,
43+
EVENT_DISPATCHER_STATUS_WAITING_BREAK_PROCESSING = 1,
44+
EVENT_DISPATCHER_STATUS_BREAK_PROCESSING = 2
45+
};
46+
4047
EventDispatcher();
4148
virtual ~EventDispatcher();
4249

4350
void processContinuously();
4451
int processOnce(bool shouldIdle = false);
4552
void processUntilBreak();
4653

47-
bool isBreakProcessing()const { return breakProcessing_; }
54+
bool isBreakProcessing()const { return breakProcessing_ == EVENT_DISPATCHER_STATUS_BREAK_PROCESSING; }
55+
bool isWaitBreakProcessing()const { return breakProcessing_ == EVENT_DISPATCHER_STATUS_WAITING_BREAK_PROCESSING; }
4856

4957
void breakProcessing(bool breakState = true);
58+
INLINE void setWaitBreakProcessing();
5059
bool processingBroken()const;
5160

5261
void attach(EventDispatcher & childDispatcher);
@@ -99,7 +108,8 @@ class EventDispatcher
99108
ChildDispatchers childDispatchers_;
100109

101110
protected:
102-
bool breakProcessing_;
111+
int8 breakProcessing_;
112+
103113
double maxWait_;
104114
uint32 numTimerCalls_;
105115

‎kbe/src/lib/network/event_dispatcher.ipp‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,20 @@ INLINE TimerHandle EventDispatcher::addOnceOffTimer(int64 microseconds,
3535

3636
INLINE void EventDispatcher::breakProcessing(bool breakState)
3737
{
38-
breakProcessing_ = breakState;
38+
if(breakState)
39+
breakProcessing_ = EVENT_DISPATCHER_STATUS_BREAK_PROCESSING;
40+
else
41+
breakProcessing_ = EVENT_DISPATCHER_STATUS_RUNNING;
42+
}
43+
44+
INLINE void EventDispatcher::setWaitBreakProcessing()
45+
{
46+
breakProcessing_ = EVENT_DISPATCHER_STATUS_WAITING_BREAK_PROCESSING;
3947
}
4048

4149
INLINE bool EventDispatcher::processingBroken() const
4250
{
43-
return breakProcessing_;
51+
return breakProcessing_ == EVENT_DISPATCHER_STATUS_BREAK_PROCESSING;
4452
}
4553

4654
INLINE double EventDispatcher::maxWait() const

‎kbe/src/lib/network/network_interface.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ Reason NetworkInterface::basicSendWithRetries(Channel * pChannel, Packet * pPack
570570
}
571571

572572
// 其他错误退出尝试
573-
ERROR_MSG("NetworkInterface::basicSendWithRetries: packet discarded.\n");
573+
ERROR_MSG(boost::format("NetworkInterface::basicSendWithRetries: packet discarded(reason=%1%).\n") % (reasonToString(reason)));
574574
return reason;
575575
}
576576

‎kbe/src/lib/server/componentbridge.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Componentbridge::~Componentbridge()
111111
//-------------------------------------------------------------------------------------
112112
Mercury::EventDispatcher & Componentbridge::dispatcher()
113113
{
114-
return networkInterface_.dispatcher();
114+
return networkInterface_.mainDispatcher();
115115
}
116116

117117
//-------------------------------------------------------------------------------------
@@ -136,7 +136,7 @@ bool Componentbridge::findInterfaces()
136136

137137
while(findComponentTypes_[findIdx_] != UNKNOWN_COMPONENT_TYPE)
138138
{
139-
if(dispatcher().isBreakProcessing())
139+
if(dispatcher().isBreakProcessing() || dispatcher().isWaitBreakProcessing())
140140
return false;
141141

142142
int8 findComponentType = findComponentTypes_[findIdx_];

‎kbe/src/lib/server/components.hpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class Address;
4646
class NetworkInterface;
4747
}
4848

49+
// ComponentInfos.flags标志
50+
#define COMPONENT_FLAG_NORMAL 0x00000000
51+
#define COMPONENT_FLAG_SHUTTINGDOWN 0x00000001
4952

5053
class Components : public Singleton<Components>
5154
{
@@ -54,13 +57,24 @@ class Components : public Singleton<Components>
5457

5558
struct ComponentInfos
5659
{
60+
ComponentInfos()
61+
{
62+
uid = 0;
63+
flags = COMPONENT_FLAG_NORMAL;
64+
cid = 0;
65+
groupOrderid = 0;
66+
globalOrderid = 0;
67+
pChannel = NULL;
68+
}
69+
5770
KBEShared_ptr<Mercury::Address > pIntAddr, pExtAddr; // 内部和外部地址
5871
int32 uid;
5972
COMPONENT_ID cid;
6073
COMPONENT_ORDER groupOrderid, globalOrderid;
6174
char username[MAX_NAME + 1];
6275
Mercury::Channel* pChannel;
6376
COMPONENT_TYPE componentType;
77+
uint32 flags;
6478
};
6579

6680
typedef std::vector<ComponentInfos> COMPONENTS;

‎kbe/src/lib/server/serverapp.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ void ServerApp::shutDown()
9999
//-------------------------------------------------------------------------------------
100100
void ServerApp::onShutdownBegin()
101101
{
102+
mainDispatcher_.setWaitBreakProcessing();
103+
networkInterface_.dispatcher().setWaitBreakProcessing();
102104
}
103105

104106
//-------------------------------------------------------------------------------------
@@ -110,6 +112,7 @@ void ServerApp::onShutdown(bool first)
110112
void ServerApp::onShutdownEnd()
111113
{
112114
mainDispatcher_.breakProcessing();
115+
networkInterface_.dispatcher().breakProcessing();
113116
}
114117

115118
//-------------------------------------------------------------------------------------

‎kbe/src/lib/server/shutdown_handler.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ShutdownHandler
4343
virtual bool canShutdown(){ return true; }
4444

4545
void setShuttingdown(){ shuttingdown_ = true; }
46-
46+
bool shuttingdown()const{ return shuttingdown_; }
4747
const std::string& lastShutdownFailReason(){ return lastShutdownFailReason_; }
4848
protected:
4949
std::string lastShutdownFailReason_; // 最后一次关机失败的原因

‎kbe/src/server/machine/machine.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,11 @@ void Machine::stopserver(Mercury::Channel* pChannel, KBEngine::MemoryStream& s)
641641
continue;
642642
}
643643

644+
if(((*iter).flags & COMPONENT_FLAG_SHUTTINGDOWN) > 0)
645+
continue;
646+
647+
(*iter).flags |= COMPONENT_FLAG_SHUTTINGDOWN;
648+
644649
Mercury::Bundle closebundle;
645650
if(componentType != BOTS_TYPE)
646651
{

0 commit comments

Comments
 (0)