Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes
FIX::SocketMonitor Class Reference

Monitors events on a collection of sockets. More...

#include <SocketMonitor.h>

List of all members.

Classes

class  Strategy

Public Member Functions

 SocketMonitor (int timeout=0)
virtual ~SocketMonitor ()
bool addConnect (int socket)
bool addRead (int socket)
bool addWrite (int socket)
bool drop (int socket)
void signal (int socket)
void unsignal (int socket)
void block (Strategy &strategy, bool poll=0, double timeout=0.0)
int numSockets ()

Private Types

typedef std::set< int > Sockets
typedef std::queue< int > Queue

Private Member Functions

void setsockopt ()
bool bind ()
bool listen ()
void buildSet (const Sockets &, fd_set &)
timeval * getTimeval (bool poll, double timeout)
bool sleepIfEmpty (bool poll)
void processReadSet (Strategy &, fd_set &)
void processWriteSet (Strategy &, fd_set &)
void processExceptSet (Strategy &, fd_set &)

Private Attributes

int m_timeout
timeval m_timeval
clock_t m_ticks
int m_signal
int m_interrupt
Sockets m_connectSockets
Sockets m_readSockets
Sockets m_writeSockets
Queue m_dropped

Detailed Description

Monitors events on a collection of sockets.

Definition at line 47 of file SocketMonitor.h.


Member Typedef Documentation

typedef std::queue< int > FIX::SocketMonitor::Queue [private]

Definition at line 68 of file SocketMonitor.h.

typedef std::set< int > FIX::SocketMonitor::Sockets [private]

Definition at line 67 of file SocketMonitor.h.


Constructor & Destructor Documentation

FIX::SocketMonitor::SocketMonitor ( int  timeout = 0)

Definition at line 35 of file SocketMonitor.cpp.

References m_interrupt, m_readSockets, m_signal, m_ticks, m_timeval, FIX::socket_createpair(), FIX::socket_init(), and FIX::socket_setnonblock().

: m_timeout( timeout )
{
  socket_init();

  std::pair<int, int> sockets = socket_createpair();
  m_signal = sockets.first;
  m_interrupt = sockets.second;
  socket_setnonblock( m_signal );
  socket_setnonblock( m_interrupt );
  m_readSockets.insert( m_interrupt );

  m_timeval.tv_sec = 0;
  m_timeval.tv_usec = 0;
#ifndef SELECT_DECREMENTS_TIME
  m_ticks = clock();
#endif
}

Definition at line 54 of file SocketMonitor.cpp.

References m_readSockets, m_signal, FIX::socket_close(), and FIX::socket_term().

{
  Sockets::iterator i;
  for ( i = m_readSockets.begin(); i != m_readSockets.end(); ++i ) {
    socket_close( *i );
  }

  socket_close( m_signal );
  socket_term();
}

Member Function Documentation

bool FIX::SocketMonitor::addConnect ( int  socket)

Definition at line 65 of file SocketMonitor.cpp.

References m_connectSockets, and FIX::socket_setnonblock().

Referenced by FIX::SocketServer::accept(), and FIX::SocketConnector::connect().

{
  socket_setnonblock( s );
  Sockets::iterator i = m_connectSockets.find( s );
  if( i != m_connectSockets.end() ) return false;

  m_connectSockets.insert( s );
  return true;
}
bool FIX::SocketMonitor::addRead ( int  socket)

Definition at line 75 of file SocketMonitor.cpp.

References m_readSockets, and FIX::socket_setnonblock().

{
  socket_setnonblock( s );
  Sockets::iterator i = m_readSockets.find( s );
  if( i != m_readSockets.end() ) return false;

  m_readSockets.insert( s );
  return true;
}
bool FIX::SocketMonitor::addWrite ( int  socket)

Definition at line 85 of file SocketMonitor.cpp.

References m_readSockets, m_writeSockets, and FIX::socket_setnonblock().

Referenced by processReadSet().

{
  if( m_readSockets.find(s) == m_readSockets.end() )
    return false;

  socket_setnonblock( s );
  Sockets::iterator i = m_writeSockets.find( s );
  if( i != m_writeSockets.end() ) return false;

  m_writeSockets.insert( s );
  return true;
}
bool FIX::SocketMonitor::bind ( ) [private]
void FIX::SocketMonitor::block ( Strategy strategy,
bool  poll = 0,
double  timeout = 0.0 
)

Definition at line 181 of file SocketMonitor.cpp.

References buildSet(), getTimeval(), m_connectSockets, m_dropped, m_readSockets, m_writeSockets, FIX::SocketMonitor::Strategy::onError(), FIX::SocketMonitor::Strategy::onTimeout(), processExceptSet(), processReadSet(), processWriteSet(), and sleepIfEmpty().

Referenced by FIX::SocketConnector::block(), and FIX::SocketServer::block().

