ThreadedSocketAcceptor.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_THREADEDSOCKETACCEPTOR_H
  21. #define FIX_THREADEDSOCKETACCEPTOR_H
  22. #ifdef _MSC_VER
  23. #pragma warning( disable : 4503 4355 4786 4290 )
  24. #endif
  25. #include "Acceptor.h"
  26. #include "ThreadedSocketConnection.h"
  27. #include "Mutex.h"
  28. namespace FIX
  29. {
  30. /// Threaded Socket implementation of Acceptor.
  31. class ThreadedSocketAcceptor : public Acceptor
  32. {
  33. friend class SocketConnection;
  34. public:
  35. ThreadedSocketAcceptor( Application&, MessageStoreFactory&,
  36. const SessionSettings& ) throw( ConfigError );
  37. ThreadedSocketAcceptor( Application&, MessageStoreFactory&,
  38. const SessionSettings&,
  39. LogFactory& ) throw( ConfigError );
  40. virtual ~ThreadedSocketAcceptor();
  41. private:
  42. struct AcceptorThreadInfo
  43. {
  44. AcceptorThreadInfo( ThreadedSocketAcceptor* pAcceptor, int socket, int port )
  45. : m_pAcceptor( pAcceptor ), m_socket( socket ), m_port( port ) {}
  46. ThreadedSocketAcceptor* m_pAcceptor;
  47. int m_socket;
  48. int m_port;
  49. };
  50. struct ConnectionThreadInfo
  51. {
  52. ConnectionThreadInfo( ThreadedSocketAcceptor* pAcceptor,
  53. ThreadedSocketConnection* pConnection )
  54. : m_pAcceptor( pAcceptor ), m_pConnection( pConnection ) {}
  55. ThreadedSocketAcceptor* m_pAcceptor;
  56. ThreadedSocketConnection* m_pConnection;
  57. };
  58. bool readSettings( const SessionSettings& );
  59. typedef std::set < int > Sockets;
  60. typedef std::set < SessionID > Sessions;
  61. typedef std::map < int, Sessions > PortToSessions;
  62. typedef std::map < int, int > SocketToPort;
  63. typedef std::map < int, thread_id > SocketToThread;
  64. void onConfigure( const SessionSettings& ) throw ( ConfigError );
  65. void onInitialize( const SessionSettings& ) throw ( RuntimeError );
  66. void onStart();
  67. bool onPoll( double timeout );
  68. void onStop();
  69. void addThread( int s, thread_id t );
  70. void removeThread( int s );
  71. static THREAD_PROC socketAcceptorThread( void* p );
  72. static THREAD_PROC socketConnectionThread( void* p );
  73. Sockets m_sockets;
  74. PortToSessions m_portToSessions;
  75. SocketToPort m_socketToPort;
  76. SocketToThread m_threads;
  77. Mutex m_mutex;
  78. };
  79. /*! @} */
  80. }
  81. #endif //FIX_THREADEDSOCKETACCEPTOR_H