FIX::SocketMonitor Class Reference

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

`#include <SocketMonitor.h>`

## 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)
size_t | 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 64 of file SocketMonitor.h.

## Member Typedef Documentation

## Queue

typedef std::queue< int > FIX::SocketMonitor::Queue

private

Definition at line 102 of file SocketMonitor.h.

## Sockets

typedef std::set< int > FIX::SocketMonitor::Sockets

private

Definition at line 101 of file SocketMonitor.h.

## Constructor & Destructor Documentation

## SocketMonitor()

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

Definition at line 52 of file SocketMonitor.cpp.

{
Sockets::iterator i;
for ( i = m_readSockets.begin(); i != m_readSockets.end(); ++i ) {
socket_close( *i );
}
socket_close( m_signal );
socket_term();
}
bool SocketMonitor::addConnect( int s )
{
socket_setnonblock( s );
Sockets::iterator i = m_connectSockets.find( s );
if( i != m_connectSockets.end() ) return false;

## ~SocketMonitor()

FIX::SocketMonitor::~SocketMonitor | ( |  | ) |

virtual

Definition at line 71 of file SocketMonitor.cpp.

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

## Member Function Documentation

## addConnect()

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

Definition at line 82 of file SocketMonitor.cpp.

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

## addRead()

bool FIX::SocketMonitor::addRead | ( | int | socket | ) |

Definition at line 92 of file SocketMonitor.cpp.

{
Sockets::iterator i = m_readSockets.find( s );

## addWrite()

bool FIX::SocketMonitor::addWrite | ( | int | socket | ) |

Definition at line 102 of file SocketMonitor.cpp.

{
socket_close( s );
m_readSockets.erase( s );
m_writeSockets.erase( s );
m_connectSockets.erase( s );
m_dropped.push( s );
return true;

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

Referenced by processReadSet().

## bind()

bool FIX::SocketMonitor::bind | ( |  | ) |

private

## block()

void FIX::SocketMonitor::block | ( | Strategy & | strategy,
|  | bool | poll = `0`,
|  | double | timeout = `0.0`
| ) |  |

Definition at line 198 of file SocketMonitor.cpp.

{
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 SocketMonitor::processReadSet( Strategy& strategy, fd_set& readSet )
{
#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;
socket_recv( s, (char*)&socket, sizeof(socket) );
addWrite( socket );
}
else
{
strategy.onEvent( *this, s );
}

## buildSet()

void FIX::SocketMonitor::buildSet | ( | const Sockets & | sockets,
|  | fd_set & | watchSet
| ) |  |

private

Definition at line 345 of file SocketMonitor.cpp.

## drop()

bool FIX::SocketMonitor::drop | ( | int | socket | ) |

Definition at line 115 of file SocketMonitor.cpp.

{
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;

## getTimeval()

timeval * FIX::SocketMonitor::getTimeval | ( | bool | poll,
|  | double | timeout
| ) |  |

inline private

Definition at line 135 of file SocketMonitor.cpp.

{
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 SocketMonitor::sleepIfEmpty( bool poll )
{
if( poll )
return false;
if ( m_readSockets.empty() &&
m_writeSockets.empty() &&
m_connectSockets.empty() )
{
process_sleep( m_timeout );
return true;
}
else
return false;
}

References m_ticks, and m_timeval.

## listen()

bool FIX::SocketMonitor::listen | ( |  | ) |

private

## numSockets()

size_t FIX::SocketMonitor::numSockets | ( |  | ) |

inline

Definition at line 97 of file SocketMonitor.h.

:
virtual ~Strategy()

## processExceptSet()

void FIX::SocketMonitor::processExceptSet | ( | Strategy & | strategy,
|  | fd_set & | exceptSet
| ) |  |

private

Definition at line 324 of file SocketMonitor.cpp.

{
Sockets::const_iterator iter;
for ( iter = sockets.begin(); iter != sockets.end(); ++iter ) {
FD_SET( *iter, &watchSet );
}
}
}

## processReadSet()

void FIX::SocketMonitor::processReadSet | ( | Strategy & | strategy,
|  | fd_set & | readSet
| ) |  |

private

Definition at line 244 of file SocketMonitor.cpp.

{
int s = *i;
if ( !FD_ISSET( *i, &readSet ) )
continue;
if( s == m_interrupt )
{
int socket = 0;
socket_recv( s, (char*)&socket, sizeof(socket) );
addWrite( socket );
}
else
{
strategy.onEvent( *this, s );
}
}
#endif
}
void SocketMonitor::processWriteSet( Strategy& strategy, fd_set& writeSet )
{
#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 );
}

References addWrite(), m_interrupt, and FIX::socket_recv().

## processWriteSet()

void FIX::SocketMonitor::processWriteSet | ( | Strategy & | strategy,
|  | fd_set & | writeSet
| ) |  |

private

Definition at line 283 of file SocketMonitor.cpp.

{
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 SocketMonitor::processExceptSet( Strategy& strategy, fd_set& exceptSet )
{
#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;

References m_connectSockets, and m_readSockets.

## setsockopt()

void FIX::SocketMonitor::setsockopt | ( |  | ) |

private

## signal()

void FIX::SocketMonitor::signal | ( | int | socket | ) |

Definition at line 185 of file SocketMonitor.cpp.

{

## sleepIfEmpty()

bool FIX::SocketMonitor::sleepIfEmpty | ( | bool | poll | ) |

inline private

Definition at line 169 of file SocketMonitor.cpp.

{
socket_send( m_signal, (char*)&socket, sizeof(socket) );
}
void SocketMonitor::unsignal( int s )
{
Sockets::iterator i = m_writeSockets.find( s );
if( i == m_writeSockets.end() ) return;
m_writeSockets.erase( s );
}
void SocketMonitor::block( Strategy& strategy, bool poll, double timeout )
{
while ( m_dropped.size() )

References m_signal, and FIX::socket_send().

## unsignal()

void FIX::SocketMonitor::unsignal | ( | int | socket | ) |

Definition at line 190 of file SocketMonitor.cpp.

{

## Member Data Documentation

## m_connectSockets

Sockets FIX::SocketMonitor::m_connectSockets

private

Definition at line 123 of file SocketMonitor.h.

Referenced by addWrite(), and processWriteSet().

## m_dropped

Queue FIX::SocketMonitor::m_dropped

private

Definition at line 126 of file SocketMonitor.h.

Referenced by addWrite().

## m_interrupt

int FIX::SocketMonitor::m_interrupt

private

Definition at line 122 of file SocketMonitor.h.

Referenced by processReadSet().

## m_readSockets

Sockets FIX::SocketMonitor::m_readSockets

private

Definition at line 124 of file SocketMonitor.h.

Referenced by addWrite(), and processWriteSet().

## m_signal

int FIX::SocketMonitor::m_signal

private

Definition at line 121 of file SocketMonitor.h.

Referenced by sleepIfEmpty().

## m_ticks

clock_t FIX::SocketMonitor::m_ticks

private

Definition at line 118 of file SocketMonitor.h.

Referenced by getTimeval().

## m_timeout

int FIX::SocketMonitor::m_timeout

private

Definition at line 115 of file SocketMonitor.h.

## m_timeval

timeval FIX::SocketMonitor::m_timeval

private

Definition at line 116 of file SocketMonitor.h.

Referenced by getTimeval().

## m_writeSockets

Sockets FIX::SocketMonitor::m_writeSockets

private

Definition at line 125 of file SocketMonitor.h.

Referenced by addWrite().

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

- SocketMonitor.h
- SocketMonitor.cpp

FIX::SocketMonitor::m_timeout
int m_timeout
Definition: SocketMonitor.h:115
FIX::SocketMonitor::unsignal
void unsignal(int socket)
Definition: SocketMonitor.cpp:190
FIX::SocketMonitor::m_signal
int m_signal
Definition: SocketMonitor.h:121
FIX::SocketMonitor::block
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
Definition: SocketMonitor.cpp:198
FIX::SocketMonitor::m_connectSockets
Sockets m_connectSockets
Definition: SocketMonitor.h:123
FIX::SocketMonitor::addWrite
bool addWrite(int socket)
Definition: SocketMonitor.cpp:102
FIX::SocketMonitor::m_interrupt
int m_interrupt
Definition: SocketMonitor.h:122
FIX::SocketMonitor::processWriteSet
void processWriteSet(Strategy &, fd_set &)
Definition: SocketMonitor.cpp:283
FIX::SocketMonitor::m_readSockets
Sockets m_readSockets
Definition: SocketMonitor.h:124
FIX::SocketMonitor::addConnect
bool addConnect(int socket)
Definition: SocketMonitor.cpp:82
FIX::socket_term
void socket_term()
Definition: Utility.cpp:113
FIX::SocketMonitor::sleepIfEmpty
bool sleepIfEmpty(bool poll)
Definition: SocketMonitor.cpp:169
FIX::socket_recv
ssize_t socket_recv(int s, char *buf, size_t length)
Definition: Utility.cpp:187
FIX::socket_send
ssize_t socket_send(int s, const char *msg, size_t length)
Definition: Utility.cpp:192
FIX::SocketMonitor::m_writeSockets
Sockets m_writeSockets
Definition: SocketMonitor.h:125
FIX::SocketMonitor::getTimeval
timeval * getTimeval(bool poll, double timeout)
Definition: SocketMonitor.cpp:135
FIX::process_sleep
void process_sleep(double s)
Definition: Utility.cpp:483
FIX::SocketMonitor::m_ticks
clock_t m_ticks
Definition: SocketMonitor.h:118
FIX::socket_setnonblock
void socket_setnonblock(int socket)
Definition: Utility.cpp:285
FIX::socket_close
void socket_close(int s)
Definition: Utility.cpp:197
FIX::SocketMonitor::processExceptSet
void processExceptSet(Strategy &, fd_set &)
Definition: SocketMonitor.cpp:324
FIX::SocketMonitor::m_timeval
timeval m_timeval
Definition: SocketMonitor.h:116
FIX::SocketMonitor::processReadSet
void processReadSet(Strategy &, fd_set &)
Definition: SocketMonitor.cpp:244
FIX::SocketMonitor::Sockets
std::set< int > Sockets
Definition: SocketMonitor.h:101
FIX::SocketMonitor::m_dropped
Queue m_dropped
Definition: SocketMonitor.h:126