Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes
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]

List of all members.

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

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

Definition at line 80 of file SocketConnection.h.

Definition at line 49 of file SocketConnection.h.


Constructor & Destructor Documentation

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

Definition at line 35 of file SocketConnection.cpp.

References m_fds, and m_socket.

: 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::SocketConnection ( SocketInitiator i,
const SessionID sessionID,
int  s,
SocketMonitor pMonitor 
)

Definition at line 44 of file SocketConnection.cpp.

References m_fds, m_sessions, and m_socket.

: 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 );
}

Member Function Documentation

void FIX::SocketConnection::disconnect ( ) [private, virtual]

Implements FIX::Responder.

Definition at line 100 of file SocketConnection.cpp.

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

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

Definition at line 55 of file SocketConnection.h.

References m_socket.

{ return m_socket; }

Definition at line 187 of file SocketConnection.cpp.

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

Referenced by read().

{
  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() );
}

Definition at line 234 of file SocketConnection.cpp.

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

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

{
  if ( m_pSession ) m_pSession->next();
}

Definition at line 72 of file SocketConnection.cpp.

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

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

  int 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();
}

Definition at line 106 of file SocketConnection.cpp.

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

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

{
  if ( !m_pSession ) return false;

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

Definition at line 123 of file SocketConnection.cpp.

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.

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

Definition at line 197 of file SocketConnection.cpp.

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

Referenced by read().

{
  int size = recv( m_socket, m_buffer, sizeof(m_buffer), 0 );
  if( size <= 0 ) throw SocketRecvFailed( size );
  m_parser.addToStream( m_buffer, size );
}
bool FIX::SocketConnection::readMessage ( std::string &  msg) [private]

Definition at line 205 of file SocketConnection.cpp.

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

Referenced by read(), and readMessages().

{
  try
  {
    return m_parser.readFixMessage( msg );
  }
  catch ( MessageParseError& ) {}
  return true;
}

Definition at line 215 of file SocketConnection.cpp.

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

Referenced by read().

{
  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 );
    }
  }
}
bool FIX::SocketConnection::send ( const std::string &  msg) [private, virtual]

Implements FIX::Responder.

Definition at line 62 of file SocketConnection.cpp.

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

{
  Locker l( m_mutex );

  m_sendQueue.push_back( msg );
  processQueue();
  signal();
  return true;
}
void FIX::SocketConnection::signal ( ) [inline]

Definition at line 62 of file SocketConnection.h.

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

Referenced by send().

  {
    Locker l( m_mutex );
    if( m_sendQueue.size() == 1 )
      m_pMonitor->signal( m_socket );
  }

Member Data Documentation

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

Definition at line 90 of file SocketConnection.h.

Referenced by readFromSocket().

fd_set FIX::SocketConnection::m_fds [private]

Definition at line 99 of file SocketConnection.h.

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

Definition at line 98 of file SocketConnection.h.

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

Definition at line 92 of file SocketConnection.h.

Referenced by readFromSocket(), and readMessage().

Definition at line 97 of file SocketConnection.h.

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

Definition at line 94 of file SocketConnection.h.

Referenced by processQueue().

Definition at line 93 of file SocketConnection.h.

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

Definition at line 95 of file SocketConnection.h.

Referenced by isValidSession(), and SocketConnection().


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

Generated on Mon Jun 23 2014 23:49:39 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001