Example #1
0
// DESTRUCTOR
//------------------------------------------------------------------------------
Client::~Client()
{
	SetShuttingDown();

	m_ShouldExit = true;
	while ( m_Exited == false )
	{
		Thread::Sleep( 1 );
	}

	ShutdownAllConnections();

	Thread::CloseHandle( m_Thread );
}
Example #2
0
// DESTRUCTOR
//------------------------------------------------------------------------------
Server::~Server()
{
	m_ShouldExit = true;
	while ( m_Exited == false )
	{
		Thread::Sleep( 1 );
	}

	ShutdownAllConnections();

	Thread::CloseHandle( m_Thread );

	const ToolManifest * const * end = m_Tools.End();
	for ( ToolManifest ** it = m_Tools.Begin(); it != end; ++it )
	{
		FDELETE *it;
	}
}
// DESTRUCTOR
//------------------------------------------------------------------------------
TCPConnectionPool::~TCPConnectionPool()
{
	m_ShuttingDown = true;
	ShutdownAllConnections();
}
void CConnManager::Execute(Thread::Arg arg)
{
  TcpSocket *new_socket;
  CConnHandler *cconnhandler;
  CConnHandler *new_cconnhandler;
  TcpServerSocket* listening_socket=0;
  tUInt32 counter=0;
  //tSInt64 starttime;
  //tSInt64 endtime;

  //starttime=Timer::GetAbsoluteTime();
  
  LOGINFO("CConnManager starting up...");

  try
  {
	  listening_socket = new TcpServerSocket(_listen_port);
  }
  catch(SocketException &e)
  {
	  LOGINFO("CConnManager::Execute:Failed to instantiate the listening socket,exception:"<<e.Description()<<".");
	  if (listening_socket) 
	  {
		  listening_socket->Disconnect();
		  delete listening_socket;
	  }
	  return;
  }

  /** Start a pool of permanert handlers.*/
  for (tUInt32 i=0;i<_permanent_handler_num;i++)
  {
	  new_cconnhandler = new CConnHandler(this,TRUE,_filelog);
	  new_cconnhandler->Start();
	  _handler_vec.push_back(new_cconnhandler);
  }
  
  while(!_is_shutdown_requested)
   {
	  //For it's time consuming, we do it once every 250 loops.
	  counter++;
	  if (counter==250)
	  {   
          counter=0;
		  CleanShutdownConnections();
	  }
	  
      // Check if there is a new connection request
      // Wait and check within 100 milliseconds
      if (!listening_socket->IsReadyForRead(100))
	  {
	    // No. Then just yield CPU to others
	    // and retry later.
		//LOGDEBUG("CConnManager: not ready for read");
	    Yield();
	    continue;
	  }

      try 
	  {
	    new_socket=listening_socket->Accept();
	  }
      catch(SocketException &e)
	  {
		LOGERROR("CConnManager::Execute:Failed to instanciate socket for new incoming connection:'"<<e.Description()<<"'.");
		continue;
	  }
	  catch (exception &e)
	  {
		LOGERROR("CConnManager::Execute:Failed to instanciate socket for new incoming connection:'"<<e.what()<<"'.");
		continue;
	  }

	  LOGDEBUG("CConnManager::Execute:Received a new connection from '"<<new_socket->PeerHostAddress()<<"'.");
      
	  new_socket->SetKeepAlive();
      
	  cconnhandler = FindMostLeisureHandler();

	  if (!cconnhandler)
	  {
		  new_cconnhandler = new CConnHandler(this,FALSE,_filelog);
		  new_cconnhandler->Start();
		  _handler_vec.push_back(new_cconnhandler);
	  }
	  else if (cconnhandler->CanContainMore())
		  cconnhandler->SaveSocket(new_socket);
	  else
	  {
		  if (_handler_vec.size()<_handler_max)
		  {
			  new_cconnhandler = new CConnHandler(this,FALSE,_filelog);
			  new_cconnhandler->Start();
			  _handler_vec.push_back(new_cconnhandler);

			  new_cconnhandler->SaveSocket(new_socket);
		  }
		  else
			  cconnhandler->SaveSocket(new_socket);	// Ask the handler to forward it to other slave server.
	  }

    } // while(!_is_shutdown_requested)
  
  // Here we received a shutdown signal!

  // Then shuting down all current connections.  
  ShutdownAllConnections();
  
  CleanShutdownConnections();

  //endtime=Timer::GetAbsoluteTime();
  
  //Timer::Shutdown();
  
  LOGINFO("CConnManager shutdowned.");
}