FIX::SocketConnection Class Reference

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

`#include <SocketConnection.h>`

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

## Public Types

typedef std::set< SessionID > | Sessions

## Public Member Functions

| SocketConnection (int s, Sessions sessions, SocketMonitor *pMonitor)
| SocketConnection (SocketInitiator &, const SessionID &, int, SocketMonitor *)
virtual | ~SocketConnection ()
int | getSocket () const
Session * | getSession () 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
Session * | m_pSession
SocketMonitor * | m_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

typedef std::set<SessionID> FIX::SocketConnection::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.

: m_socket( s ), m_sendLength( 0 ),
m_sessions(sessions), m_pSession( 0 ), m_pMonitor( pMonitor )
{
FD_ZERO( &m_fds );
FD_SET( m_socket, &m_fds );
}
FIX::SocketConnection::m_pSession
Session * m_pSession
Definition: SocketConnection.h:96
FIX::SocketConnection::m_fds
fd_set m_fds
Definition: SocketConnection.h:99
FIX::SocketConnection::m_socket
int m_socket
Definition: SocketConnection.h:89
FIX::SocketConnection::m_pMonitor
SocketMonitor * m_pMonitor
Definition: SocketConnection.h:97
FIX::SocketConnection::m_sessions
Sessions m_sessions
Definition: SocketConnection.h:95
FIX::SocketConnection::m_sendLength
unsigned m_sendLength
Definition: SocketConnection.h:94

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.

: m_socket( s ), m_sendLength( 0 ),
m_pSession( i.getSession( sessionID, *this ) ),
m_pMonitor( pMonitor )
{
FD_ZERO( &m_fds );
FD_SET( m_socket, &m_fds );
m_sessions.insert( sessionID );
}

References m_fds, m_sessions, and m_socket.

## ~SocketConnection()

FIX::SocketConnection::~SocketConnection | ( |  | ) |

virtual

Definition at line 56 of file SocketConnection.cpp.

{
if ( m_pSession )
Session::unregisterSession( m_pSession->getSessionID() );
}
FIX::Session::unregisterSession
static void unregisterSession(const SessionID &)
Definition: Session.cpp:1547
FIX::Session::getSessionID
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 | ( |  | ) |

private virtual

Implements FIX::Responder.

Definition at line 100 of file SocketConnection.cpp.

{
if ( m_pMonitor )
m_pMonitor->drop( m_socket );
}
FIX::SocketMonitor::drop
bool drop(int socket)
Definition: SocketMonitor.cpp:98

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

## getSession()

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

inline

Definition at line 56 of file SocketConnection.h.

{ return m_pSession; }

References m_pSession.

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

## getSocket()

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

inline

Definition at line 55 of file SocketConnection.h.

{ return m_socket; }

References m_socket.

## isValidSession()

bool FIX::SocketConnection::isValidSession | ( |  | ) |

private

Definition at line 187 of file SocketConnection.cpp.

{
if( m_pSession == 0 )
return false;
SessionID sessionID = m_pSession->getSessionID();
if( Session::isSessionRegistered(sessionID) )
return false;
return !( m_sessions.find(sessionID) == m_sessions.end() );
}
FIX::Session::isSessionRegistered
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.

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

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

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

## processQueue()

bool FIX::SocketConnection::processQueue | ( |  | ) |

Definition at line 72 of file SocketConnection.cpp.

{
Locker l( m_mutex );
if( !m_sendQueue.size() ) return true;
struct timeval timeout = { 0, 0 };
fd_set writeset = m_fds;
if( select( 1 + m_socket, 0, &writeset, 0, &timeout ) <= 0 )
return false;
const std::string& msg = m_sendQueue.front();
ssize_t result = socket_send
( m_socket, msg.c_str() + m_sendLength, msg.length() - m_sendLength );
if( result > 0 )
m_sendLength += result;
if( m_sendLength == msg.length() )
{
m_sendLength = 0;
m_sendQueue.pop_front();
}
return !m_sendQueue.size();
}
FIX::SocketConnection::m_mutex
Mutex m_mutex
Definition: SocketConnection.h:98
FIX::SocketConnection::m_sendQueue
Queue m_sendQueue
Definition: SocketConnection.h:93
FIX::socket_send
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.

