Added a bunch of NULL pointer guards, changed undertake to cleanup dead units more safely and added more debug tracing

This commit is contained in:
Mark Vejvoda 2010-04-20 02:19:37 +00:00
parent 071b320940
commit d412cb5f79
7 changed files with 75 additions and 107 deletions

View File

@ -340,6 +340,12 @@ void Commander::giveNetworkCommand(NetworkCommand* networkCommand) const {
Command* Commander::buildCommand(const NetworkCommand* networkCommand) const{
assert(networkCommand->getNetworkCommandType()==nctGiveCommand);
if(world == NULL) {
char szBuf[1024]="";
sprintf(szBuf,"In [%s::%s Line: %d] world == NULL for unit with id: %d",__FILE__,__FUNCTION__,__LINE__,networkCommand->getUnitId());
throw runtime_error(szBuf);
}
Unit* target= NULL;
const CommandType* ct= NULL;
const Unit* unit= world->findUnitById(networkCommand->getUnitId());
@ -347,9 +353,17 @@ Command* Commander::buildCommand(const NetworkCommand* networkCommand) const{
//validate unit
if(unit == NULL) {
char szBuf[1024]="";
sprintf(szBuf,"In [%s::%s - %d] Can not find unit with id: %d. Game out of synch.",
__FILE__,__FUNCTION__,__LINE__,networkCommand->getUnitId());
sprintf(szBuf,"In [%s::%s - %d] Can not find unit with id: %d. Game out of synch.",__FILE__,__FUNCTION__,__LINE__,networkCommand->getUnitId());
throw runtime_error(szBuf);
}
else if(unit->getType() == NULL) {
char szBuf[1024]="";
sprintf(szBuf,"In [%s::%s - %d] unit->getType() == NULL for unit with id: %d",__FILE__,__FUNCTION__,__LINE__,networkCommand->getUnitId());
throw runtime_error(szBuf);
}
else if(unit->getFaction() == NULL) {
char szBuf[1024]="";
sprintf(szBuf,"In [%s::%s - %d] unit->getFaction() == NULL for unit with id: %d",__FILE__,__FUNCTION__,__LINE__,networkCommand->getUnitId());
throw runtime_error(szBuf);
}
@ -358,10 +372,9 @@ Command* Commander::buildCommand(const NetworkCommand* networkCommand) const{
//validate command type
if(ct == NULL) {
char szBuf[1024]="";
sprintf(szBuf,"In [%s::%s - %d] Can not find command type with id = %d in unit = %d [%s][%s]. Game out of synch.",
__FILE__,__FUNCTION__,__LINE__,networkCommand->getCommandTypeId(),unit->getId(), unit->getFullName().c_str(),unit->getDesc().c_str());
sprintf(szBuf,"In [%s::%s - %d] Can not find command type with id = %d\n%s\n in unit = %d [%s][%s].\nGame out of synch.",
__FILE__,__FUNCTION__,__LINE__,networkCommand->getCommandTypeId(),unit->getType()->getCommandTypeListDesc().c_str(),unit->getId(), unit->getFullName().c_str(),unit->getDesc().c_str());
throw runtime_error(szBuf);
}
@ -371,7 +384,8 @@ Command* Commander::buildCommand(const NetworkCommand* networkCommand) const{
if (ct->getClass() == ccBuild) {
assert(networkCommand->getTargetId() >= 0 && networkCommand->getTargetId() < 4);
facing = CardinalDir(networkCommand->getTargetId());
} else if (networkCommand->getTargetId() != Unit::invalidId ) {
}
else if (networkCommand->getTargetId() != Unit::invalidId ) {
target= world->findUnitById(networkCommand->getTargetId());
}

View File

@ -61,10 +61,10 @@ NetworkMessageType NetworkInterface::getNextMessageType(bool checkHasDataFirst)
int iPeek = socket->peek(&messageType, sizeof(messageType));
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] socket->getDataToRead() iPeek = %d, messageType = %d\n",__FILE__,__FUNCTION__,iPeek,messageType);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] socket->getDataToRead() iPeek = %d, messageType = %d [size = %d]\n",__FILE__,__FUNCTION__,iPeek,messageType,sizeof(messageType));
}
else {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] PEEK WARNING, socket->getDataToRead() messageType = %d, dataSize = %d\n",__FILE__,__FUNCTION__,messageType,dataSize);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] PEEK WARNING, socket->getDataToRead() messageType = %d [size = %d], dataSize = %d, checkHasDataFirst = %d\n",__FILE__,__FUNCTION__,messageType,sizeof(messageType),dataSize,checkHasDataFirst);
}
//sanity check new message type

View File

