Logo Search packages:      
Sourcecode: znc version File versions  Download package

template<class T>
virtual void TSocketManager< T >::Loop (  )  [inline, virtual]

Best place to call this class for running, all the call backs are called. You should through this in your main while loop (long as its not blocking) all the events are called as needed.

Definition at line 1401 of file Csocket.h.

      {
            for( u_int a = 0; a < this->size(); a++ )
            {
                  T *pcSock = (*this)[a];

                  if ( ( pcSock->GetType() != T::OUTBOUND ) || ( pcSock->GetConState() == T::CST_OK ) )
                        continue;

                  if ( pcSock->GetConState() == T::CST_DNS )
                  {
                        if ( pcSock->DNSLookup( T::DNS_DEST ) == ETIMEDOUT )
                        {
                              pcSock->SockError( EDOM );
                              DelSock( a-- );
                              continue;
                        }
                  }

                  if ( pcSock->GetConState() == T::CST_VHOSTDNS )
                  {
                        if ( pcSock->DNSLookup( T::DNS_VHOST ) == ETIMEDOUT )
                        {
                              pcSock->SockError( EADDRNOTAVAIL );
                              DelSock( a-- );
                              continue;
                        }
                  }

                  if ( pcSock->GetConState() == T::CST_BINDVHOST )
                  {
                        if ( !pcSock->SetupVHost() )
                        {
                              pcSock->SockError( errno );
                              DelSock( a-- );
                              continue;
                        }
                  }

                  if ( pcSock->GetConState() == T::CST_CONNECT )
                  {
                        if ( !pcSock->Connect( pcSock->GetBindHost(), true ) )
                        {
                              if ( GetSockError() == ECONNREFUSED )
                                    pcSock->ConnectionRefused();
                              else
                                    pcSock->SockError( ECONNABORTED );

                              DelSock( a-- );
                              continue;
                        }

#ifdef HAVE_LIBSSL
                        if ( pcSock->GetSSL() )
                        {
                              if ( !pcSock->ConnectSSL() )
                              {
                                    if ( GetSockError() == ECONNREFUSED )
                                          pcSock->ConnectionRefused();
                                    else
                                          pcSock->SockError( ECONNABORTED );

                                    DelSock( a-- );
                                    continue;
                              }
                        }
#endif /* HAVE_LIBSSL */
                  }
            }

            std::map<T *, EMessages> mpeSocks;
            Select( mpeSocks );

            switch( m_errno )
            {
                  case SUCCESS:
                  {
                        for( typename std::map<T *, EMessages>::iterator itSock = mpeSocks.begin(); itSock != mpeSocks.end(); itSock++ )
                        {
                              T * pcSock = itSock->first;
                              EMessages iErrno = itSock->second;

                              if ( iErrno == SUCCESS )
                              {
                                    // read in data
                                    // if this is a
                                    int iLen = 0;

                                    if ( pcSock->GetSSL() )
                                          iLen = pcSock->GetPending();

                                    if ( iLen <= 0 )
                                          iLen = CS_BLOCKSIZE;
                                    
                                    CSCharBuffer cBuff( iLen );

                                    int bytes = pcSock->Read( cBuff(), iLen );

                                    if ( ( bytes != T::READ_TIMEDOUT ) && ( bytes != T::READ_CONNREFUSED )
                                          && ( !pcSock->IsConnected() ) )
                                    {
                                          pcSock->SetIsConnected( true );
                                          pcSock->Connected();
                                    }

                                    switch( bytes )
                                    {
                                          case T::READ_EOF:
                                          {
                                                DelSockByAddr( pcSock );
                                                break;
                                          }

                                          case T::READ_ERR:
                                          {
                                                pcSock->SockError( GetSockError() );
                                                DelSockByAddr( pcSock );
                                                break;
                                          }

                                          case T::READ_EAGAIN:
                                                break;

                                          case T::READ_CONNREFUSED:
                                                pcSock->ConnectionRefused();
                                                DelSockByAddr( pcSock );
                                                break;

                                          case T::READ_TIMEDOUT:
                                                pcSock->Timeout();
                                                DelSockByAddr( pcSock );
                                                break;

                                          default:
                                          {
                                                if ( T::TMO_READ & pcSock->GetTimeoutType() )
                                                      pcSock->ResetTimer();   // reset the timeout timer

                                                pcSock->ReadData( cBuff(), bytes ); // Call ReadData() before PushBuff() so that it is called before the ReadLine() event - LD  07/18/05
                                                pcSock->PushBuff( cBuff(), bytes );
                                                break;
                                          }
                                    }

                              } else if ( iErrno == SELECT_ERROR )
                              {
                                    // a socket came back with an error
                                    // usually means it was closed
                                    DelSockByAddr( pcSock );
                              }
                        }
                        break;
                  }

                  case SELECT_TIMEOUT:
                  case SELECT_ERROR:
                  default     :
                        break;
            }

            unsigned long long iMilliNow = millitime();
            if ( ( iMilliNow - m_iCallTimeouts ) >= 1000 )
            {
                  m_iCallTimeouts = iMilliNow;
                  // call timeout on all the sockets that recieved no data
                  for( unsigned int i = 0; i < this->size(); i++ )
                  {
                        if ( (*this)[i]->GetConState() != T::CST_OK )
                              continue;

                        if ( (*this)[i]->CheckTimeout( iMilliNow / 1000 ) )
                              DelSock( i-- );
                  }
            }
            // run any Manager Crons we may have
            Cron();
      }


Generated by  Doxygen 1.6.0   Back to index