int connectionReceiveCommand(connection *con) { int socketFD = con->tArgs->socketFD; command *cmd = con->cmd; error *err = con->err; // if (cmd->args != NULL) { // free(cmd->args); // cmd->args = NULL; // } destroyCommandArgs(cmd); printf("\n>====== Waiting to receive command from client...\n"); char buf[BUFSIZE]; memset(buf, 0, BUFSIZE); int term; if (messageReceive(socketFD, buf, &(con->dataBytes), &(con->data), &term)) { if (term) { ERROR(err, E_EXIT); } else { ERROR(err, E_MSG); } return 1; } return parseCommand(buf, cmd, err); }
PTR_MESSAGE gameQuestionAnswered(int myID, int socket, int seconds, int answer) { PTR_MESSAGE msg; struct timeval startzeit; struct timeval endzeit; unsigned long points; gettimeofday(&startzeit, 0); msg = messageReceive(QuestionAnswered, socket, seconds); if(msg == NULL) { syslogWriteFormatDebug(ldebug, "Could not receive QuestionAnswered message."); return NULL; } else if(msg == (PTR_MESSAGE) -1) { syslogWriteFormatDebug(ldebug, "Client %i timed out.", myID); return NULL; } gettimeofday(&endzeit,0); if((endzeit.tv_sec > (startzeit.tv_sec + seconds)) || ((endzeit.tv_sec == (startzeit.tv_sec + seconds)) && (endzeit.tv_usec > startzeit.tv_usec))) { free(msg); return NULL; } else { if((uint8_t) msg->content[0] == (uint8_t) answer) { points = 0; if(1000 * seconds - (endzeit.tv_sec * 1000 + endzeit.tv_usec / 1000 - startzeit.tv_sec * 1000 - startzeit.tv_usec / 1000) > 0) { points = scoreForTimeLeft(1000 * seconds - (endzeit.tv_sec * 1000 + endzeit.tv_usec / 1000 - startzeit.tv_sec * 1000 - startzeit.tv_usec / 1000), 1000 * seconds); } if(points != 0) { syslogWriteFormatDebug(ldebug,"Points have changed. Adjusting points in the memory now."); userdataLock(); userdataSetPoints(myID, userdataGetPoints(myID) + (int) points); userdataUnlock(); if(gameBroadcast((PTR_MESSAGE) userdataGetList()) == false) { syslogWriteFormatDebug(lerror, "Could not send the PlayerList after the points changed"); } } } return msg; } }
HRESULT gameSendCatalogs(int myID, int socket) { int numCatalogs; int i; PTR_MESSAGE msg; syslogWriteFormatDebug(ldebug, "Player with myID: %i entering gameSendCatalogs.", myID); // wait infinitely to check for closed sockets msg = messageReceive(CatalogRequest, socket, -1); if(msg == NULL) { syslogWriteFormatDebug(lerror, "No CatalogRequest message received, Player %i leaves the server.", myID); return ERROR; } syslogWriteFormatDebug(ldebug, "received catalogrequest message: type: %i length: %i", msg->type, msg->length); free(msg); numCatalogs = CatalogGetNumCatalogs(); syslogWriteFormatDebug(ldebug, "Number of Catalogs: %i", numCatalogs); for(i = 0; i < numCatalogs; i++) { syslogWriteFormatDebug(ldebug, "CatalogFilename number %i: %s", i, (char *) CatalogGetFilename(i)); if(messageFS(CatalogResponse, -1, (char *) CatalogGetFilename(i), socket) == false) { syslogWriteFormatDebug(lerror, "Could not send CatalogResponse message, Player %i leaves the server.", myID); return ERROR; } } if(messageFS(CatalogResponse, -1, "", socket) == false) { syslogWriteFormatDebug(lerror, "Could not send empty CatalogResponse message, Player %i leaves the server.", myID); return ERROR; } return OK; }
HRESULT gameGamemaster(int myID, int socket, char* currentCatalog, int* catalogLength, int* actualQuestion) { PTR_MESSAGE msg; if(gameSendCatalogs(myID, socket) == ERROR) { return ERROR; } msg = NULL; do { Next: // waiting infinitely here, to determine if the socket was closed msg = messageReceive(0, socket, -1); if(msg != NULL) { if(msg->type == CatalogChange) { memset(currentCatalog, '\0', 400); memcpy(currentCatalog, msg->content, msg->length); *catalogLength = msg->length; gameBroadcast(msg); CatalogClose(); if(CatalogLoad(currentCatalog) == false) { if(messageFS(ErrorWarning, -1, "0Game could not be started yet Catalog could not be loaded", socket) == false) { syslogWriteFormatDebug(lerror, "Could not send the ErrorWarning to the GameMaster to tell him the game could not be started."); free(msg); return ERROR; } free(msg); return ERROR; } free(msg); } else if(msg->type == StartGame) { if(userdataCount() < 2) { if(messageFS(ErrorWarning, -1, "0Game could not be started yet too less players", socket) == false) { syslogWriteFormatDebug(lerror, "Could not send the ErrorWarning to the GameMaster to tell him the game could not be started."); free(msg); return ERROR; } free(msg); goto Next; } else { if(gameBroadcast(msg) == false) { if(messageFS(ErrorWarning, -1, "0Game could not be started yet", socket) == false) { syslogWriteFormatDebug(lerror, "Could not send the ErrorWarning to the GameMaster to tell him the game could not be started."); free(msg); return ERROR; } } CatalogClose(); if(CatalogLoad(currentCatalog) == false) { if(messageFS(ErrorWarning, -1, "0Game could not be started yet Catalog could not be loaded", socket) == false) { syslogWriteFormatDebug(lerror, "Could not send the ErrorWarning to the GameMaster to tell him the game could not be started."); free(msg); return ERROR; } free(msg); return ERROR; } free(msg); userdataLock(); userdataSetStage(STAGE_GAME); userdataUnlock(); break; } } } else { syslogWriteFormatDebug(ldebug, "Player %i timed out", myID); return ERROR; } } while(1); if(gamePlay(myID, socket, actualQuestion) == ERROR) { return ERROR; } return OK; }
HRESULT gamePlay(int myID, int socket, int* actualQuestion) { PTR_MESSAGE msg; int numQuest; uint16_t seconds; int answer; uint8_t selection; uint8_t result[2]; Question* startofquestion; syslogWriteFormatDebug(ldebug, "Player %i entered gamePlay.", myID); if(myID == 0 && gameBroadcast((PTR_MESSAGE) userdataGetList()) == false) { syslogWriteFormatDebug(lerror, "Could not send the PlayerList as the game starts"); } do { msg = NULL; do { // wait infinitely to check for closed sockets msg = messageReceive(0, socket, -1); if(msg == NULL) { syslogWriteFormatDebug(lerror, "Could not receive QuestionRequest as expected, might be a timeout."); return ERROR; } else if(msg->type == CatalogChange) { syslogWriteFormatDebug(lwarn, "Received CatalogChange where it is not expected."); free(msg); } else if(msg->type == QuestionRequest) { while(userdataGetStage() != STAGE_GAME) { usleep(10000); } break; } } while(1 == 1); numQuest = CatalogGetCatalogByteSize() / sizeof(Question); if(*actualQuestion < numQuest) { if(messageFS(QUestion, sizeof(QUESTION) - 3, (char*) (CatalogGetCatalogPtr() + sizeof(Question) * *actualQuestion), socket) == false) { syslogWriteFormatDebug(lerror, "Could not send Question %s to player %i.", CatalogGetCatalogPtr(), myID); return ERROR; } (*actualQuestion)++; } else { syslogWriteFormatDebug(lerror, "Something regarding Catalog and Question answered went wrong."); return ERROR; } startofquestion = (Question*) (CatalogGetCatalogPtr() + sizeof(Question) * (*actualQuestion-1)); seconds = (uint16_t) startofquestion->timeout; answer = (int) startofquestion->correct; msg = gameQuestionAnswered(myID, socket, seconds, answer); if(msg == NULL) { selection = 255; } else { selection = (uint8_t) msg->content[0]; } free(msg); result[0] = selection; result[1] = (uint8_t) answer; if(messageFS(QuestionResult, 2, (char*) result, socket) == false) { syslogWriteFormatDebug(lerror, "Could not send QuestionResult to player %i selection %i answer %i.", myID, selection, answer); return ERROR; } syslogWriteFormatDebug(ldebug, "Question Player %i is working at: %i, total number of Questions: %i", myID, *actualQuestion, numQuest); if (*actualQuestion == numQuest) { if(messageFS(QUestion, -1, "", socket) == false) { syslogWriteFormatDebug(lerror, "Could not send EmptyQuestion to player %i.", myID); return ERROR; } } } while(*actualQuestion < numQuest); return OK; }