FIX::ThreadedSocketConnection Class Reference

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

`#include <ThreadedSocketConnection.h>`

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

## Public Types

typedef std::set< SessionID > | Sessions

## Public Member Functions

| ThreadedSocketConnection (int s, Sessions sessions, Log *pLog)
| ThreadedSocketConnection (const SessionID &, int s, const std::string &address, short port, Log *pLog, const std::string &sourceAddress="", short sourcePort=0)
virtual | ~ThreadedSocketConnection ()
Session * | getSession () const
int | getSocket () const
bool | connect ()
void | disconnect ()
bool | read ()

## Private Member Functions

bool | readMessage (std::string &msg) throw ( SocketRecvFailed )
void | processStream ()
bool | send (const std::string &)
bool | setSession (const std::string &msg)
Private Member Functions inherited from FIX::Responder
virtual | ~Responder ()

## Private Attributes

int | m_socket
char | m_buffer [BUFSIZ]
std::string | m_address
int | m_port
std::string | m_sourceAddress
int | m_sourcePort
Log * | m_pLog
Parser | m_parser
Sessions | m_sessions
Session * | m_pSession
bool | m_disconnect
fd_set | m_fds

## Detailed Description

Encapsulates a socket file descriptor (multi-threaded).

Definition at line 61 of file ThreadedSocketConnection.h.

## Member Typedef Documentation

## Sessions

typedef std::set<SessionID> FIX::ThreadedSocketConnection::Sessions

Definition at line 64 of file ThreadedSocketConnection.h.

## Constructor & Destructor Documentation

## ThreadedSocketConnection() [1/2]

FIX::ThreadedSocketConnection::ThreadedSocketConnection | ( | int | s,
|  | Sessions | sessions,
|  | Log * | pLog
| ) |  |

Definition at line 52 of file ThreadedSocketConnection.cpp.

{
FD_ZERO( &m_fds );
FD_SET( m_socket, &m_fds );
if ( m_pSession ) m_pSession->setResponder( this );
}

## ThreadedSocketConnection() [2/2]

FIX::ThreadedSocketConnection::ThreadedSocketConnection | ( | const SessionID & | sessionID,
|  | int | s,
|  | const std::string & | address,
|  | short | port,
|  | Log * | pLog,
|  | const std::string & | sourceAddress = `""`,
|  | short | sourcePort = `0`
| ) |  |

Definition at line 62 of file ThreadedSocketConnection.cpp.

