Skip to content

Commit b06839b

Browse files
committed
一些小调整, 一些简化。
1 parent 02b7dd7 commit b06839b

43 files changed

Lines changed: 177 additions & 268 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎kbe/src/lib/client_lib/clientapp.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ scriptBaseTypes_(),
6060
gameTimer_(),
6161
componentType_(componentType),
6262
componentID_(componentID),
63-
mainDispatcher_(dispatcher),
63+
dispatcher_(dispatcher),
6464
networkInterface_(ninterface),
6565
pTCPPacketReceiver_(NULL),
6666
pBlowfishFilter_(NULL),
@@ -109,7 +109,7 @@ int ClientApp::unregisterPyObjectToScript(const char* attrName)
109109
//-------------------------------------------------------------------------------------
110110
bool ClientApp::initializeBegin()
111111
{
112-
gameTimer_ = this->mainDispatcher().addTimer(1000000 / g_kbeConfig.gameUpdateHertz(), this,
112+
gameTimer_ = this->dispatcher().addTimer(1000000 / g_kbeConfig.gameUpdateHertz(), this,
113113
reinterpret_cast<void *>(TIMEOUT_GAME_TICK));
114114

115115
ProfileVal::setWarningPeriod(stampsPerSecond() / g_kbeConfig.gameUpdateHertz());
@@ -420,14 +420,14 @@ void ClientApp::handleGameTick()
420420
//-------------------------------------------------------------------------------------
421421
bool ClientApp::run(void)
422422
{
423-
mainDispatcher_.processUntilBreak();
423+
dispatcher_.processUntilBreak();
424424
return true;
425425
}
426426

427427
//-------------------------------------------------------------------------------------
428428
int ClientApp::processOnce(bool shouldIdle)
429429
{
430-
return mainDispatcher_.processOnce(shouldIdle);
430+
return dispatcher_.processOnce(shouldIdle);
431431
}
432432

433433
//-------------------------------------------------------------------------------------
@@ -544,7 +544,7 @@ PyObject* ClientApp::__py_setScriptLogType(PyObject* self, PyObject* args)
544544
void ClientApp::shutDown()
545545
{
546546
INFO_MSG("ClientApp::shutDown: shutting down\n");
547-
mainDispatcher_.breakProcessing();
547+
dispatcher_.breakProcessing();
548548
}
549549

550550
//-------------------------------------------------------------------------------------

‎kbe/src/lib/client_lib/clientapp.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ class ClientApp :
112112
GAME_TIME time() const { return g_kbetime; }
113113
double gameTimeInSeconds() const;
114114

115-
Network::EventDispatcher & mainDispatcher() { return mainDispatcher_; }
116-
Network::NetworkInterface & networkInterface() { return networkInterface_; }
115+
Network::EventDispatcher & dispatcher() { return dispatcher_; }
116+
Network::NetworkInterface & networkInterface() { return networkInterface_; }
117117

118118
COMPONENT_ID componentID()const { return componentID_; }
119119
COMPONENT_TYPE componentType()const { return componentType_; }
@@ -225,7 +225,7 @@ class ClientApp :
225225
// 本组件的ID
226226
COMPONENT_ID componentID_;
227227

228-
Network::EventDispatcher& mainDispatcher_;
228+
Network::EventDispatcher& dispatcher_;
229229
Network::NetworkInterface& networkInterface_;
230230

231231
Network::TCPPacketReceiver* pTCPPacketReceiver_;

‎kbe/src/lib/common/timer.h‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,6 @@ class TimersT : public TimersBase
143143
TimerHandle add(TimeStamp startTime, TimeStamp interval,
144144
TimerHandler* pHandler, void * pUser);
145145

146-
TIME_STAMP timerDeliveryTime(TimerHandle handle) const;
147-
TIME_STAMP timerIntervalTime(TimerHandle handle) const;
148-
TIME_STAMP& timerIntervalTime(TimerHandle handle);
149146
private:
150147

151148
typedef std::vector<KBEngine::TimeBase *> Container;
@@ -162,9 +159,6 @@ class TimersT : public TimersBase
162159

163160
TIME_STAMP time() const { return time_; }
164161
TIME_STAMP interval() const { return interval_; }
165-
TIME_STAMP &intervalRef() { return interval_; }
166-
167-
TIME_STAMP deliveryTime() const;
168162

169163
void triggerTimer();
170164

@@ -220,7 +214,7 @@ class TimersT : public TimersBase
220214

221215
Container & container() { return container_; }
222216

223-
void heapify()
217+
void make_heap()
224218
{
225219
std::make_heap( container_.begin(), container_.end(),
226220
Comparator() );

‎kbe/src/lib/common/timer.inl‎

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void TimersT< TIME_STAMP >::purgeCancelledTimes()
118118
KBE_ASSERT( (numCancelled_ == 0) || (numCancelled_ == 1) );
119119

120120
container.erase( newEnd, container.end() );
121-
timeQueue_.heapify();
121+
timeQueue_.make_heap();
122122
}
123123

124124
template <class TIME_STAMP>
@@ -219,29 +219,6 @@ bool TimersT< TIME_STAMP >::getTimerInfo( TimerHandle handle,
219219
return false;
220220
}
221221

222-
template <class TIME_STAMP>
223-
TIME_STAMP
224-
TimersT< TIME_STAMP >::timerDeliveryTime(TimerHandle handle) const
225-
{
226-
Time * pTime = static_cast< Time * >( handle.time() );
227-
return pTime->deliveryTime();
228-
}
229-
230-
template <class TIME_STAMP>
231-
TIME_STAMP
232-
TimersT< TIME_STAMP >::timerIntervalTime(TimerHandle handle) const
233-
{
234-
Time * pTime = static_cast< Time * >( handle.time() );
235-
return pTime->interval();
236-
}
237-
238-
template <class TIME_STAMP>
239-
TIME_STAMP & TimersT< TIME_STAMP >::timerIntervalTime( TimerHandle handle )
240-
{
241-
Time * pTime = static_cast< Time * >( handle.time() );
242-
return pTime->intervalRef();
243-
}
244-
245222

246223
inline TimeBase::TimeBase(TimersBase & owner, TimerHandler * pHandler, void * pUserData) :
247224
owner_(owner),
@@ -280,13 +257,6 @@ TimersT< TIME_STAMP >::Time::Time( TimersBase & owner,
280257
{
281258
}
282259

283-
template <class TIME_STAMP>
284-
TIME_STAMP TimersT< TIME_STAMP >::Time::deliveryTime() const
285-
{
286-
return this->isExecuting() ? (time_ + interval_) : time_;
287-
}
288-
289-
290260
template <class TIME_STAMP>
291261
void TimersT< TIME_STAMP >::Time::triggerTimer()
292262
{

‎kbe/src/lib/helper/profile_handler.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ CProfileHandler::CProfileHandler(Network::NetworkInterface & networkInterface, u
6969
std::string name, const Network::Address& addr) :
7070
ProfileHandler(networkInterface, timinglen, name, addr)
7171
{
72-
networkInterface_.dispatcher().addFrequentTask(this);
72+
networkInterface_.dispatcher().addTask(this);
7373

7474
ProfileGroup& defaultGroup = ProfileGroup::defaultGroup();
7575
ProfileGroup::PROFILEVALS::const_iterator iter = defaultGroup.profiles().begin();
@@ -95,7 +95,7 @@ ProfileHandler(networkInterface, timinglen, name, addr)
9595
//-------------------------------------------------------------------------------------
9696
CProfileHandler::~CProfileHandler()
9797
{
98-
networkInterface_.dispatcher().cancelFrequentTask(this);
98+
networkInterface_.dispatcher().cancelTask(this);
9999
}
100100

101101
//-------------------------------------------------------------------------------------

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ namespace Network
3333
void DelayedChannels::init(EventDispatcher & dispatcher, NetworkInterface* pNetworkInterface)
3434
{
3535
pNetworkInterface_ = pNetworkInterface;
36-
dispatcher.addFrequentTask( this );
36+
dispatcher.addTask( this );
3737
}
3838

3939
//-------------------------------------------------------------------------------------
4040
void DelayedChannels::fini(EventDispatcher & dispatcher)
4141
{
42-
dispatcher.cancelFrequentTask( this );
42+
dispatcher.cancelTask( this );
4343
}
4444

4545
//-------------------------------------------------------------------------------------

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void ErrorReporter::reportException(Reason reason,
155155
const Address & addr,
156156
const char* prefix)
157157
{
158-
NubException ne(reason, addr);
158+
NetworkException ne(reason, addr);
159159
this->reportException(ne, prefix);
160160

161161
}
@@ -170,7 +170,7 @@ void ErrorReporter::reportException(Reason reason,
170170
* @param prefix any prefix to add to the error message, or NULL if no prefix
171171
*
172172
*/
173-
void ErrorReporter::reportException(const NubException & ne, const char* prefix)
173+
void ErrorReporter::reportException(const NetworkException & ne, const char* prefix)
174174
{
175175
Address offender(0, 0);
176176
ne.getAddress(offender);

‎kbe/src/lib/network/error_reporter.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
2525
#include "common/timer.h"
2626
#include "helper/debug_helper.h"
2727
#include "network/common.h"
28-
#include "network/nub_exception.h"
28+
#include "network/network_exception.h"
2929

3030
namespace KBEngine {
3131
namespace Network
@@ -57,7 +57,7 @@ class ErrorReporter : public TimerHandler
5757
void reportPendingExceptions(bool reportBelowThreshold = false);
5858

5959
private:
60-
void reportException(const NubException & ne, const char * prefix = NULL);
60+
void reportException(const NetworkException & ne, const char * prefix = NULL);
6161

6262
void reportError(const Address & address, const char* format, ...);
6363

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

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ EventDispatcher::EventDispatcher() :
3939
oldSpareTime_(0),
4040
totSpareTime_(0),
4141
lastStatisticsGathered_(0),
42-
pFrequentTasks_(new Tasks),
42+
pTasks_(new Tasks),
4343
pErrorReporter_(NULL),
4444
pTimers_(new Timers64)
4545

@@ -52,7 +52,7 @@ EventDispatcher::EventDispatcher() :
5252
EventDispatcher::~EventDispatcher()
5353
{
5454
SAFE_RELEASE(pErrorReporter_);
55-
SAFE_RELEASE(pFrequentTasks_);
55+
SAFE_RELEASE(pTasks_);
5656
SAFE_RELEASE(pPoller_);
5757

5858
if (!pTimers_->empty())
@@ -132,41 +132,16 @@ void EventDispatcher::clearSpareTime()
132132
pPoller_->clearSpareTime();
133133
}
134134

135-
uint64 EventDispatcher::timerDeliveryTime(TimerHandle handle) const
136-
{
137-
return pTimers_->timerDeliveryTime(handle);
138-
}
139-
140-
//-------------------------------------------------------------------------------------
141-
uint64 EventDispatcher::timerIntervalTime(TimerHandle handle) const
142-
{
143-
return pTimers_->timerIntervalTime(handle);
144-
}
145-
146-
//-------------------------------------------------------------------------------------
147-
uint64 & EventDispatcher::timerIntervalTime(TimerHandle handle)
148-
{
149-
return pTimers_->timerIntervalTime(handle);
150-
}
151-
152-
//-------------------------------------------------------------------------------------
153-
double EventDispatcher::proportionalSpareTime() const
154-
{
155-
double ret = (double)(int64)(totSpareTime_ - oldSpareTime_);
156-
return ret / stampsPerSecondD();
157-
return 0;
158-
}
159-
160135
//-------------------------------------------------------------------------------------
161-
void EventDispatcher::addFrequentTask(Task * pTask)
136+
void EventDispatcher::addTask(Task * pTask)
162137
{
163-
pFrequentTasks_->add(pTask);
138+
pTasks_->add(pTask);
164139
}
165140

166141
//-------------------------------------------------------------------------------------
167-
bool EventDispatcher::cancelFrequentTask(Task * pTask)
142+
bool EventDispatcher::cancelTask(Task * pTask)
168143
{
169-
return pFrequentTasks_->cancel(pTask);
144+
return pTasks_->cancel(pTask);
170145
}
171146

172147
//-------------------------------------------------------------------------------------
@@ -180,21 +155,13 @@ double EventDispatcher::calculateWait() const
180155
pTimers_->nextExp(timestamp()) / stampsPerSecondD());
181156
}
182157

183-
ChildDispatchers::const_iterator iter = childDispatchers_.begin();
184-
185-
while (iter != childDispatchers_.end())
186-
{
187-
maxWait = std::min(maxWait, (*iter)->calculateWait());
188-
++iter;
189-
}
190-
191158
return maxWait;
192159
}
193160

194161
//-------------------------------------------------------------------------------------
195-
void EventDispatcher::processFrequentTasks()
162+
void EventDispatcher::processTasks()
196163
{
197-
pFrequentTasks_->process();
164+
pTasks_->process();
198165
}
199166

200167
//-------------------------------------------------------------------------------------
@@ -224,12 +191,6 @@ int EventDispatcher::processNetwork(bool shouldIdle)
224191

225192
//-------------------------------------------------------------------------------------
226193
void EventDispatcher::processUntilBreak()
227-
{
228-
this->processContinuously();
229-
}
230-
231-
//-------------------------------------------------------------------------------------
232-
void EventDispatcher::processContinuously()
233194
{
234195
if(breakProcessing_ != EVENT_DISPATCHER_STATUS_BREAK_PROCESSING)
235196
breakProcessing_ = EVENT_DISPATCHER_STATUS_RUNNING;
@@ -246,7 +207,7 @@ int EventDispatcher::processOnce(bool shouldIdle)
246207
if(breakProcessing_ != EVENT_DISPATCHER_STATUS_BREAK_PROCESSING)
247208
breakProcessing_ = EVENT_DISPATCHER_STATUS_RUNNING;
248209

249-
this->processFrequentTasks();
210+
this->processTasks();
250211

251212
if(breakProcessing_ != EVENT_DISPATCHER_STATUS_BREAK_PROCESSING){
252213
this->processTimers();

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,17 @@ class EventDispatcher
4747
EventDispatcher();
4848
virtual ~EventDispatcher();
4949

50-
void processContinuously();
5150
int processOnce(bool shouldIdle = false);
5251
void processUntilBreak();
5352

54-
bool isBreakProcessing()const { return breakProcessing_ == EVENT_DISPATCHER_STATUS_BREAK_PROCESSING; }
55-
bool isWaitBreakProcessing()const { return breakProcessing_ == EVENT_DISPATCHER_STATUS_WAITING_BREAK_PROCESSING; }
53+
INLINE bool hasBreakProcessing()const;
54+
INLINE bool waitingBreakProcessing()const;
5655

5756
void breakProcessing(bool breakState = true);
5857
INLINE void setWaitBreakProcessing();
59-
bool processingBroken()const;
6058

61-
void addFrequentTask(Task * pTask);
62-
bool cancelFrequentTask(Task * pTask);
59+
void addTask(Task * pTask);
60+
bool cancelTask(Task * pTask);
6361

6462
INLINE double maxWait() const;
6563
INLINE void maxWait(double seconds);
@@ -71,16 +69,9 @@ class EventDispatcher
7169

7270
INLINE TimerHandle addTimer(int64 microseconds,
7371
TimerHandler * handler, void* arg = NULL);
74-
INLINE TimerHandle addOnceOffTimer(int64 microseconds,
75-
TimerHandler * handler, void * arg = NULL);
7672

77-
uint64 timerDeliveryTime(TimerHandle handle) const;
78-
uint64 timerIntervalTime(TimerHandle handle) const;
79-
uint64 & timerIntervalTime(TimerHandle handle);
80-
8173
uint64 getSpareTime() const;
8274
void clearSpareTime();
83-
double proportionalSpareTime() const;
8475

8576
ErrorReporter & errorReporter() { return *pErrorReporter_; }
8677

@@ -94,18 +85,12 @@ class EventDispatcher
9485
void * arg,
9586
bool recurrent);
9687

97-
void processFrequentTasks();
88+
void processTasks();
9889
void processTimers();
9990
void processStats();
10091

10192
double calculateWait() const;
10293

103-
void attachTo(EventDispatcher & parentDispatcher);
104-
void detachFrom(EventDispatcher & parentDispatcher);
105-
106-
typedef std::vector<EventDispatcher *> ChildDispatchers;
107-
ChildDispatchers childDispatchers_;
108-
10994
protected:
11095
int8 breakProcessing_;
11196

@@ -118,7 +103,7 @@ class EventDispatcher
118103
TimeStamp totSpareTime_;
119104
TimeStamp lastStatisticsGathered_;
120105

121-
Tasks* pFrequentTasks_;
106+
Tasks* pTasks_;
122107
ErrorReporter * pErrorReporter_;
123108
Timers64* pTimers_;
124109
EventPoller* pPoller_;

0 commit comments

Comments
 (0)