@@ -31,15 +31,17 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
3131namespace KBEngine {
3232
3333// -------------------------------------------------------------------------------------
34- RestoreEntityHandler::RestoreEntityHandler (Mercury::NetworkInterface & networkInterface):
34+ RestoreEntityHandler::RestoreEntityHandler (COMPONENT_ID cellappID, Mercury::NetworkInterface & networkInterface):
3535Task (),
3636networkInterface_ (networkInterface),
3737entities_ (),
3838inProcess_ (false ),
3939restoreSpaces_ (),
40+ otherRestoredSpaces_ (),
4041broadcastOtherBaseapps_ (false ),
4142tickReport_ (0 ),
42- spaceIDs_ ()
43+ spaceIDs_ (),
44+ cellappID_ (cellappID)
4345{
4446 // networkInterface_.mainDispatcher().addFrequentTask(this);
4547}
@@ -50,8 +52,8 @@ RestoreEntityHandler::~RestoreEntityHandler()
5052 if (inProcess_)
5153 networkInterface_.mainDispatcher ().cancelFrequentTask (this );
5254
53- std::vector<RestoreData>::iterator restoreSpacesIter = ohterRestoredSpaces_ .begin ();
54- for (; restoreSpacesIter != ohterRestoredSpaces_ .end (); restoreSpacesIter++)
55+ std::vector<RestoreData>::iterator restoreSpacesIter = otherRestoredSpaces_ .begin ();
56+ for (; restoreSpacesIter != otherRestoredSpaces_ .end (); restoreSpacesIter++)
5557 {
5658 if ((*restoreSpacesIter).cell )
5759 {
@@ -60,7 +62,7 @@ RestoreEntityHandler::~RestoreEntityHandler()
6062 }
6163 }
6264
63- DEBUG_MSG (" RestoreEntityHandler::~RestoreEntityHandler()\n " );
65+ DEBUG_MSG (boost::format ( " RestoreEntityHandler::~RestoreEntityHandler(%1% )\n " ) % cellappID_ );
6466}
6567
6668// -------------------------------------------------------------------------------------
@@ -131,8 +133,8 @@ bool RestoreEntityHandler::process()
131133 if (timestamp () - tickReport_ > uint64 ( 3 * stampsPerSecond () ))
132134 {
133135 tickReport_ = timestamp ();
134- INFO_MSG (boost::format (" RestoreEntityHandler::process(): wait for localSpace to get cell!, entitiesSize(%1%), spaceSize=%2%\n " ) %
135- entities_.size () % restoreSpaces_.size ());
136+ INFO_MSG (boost::format (" RestoreEntityHandler::process(%3% ): wait for localSpace to get cell!, entitiesSize(%1%), spaceSize=%2%\n " ) %
137+ entities_.size () % restoreSpaces_.size () % cellappID_ );
136138 }
137139
138140 int spaceCellCount = 0 ;
@@ -174,7 +176,7 @@ bool RestoreEntityHandler::process()
174176 }
175177 else
176178 {
177- ERROR_MSG (boost::format (" RestoreEntityHandler::process(): lose space(%1 %).\n " ) % (*restoreSpacesIter).id );
179+ ERROR_MSG (boost::format (" RestoreEntityHandler::process(%1% ): lose space(%2 %).\n " ) % cellappID_ % (*restoreSpacesIter).id );
178180 }
179181 }
180182
@@ -186,7 +188,7 @@ bool RestoreEntityHandler::process()
186188 {
187189 broadcastOtherBaseapps_ = true ;
188190
189- INFO_MSG (" RestoreEntityHandler::process(): begin broadcast-spaceGetCell to otherBaseapps...\n " );
191+ INFO_MSG (boost::format ( " RestoreEntityHandler::process(%1% ): begin broadcast-spaceGetCell to otherBaseapps...\n " ) % cellappID_ );
190192
191193 std::vector<RestoreData>::iterator restoreSpacesIter = restoreSpaces_.begin ();
192194 for (; restoreSpacesIter != restoreSpaces_.end (); restoreSpacesIter++)
@@ -216,8 +218,8 @@ bool RestoreEntityHandler::process()
216218
217219 if (pChannel)
218220 {
219- INFO_MSG (boost::format (" RestoreEntityHandler::process(): broadcast baseapp[%1%, %2%], spaceID[%3%], utype[%4%]...\n " ) %
220- (*comsiter).cid % pChannel->c_str () % spaceID % utype);
221+ INFO_MSG (boost::format (" RestoreEntityHandler::process(%5% ): broadcast baseapp[%1%, %2%], spaceID[%3%], utype[%4%]...\n " ) %
222+ (*comsiter).cid % pChannel->c_str () % spaceID % utype % cellappID_ );
221223
222224 Mercury::Bundle* pBundle = Mercury::Bundle::ObjPool ().createObject ();
223225 (*pBundle).newMessage (BaseappInterface::onRestoreSpaceCellFromOtherBaseapp);
@@ -235,8 +237,8 @@ bool RestoreEntityHandler::process()
235237 if (timestamp () - tickReport_ > uint64 ( 3 * stampsPerSecond () ))
236238 {
237239 tickReport_ = timestamp ();
238- INFO_MSG (boost::format (" RestoreEntityHandler::process(): wait for otherBaseappSpaces to get cell!, entitiesSize(%1 %), spaceSize=%2 %\n " ) %
239- entities_.size () % spaceIDs_.size ());
240+ INFO_MSG (boost::format (" RestoreEntityHandler::process(%1% ): wait for otherBaseappSpaces to get cell!, entitiesSize(%2 %), spaceSize=%3 %\n " ) %
241+ cellappID_ % entities_.size () % spaceIDs_.size ());
240242 }
241243
242244 return true ;
@@ -286,8 +288,8 @@ bool RestoreEntityHandler::process()
286288
287289 if (cellMailbox == NULL )
288290 {
289- restoreSpacesIter = ohterRestoredSpaces_ .begin ();
290- for (; restoreSpacesIter != ohterRestoredSpaces_ .end (); restoreSpacesIter++)
291+ restoreSpacesIter = otherRestoredSpaces_ .begin ();
292+ for (; restoreSpacesIter != otherRestoredSpaces_ .end (); restoreSpacesIter++)
291293 {
292294 if (pBase->getSpaceID () == (*restoreSpacesIter).spaceID && (*restoreSpacesIter).cell )
293295 {
@@ -303,8 +305,15 @@ bool RestoreEntityHandler::process()
303305 }
304306 else
305307 {
308+ ENTITY_ID delID = pBase->getID ();
309+
306310 pBase->destroy ();
307- iter = entities_.erase (iter);
311+ WARNING_MSG (boost::format (" RestoreEntityHandler::process(%1%): not fount spaceCell, killed base(%2%)!" )
312+ % cellappID_ % delID);
313+
314+ if (Baseapp::getSingleton ().findEntity (delID) == NULL )
315+ iter = entities_.erase (iter);
316+
308317 continue ;
309318 }
310319 }
@@ -320,8 +329,8 @@ bool RestoreEntityHandler::process()
320329
321330 if (entities_.size () == 0 )
322331 {
323- std::vector<RestoreData>::iterator restoreSpacesIter = ohterRestoredSpaces_ .begin ();
324- for (; restoreSpacesIter != ohterRestoredSpaces_ .end (); restoreSpacesIter++)
332+ std::vector<RestoreData>::iterator restoreSpacesIter = otherRestoredSpaces_ .begin ();
333+ for (; restoreSpacesIter != otherRestoredSpaces_ .end (); restoreSpacesIter++)
325334 {
326335 if ((*restoreSpacesIter).cell )
327336 {
@@ -330,7 +339,7 @@ bool RestoreEntityHandler::process()
330339 }
331340 }
332341
333- ohterRestoredSpaces_ .clear ();
342+ otherRestoredSpaces_ .clear ();
334343
335344 inProcess_ = false ;
336345 Baseapp::getSingleton ().onRestoreEntitiesOver (this );
@@ -344,7 +353,10 @@ bool RestoreEntityHandler::process()
344353void RestoreEntityHandler::onRestoreSpaceCellFromOtherBaseapp (COMPONENT_ID baseappID, COMPONENT_ID cellappID,
345354 SPACE_ID spaceID, ENTITY_ID spaceEntityID, ENTITY_SCRIPT_UID utype, bool destroyed)
346355{
356+ SIZE_T oldsize = spaceIDs_.size ();
347357 spaceIDs_.erase (std::remove (spaceIDs_.begin (), spaceIDs_.end (), spaceID), spaceIDs_.end ());
358+ if (oldsize == spaceIDs_.size ())
359+ return ;
348360
349361 RestoreData data;
350362 data.id = spaceEntityID;
@@ -358,7 +370,7 @@ void RestoreEntityHandler::onRestoreSpaceCellFromOtherBaseapp(COMPONENT_ID basea
358370 ScriptDefModule* sm = EntityDef::findScriptModule (utype);
359371 if (sm == NULL )
360372 {
361- ERROR_MSG (boost::format (" RestoreEntityHandler::onRestoreSpaceCellFromOtherBaseapp: not found utype %1 %!\n " ) % utype);
373+ ERROR_MSG (boost::format (" RestoreEntityHandler::onRestoreSpaceCellFromOtherBaseapp(%1%) : not found utype %2 %!\n " ) % cellappID_ % utype);
362374 }
363375 else
364376 {
@@ -367,7 +379,7 @@ void RestoreEntityHandler::onRestoreSpaceCellFromOtherBaseapp(COMPONENT_ID basea
367379 }
368380 }
369381
370- ohterRestoredSpaces_ .push_back (data);
382+ otherRestoredSpaces_ .push_back (data);
371383}
372384
373385// -------------------------------------------------------------------------------------
0 commit comments