Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
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 47 of file SocketMonitor.h.

Member Typedef Documentation

◆ Queue

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

Definition at line 68 of file SocketMonitor.h.

◆ Sockets

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

Definition at line 67 of file SocketMonitor.h.

Constructor & Destructor Documentation

◆ SocketMonitor()

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().

36 : m_timeout( timeout )
37 {
38  socket_init();
39 
40  std::pair<int, int> sockets = socket_createpair();
41  m_signal = sockets.first;
42  m_interrupt = sockets.second;
45  m_readSockets.insert( m_interrupt );
46 
47  m_timeval.tv_sec = 0;
48  m_timeval.tv_usec = 0;
49 #ifndef SELECT_DECREMENTS_TIME
50  m_ticks = clock();
51 #endif
52 }
void socket_init()
Definition: Utility.cpp:81
void socket_setnonblock(int socket)
Definition: Utility.cpp:268
std::pair< int, int > socket_createpair()
Definition: Utility.cpp:366

◆ ~SocketMonitor()

FIX::SocketMonitor::~SocketMonitor ( )
virtual

Definition at line 54 of file SocketMonitor.cpp.

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

55 {
56  Sockets::iterator i;
57  for ( i = m_readSockets.begin(); i != m_readSockets.end(); ++i ) {
58  socket_close( *i );
59  }
60 
62  socket_term();
63 }
void socket_term()
Definition: Utility.cpp:96
void socket_close(int s)
Definition: Utility.cpp:180

Member Function Documentation

◆ addConnect()

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().

66 {
67  socket_setnonblock( s );
68  Sockets::iterator i = m_connectSockets.find( s );
69  if( i != m_connectSockets.end() ) return false;
70 
71  m_connectSockets.insert( s );
72  return true;
73 }
Sockets m_connectSockets
Definition: SocketMonitor.h:89
void socket_setnonblock(int socket)
Definition: Utility.cpp:268

◆ addRead()

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

Definition at line 75 of file SocketMonitor.cpp.

References m_readSockets, and FIX::socket_setnonblock().

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

76 {
77  socket_setnonblock( s );
78  Sockets::iterator i = m_readSockets.find( s );
79  if( i != m_readSockets.end() ) return false;
80 
81  m_readSockets.insert( s );
82  return true;
83 }
void socket_setnonblock(int socket)
Definition: Utility.cpp:268

◆ addWrite()

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().

86 {
87  if( m_readSockets.find(s) == m_readSockets.end() )
88  return false;
89 
90  socket_setnonblock( s );
91  Sockets::iterator i = m_writeSockets.find( s );
92  if( i != m_writeSockets.end() ) return false;
93 
94  m_writeSockets.insert( s );
95  return true;
96 }
void socket_setnonblock(int socket)
Definition: Utility.cpp:268

◆ bind()

bool FIX::SocketMonitor::bind ( )
private

◆ block()

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().

182 {
183  while ( m_dropped.size() )
184  {
185  strategy.onError( *this, m_dropped.front() );
186  m_dropped.pop();
187  if ( m_dropped.size() == 0 )
188  return ;
189  }
190 
191  fd_set readSet;
192  FD_ZERO( &readSet );
193  buildSet( m_readSockets, readSet );
194  fd_set writeSet;
195  FD_ZERO( &writeSet );
196  buildSet( m_connectSockets, writeSet );
197  buildSet( m_writeSockets, writeSet );
198  fd_set exceptSet;
199  FD_ZERO( &exceptSet );
200  buildSet( m_connectSockets, exceptSet );
201 
202  if ( sleepIfEmpty(poll) )
203  {
204  strategy.onTimeout( *this );
205  return;
206  }
207 
208  int result = select( FD_SETSIZE, &readSet, &writeSet, &exceptSet, getTimeval(poll, timeout) );
209 
210  if ( result == 0 )
211  {
212  strategy.onTimeout( *this );
213  return;
214  }
215  else if ( result > 0 )
216  {
217  processExceptSet( strategy, exceptSet );
218  processWriteSet( strategy, writeSet );
219  processReadSet( strategy, readSet );
220  }
221  else
222  {
223  strategy.onError( *this );
224  }
225 }
Sockets m_connectSockets
Definition: SocketMonitor.h:89
void processExceptSet(Strategy &, fd_set &)
void buildSet(const Sockets &, fd_set &)
void processReadSet(Strategy &, fd_set &)
bool sleepIfEmpty(bool poll)
void processWriteSet(Strategy &, fd_set &)
timeval * getTimeval(bool poll, double timeout)

◆ buildSet()

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

Definition at line 328 of file SocketMonitor.cpp.

Referenced by block().

329 {
330  Sockets::const_iterator iter;
331  for ( iter = sockets.begin(); iter != sockets.end(); ++iter ) {
332  FD_SET( *iter, &watchSet );
333  }
334 }

◆ drop()

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().

99 {
100  Sockets::iterator i = m_readSockets.find( s );
101  Sockets::iterator j = m_writeSockets.find( s );
102  Sockets::iterator k = m_connectSockets.find( s );
103 
104  if ( i != m_readSockets.end() ||
105  j != m_writeSockets.end() ||
106  k != m_connectSockets.end() )
107  {
108  socket_close( s );
109  m_readSockets.erase( s );
110  m_writeSockets.erase( s );
111  m_connectSockets.erase( s );
112  m_dropped.push( s );
113  return true;
114  }
115  return false;
116 }
Sockets m_connectSockets
Definition: SocketMonitor.h:89
void socket_close(int s)
Definition: Utility.cpp:180

◆ getTimeval()

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

Definition at line 118 of file SocketMonitor.cpp.

References m_ticks, m_timeout, and m_timeval.

Referenced by block().

119 {
120  if ( poll )
121  {
122  m_timeval.tv_sec = 0;
123  m_timeval.tv_usec = 0;
124  return &m_timeval;
125  }
126 
127  timeout = m_timeout;
128 
129  if ( !timeout )
130  return 0;
131 #ifdef SELECT_MODIFIES_TIMEVAL
132  if ( !m_timeval.tv_sec && !m_timeval.tv_usec && timeout )
133  m_timeval.tv_sec = timeout;
134  return &m_timeval;
135 #else
136  double elapsed = ( double ) ( clock() - m_ticks ) / ( double ) CLOCKS_PER_SEC;
137  if ( elapsed >= timeout || elapsed == 0.0 )
138  {
139  m_ticks = clock();
140  m_timeval.tv_sec = 0;
141  m_timeval.tv_usec = (long)(timeout * 1000000);
142  }
143  else
144  {
145  m_timeval.tv_sec = 0;
146  m_timeval.tv_usec = (long)( ( timeout - elapsed ) * 1000000 );
147  }
148  return &m_timeval;
149 #endif
150 }

◆ listen()

bool FIX::SocketMonitor::listen ( )
private

◆ numSockets()

size_t FIX::SocketMonitor::numSockets ( )
inline

Definition at line 63 of file SocketMonitor.h.

References m_readSockets.

64  { return m_readSockets.size() - 1; }

◆ processExceptSet()

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().

308 {
309 #ifdef _MSC_VER
310  for ( unsigned i = 0; i < exceptSet.fd_count; ++i )
311  {
312  int s = exceptSet.fd_array[ i ];
313  strategy.onError( *this, s );
314  }
315 #else
316  Sockets::iterator i;
317  Sockets sockets = m_connectSockets;
318  for ( i = sockets.begin(); i != sockets.end(); ++i )
319  {
320  int s = *i;
321  if ( !FD_ISSET( *i, &exceptSet ) )
322  continue;
323  strategy.onError( *this, s );
324  }
325 #endif
326 }
Sockets m_connectSockets
Definition: SocketMonitor.h:89
std::set< int > Sockets
Definition: SocketMonitor.h:67

◆ processReadSet()

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

Definition at line 227 of file SocketMonitor.cpp.

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

Referenced by block().

228 {
229 #ifdef _MSC_VER
230  for ( unsigned i = 0; i < readSet.fd_count; ++i )
231  {
232  int s = readSet.fd_array[ i ];
233  if( s == m_interrupt )
234  {
235  int socket = 0;
236  socket_recv( s, (char*)&socket, sizeof(socket) );
237  addWrite( socket );
238  }
239  else
240  {
241  strategy.onEvent( *this, s );
242  }
243  }
244 #else
245  Sockets::iterator i;
246  Sockets sockets = m_readSockets;
247  for ( i = sockets.begin(); i != sockets.end(); ++i )
248  {
249  int s = *i;
250  if ( !FD_ISSET( *i, &readSet ) )
251  continue;
252  if( s == m_interrupt )
253  {
254  int socket = 0;
255  socket_recv( s, (char*)&socket, sizeof(socket) );
256  addWrite( socket );
257  }
258  else
259  {
260  strategy.onEvent( *this, s );
261  }
262  }
263 #endif
264 }
bool addWrite(int socket)
ssize_t socket_recv(int s, char *buf, size_t length)
Definition: Utility.cpp:170
std::set< int > Sockets
Definition: SocketMonitor.h:67

