SocketServer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_SOCKETSERVER_H
  21. #define FIX_SOCKETSERVER_H
  22. #ifdef _MSC_VER
  23. #pragma warning( disable : 4503 4355 4786 4290 )
  24. #endif
  25. #include "SocketMonitor.h"
  26. #include "Exceptions.h"
  27. #include <map>
  28. #include <set>
  29. #include <queue>
  30. namespace FIX
  31. {
  32. /// Information about listening socket
  33. struct SocketInfo
  34. {
  35. SocketInfo()
  36. : m_socket( -1 ), m_port( 0 ), m_noDelay( false ),
  37. m_sendBufSize( 0 ), m_rcvBufSize( 0 ) {}
  38. SocketInfo( int socket, short port, bool noDelay, int sendBufSize, int rcvBufSize )
  39. : m_socket( socket ), m_port( port ), m_noDelay( noDelay ),
  40. m_sendBufSize( sendBufSize ), m_rcvBufSize( rcvBufSize ) {}
  41. int m_socket;
  42. short m_port;
  43. bool m_noDelay;
  44. int m_sendBufSize;
  45. int m_rcvBufSize;
  46. };
  47. /// Listens for and accepts incoming socket connections on a port.
  48. class SocketServer
  49. {
  50. public:
  51. class Strategy;
  52. SocketServer( int timeout = 0 );
  53. int add( int port, bool reuse = false, bool noDelay = false,
  54. int sendBufSize = 0, int rcvBufSize = 0 ) throw( SocketException& );
  55. int accept( int socket );
  56. void close();
  57. bool block( Strategy& strategy, bool poll = 0, double timeout = 0.0 );
  58. int numConnections() { return m_monitor.numSockets() - 1; }
  59. SocketMonitor& getMonitor() { return m_monitor; }
  60. int socketToPort( int socket );
  61. int portToSocket( int port );
  62. private:
  63. typedef std::map<int, SocketInfo>
  64. SocketToInfo;
  65. typedef std::map<int, SocketInfo>
  66. PortToInfo;
  67. SocketToInfo m_socketToInfo;
  68. PortToInfo m_portToInfo;
  69. SocketMonitor m_monitor;
  70. public:
  71. class Strategy
  72. {
  73. public:
  74. virtual ~Strategy() {}
  75. virtual void onConnect( SocketServer&, int acceptSocket, int socket ) = 0;
  76. virtual void onWrite( SocketServer&, int socket ) = 0;
  77. virtual bool onData( SocketServer&, int socket ) = 0;
  78. virtual void onDisconnect( SocketServer&, int socket ) = 0;
  79. virtual void onError( SocketServer& ) = 0;
  80. virtual void onTimeout( SocketServer& ) {};
  81. };
  82. };
  83. }
  84. #endif //FIX_SOCKETSERVER_H