Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. Server::~Server() {
  2.     stop();
  3. }
  4.  
  5. const void Server::stop() throw(ServerException){
  6.     this->generalInterrupt = true;
  7.     this->timerInterrupt = true;
  8.  
  9.     bool tryLockArray[9];
  10.     lockAll(tryLockArray);
  11.  
  12.     if(generalSocket != INVALID_SOCKET) {
  13.         unsigned int iMode = 0;
  14.         auto ioctlResult = ioctlsocket(generalSocket, 0x8004667E, &iMode);
  15.         if(ioctlResult == SOCKET_ERROR)
  16.             throw ServerException(COULD_NOT_SET_NON_BLOCKING);
  17.     }
  18.  
  19.     if(timerThread != nullptr && timerThread.get()->joinable())
  20.         timerThread.get()->join();
  21.  
  22.     if(commandThread != nullptr && commandThread.get()->joinable())
  23.         commandThread.get()->join();
  24.  
  25.     for (auto &current: this->users) {
  26.         current.second->clientInterrupt = false;
  27.         current.second->serverInterrupt = true;
  28.  
  29.         if (current.second->thread != nullptr && current.second->thread->joinable())
  30.             current.second->thread->join();
  31.     }
  32.  
  33.     clearSocket(-1, generalSocket);
  34.     WSACleanup();
  35.  
  36.     unlockAll(tryLockArray);
  37.  
  38.     this->controller->finit(BACKUP_FILENAME);
  39.     try { this->controller->save(); }
  40.     catch (const ServerController::ControllerException& exception) {  }
  41. }
  42.  
  43. const void Server::clearSocket(const int threadId, const SOCKET socket) throw(ServerException) {
  44.  
  45.     if(socket == INVALID_SOCKET)
  46.         return;
  47.  
  48.     if(threadId != -1) {
  49.         auto socketShutdown = shutdown(socket, SD_BOTH);
  50.         if(socketShutdown == SOCKET_ERROR)
  51.             throw ServerException(COULD_NOT_SHUT_SOCKET_DOWN);
  52.     }
  53.  
  54.     bool lockOut = this->mutexOut.try_lock();
  55.     *this->out <<  "Thread 0x" << threadId << ". Socket is down." << std::endl;
  56.     if(lockOut)
  57.         this->mutexOut.unlock();
  58.  
  59.     auto socketClose = closesocket(socket);
  60.     if(socketClose == SOCKET_ERROR)
  61.         throw ServerException(COULD_NOT_CLOSE_SOCKET);
  62.  
  63.     lockOut = this->mutexOut.try_lock();
  64.     *this->out <<  "Thread 0x" << threadId << ". Socket is closed." << std::endl;
  65.     if(lockOut)
  66.         this->mutexOut.unlock();
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement