SocketMonitor.h 2.7 KB

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