Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Server::~Server() {
- stop();
- }
- const void Server::stop() throw(ServerException){
- this->generalInterrupt = true;
- this->timerInterrupt = true;
- bool tryLockArray[9];
- lockAll(tryLockArray);
- if(generalSocket != INVALID_SOCKET) {
- unsigned int iMode = 0;
- auto ioctlResult = ioctlsocket(generalSocket, 0x8004667E, &iMode);
- if(ioctlResult == SOCKET_ERROR)
- throw ServerException(COULD_NOT_SET_NON_BLOCKING);
- }
- if(timerThread != nullptr && timerThread.get()->joinable())
- timerThread.get()->join();
- if(commandThread != nullptr && commandThread.get()->joinable())
- commandThread.get()->join();
- for (auto ¤t: this->users) {
- current.second->clientInterrupt = false;
- current.second->serverInterrupt = true;
- if (current.second->thread != nullptr && current.second->thread->joinable())
- current.second->thread->join();
- }
- clearSocket(-1, generalSocket);
- WSACleanup();
- unlockAll(tryLockArray);
- this->controller->finit(BACKUP_FILENAME);
- try { this->controller->save(); }
- catch (const ServerController::ControllerException& exception) { }
- }
- const void Server::clearSocket(const int threadId, const SOCKET socket) throw(ServerException) {
- if(socket == INVALID_SOCKET)
- return;
- if(threadId != -1) {
- auto socketShutdown = shutdown(socket, SD_BOTH);
- if(socketShutdown == SOCKET_ERROR)
- throw ServerException(COULD_NOT_SHUT_SOCKET_DOWN);
- }
- bool lockOut = this->mutexOut.try_lock();
- *this->out << "Thread 0x" << threadId << ". Socket is down." << std::endl;
- if(lockOut)
- this->mutexOut.unlock();
- auto socketClose = closesocket(socket);
- if(socketClose == SOCKET_ERROR)
- throw ServerException(COULD_NOT_CLOSE_SOCKET);
- lockOut = this->mutexOut.try_lock();
- *this->out << "Thread 0x" << threadId << ". Socket is closed." << std::endl;
- if(lockOut)
- this->mutexOut.unlock();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement