SocketMonitor.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 
3 /****************************************************************************
4 ** Copyright (c) 2001-2014
5 **
6 ** This file is part of the QuickFIX FIX Engine
7 **
8 ** This file may be distributed under the terms of the quickfixengine.org
9 ** license as defined by quickfixengine.org and appearing in the file
10 ** LICENSE included in the packaging of this file.
11 **
12 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
13 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 **
15 ** See http://www.quickfixengine.org/LICENSE for licensing information.
16 **
17 ** Contact ask@quickfixengine.org if any conditions of this licensing are
18 ** not clear to you.
19 **
20 ****************************************************************************/
21 
22 #ifndef FIX_SOCKETMONITOR_H
23 #define FIX_SOCKETMONITOR_H
24 
25 #ifdef _MSC_VER
26 #pragma warning( disable : 4503 4355 4786 4290 )
27 #endif
28 
29 #ifdef _MSC_VER
30 #include <Winsock2.h>
31 typedef int socklen_t;
32 #else
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/time.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #endif
39 
40 #include <set>
41 #include <queue>
42 #include <time.h>
43 
44 namespace FIX
45 {
47 class SocketMonitor
48 {
49 public:
50  class Strategy;
51 
52  SocketMonitor( int timeout = 0 );
53  virtual ~SocketMonitor();
54 
55  bool addConnect( int socket );
56  bool addRead( int socket );
57  bool addWrite( int socket );
58  bool drop( int socket );
59  void signal( int socket );
60  void unsignal( int socket );
61  void block( Strategy& strategy, bool poll = 0, double timeout = 0.0 );
62 
63  size_t numSockets()
64  { return m_readSockets.size() - 1; }
65 
66 private:
67  typedef std::set < int > Sockets;
68  typedef std::queue < int > Queue;
69 
70  void setsockopt();
71  bool bind();
72  bool listen();
73  void buildSet( const Sockets&, fd_set& );
74  inline timeval* getTimeval( bool poll, double timeout );
75  inline bool sleepIfEmpty( bool poll );
76 
77  void processReadSet( Strategy&, fd_set& );
78  void processWriteSet( Strategy&, fd_set& );
79  void processExceptSet( Strategy&, fd_set& );
80 
81  int m_timeout;
82  timeval m_timeval;
83 #ifndef SELECT_DECREMENTS_TIME
84  clock_t m_ticks;
85 #endif
86 
87  int m_signal;
88  int m_interrupt;
93 
94 public:
95  class Strategy
96  {
97  public:
98  virtual ~Strategy()
99  {}
100  virtual void onConnect( SocketMonitor&, int socket ) = 0;
101  virtual void onEvent( SocketMonitor&, int socket ) = 0;
102  virtual void onWrite( SocketMonitor&, int socket ) = 0;
103  virtual void onError( SocketMonitor&, int socket ) = 0;
104  virtual void onError( SocketMonitor& ) = 0;
105  virtual void onTimeout( SocketMonitor& )
106  {}}
107  ;
108 };
109 }
110 
111 #endif //FIX_SOCKETMONITOR_H
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::addRead
bool addRead(int socket)
Definition: SocketMonitor.cpp:92
FIX::SocketMonitor::m_connectSockets
Sockets m_connectSockets
Definition: SocketMonitor.h:123
FIX::SocketMonitor::Queue
std::queue< int > Queue
Definition: SocketMonitor.h:102
FIX::SocketMonitor::addWrite
bool addWrite(int socket)
Definition: SocketMonitor.cpp:102
FIX::SocketMonitor::Strategy::onWrite
virtual void onWrite(SocketMonitor &, int socket)=0
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
Monitors events on a collection of sockets.
Definition: SocketMonitor.h:64
FIX::SocketMonitor::signal
void signal(int socket)
Definition: SocketMonitor.cpp:185
FIX::SocketMonitor::buildSet
void buildSet(const Sockets &, fd_set &)
Definition: SocketMonitor.cpp:345
FIX::SocketMonitor::addConnect
bool addConnect(int socket)
Definition: SocketMonitor.cpp:82
FIX::SocketMonitor::sleepIfEmpty
bool sleepIfEmpty(bool poll)
Definition: SocketMonitor.cpp:169
FIX::SocketMonitor::numSockets
size_t numSockets()
Definition: SocketMonitor.h:97
FIX::SocketMonitor::Strategy::onEvent
virtual void onEvent(SocketMonitor &, int socket)=0
FIX::SocketMonitor::bind
bool bind()
FIX::SocketMonitor::m_writeSockets
Sockets m_writeSockets
Definition: SocketMonitor.h:125
FIX::SocketMonitor::Strategy::onConnect
virtual void onConnect(SocketMonitor &, int socket)=0
FIX::SocketMonitor::getTimeval
timeval * getTimeval(bool poll, double timeout)
Definition: SocketMonitor.cpp:135
FIX::SocketMonitor::m_ticks
clock_t m_ticks
Definition: SocketMonitor.h:118
FIX
Definition: Acceptor.cpp:34
FIX::SocketMonitor::setsockopt
void setsockopt()
FIX::Queue
A thread safe monitored queue.
Definition: Queue.h:50
FIX::SocketMonitor::drop
bool drop(int socket)
Definition: SocketMonitor.cpp:115
FIX::SocketMonitor::Strategy::onTimeout
virtual void onTimeout(SocketMonitor &)
Definition: SocketMonitor.h:139
FIX::SocketMonitor::processExceptSet
void processExceptSet(Strategy &, fd_set &)
Definition: SocketMonitor.cpp:324
FIX::SocketMonitor::listen
bool listen()
FIX::SocketMonitor::m_timeval
timeval m_timeval
Definition: SocketMonitor.h:116
FIX::SocketMonitor::SocketMonitor
SocketMonitor(int timeout=0)
Definition: SocketMonitor.cpp:52
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::Strategy::onError
virtual void onError(SocketMonitor &, int socket)=0
FIX::SocketMonitor::Strategy::~Strategy
virtual ~Strategy()
Definition: SocketMonitor.h:132
FIX::SocketMonitor::m_dropped
Queue m_dropped
Definition: SocketMonitor.h:126
FIX::SocketMonitor::~SocketMonitor
virtual ~SocketMonitor()
Definition: SocketMonitor.cpp:71

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001