Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
FIX::SocketConnection Class Reference

Encapsulates a socket file descriptor (single-threaded). More...

#include <SocketConnection.h>

Inheritance diagram for FIX::SocketConnection:
Inheritance graph
[legend]
Collaboration diagram for FIX::SocketConnection:
Collaboration graph
[legend]

Public Types

typedef std::set< SessionIDSessions
 

Public Member Functions

 SocketConnection (int s, Sessions sessions, SocketMonitor *pMonitor)
 
 SocketConnection (SocketInitiator &, const SessionID &, int, SocketMonitor *)
 
virtual ~SocketConnection ()
 
int getSocket () const
 
SessiongetSession () const
 
bool read (SocketConnector &s)
 
bool read (SocketAcceptor &, SocketServer &)
 
bool processQueue ()
 
void signal ()
 
void unsignal ()
 
void onTimeout ()
 

Private Types

typedef std::deque< std::string, ALLOCATOR< std::string > > Queue
 

Private Member Functions

bool isValidSession ()
 
void readFromSocket () throw ( SocketRecvFailed )
 
bool readMessage (std::string &msg)
 
void readMessages (SocketMonitor &s)
 
bool send (const std::string &)
 
void disconnect ()
 
- Private Member Functions inherited from FIX::Responder
virtual ~Responder ()
 

Private Attributes

int m_socket
 
char m_buffer [BUFSIZ]
 
Parser m_parser
 
Queue m_sendQueue
 
unsigned m_sendLength
 
Sessions m_sessions
 
Sessionm_pSession
 
SocketMonitorm_pMonitor
 
Mutex m_mutex
 
fd_set m_fds
 

Detailed Description

Encapsulates a socket file descriptor (single-threaded).

Definition at line 63 of file SocketConnection.h.

Member Typedef Documentation

◆ Queue

typedef std::deque<std::string, ALLOCATOR<std::string> > FIX::SocketConnection::Queue
private

Definition at line 97 of file SocketConnection.h.

◆ Sessions

Definition at line 66 of file SocketConnection.h.

Constructor & Destructor Documentation

◆ SocketConnection() [1/2]

FIX::SocketConnection::SocketConnection ( int  s,
Sessions  sessions,
SocketMonitor pMonitor 
)

Definition at line 52 of file SocketConnection.cpp.

◆ SocketConnection() [2/2]

FIX::SocketConnection::SocketConnection ( SocketInitiator i,
const SessionID sessionID,
int  s,
SocketMonitor pMonitor 
)

Definition at line 61 of file SocketConnection.cpp.

63 {
64  Locker l( m_mutex );
65 
66  m_sendQueue.push_back( msg );
67  processQueue();
68  signal();
69  return true;
70 }
71 

References m_mutex, m_sendQueue, processQueue(), and signal().

◆ ~SocketConnection()

FIX::SocketConnection::~SocketConnection ( )
virtual

Definition at line 73 of file SocketConnection.cpp.