{
m_pSession->setResponder( 0 );
Session::unregisterSession( m_pSession->getSessionID() );
}
}
bool ThreadedSocketConnection::send( const std::string& msg )
{
int totalSent = 0;
while(totalSent < (int)msg.length())
{
ssize_t sent = socket_send( m_socket, msg.c_str() + totalSent, msg.length() );
if(sent < 0) return false;

References FIX::Session::unregisterSession().

## ~ThreadedSocketConnection()

FIX::ThreadedSocketConnection::~ThreadedSocketConnection | ( |  | ) |

virtual

Definition at line 77 of file ThreadedSocketConnection.cpp.

{
// do the bind in the thread as name resolution may block

## Member Function Documentation

## connect()

bool FIX::ThreadedSocketConnection::connect | ( |  | ) |

Definition at line 99 of file ThreadedSocketConnection.cpp.

{ 1, 0 };
fd_set readset = m_fds;
try
{
// Wait for input (1 second timeout)
int result = select( 1 + m_socket, &readset, 0, 0, &timeout );

## disconnect()

void FIX::ThreadedSocketConnection::disconnect | ( |  | ) |

virtual

Implements FIX::Responder.

Definition at line 108 of file ThreadedSocketConnection.cpp.

{
// We can read without blocking
ssize_t size = socket_recv( m_socket, m_buffer, sizeof(m_buffer) );
if ( size <= 0 ) { throw SocketRecvFailed( size ); }
m_parser.addToStream( m_buffer, size );

References FIX::socket_recv().

## getSession()

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

inline

Definition at line 73 of file ThreadedSocketConnection.h.

## getSocket()

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

inline

Definition at line 74 of file ThreadedSocketConnection.h.

## processStream()

void FIX::ThreadedSocketConnection::processStream | ( |  | ) |

private

Definition at line 173 of file ThreadedSocketConnection.cpp.

{
disconnect();
return;
}
}
}
}
bool ThreadedSocketConnection::setSession( const std::string& msg )
{
m_pSession = Session::lookupSession( msg, true );
if ( !m_pSession )
{
if( m_pLog )
{
m_pLog->onEvent( "Session not found for incoming message: " + msg );
m_pLog->onIncoming( msg );
}
return false;
}
SessionID sessionID = m_pSession->getSessionID();
m_pSession = 0;

## read()

bool FIX::ThreadedSocketConnection::read | ( |  | ) |

Definition at line 114 of file ThreadedSocketConnection.cpp.

{
m_pSession->next();
}
else if( result < 0 ) // Error
{
throw SocketRecvFailed( result );
}
processStream();
return true;
}
catch ( SocketRecvFailed& e )
{
if( m_disconnect )
return false;
if( m_pSession )
{
m_pSession->getLog()->onEvent( e.what() );
m_pSession->disconnect();
}
else
{
disconnect();
}
return false;
}
}
bool ThreadedSocketConnection::readMessage( std::string& msg )
throw( SocketRecvFailed )
{
try
{
return m_parser.readFixMessage( msg );
}
catch ( MessageParseError& ) {}
return true;
}
void ThreadedSocketConnection::processStream()
{
std::string msg;
while( readMessage(msg) )
{

## readMessage()

bool FIX::ThreadedSocketConnection::readMessage | ( | std::string & | msg | ) |
throw | ( | SocketRecvFailed
| ) |  |

private

Definition at line 162 of file ThreadedSocketConnection.cpp.

{
if ( !setSession( msg ) )
{ disconnect(); continue; }
}
try
{
m_pSession->next( msg, UtcTimeStamp() );
}
catch( InvalidMessage& )
{

## send()

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

private virtual

Implements FIX::Responder.

Definition at line 86 of file ThreadedSocketConnection.cpp.

{
m_disconnect = true;
socket_close( m_socket );
}
bool ThreadedSocketConnection::read()

## setSession()

bool FIX::ThreadedSocketConnection::setSession | ( | const std::string & | msg | ) |

private

Definition at line 198 of file ThreadedSocketConnection.cpp.

{
if( !Session::isSessionRegistered( sessionID ) )
m_pSession = Session::registerSession( sessionID );
if( m_pSession ) break;
process_sleep( 1 );
}
if ( !m_pSession )
return false;
if ( m_sessions.find(m_pSession->getSessionID()) == m_sessions.end() )
return false;
m_pSession->setResponder( this );
return true;
}
} // namespace FIX

References FIX::Session::isSessionRegistered(), FIX::process_sleep(), and FIX::Session::registerSession().

## Member Data Documentation

## m_address

std::string FIX::ThreadedSocketConnection::m_address

private

Definition at line 88 of file ThreadedSocketConnection.h.

## m_buffer

char FIX::ThreadedSocketConnection::m_buffer[BUFSIZ]

private

Definition at line 86 of file ThreadedSocketConnection.h.

## m_disconnect

bool FIX::ThreadedSocketConnection::m_disconnect

private

Definition at line 97 of file ThreadedSocketConnection.h.

## m_fds

fd_set FIX::ThreadedSocketConnection::m_fds

private

Definition at line 98 of file ThreadedSocketConnection.h.

## m_parser

Parser FIX::ThreadedSocketConnection::m_parser

private

Definition at line 94 of file ThreadedSocketConnection.h.

## m_pLog

Log* FIX::ThreadedSocketConnection::m_pLog

private

Definition at line 93 of file ThreadedSocketConnection.h.

## m_port

int FIX::ThreadedSocketConnection::m_port

private

Definition at line 89 of file ThreadedSocketConnection.h.

## m_pSession

Session* FIX::ThreadedSocketConnection::m_pSession

private

Definition at line 96 of file ThreadedSocketConnection.h.

## m_sessions

Sessions FIX::ThreadedSocketConnection::m_sessions

private

Definition at line 95 of file ThreadedSocketConnection.h.

## m_socket

int FIX::ThreadedSocketConnection::m_socket

private

Definition at line 85 of file ThreadedSocketConnection.h.

## m_sourceAddress

std::string FIX::ThreadedSocketConnection::m_sourceAddress

private

Definition at line 90 of file ThreadedSocketConnection.h.

## m_sourcePort

int FIX::ThreadedSocketConnection::m_sourcePort

private

Definition at line 91 of file ThreadedSocketConnection.h.

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

- ThreadedSocketConnection.h
- ThreadedSocketConnection.cpp

FIX::ThreadedSocketConnection::readMessage
bool readMessage(std::string &msg)
Definition: ThreadedSocketConnection.cpp:162
FIX::ThreadedSocketConnection::setSession
bool setSession(const std::string &msg)
Definition: ThreadedSocketConnection.cpp:198
FIX::Session::lookupSession
static Session * lookupSession(const SessionID &)
Definition: Session.cpp:1513
FIX::Session::unregisterSession
static void unregisterSession(const SessionID &)
Definition: Session.cpp:1564
FIX::ThreadedSocketConnection::send
bool send(const std::string &)
Definition: ThreadedSocketConnection.cpp:86
FIX::Session::getSessionID
const SessionID & getSessionID() const
Definition: Session.h:109
FIX::Session::registerSession
static Session * registerSession(const SessionID &)
Definition: Session.cpp:1554
FIX::ThreadedSocketConnection::processStream
void processStream()
Definition: ThreadedSocketConnection.cpp:173
FIX::ThreadedSocketConnection::disconnect
void disconnect()
Definition: ThreadedSocketConnection.cpp:108
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::TYPE::UtcTimeStamp
@ UtcTimeStamp
Definition: FieldTypes.h:940
FIX::ThreadedSocketConnection::m_pSession
Session * m_pSession
Definition: ThreadedSocketConnection.h:96
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::socket_send
ssize_t socket_send(int s, const char *msg, size_t length)
Definition: Utility.cpp:192
FIX::ThreadedSocketConnection::m_fds
fd_set m_fds
Definition: ThreadedSocketConnection.h:98
FIX::ThreadedSocketConnection::read
bool read()
Definition: ThreadedSocketConnection.cpp:114
FIX::ThreadedSocketConnection::m_disconnect
bool m_disconnect
Definition: ThreadedSocketConnection.h:97
FIX::process_sleep
void process_sleep(double s)
Definition: Utility.cpp:483
FIX::ThreadedSocketConnection::m_parser
Parser m_parser
Definition: ThreadedSocketConnection.h:94
FIX::Parser::addToStream
void addToStream(const char *str, size_t len)
Definition: Parser.h:82
FIX::Session::disconnect
void disconnect()
Definition: Session.cpp:630
FIX::Parser::readFixMessage
bool readFixMessage(std::string &str)
Definition: Parser.cpp:76
FIX::ThreadedSocketConnection::m_socket
int m_socket
Definition: ThreadedSocketConnection.h:85
FIX::socket_close
void socket_close(int s)
Definition: Utility.cpp:197
FIX::ThreadedSocketConnection::m_buffer
char m_buffer[BUFSIZ]
Definition: ThreadedSocketConnection.h:86
FIX::Session::setResponder
void setResponder(Responder *pR)
Definition: Session.h:244
FIX::ThreadedSocketConnection::m_pLog
Log * m_pLog
Definition: ThreadedSocketConnection.h:93
FIX::ThreadedSocketConnection::m_sessions
Sessions m_sessions
Definition: ThreadedSocketConnection.h:95
FIX::Log::onIncoming
virtual void onIncoming(const std::string &)=0