@ -601,7 +601,10 @@ void Unit::kill(){
clearCommands();
}
void Unit::undertake(){
void Unit::undertake() {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to undertake unit id = %d [%s] [%s]\n",
__FILE__,__FUNCTION__,__LINE__,this->id, this->getFullName().c_str(),this->getDesc().c_str());
livingUnits.erase(id);
livingUnitsp.erase(this);
faction->removeUnit(this);
@ -1141,83 +1144,5 @@ void Unit::startDamageParticles(){
}
}
#if 0
bool Unit::getCellMapCell(int x, int y) const {
const UnitType *ut= getType();
if(allowRotateUnits == true && ut != NULL && rotateAmount > 0) {
return cellMap[ut->getSize() * y + x];
}
else if(ut != NULL) {
return ut->getCellMapCell(x,y);
}
else {
throw runtime_error("ut == NULL in Unit::getCellMapCell()!");
}
}
void Unit::setRotateAmount(float value) {
if(allowRotateUnits == true) {
rotateAmount = value;
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unit id = %d rotate amount = %f cellMap = %s\n",__FILE__,__FUNCTION__,__LINE__, getId(), rotateAmount,(cellMap == NULL ? "NULL" : "Valid"));
const UnitType *ut= getType();
if(ut != NULL && ut->hasCellMap() == true) {
int matrixSize = ut->getSize();
if(rotateAmount > 0) {
if(cellMap == NULL) delete [] cellMap;
cellMap = new bool[matrixSize * matrixSize];
for(int iRow = 0; iRow < matrixSize; ++iRow) {
for(int iCol = 0; iCol < matrixSize; ++iCol) {
bool getCellResult = ut->getCellMapCell(iCol, iRow);
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] [%d,%d] = %d\n",__FILE__,__FUNCTION__,iRow,iCol,getCellResult);
int newRow = 0;
int newCol = 0;
switch((int)rotateAmount)
{
case 90:
newRow = (matrixSize - iCol - 1);
newCol = iRow;
break;
case 180:
newRow = (matrixSize - iRow - 1);
newCol = (matrixSize - iCol - 1);
break;
case 270:
newRow = iCol;
newCol = (matrixSize - iRow - 1);
break;
}
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] ABOUT TO Transform to [%d,%d] = %d\n",__FILE__,__FUNCTION__,newRow,newCol,getCellResult);
// bool getCellMapCell(int x, int y) const {return cellMap[size*y+x];}
// cellMap[i*size+j]= row[j]=='0'? false: true;
cellMap[matrixSize * newRow + newCol] = getCellResult;
}
}
}
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Transformed matrix below:\n",__FILE__,__FUNCTION__);
/*
for(int iRow = 0; iRow < matrixSize; ++iRow) {
for(int iCol = 0; iCol < matrixSize; ++iCol) {
bool getCellResult = ut->getCellMapCell(iCol, iRow);
bool getCellResultRotated = getCellMapCell(iRow, iCol);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] matrix [%d,%d] = %d, rotated = %d\n",__FILE__,__FUNCTION__,iRow,iCol,getCellResult,getCellResultRotated);
}
}
*/
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unit id = %d rotate amount = %f\n",__FILE__,__FUNCTION__,__LINE__, getId(),rotateAmount);
}
}
#endif
}}//end namespace

View File

@ -569,4 +569,14 @@ const CommandType* UnitType::findCommandTypeById(int id) const{
return NULL;
}
string UnitType::getCommandTypeListDesc() const {
string desc = "Commands: ";
for(int i=0; i<getCommandTypeCount(); ++i){
const CommandType* commandType= getCommandType(i);
desc += " id = " + intToStr(commandType->getId()); + " toString: " + commandType->toString();
}
return desc;
}
}}//end namespace

View File

@ -184,6 +184,7 @@ public:
//find
const CommandType* findCommandTypeById(int id) const;
string getCommandTypeListDesc() const;
float getRotatedBuildPos() { return rotatedBuildPos; }
float setRotatedBuildPos(float value) { rotatedBuildPos = value; }

View File

@ -135,12 +135,13 @@ void World::update(){
//undertake the dead
for(int i=0; i<getFactionCount(); ++i){
for(int j=0; j<getFaction(i)->getUnitCount(); ++j){
int unitCount = getFaction(i)->getUnitCount();
for(int j= unitCount - 1; j >= 0; j--){
Unit *unit= getFaction(i)->getUnit(j);
if(unit->getToBeUndertaken()){
if(unit->getToBeUndertaken()) {
unit->undertake();
delete unit;
j--;
//j--;
}
}
}

View File

@ -833,22 +833,39 @@ int Socket::getDataToRead(){
//if(retval)
if(isSocketValid() == true)
{
/* ioctl isn't posix, but the following seems to work on all modern
* unixes */
#ifndef WIN32
int err = ioctl(sock, FIONREAD, &size);
#else
int err= ioctlsocket(sock, FIONREAD, &size);
#endif
if(err < 0 && getLastSocketError() != PLATFORM_SOCKET_TRY_AGAIN)
{
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] ERROR PEEKING SOCKET DATA, err = %d %s\n",__FILE__,__FUNCTION__,err,getLastSocketErrorFormattedText().c_str());
//throwException(szBuf);
}
else if(err == 0)
{
//if(Socket::enableNetworkDebugInfo) printf("In [%s] ioctl returned = %d, size = %ld\n",__FUNCTION__,err,size);
}
int loopCount = 1;
for(time_t elapsed = time(NULL); difftime(time(NULL),elapsed) < 1;) {
/* ioctl isn't posix, but the following seems to work on all modern
* unixes */
#ifndef WIN32
int err = ioctl(sock, FIONREAD, &size);
#else
int err= ioctlsocket(sock, FIONREAD, &size);
#endif
if(err < 0 && getLastSocketError() != PLATFORM_SOCKET_TRY_AGAIN)
{
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] ERROR PEEKING SOCKET DATA, err = %d %s\n",__FILE__,__FUNCTION__,err,getLastSocketErrorFormattedText().c_str());
break;
//throwException(szBuf);
}
else if(err == 0)
{
//if(Socket::enableNetworkDebugInfo) printf("In [%s] ioctl returned = %d, size = %ld\n",__FUNCTION__,err,size);
}
if(size > 0) {
break;
}
else if(hasDataToRead() == true) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING PEEKING SOCKET DATA, (hasDataToRead() == true) err = %d, sock = %d, size = %u, loopCount = %d\n",__FILE__,__FUNCTION__,__LINE__,err,sock,size,loopCount);
}
else {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING PEEKING SOCKET DATA, err = %d, sock = %d, size = %u, loopCount = %d\n",__FILE__,__FUNCTION__,__LINE__,err,sock,size,loopCount);
break;
}
loopCount++;
}
}
return static_cast<int>(size);