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 {
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;
90  Sockets m_readSockets;
91  Sockets m_writeSockets;
92  Queue m_dropped;
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
void unsignal(int socket)
virtual void onError(SocketMonitor &, int socket)=0
Sockets m_connectSockets
Definition: SocketMonitor.h:89
virtual void onTimeout(SocketMonitor &)
bool addWrite(int socket)
void processExceptSet(Strategy &, fd_set &)
bool addRead(int socket)
virtual void onEvent(SocketMonitor &, int socket)=0
virtual ~SocketMonitor()
virtual void onWrite(SocketMonitor &, int socket)=0
Definition: Acceptor.cpp:34
void buildSet(const Sockets &, fd_set &)
Monitors events on a collection of sockets.
Definition: SocketMonitor.h:47
bool addConnect(int socket)
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
void processReadSet(Strategy &, fd_set &)
SocketMonitor(int timeout=0)
bool sleepIfEmpty(bool poll)
bool drop(int socket)
virtual void onConnect(SocketMonitor &, int socket)=0
std::queue< int > Queue
Definition: SocketMonitor.h:68
void processWriteSet(Strategy &, fd_set &)
timeval * getTimeval(bool poll, double timeout)
std::set< int > Sockets
Definition: SocketMonitor.h:67
void signal(int socket)

Generated on Wed Aug 28 2019 14:13:46 for QuickFIX by doxygen 1.8.13 written by Dimitri van Heesch, © 1997-2001