Public Types | Public Member Functions | Private Member Functions | Private Attributes
FIX::ThreadedSocketConnection Class Reference

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

#include <ThreadedSocketConnection.h>

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

List of all members.

Public Types

typedef std::set< SessionIDSessions

Public Member Functions

 ThreadedSocketConnection (int s, Sessions sessions, Log *pLog)
 ThreadedSocketConnection (const SessionID &, int s, const std::string &address, short port, Log *pLog)
virtual ~ThreadedSocketConnection ()
SessiongetSession () 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 Attributes

int m_socket
char m_buffer [BUFSIZ]
std::string m_address
int m_port
Logm_pLog
Parser m_parser
Sessions m_sessions
Sessionm_pSession
bool m_disconnect
fd_set m_fds

Detailed Description

Encapsulates a socket file descriptor (multi-threaded).

Definition at line 44 of file ThreadedSocketConnection.h.


Member Typedef Documentation

Definition at line 47 of file ThreadedSocketConnection.h.


Constructor & Destructor Documentation

Definition at line 35 of file ThreadedSocketConnection.cpp.

: m_socket( s ), m_pLog( pLog ),
  m_sessions( sessions ), m_pSession( 0 ),
  m_disconnect( false )
{
  FD_ZERO( &m_fds );
  FD_SET( m_socket, &m_fds );
}
FIX::ThreadedSocketConnection::ThreadedSocketConnection ( const SessionID sessionID,
int  s,
const std::string &  address,
short  port,
Log pLog 
)

Definition at line 45 of file ThreadedSocketConnection.cpp.

  : m_socket( s ), m_address( address ), m_port( port ),
    m_pLog( pLog ),
    m_pSession( Session::lookupSession( sessionID ) ),
    m_disconnect( false )
{
  FD_ZERO( &m_fds );
  FD_SET( m_socket, &m_fds );
  if ( m_pSession ) m_pSession->setResponder( this );
}

Member Function Documentation

Definition at line 55 of file ThreadedSocketConnection.h.

References m_pSession.

Referenced by FIX::ThreadedSocketInitiator::socketThread().

{ return m_pSession; }

Definition at line 150 of file ThreadedSocketConnection.cpp.

References disconnect(), FIX::Session::isLoggedOn(), m_pSession, FIX::Session::next(), readMessage(), setSession(), and FIX::TYPE::UtcTimeStamp.

Referenced by read().

{
  std::string msg;
  while( readMessage(msg) )
  {
    if ( !m_pSession )
    {
      if ( !setSession( msg ) )
      { disconnect(); continue; }
    }
    try
    {
      m_pSession->next( msg, UtcTimeStamp() );
    }
    catch( InvalidMessage& )
    {
      if( !m_pSession->isLoggedOn() )
      {
        disconnect();
        return;
      }
    }
  }
}

Definition at line 91 of file ThreadedSocketConnection.cpp.

References FIX::Parser::addToStream(), disconnect(), FIX::Session::disconnect(), FIX::Session::getLog(), m_buffer, m_disconnect, m_fds, m_parser, m_pSession, m_socket, FIX::Session::next(), FIX::Log::onEvent(), and processStream().

Referenced by FIX::ThreadedSocketAcceptor::socketConnectionThread(), and FIX::ThreadedSocketInitiator::socketThread().

{
  struct timeval timeout = { 1, 0 };
  fd_set readset = m_fds;

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

    if( result > 0 ) // Something to read
    {
      // We can read without blocking
      ssize_t size = recv( m_socket, m_buffer, sizeof(m_buffer), 0 );
      if ( size <= 0 ) { throw SocketRecvFailed( size ); }
      m_parser.addToStream( m_buffer, size );
    }
    else if( result == 0 && m_pSession ) // Timeout
    {
      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 FIX::ThreadedSocketConnection::readMessage ( std::string &  msg) throw ( SocketRecvFailed ) [private]

Definition at line 139 of file ThreadedSocketConnection.cpp.

Referenced by processStream().

{
  try
  {
    return m_parser.readFixMessage( msg );
  }
  catch ( MessageParseError& ) {}
  return true;
}
bool FIX::ThreadedSocketConnection::send ( const std::string &  msg) [private, virtual]

Implements FIX::Responder.

Definition at line 67 of file ThreadedSocketConnection.cpp.

References m_socket, and FIX::socket_send().

{
  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;
    totalSent += sent;
  }

  return true;
}
bool FIX::ThreadedSocketConnection::setSession ( const std::string &  msg) [private]

Definition at line 175 of file ThreadedSocketConnection.cpp.

References FIX::Session::getSessionID(), FIX::Session::isSessionRegistered(), FIX::Session::lookupSession(), m_pLog, m_pSession, m_sessions, FIX::Log::onEvent(), FIX::Log::onIncoming(), FIX::process_sleep(), FIX::Session::registerSession(), and FIX::Session::setResponder().

Referenced by processStream().

{
  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;

  // see if the session frees up within 5 seconds
  for( int i = 1; i <= 5; i++ )
  {
    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;
}

Member Data Documentation

Definition at line 70 of file ThreadedSocketConnection.h.

Referenced by connect().

Definition at line 68 of file ThreadedSocketConnection.h.

Referenced by read().

Definition at line 77 of file ThreadedSocketConnection.h.

Referenced by disconnect(), and read().

Definition at line 78 of file ThreadedSocketConnection.h.

Referenced by read().

Definition at line 74 of file ThreadedSocketConnection.h.

Referenced by read().

Definition at line 73 of file ThreadedSocketConnection.h.

Referenced by setSession().

Definition at line 71 of file ThreadedSocketConnection.h.

Referenced by connect().

Definition at line 75 of file ThreadedSocketConnection.h.

Referenced by setSession().

Definition at line 67 of file ThreadedSocketConnection.h.

Referenced by disconnect(), getSocket(), read(), and send().


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

Generated on Mon Sep 15 2014 01:23:57 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001