void doCashierStuff(int mySSN, int* cash){ int socialSecurityNum = mySSN; //int sizeOfInt = sizeof(int); //First get in line with a generic method //cout<<"Customer #"<<socialSecurityNum<<" getting in cashier line\n"; int myLine = getInLine(&cashier,socialSecurityNum,cash); //Enter interaction monitor with passport clerk Lock* workLock = &cashier.clerkLock[myLine]; //int workLock = *(cashier.clerkLock + (myLine * sizeOfInt)); Condition* workCV = &cashier.clerkCV[myLine]; //int workCV = *(cashier.clerkCV + (myLine * sizeOfInt)); workLock->Acquire(); //Acquire(workLock); //Tell Clerk CV, then wait cout<<"Customer #"<<socialSecurityNum<<" has given SSN "<<socialSecurityNum<<" to Cashier #"<<myLine<<"\n"; tellCashierSSN(mySSN,myLine); workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); //Decide weather to self-punish bool readyToPay = cashierChecked[mySSN]; //int readyToPay = *(cashierChecked + (mySSN * sizeOfInt)); if(!readyToPay/*readyToPay == 0*/) { //Release, punish, and leave cout<<"Customer #"<<socialSecurityNum<<" has gone to Cashier #"<<myLine<<" too soon. "<< "They are going to the back of the line.\n"; workCV->Signal(workLock); //Signal(workCV, workLock); workLock->Release(); //Release(workLock); punish(punishTime); return; } //Now you can pay cout<<"Customer #"<<socialSecurityNum<<" has given Cashier #"<<myLine<<" $100\n"; payCashier(mySSN,cash); workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); //Now you've been woken up because you have the passport, so leave //cout<<"Customer #"<<socialSecurityNum<<" got passport and is leaving cashier #"<<myLine<<"\n"; workCV->Signal(workLock); //Signal(workCV, workLock); workLock->Release(); //Release(workLock); return; }
int main() { SetConsoleTitle("LogIn Server for Knight Online v" STRINGIFY(__VERSION)" Max Online :" STRINGIFY(MAX_USER)); // Override the console handler SetConsoleCtrlHandler(_ConsoleHandler, TRUE); HookSignals(&s_hEvent); g_pMain = new LoginServer(); // Startup server if (g_pMain->Startup()) { printf("\nServer started up successfully!\n\n"); // Wait until console's signaled as closing s_hEvent.Wait(); } else { system("pause"); } printf("Server shutting down, please wait...\n"); g_bRunning = false; delete g_pMain; UnhookSignals(); return 0; }
void passportClerk(int id) { int myLineID = id; //set ID bool firstTime = true; //int firstTime = 1; bool ifBribed; //int ifBribed; //int sizeOfInt = sizeof(int); while(true) { //Wait fot the next cust to signal ifBribed = waitForLine(&passPClerk, id, firstTime); //Set up some convenient variables Lock *workLock = &passPClerk.clerkLock[myLineID]; //int workLock = *(passPClerk.clerkLock + (myLineID * sizeOfInt)); Condition *workCV = &passPClerk.clerkCV[myLineID]; //int workCV = *(passPClerk.clerkCV + (myLineID * sizeOfInt)); //Now the clerk has been woken up and has been told the customer ID //Check int customerSSN = passportClerkCurrentCustomer[myLineID]; //int customerSSN = *(passportClerkCurrentCustomer + (myLineID * sizeOfInt)); cout<<"PassportClerk #" << id << " has received SSN " << customerSSN << " from Customer #" << customerSSN << ".\n"; passportClerkChecked[customerSSN] = customersWithCompletedApps[customerSSN] && customersWithCompletedPics[customerSSN]; //*(passportClerkChecked + (customerSSN * sizeOfInt)) = *(customersWithCompletedApps + (customerSSN * sizeOfInt)) && *(customersWithCompletedPics + (customerSSN * sizeOfInt)); if(!passportClerkChecked[customerSSN]/* *(passportClerkChecked (customerSSN + sizeOfInt)) == 0 */){ cout<<"PassportClerk #"<<id<<" has determined that Customer #"<<customerSSN<< " does not have both their application and picture completed\n"; } else{ cout<<"PassportClerk #"<<id<<" has determined that Customer #"<<customerSSN<< " has both their application and picture completed\n"; } //And Signal workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); //Now customer is gone if(passportClerkChecked[customerSSN]/* *(passportClerkChecked + (customerSSN * sizeOfInt)) == 1 */) { cout << "PassportClerk #" << id << " has recorded Customer #" << customerSSN << " passport documentation\n"; } if(ifBribed /*ifBribed == 1*/) { cout<<"PassportClerk #" << id << " has received $500 from Customer #" << customerSSN << ".\n"; passPClerk.cashReceived+=500; } firstTime = false; //firstTime = 0; workLock->Release(); //Release(workLock); } }
int main() { SetConsoleTitle("Game Server for Knight Online v" STRINGIFY(__VERSION)); #ifdef WIN32 // Override the console handler SetConsoleCtrlHandler(_ConsoleHandler, TRUE); #endif HookSignals(&s_hEvent); // Start up the time updater thread StartTimeThread(); // Start up the console input thread StartConsoleInputThread(); g_pMain = new CGameServerDlg(); // Start up server if (g_pMain->Startup()) { printf("\nServer started up successfully!\n"); // Wait until console's signaled as closing s_hEvent.Wait(); } else { #ifdef WIN32 system("pause"); #endif } printf("Server shutting down, please wait...\n"); // This seems redundant, but it's not. // We still have the destructor for the dialog instance, which allows time for threads to properly cleanup. g_bRunning = false; delete g_pMain; CleanupTimeThread(); CleanupConsoleInputThread(); UnhookSignals(); return 0; }
void doPassportClerkStuff(int socialSecurityNum,int*cash){ int mySSN = socialSecurityNum; //int sizeOfInt = sizeof(int); //First get in line with a generic method //cout<<"Customer #"<<socialSecurityNum<<" getting in passport Line\n"; int myLine = getInLine(&passPClerk,socialSecurityNum,cash); //Enter interaction monitor with passport clerk Lock* workLock = &passPClerk.clerkLock[myLine]; //int workLock = *(passPClerk.clerkLock + (myLine * sizeOfInt)); Condition* workCV = &passPClerk.clerkCV[myLine]; //int workCV = *(passPClerk.clerkCV + (myLine * sizeOfInt)); workLock->Acquire(); //Acquire(workLock); //Tell Clerk CV, then wait cout<<"Customer #"<<socialSecurityNum<<" has given SSN "<<socialSecurityNum<<" to PassportClerk #"<<myLine<<"\n"; tellPassportClerkSSN(mySSN,myLine); workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); //Now leave //cout<<"Customer #"<<socialSecurityNum<<" leaving passportClerk #"<<myLine<<"\n"; workCV->Signal(workLock); //Signal(workCV, workLock); workLock->Release(); //Release(workLock); //Decide weather to self-punish bool myPassportChecked = passportClerkChecked[mySSN]; //int myPassportChecked = *(passportClerkChecked + (mySSN * sizeOfInt)); if(!myPassportChecked /*myPassportChecked == 0 */) { cout<<"Customer #"<<socialSecurityNum<<" has gone to PassportClerk #"<<myLine<<" too soon. "<< "They are going to the back of the line.\n"; punish(punishTime); } return; }
void Timer::clockHandler() { TRACE_BEGIN( LOG_LVL_INFO ); struct timespec start, cur; Condition c; Mutex m; AutoLock l(m); TimeUtils::getCurTime( &start ); while ( !mClockThread->CheckStop() ) { // Wait for the tick to timeout if (not c.Wait(m, mMsPerTick)) { TimeUtils::getCurTime( &cur ); // Check to see if the clock was set back, or jumps // forward by more than 10 seconds. If so, update the // starting time to the current time and wait for the next // tick. if ( TimeUtils::getDifference( &cur, &start ) < 0 or TimeUtils::getDifference( &cur, &start ) > 10000 ) { TimeUtils::getCurTime( &start ); handleTick(); } else { // Get the current time and count ticks until cur time and the // actual tick time are less the mMsPerTick apart. while ( TimeUtils::getDifference( &cur, &start ) > mMsPerTick ) { TimeUtils::addOffset( &start, mMsPerTick ); handleTick(); } } } else { LOG_ERR_FATAL("Signal recieved on timer condition????\n"); } } }
uint32 THREADCALL DatabaseThread::ThreadProc(void * lpParam) { while (true) { Packet *p = nullptr; // Pull the next packet from the shared queue _lock.Acquire(); if (_queue.size()) { p = _queue.front(); _queue.pop(); } _lock.Release(); // If there's no more packets to handle, wait until there are. if (p == nullptr) { // If we're shutting down, don't bother waiting for more (there are no more). if (!_running) break; s_hEvent.Wait(); continue; } // References are fun =p Packet & pkt = *p; // First 2 bytes are always going to be the socket ID // or -1 for no user. int16 uid = pkt.read<int16>(); // Attempt to lookup the user if necessary CUser *pUser = nullptr; if (uid >= 0) { pUser = g_pMain->GetUserPtr(uid); // Check to make sure they're still connected. if (pUser == nullptr) continue; } uint8 subOpcode; switch (pkt.GetOpcode()) { case WIZ_LOGIN: if (pUser) pUser->ReqAccountLogIn(pkt); break; case WIZ_SEL_NATION: if (pUser) pUser->ReqSelectNation(pkt); break; case WIZ_ALLCHAR_INFO_REQ: if (pUser) pUser->ReqAllCharInfo(pkt); break; case WIZ_CHANGE_HAIR: if (pUser) pUser->ReqChangeHair(pkt); break; case WIZ_NEW_CHAR: if (pUser) pUser->ReqCreateNewChar(pkt); break; case WIZ_DEL_CHAR: if (pUser) pUser->ReqDeleteChar(pkt); break; case WIZ_SEL_CHAR: if (pUser) pUser->ReqSelectCharacter(pkt); break; case WIZ_CHAT: pkt >> subOpcode; if (subOpcode == CLAN_NOTICE) CKnightsManager::ReqUpdateClanNotice(pkt); break; case WIZ_DATASAVE: if (pUser) pUser->ReqSaveCharacter(); break; case WIZ_KNIGHTS_PROCESS: CKnightsManager::ReqKnightsPacket(pUser, pkt); break; case WIZ_LOGIN_INFO: if (pUser) pUser->ReqSetLogInInfo(pkt); break; case WIZ_BATTLE_EVENT: // g_pMain->BattleEventResult(pkt); break; case WIZ_SHOPPING_MALL: if (pUser) pUser->ReqShoppingMall(pkt); break; case WIZ_SKILLDATA: if (pUser) pUser->ReqSkillDataProcess(pkt); break; case WIZ_FRIEND_PROCESS: if (pUser) pUser->ReqFriendProcess(pkt); break; case WIZ_NAME_CHANGE: if (pUser) pUser->ReqChangeName(pkt); break; case WIZ_CAPE: if (pUser) pUser->ReqChangeCape(pkt); break; case WIZ_LOGOUT: if (pUser) pUser->ReqUserLogOut(); break; case WIZ_KING: CKingSystem::HandleDatabaseRequest(pUser, pkt); break; case WIZ_ITEM_UPGRADE: if (pUser) pUser->ReqSealItem(pkt); break; case WIZ_ZONE_CONCURRENT: { uint32 serverNo, count; pkt >> serverNo >> count; g_DBAgent.UpdateConCurrentUserCount(serverNo, 1, count); } break; } // Free the packet. delete p; } TRACE("[Thread %d] Exiting...\n", lpParam); return 0; }
void cashierDo(int id) { int myLineID = id; //set ID bool firstTime = true; //int firstTime = 1; bool ifBribed; //int ifBribed; //int sizeOfInt = sizeof(int); while (true) { //Wait fot the next customer to signal //cout << "Cashier #" << id << " about to wait for customer\n"; waitForLine(&cashier, id, firstTime); //Set up some convenient variables Lock *workLock = &cashier.clerkLock[myLineID]; //int workLock = *(cashier.clerkLock + (myLineID * sizeOfInt)); Condition *workCV = &cashier.clerkCV[myLineID]; //int workCV = *(cashier.clerkCV + (myLineID * sizeOfInt)); //Now the clerk has been woken up and has been told the customer ID //Check int customerSSN = cashierCurrentCustomer[myLineID]; //int customerSSN = *(cashierCurrentCustomer + (myLineID * sizeOfInt)); cout<<"Cashier #" << id << " has received SSN " << customerSSN << " from Customer #" << customerSSN << ".\n"; cashierChecked[customerSSN] = passportClerkChecked[customerSSN]; //*(cashierChecked + (customerSSN * sizeOfInt)) = *(passportClerkChecked + (customerSSN * sizeOfInt)); //And Signal workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); if (!cashierChecked[customerSSN]/* *(cashierChecked + (customerSSN * sizeOfInt)) == 0 */) { //Now customer is gone cout<<"Cashier #"<<id<<" has recieved $100 from Customer #"<<customerSSN<< " before certification. They are to go to the back of the line\n"; workLock->Release(); //Release(workLock); return; } //Now customer has paid, so give passport and mark cout<<"Cashier #"<<id<<" has verified that Customer #"<<customerSSN<<" has been certified by a PassportClerk\n"; cout<<"Cashier #"<<id<<" has recieved $100 from Customer #"<<customerSSN<<" after certification\n"; cout << "Cashier #" << id << " provided Customer #" << customerSSN << " with their completed passport\n"; cashier.cashReceived+=100; gottenPassport[customerSSN] = true; //*(gottenPassport + (customerSSN * sizeOfInt)) = 1; workCV->Signal(workLock); //Signal(workCV, workLock); workCV->Wait(workLock); //Wait(workCV, workLock); //Now customer has left cout << "Cashier #" << id << " has recorded that Customer #" << customerSSN << " has been given their completed passport\n"; firstTime = false; //firstTime = 0; workLock->Release(); //Release(workLock); } //Exit(0); }