{
  while ( m_dropped.size() )
  {
    strategy.onError( *this, m_dropped.front() );
    m_dropped.pop();
    if ( m_dropped.size() == 0 )
      return ;
  }

  fd_set readSet;
  FD_ZERO( &readSet );
  buildSet( m_readSockets, readSet );
  fd_set writeSet;
  FD_ZERO( &writeSet );
  buildSet( m_connectSockets, writeSet );
  buildSet( m_writeSockets, writeSet );
  fd_set exceptSet;
  FD_ZERO( &exceptSet );
  buildSet( m_connectSockets, exceptSet );

  if ( sleepIfEmpty(poll) )
  {
    strategy.onTimeout( *this );
    return;
  }

  int result = select( FD_SETSIZE, &readSet, &writeSet, &exceptSet, getTimeval(poll, timeout) );

  if ( result == 0 )
  {
    strategy.onTimeout( *this );
    return;
  }
  else if ( result > 0 )
  {
    processExceptSet( strategy, exceptSet );
    processWriteSet( strategy, writeSet );
    processReadSet( strategy, readSet );
  }
  else
  {
    strategy.onError( *this );
  }
}
void FIX::SocketMonitor::buildSet ( const Sockets sockets,
fd_set &  watchSet 
) [private]

Definition at line 328 of file SocketMonitor.cpp.

Referenced by block().

{
  Sockets::const_iterator iter;
  for ( iter = sockets.begin(); iter != sockets.end(); ++iter ) {
    FD_SET( *iter, &watchSet );
  }
}
bool FIX::SocketMonitor::drop ( int  socket)

Definition at line 98 of file SocketMonitor.cpp.

References m_connectSockets, m_dropped, m_readSockets, m_writeSockets, and FIX::socket_close().

Referenced by FIX::SocketConnection::disconnect(), FIX::HttpServer::onConnect(), FIX::ServerWrapper::onError(), FIX::SocketConnection::read(), and FIX::SocketConnection::readMessages().

{
  Sockets::iterator i = m_readSockets.find( s );
  Sockets::iterator j = m_writeSockets.find( s );
  Sockets::iterator k = m_connectSockets.find( s );

  if ( i != m_readSockets.end() || 
       j != m_writeSockets.end() ||
       k != m_connectSockets.end() )
  {
    socket_close( s );
    m_readSockets.erase( s );
    m_writeSockets.erase( s );
    m_connectSockets.erase( s );
    m_dropped.push( s );
    return true;
  }
  return false;
}
timeval * FIX::SocketMonitor::getTimeval ( bool  poll,
double  timeout 
) [inline, private]

Definition at line 118 of file SocketMonitor.cpp.

References m_ticks, m_timeout, and m_timeval.

Referenced by block().

{
  if ( poll )
  {
    m_timeval.tv_sec = 0;
    m_timeval.tv_usec = 0;
    return &m_timeval;
  }

  timeout = m_timeout;

  if ( !timeout )
    return 0;
#ifdef SELECT_MODIFIES_TIMEVAL
  if ( !m_timeval.tv_sec && !m_timeval.tv_usec && timeout )
    m_timeval.tv_sec = timeout;
  return &m_timeval;
#else
double elapsed = ( double ) ( clock() - m_ticks ) / ( double ) CLOCKS_PER_SEC;
  if ( elapsed >= timeout || elapsed == 0.0 )
  {
    m_ticks = clock();
    m_timeval.tv_sec = 0;
    m_timeval.tv_usec = (long)(timeout * 1000000);
  }
  else
  {
    m_timeval.tv_sec = 0;
    m_timeval.tv_usec = ( long ) ( ( timeout - elapsed ) * 1000000 );
  }
  return &m_timeval;
#endif
}
bool FIX::SocketMonitor::listen ( ) [private]

Definition at line 63 of file SocketMonitor.h.

References m_readSockets.

Referenced by FIX::SocketServer::numConnections().

  { return m_readSockets.size() - 1; }
void FIX::SocketMonitor::processExceptSet ( Strategy strategy,
fd_set &  exceptSet 
) [private]

Definition at line 307 of file SocketMonitor.cpp.

References m_connectSockets, and FIX::SocketMonitor::Strategy::onError().

Referenced by block().

{
#ifdef _MSC_VER
  for ( unsigned i = 0; i < exceptSet.fd_count; ++i )
  {
    int s = exceptSet.fd_array[ i ];
    strategy.onError( *this, s );
  }
#else
    Sockets::iterator i;
    Sockets sockets = m_connectSockets;
    for ( i = sockets.begin(); i != sockets.end(); ++i )
    {
      int s = *i;
      if ( !FD_ISSET( *i, &exceptSet ) )
        continue;
      strategy.onError( *this, s );
    }
#endif
}
void FIX::SocketMonitor::processReadSet ( Strategy strategy,
fd_set &  readSet 
) [private]

Definition at line 227 of file SocketMonitor.cpp.

