SocketConnection.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_SOCKETCONNECTION_H
  21. #define FIX_SOCKETCONNECTION_H
  22. #ifdef _MSC_VER
  23. #pragma warning( disable : 4503 4355 4786 4290 )
  24. #endif
  25. #include "Parser.h"
  26. #include "Responder.h"
  27. #include "SessionID.h"
  28. #include "SocketMonitor.h"
  29. #include "Utility.h"
  30. #include "Mutex.h"
  31. #include <set>
  32. namespace FIX
  33. {
  34. class SocketAcceptor;
  35. class SocketServer;
  36. class SocketConnector;
  37. class SocketInitiator;
  38. class Session;
  39. /// Encapsulates a socket file descriptor (single-threaded).
  40. class SocketConnection : Responder
  41. {
  42. public:
  43. typedef std::set<SessionID> Sessions;
  44. SocketConnection( int s, Sessions sessions, SocketMonitor* pMonitor );
  45. SocketConnection( SocketInitiator&, const SessionID&, int, SocketMonitor* );
  46. virtual ~SocketConnection();
  47. int getSocket() const { return m_socket; }
  48. Session* getSession() const { return m_pSession; }
  49. bool read( SocketConnector& s );
  50. bool read( SocketAcceptor&, SocketServer& );
  51. bool processQueue();
  52. void signal()
  53. {
  54. Locker l( m_mutex );
  55. if( m_sendQueue.size() == 1 )
  56. m_pMonitor->signal( m_socket );
  57. }
  58. void unsignal()
  59. {
  60. Locker l( m_mutex );
  61. if( m_sendQueue.size() == 0 )
  62. m_pMonitor->unsignal( m_socket );
  63. }
  64. void onTimeout();
  65. private:
  66. typedef std::deque<std::string, ALLOCATOR<std::string> >
  67. Queue;
  68. bool isValidSession();
  69. void readFromSocket() throw( SocketRecvFailed );
  70. bool readMessage( std::string& msg );
  71. void readMessages( SocketMonitor& s );
  72. bool send( const std::string& );
  73. void disconnect();
  74. int m_socket;
  75. char m_buffer[BUFSIZ];
  76. Parser m_parser;
  77. Queue m_sendQueue;
  78. unsigned m_sendLength;
  79. Sessions m_sessions;
  80. Session* m_pSession;
  81. SocketMonitor* m_pMonitor;
  82. Mutex m_mutex;
  83. fd_set m_fds;
  84. };
  85. }
  86. #endif //FIX_SOCKETCONNECTION_H