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 46 of file SocketConnection.h.

Member Typedef Documentation

◆ Queue

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

Definition at line 80 of file SocketConnection.h.

◆ Sessions

Definition at line 49 of file SocketConnection.h.

Constructor & Destructor Documentation

◆ SocketConnection() [1/2]

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

Definition at line 35 of file SocketConnection.cpp.

37: m_socket( s ), m_sendLength( 0 ),
38 m_sessions(sessions), m_pSession( 0 ), m_pMonitor( pMonitor )
39{
40 FD_ZERO( &m_fds );
41 FD_SET( m_socket, &m_fds );
42}
SocketMonitor * m_pMonitor

References m_fds, and m_socket.

◆ SocketConnection() [2/2]

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

Definition at line 44 of file SocketConnection.cpp.

47: m_socket( s ), m_sendLength( 0 ),
48 m_pSession( i.getSession( sessionID, *this ) ),
49 m_pMonitor( pMonitor )
50{
51 FD_ZERO( &m_fds );
52 FD_SET( m_socket, &m_fds );
53 m_sessions.insert( sessionID );
54}

References m_fds, m_sessions, and m_socket.

◆ ~SocketConnection()

FIX::SocketConnection::~SocketConnection ( )
virtual

Definition at line 56 of file SocketConnection.cpp.

57{
58 if ( m_pSession )
60}
static void unregisterSession(const SessionID &)
Definition Session.cpp:1547
const SessionID & getSessionID() const
Definition Session.h:75

References FIX::Session::getSessionID(), m_pSession, and FIX::Session::unregisterSession().

Member Function Documentation

◆ disconnect()

void FIX::SocketConnection::disconnect ( )
privatevirtual

Implements FIX::Responder.

Definition at line 100 of file SocketConnection.cpp.

101{
102 if ( m_pMonitor )
104}
bool drop(int socket)

References FIX::SocketMonitor::drop(), m_pMonitor, and m_socket.

◆ getSession()

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

◆ getSocket()

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

Definition at line 55 of file SocketConnection.h.

55{ return m_socket; }

References m_socket.

◆ isValidSession()

bool FIX::SocketConnection::isValidSession ( )
private

Definition at line 187 of file SocketConnection.cpp.

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}
static bool isSessionRegistered(const SessionID &)
Definition Session.cpp:1531

References FIX::Session::getSessionID(), FIX::Session::isSessionRegistered(), m_pSession, and m_sessions.

Referenced by read().

◆ onTimeout()

void FIX::SocketConnection::onTimeout ( )

Definition at line 234 of file SocketConnection.cpp.

235{
236 if ( m_pSession ) m_pSession->next();
237}
void next()
Definition Session.cpp:125

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

Referenced by FIX::SocketInitiator::onConnect(), and FIX::SocketAcceptor::onTimeout().

◆ processQueue()

bool FIX::SocketConnection::processQueue ( )

Definition at line 72 of file SocketConnection.cpp.

73{
74 Locker l( m_mutex );
75
76 if( !m_sendQueue.size() ) return true;
77
78 struct timeval timeout = { 0, 0 };
79 fd_set writeset = m_fds;
80 if( select( 1 + m_socket, 0, &writeset, 0, &timeout ) <= 0 )
81 return false;
82
83 const std::string& msg = m_sendQueue.front();
84
85 ssize_t result = socket_send
86 ( m_socket, msg.c_str() + m_sendLength, msg.length() - m_sendLength );
87
88 if( result > 0 )
89 m_sendLength += result;
90
91 if( m_sendLength == msg.length() )
92 {
93 m_sendLength = 0;
94 m_sendQueue.pop_front();
95 }
96
97 return !m_sendQueue.size();
98}
ssize_t socket_send(int s, const char *msg, size_t length)
Definition Utility.cpp:175

References m_fds, m_mutex, m_sendLength, m_sendQueue, m_socket, and FIX::socket_send().

Referenced by FIX::SocketInitiator::onWrite(), FIX::SocketAcceptor::onWrite(), and send().

◆ read() [1/2]

bool FIX::SocketConnection::read ( SocketAcceptor a,
SocketServer 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 )
138 else if( result == 0 )
139 return false;
140 else if( result < 0 )
141 return false;
142 }
143
144 m_pSession = Session::lookupSession( msg, true );
145 if( !isValidSession() )
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 {
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}
virtual void onEvent(const std::string &)=0
static Session * registerSession(const SessionID &)
Definition Session.cpp:1537
static Session * lookupSession(const SessionID &)
Definition Session.cpp:1496
Log * getLog()
Definition Session.h:227
void readMessages(SocketMonitor &s)
bool readMessage(std::string &msg)

References FIX::SocketMonitor::drop(), FIX::Acceptor::getLog(), FIX::Session::getLog(), FIX::SocketServer::getMonitor(), FIX::Acceptor::getSession(), FIX::Session::getSessionID(), isValidSession(), FIX::Session::lookupSession(), m_fds, m_pSession, m_socket, FIX::Session::next(), FIX::Log::onEvent(), FIX::Log::onIncoming(), readFromSocket(), readMessage(), readMessages(), and FIX::Session::registerSession().