{
std::string msg;
try
{
if ( !m_pSession )
{
struct timeval timeout = { 1, 0 };
fd_set readset = m_fds;
while( !readMessage( msg ) )
{
int result = select( 1 + m_socket, &readset, 0, 0, &timeout );
if( result > 0 )
readFromSocket();
else if( result == 0 )
return false;
else if( result < 0 )
return false;
}
m_pSession = Session::lookupSession( msg, true );
if( !isValidSession() )
{
m_pSession = 0;
if( a.getLog() )
{
a.getLog()->onEvent( "Session not found for incoming message: " + msg );
a.getLog()->onIncoming( msg );
}
}
if( m_pSession )
m_pSession = a.getSession( msg, *this );
if( m_pSession )
m_pSession->next( msg, UtcTimeStamp() );
if( !m_pSession )
{
s.getMonitor().drop( m_socket );
return false;
}
Session::registerSession( m_pSession->getSessionID() );
return true;
}
else
{
readFromSocket();
readMessages( s.getMonitor() );
return true;
}
}
catch ( SocketRecvFailed& e )
{
if( m_pSession )
m_pSession->getLog()->onEvent( e.what() );
s.getMonitor().drop( m_socket );
}
catch ( InvalidMessage& )
{
s.getMonitor().drop( m_socket );
}
return false;
}
FIX::Log::onEvent
virtual void onEvent(const std::string &)=0
FIX::Session::registerSession
static Session * registerSession(const SessionID &)
Definition: Session.cpp:1537
FIX::Session::lookupSession
static Session * lookupSession(const SessionID &)
Definition: Session.cpp:1496
FIX::Session::getLog
Log * getLog()
Definition: Session.h:227
FIX::SocketConnection::readMessages
void readMessages(SocketMonitor &s)
Definition: SocketConnection.cpp:215
FIX::SocketConnection::readMessage
bool readMessage(std::string &msg)
Definition: SocketConnection.cpp:205
FIX::SocketConnection::isValidSession
bool isValidSession()
Definition: SocketConnection.cpp:187
FIX::SocketConnection::readFromSocket
void readFromSocket()
Definition: SocketConnection.cpp:197
FIX::TYPE::UtcTimeStamp
@ UtcTimeStamp
Definition: FieldTypes.h:923

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(), FIX::Session::registerSession(), and FIX::TYPE::UtcTimeStamp.

## read() [2/2]

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

Definition at line 106 of file SocketConnection.cpp.

{
if ( !m_pSession ) return false;
try
{
readFromSocket();
readMessages( s.getMonitor() );
}
catch( SocketRecvFailed& e )
{
m_pSession->getLog()->onEvent( e.what() );
return false;
}
return true;
}

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.

{
ssize_t size = socket_recv( m_socket, m_buffer, sizeof(m_buffer) );
if( size <= 0 ) throw SocketRecvFailed( size );
m_parser.addToStream( m_buffer, size );
}
FIX::Parser::addToStream
void addToStream(const char *str, size_t len)
Definition: Parser.h:48
FIX::SocketConnection::m_buffer
char m_buffer[BUFSIZ]
Definition: SocketConnection.h:90
FIX::SocketConnection::m_parser
Parser m_parser
Definition: SocketConnection.h:92
FIX::socket_recv
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().

## readMessage()

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

private

Definition at line 205 of file SocketConnection.cpp.

{
try
{
return m_parser.readFixMessage( msg );
}
catch ( MessageParseError& ) {}
return true;
}
FIX::Parser::readFixMessage
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.

{
if( !m_pSession ) return;
std::string msg;
while( readMessage( msg ) )
{
try
{
m_pSession->next( msg, UtcTimeStamp() );
}
catch ( InvalidMessage& )
{
if( !m_pSession->isLoggedOn() )
s.drop( m_socket );
}
}
}
FIX::Session::isLoggedOn
bool isLoggedOn()
Definition: Session.h:65

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

Referenced by read().

## send()

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

private virtual

Implements FIX::Responder.

Definition at line 62 of file SocketConnection.cpp.

{
Locker l( m_mutex );
m_sendQueue.push_back( msg );
processQueue();
signal();
return true;
}
FIX::SocketConnection::signal
void signal()
Definition: SocketConnection.h:62
FIX::SocketConnection::processQueue
bool processQueue()
Definition: SocketConnection.cpp:72

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

## signal()

void FIX::SocketConnection::signal | ( |  | ) |

inline

Definition at line 62 of file SocketConnection.h.

{
Locker l( m_mutex );
if( m_sendQueue.size() == 1 )
m_pMonitor->signal( m_socket );
}
FIX::SocketMonitor::signal
void signal(int socket)
Definition: SocketMonitor.cpp:168

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.

{
Locker l( m_mutex );
if( m_sendQueue.size() == 0 )
m_pMonitor->unsignal( m_socket );
}
FIX::SocketMonitor::unsignal
void unsignal(int socket)
Definition: SocketMonitor.cpp:173

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(), 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

Definition at line 96 of file SocketConnection.h.

Referenced by getSession(), isValidSession(), onTimeout(), read(), readMessages(), and ~SocketConnection().

## 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

Definition at line 89 of file SocketConnection.h.

Referenced by disconnect(), getSocket(), processQueue(), read(), readFromSocket(), readMessages(), signal(), SocketConnection(), and unsignal().

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

- SocketConnection.h
- SocketConnection.cpp