73 {
74  Locker l( m_mutex );
75 
76  if( !m_sendQueue.size() ) return true;
77 

References m_mutex, and m_sendQueue.

Member Function Documentation

◆ disconnect()

void FIX::SocketConnection::disconnect ( )
privatevirtual

Implements FIX::Responder.

Definition at line 117 of file SocketConnection.cpp.

◆ getSession()

Session* FIX::SocketConnection::getSession ( ) const
inline

Definition at line 73 of file SocketConnection.h.

78 :
79  typedef std::deque<std::string, ALLOCATOR<std::string> >

Referenced by FIX::SocketAcceptor::onWrite().

◆ getSocket()

int FIX::SocketConnection::getSocket ( ) const
inline

Definition at line 72 of file SocketConnection.h.

78 :
79  typedef std::deque<std::string, ALLOCATOR<std::string> >

◆ isValidSession()

bool FIX::SocketConnection::isValidSession ( )
private

Definition at line 204 of file SocketConnection.cpp.

206 {
207  try
208  {
209  return m_parser.readFixMessage( msg );
210  }
211  catch ( MessageParseError& ) {}
212  return true;

References m_parser, and FIX::Parser::readFixMessage().

◆ onTimeout()

void FIX::SocketConnection::onTimeout ( )

Definition at line 251 of file SocketConnection.cpp.

◆ processQueue()

bool FIX::SocketConnection::processQueue ( )

Definition at line 89 of file SocketConnection.cpp.

92  {
93  m_sendLength = 0;
94  m_sendQueue.pop_front();
95  }
96 
97  return !m_sendQueue.size();
98 }
99 
101 {
102  if ( m_pMonitor )
104 }
105 
106 bool SocketConnection::read( SocketConnector& s )
107 {
108  if ( !m_pSession ) return false;
109 
110  try
111  {
112  readFromSocket();
113  readMessages( s.getMonitor() );
114  }
115  catch( SocketRecvFailed& e )

References m_sendLength, and m_sendQueue.

Referenced by FIX::SocketAcceptor::onConnect(), and SocketConnection().

◆ read() [1/2]

bool FIX::SocketConnection::read ( SocketAcceptor a,
SocketServer s 
)

Definition at line 140 of file SocketConnection.cpp.

146  {
147  m_pSession = 0;
148  if( a.getLog() )
149  {
150  a.getLog()->onEvent( "Session not found for incoming message: " + msg );
151  a.getLog()->onIncoming( msg );
152  }
153  }
154  if( m_pSession )
155  m_pSession = a.getSession( msg, *this );
156  if( m_pSession )
157  m_pSession->next( msg, UtcTimeStamp() );
158  if( !m_pSession )
159  {
160  s.getMonitor().drop( m_socket );
161  return false;
162  }
163 
165  return true;
166  }
167  else
168  {
169  readFromSocket();
170  readMessages( s.getMonitor() );
171  return true;
172  }
173  }
174  catch ( SocketRecvFailed& e )
175  {
176  if( m_pSession )
177  m_pSession->getLog()->onEvent( e.what() );
178  s.getMonitor().drop( m_socket );
179  }
180  catch ( InvalidMessage& )
181  {
182  s.getMonitor().drop( m_socket );
183  }
184  return false;
185 }
186 
188 {
189  if( m_pSession == 0 )
190  return false;
191  SessionID sessionID = m_pSession->getSessionID();
192  if( Session::isSessionRegistered(sessionID) )
193  return false;
194  return !( m_sessions.find(sessionID) == m_sessions.end() );
195 }
196 
198 throw( SocketRecvFailed )
199 {
200  ssize_t size = socket_recv( m_socket, m_buffer, sizeof(m_buffer) );
201  if( size <= 0 ) throw SocketRecvFailed( size );
202  m_parser.addToStream( m_buffer, size );

◆ read() [2/2]

bool FIX::SocketConnection::read ( SocketConnector s)

Definition at line 123 of file SocketConnection.cpp.

124 {
125  std::string msg;
126  try
127  {
128  if ( !m_pSession )
129  {
130  struct timeval timeout = { 1, 0 };
131  fd_set readset = m_fds;
132 
133  while( !readMessage( msg ) )
134  {
135  int result = select( 1 + m_socket, &readset, 0, 0, &timeout );
136  if( result > 0 )
137  readFromSocket();
138  else if( result == 0 )

References m_fds, m_pSession, m_socket, readFromSocket(), and readMessage().

◆ readFromSocket()

void FIX::SocketConnection::readFromSocket ( )
throw (SocketRecvFailed
)
private

Definition at line 214 of file SocketConnection.cpp.

216 {
217  if( !m_pSession ) return;
218 
219  std::string msg;
220  while( readMessage( msg ) )

References m_pSession, and readMessage().

Referenced by read().

◆ readMessage()

bool FIX::SocketConnection::readMessage ( std::string &  msg)
private

Definition at line 222 of file SocketConnection.cpp.

223  {
224  m_pSession->next( msg, UtcTimeStamp() );
225  }
226  catch ( InvalidMessage& )
227  {
228  if( !m_pSession->isLoggedOn() )
229  s.drop( m_socket );
230  }

References m_pSession, FIX::Session::next(), and FIX::TYPE::UtcTimeStamp.

Referenced by read(), and readFromSocket().

◆ readMessages()

void FIX::SocketConnection::readMessages ( SocketMonitor s)
private

Definition at line 232 of file SocketConnection.cpp.

235 {
236  if ( m_pSession ) m_pSession->next();
237 }
238 } // namespace FIX

◆ send()

bool FIX::SocketConnection::send ( const std::string &  msg)
privatevirtual

Implements FIX::Responder.

Definition at line 79 of file SocketConnection.cpp.

92  {

◆ signal()

void FIX::SocketConnection::signal ( )
inline

Definition at line 79 of file SocketConnection.h.

Referenced by SocketConnection().

◆ unsignal()

void FIX::SocketConnection::unsignal ( )
inline

Definition at line 86 of file SocketConnection.h.

Referenced by FIX::SocketAcceptor::onConnect().

Member Data Documentation

◆ m_buffer

char FIX::SocketConnection::m_buffer[BUFSIZ]
private

Definition at line 107 of file SocketConnection.h.

◆ m_fds

fd_set FIX::SocketConnection::m_fds
private

Definition at line 116 of file SocketConnection.h.

Referenced by read().

◆ m_mutex

Mutex FIX::SocketConnection::m_mutex
private

Definition at line 115 of file SocketConnection.h.

Referenced by SocketConnection(), and ~SocketConnection().

◆ m_parser

Parser FIX::SocketConnection::m_parser
private

Definition at line 109 of file SocketConnection.h.

Referenced by isValidSession().

◆ m_pMonitor

SocketMonitor* FIX::SocketConnection::m_pMonitor
private

Definition at line 114 of file SocketConnection.h.

◆ m_pSession

Session* FIX::SocketConnection::m_pSession
private

Definition at line 113 of file SocketConnection.h.

Referenced by read(), readFromSocket(), and readMessage().

◆ m_sendLength

unsigned FIX::SocketConnection::m_sendLength
private

Definition at line 111 of file SocketConnection.h.

Referenced by processQueue().

◆ m_sendQueue

Queue FIX::SocketConnection::m_sendQueue
private

Definition at line 110 of file SocketConnection.h.

Referenced by processQueue(), SocketConnection(), and ~SocketConnection().

◆ m_sessions

Sessions FIX::SocketConnection::m_sessions
private

Definition at line 112 of file SocketConnection.h.

◆ m_socket

int FIX::SocketConnection::m_socket
private

Definition at line 106 of file SocketConnection.h.

Referenced by read().


The documentation for this class was generated from the following files:
FIX::SocketConnection::m_socket
int m_socket
Definition: SocketConnection.h:106
FIX::Session::unregisterSession
static void unregisterSession(const SessionID &)
Definition: Session.cpp:1564
FIX::SocketConnection::m_pSession
Session * m_pSession
Definition: SocketConnection.h:113
FIX::SocketConnection::m_fds
fd_set m_fds
Definition: SocketConnection.h:116
FIX::Session::getSessionID
const SessionID & getSessionID() const
Definition: Session.h:109
FIX::SocketConnection::readMessage
bool readMessage(std::string &msg)
Definition: SocketConnection.cpp:222
FIX::SocketConnection::m_parser
Parser m_parser
Definition: SocketConnection.h:109
FIX::SocketConnection::readFromSocket
void readFromSocket()
Definition: SocketConnection.cpp:214
FIX::Session::registerSession
static Session * registerSession(const SessionID &)
Definition: Session.cpp:1554
FIX::Session::next
void next()
Definition: Session.cpp:142
FIX::socket_recv
ssize_t socket_recv(int s, char *buf, size_t length)
Definition: Utility.cpp:187
FIX::SocketConnection::m_sendLength
unsigned m_sendLength
Definition: SocketConnection.h:111
FIX::TYPE::UtcTimeStamp
@ UtcTimeStamp
Definition: FieldTypes.h:940
FIX::Log::onEvent
virtual void onEvent(const std::string &)=0
FIX::Session::getLog
Log * getLog()
Definition: Session.h:261
FIX::Session::isSessionRegistered
static bool isSessionRegistered(const SessionID &)
Definition: Session.cpp:1548
FIX::SocketConnection::isValidSession
bool isValidSession()
Definition: SocketConnection.cpp:204
FIX::SocketConnection::m_buffer
char m_buffer[BUFSIZ]
Definition: SocketConnection.h:107
FIX::Session::isLoggedOn
bool isLoggedOn()
Definition: Session.h:99
FIX::SocketConnection::disconnect
void disconnect()
Definition: SocketConnection.cpp:117
FIX::SocketConnection::m_sendQueue
Queue m_sendQueue
Definition: SocketConnection.h:110
FIX::SocketConnection::processQueue
bool processQueue()
Definition: SocketConnection.cpp:89
FIX::SocketConnection::m_mutex
Mutex m_mutex
Definition: SocketConnection.h:115
FIX::SocketMonitor::drop
bool drop(int socket)
Definition: SocketMonitor.cpp:115
FIX::Parser::addToStream
void addToStream(const char *str, size_t len)
Definition: Parser.h:82
FIX::Parser::readFixMessage
bool readFixMessage(std::string &str)
Definition: Parser.cpp:76
FIX::SocketConnection::signal
void signal()
Definition: SocketConnection.h:79
FIX::SocketConnection::read
bool read(SocketConnector &s)
Definition: SocketConnection.cpp:123
FIX::SocketConnection::m_pMonitor
SocketMonitor * m_pMonitor
Definition: SocketConnection.h:114
FIX::SocketConnection::m_sessions
Sessions m_sessions
Definition: SocketConnection.h:112
FIX::SocketConnection::readMessages
void readMessages(SocketMonitor &s)
Definition: SocketConnection.cpp:232

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001