◆ processWriteSet()

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().

267 {
268 #ifdef _MSC_VER
269  for ( unsigned i = 0; i < writeSet.fd_count; ++i )
270  {
271  int s = writeSet.fd_array[ i ];
272  if( m_connectSockets.find(s) != m_connectSockets.end() )
273  {
274  m_connectSockets.erase( s );
275  m_readSockets.insert( s );
276  strategy.onConnect( *this, s );
277  }
278  else
279  {
280  strategy.onWrite( *this, s );
281  }
282  }
283 #else
284  Sockets::iterator i;
285  Sockets sockets = m_connectSockets;
286  for( i = sockets.begin(); i != sockets.end(); ++i )
287  {
288  int s = *i;
289  if ( !FD_ISSET( *i, &writeSet ) )
290  continue;
291  m_connectSockets.erase( s );
292  m_readSockets.insert( s );
293  strategy.onConnect( *this, s );
294  }
295 
296  sockets = m_writeSockets;
297  for( i = sockets.begin(); i != sockets.end(); ++i )
298  {
299  int s = *i;
300  if ( !FD_ISSET( *i, &writeSet ) )
301  continue;
302  strategy.onWrite( *this, s );
303  }
304 #endif
305 }
Sockets m_connectSockets
Definition: SocketMonitor.h:89
std::set< int > Sockets
Definition: SocketMonitor.h:67

◆ setsockopt()

void FIX::SocketMonitor::setsockopt ( )
private

◆ signal()

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().

169 {
170  socket_send( m_signal, (char*)&socket, sizeof(socket) );
171 }
ssize_t socket_send(int s, const char *msg, size_t length)
Definition: Utility.cpp:175

◆ sleepIfEmpty()

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

Definition at line 152 of file SocketMonitor.cpp.

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

Referenced by block().

153 {
154  if( poll )
155  return false;
156 
157  if ( m_readSockets.empty() &&
158  m_writeSockets.empty() &&
159  m_connectSockets.empty() )
160  {
162  return true;
163  }
164  else
165  return false;
166 }
Sockets m_connectSockets
Definition: SocketMonitor.h:89
void process_sleep(double s)
Definition: Utility.cpp:466

◆ unsignal()

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

Definition at line 173 of file SocketMonitor.cpp.

References m_writeSockets.

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

174 {
175  Sockets::iterator i = m_writeSockets.find( s );
176  if( i == m_writeSockets.end() ) return;
177 
178  m_writeSockets.erase( s );
179 }

Member Data Documentation

◆ m_connectSockets

Sockets FIX::SocketMonitor::m_connectSockets
private

Definition at line 89 of file SocketMonitor.h.

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

◆ m_dropped

Queue FIX::SocketMonitor::m_dropped
private

Definition at line 92 of file SocketMonitor.h.

Referenced by block(), and drop().

◆ m_interrupt

int FIX::SocketMonitor::m_interrupt
private

Definition at line 88 of file SocketMonitor.h.

Referenced by processReadSet(), and SocketMonitor().

◆ m_readSockets

Sockets FIX::SocketMonitor::m_readSockets
private

◆ m_signal

int FIX::SocketMonitor::m_signal
private

Definition at line 87 of file SocketMonitor.h.

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

◆ m_ticks

clock_t FIX::SocketMonitor::m_ticks
private

Definition at line 84 of file SocketMonitor.h.

Referenced by getTimeval(), and SocketMonitor().

◆ m_timeout

int FIX::SocketMonitor::m_timeout
private

Definition at line 81 of file SocketMonitor.h.

Referenced by getTimeval(), and sleepIfEmpty().

◆ m_timeval

timeval FIX::SocketMonitor::m_timeval
private

Definition at line 82 of file SocketMonitor.h.

Referenced by getTimeval(), and SocketMonitor().

◆ m_writeSockets

Sockets FIX::SocketMonitor::m_writeSockets
private

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 Wed Aug 28 2019 14:13:46 for QuickFIX by doxygen 1.8.13 written by Dimitri van Heesch, © 1997-2001