◆ read() [2/2]

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

Definition at line 106 of file SocketConnection.cpp.

107{
108 if ( !m_pSession ) return false;
109
110 try
111 {
113 readMessages( s.getMonitor() );
114 }
115 catch( SocketRecvFailed& e )
116 {
117 m_pSession->getLog()->onEvent( e.what() );
118 return false;
119 }
120 return true;
121}

References FIX::Session::getLog(), FIX::SocketConnector::getMonitor(), m_pSession, FIX::Log::onEvent(), readFromSocket(), and readMessages().

Referenced by FIX::SocketInitiator::onData(), and FIX::SocketAcceptor::onData().

◆ readFromSocket()

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

Definition at line 197 of file SocketConnection.cpp.

199{
200 ssize_t size = socket_recv( m_socket, m_buffer, sizeof(m_buffer) );
201 if( size <= 0 ) throw SocketRecvFailed( size );
203}
void addToStream(const char *str, size_t len)
Definition Parser.h:48
ssize_t socket_recv(int s, char *buf, size_t length)
Definition Utility.cpp:170

References FIX::Parser::addToStream(), m_buffer, m_parser, m_socket, and FIX::socket_recv().

Referenced by read(), and read().

◆ readMessage()

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

Definition at line 205 of file SocketConnection.cpp.

206{
207 try
208 {
209 return m_parser.readFixMessage( msg );
210 }
211 catch ( MessageParseError& ) {}
212 return true;
213}
bool readFixMessage(std::string &str)
Definition Parser.cpp:59

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

Referenced by read(), and readMessages().

◆ readMessages()

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

Definition at line 215 of file SocketConnection.cpp.

216{
217 if( !m_pSession ) return;
218
219 std::string msg;
220 while( readMessage( msg ) )
221 {
222 try
223 {
224 m_pSession->next( msg, UtcTimeStamp() );
225 }
226 catch ( InvalidMessage& )
227 {
228 if( !m_pSession->isLoggedOn() )
229 s.drop( m_socket );
230 }
231 }
232}
bool isLoggedOn()
Definition Session.h:65

References FIX::SocketMonitor::drop(), FIX::Session::isLoggedOn(), m_pSession, m_socket, FIX::Session::next(), and readMessage().

Referenced by read(), and read().

◆ send()

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

Implements FIX::Responder.

Definition at line 62 of file SocketConnection.cpp.

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

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

◆ signal()

void FIX::SocketConnection::signal ( )
inline

Definition at line 62 of file SocketConnection.h.

63 {
64 Locker l( m_mutex );
65 if( m_sendQueue.size() == 1 )
67 }
void signal(int socket)

References m_mutex, m_pMonitor, m_sendQueue, m_socket, and FIX::SocketMonitor::signal().

Referenced by send().

◆ unsignal()

void FIX::SocketConnection::unsignal ( )
inline

Definition at line 69 of file SocketConnection.h.

70 {
71 Locker l( m_mutex );
72 if( m_sendQueue.size() == 0 )
74 }
void unsignal(int socket)

References m_mutex, m_pMonitor, m_sendQueue, m_socket, and FIX::SocketMonitor::unsignal().

Referenced by FIX::SocketInitiator::onWrite(), and FIX::SocketAcceptor::onWrite().

Member Data Documentation

◆ m_buffer

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

Definition at line 90 of file SocketConnection.h.

Referenced by readFromSocket().

◆ m_fds

fd_set FIX::SocketConnection::m_fds
private

Definition at line 99 of file SocketConnection.h.

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

◆ m_mutex

Mutex FIX::SocketConnection::m_mutex
private

Definition at line 98 of file SocketConnection.h.

Referenced by processQueue(), send(), signal(), and unsignal().

◆ m_parser

Parser FIX::SocketConnection::m_parser
private

Definition at line 92 of file SocketConnection.h.

Referenced by readFromSocket(), and readMessage().

◆ m_pMonitor

SocketMonitor* FIX::SocketConnection::m_pMonitor
private

Definition at line 97 of file SocketConnection.h.

Referenced by disconnect(), signal(), and unsignal().

◆ m_pSession

Session* FIX::SocketConnection::m_pSession
private

◆ m_sendLength

unsigned FIX::SocketConnection::m_sendLength
private

Definition at line 94 of file SocketConnection.h.

Referenced by processQueue().

◆ m_sendQueue

Queue FIX::SocketConnection::m_sendQueue
private

Definition at line 93 of file SocketConnection.h.

Referenced by processQueue(), send(), signal(), and unsignal().

◆ m_sessions

Sessions FIX::SocketConnection::m_sessions
private

Definition at line 95 of file SocketConnection.h.

Referenced by isValidSession(), and SocketConnection().

◆ m_socket

int FIX::SocketConnection::m_socket
private

The documentation for this class was generated from the following files:

Generated on Sat Feb 3 2024 04:23:15 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001