References addWrite(), m_interrupt, m_readSockets, and FIX::SocketMonitor::Strategy::onEvent().

Referenced by block().

{
#ifdef _MSC_VER
  for ( unsigned i = 0; i < readSet.fd_count; ++i )
  {
    int s = readSet.fd_array[ i ];
    if( s == m_interrupt )
    {
      int socket = 0;
      recv( s, (char*)&socket, sizeof(socket), 0 );
      addWrite( socket );
    }
    else
    {
      strategy.onEvent( *this, s );
    }
  }
#else
    Sockets::iterator i;
    Sockets sockets = m_readSockets;
    for ( i = sockets.begin(); i != sockets.end(); ++i )
    {
      int s = *i;
      if ( !FD_ISSET( *i, &readSet ) )
        continue;
      if( s == m_interrupt )
      {
        int socket = 0;
        recv( s, (char*)&socket, sizeof(socket), 0 );
        addWrite( socket );
      }
      else
      {
        strategy.onEvent( *this, s );
      }
    }
#endif
}
void FIX::SocketMonitor::processWriteSet ( Strategy strategy,
fd_set &  writeSet 
) [private]

Definition at line 266 of file SocketMonitor.cpp.

References m_connectSockets, m_readSockets, m_writeSockets, FIX::SocketMonitor::Strategy::onConnect(), and FIX::SocketMonitor::Strategy::onWrite().

Referenced by block().

{
#ifdef _MSC_VER
  for ( unsigned i = 0; i < writeSet.fd_count; ++i )
  {
    int s = writeSet.fd_array[ i ];
    if( m_connectSockets.find(s) != m_connectSockets.end() )
    {
      m_connectSockets.erase( s );
      m_readSockets.insert( s );
      strategy.onConnect( *this, s );
    }
    else
    {
      strategy.onWrite( *this, s );
    }
  }
#else
  Sockets::iterator i;
  Sockets sockets = m_connectSockets;
  for( i = sockets.begin(); i != sockets.end(); ++i )
  {
    int s = *i;
    if ( !FD_ISSET( *i, &writeSet ) )
      continue;
    m_connectSockets.erase( s );
    m_readSockets.insert( s );
    strategy.onConnect( *this, s );
  }

  sockets = m_writeSockets;
  for( i = sockets.begin(); i != sockets.end(); ++i )
  {
    int s = *i;
    if ( !FD_ISSET( *i, &writeSet ) )
      continue;
    strategy.onWrite( *this, s );
  }
#endif
}
void FIX::SocketMonitor::setsockopt ( ) [private]
void FIX::SocketMonitor::signal ( int  socket)

Definition at line 168 of file SocketMonitor.cpp.

References m_signal, and FIX::socket_send().

Referenced by FIX::SocketConnection::signal().

{
  socket_send( m_signal, (char*)&socket, sizeof(socket) );
}
bool FIX::SocketMonitor::sleepIfEmpty ( bool  poll) [inline, private]

Definition at line 152 of file SocketMonitor.cpp.

References m_connectSockets, m_readSockets, m_timeout, m_writeSockets, and FIX::process_sleep().

Referenced by block().

{
  if( poll )
    return false;

  if ( m_readSockets.empty() && 
       m_writeSockets.empty() &&
       m_connectSockets.empty() )
  {
    process_sleep( m_timeout );
    return true;
  }
  else
    return false;
}
void FIX::SocketMonitor::unsignal ( int  socket)

Definition at line 173 of file SocketMonitor.cpp.

References m_writeSockets.

Referenced by FIX::SocketConnection::unsignal().

{
  Sockets::iterator i = m_writeSockets.find( s );
  if( i == m_writeSockets.end() ) return;

  m_writeSockets.erase( s );
}

Member Data Documentation

Definition at line 89 of file SocketMonitor.h.

Referenced by addConnect(), block(), drop(), processExceptSet(), processWriteSet(), and sleepIfEmpty().

Definition at line 92 of file SocketMonitor.h.

Referenced by block(), and drop().

Definition at line 88 of file SocketMonitor.h.

Referenced by processReadSet(), and SocketMonitor().

Definition at line 87 of file SocketMonitor.h.

Referenced by signal(), SocketMonitor(), and ~SocketMonitor().

clock_t FIX::SocketMonitor::m_ticks [private]

Definition at line 84 of file SocketMonitor.h.

Referenced by getTimeval(), and SocketMonitor().

Definition at line 81 of file SocketMonitor.h.

Referenced by getTimeval(), and sleepIfEmpty().

timeval FIX::SocketMonitor::m_timeval [private]

Definition at line 82 of file SocketMonitor.h.

Referenced by getTimeval(), and SocketMonitor().

Definition at line 91 of file SocketMonitor.h.

Referenced by addWrite(), block(), drop(), processWriteSet(), sleepIfEmpty(), and unsignal().


The documentation for this class was generated from the following files:

Generated on Mon Jun 23 2014 23:49:40 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001