Index  Source Files  Annotated Class List  Alphabetical Class List  Class Hierarchy  Graphical Class Hierarchy   
 

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


Member Typedef Documentation

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

Definition at line 78 of file SocketConnection.h.

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

Definition at line 47 of file SocketConnection.h.


Constructor & Destructor Documentation

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

Definition at line 36 of file SocketConnection.cpp.

References m_fds, and m_socket.

00038 : m_socket( s ), m_sendLength( 0 ),
00039   m_sessions(sessions), m_pSession( 0 ), m_pMonitor( pMonitor )
00040 {
00041   FD_ZERO( &m_fds );
00042   FD_SET( m_socket, &m_fds );
00043 }

FIX::SocketConnection::SocketConnection SocketInitiator ,
const SessionID ,
int  ,
SocketMonitor
 

Definition at line 45 of file SocketConnection.cpp.

References m_fds, and m_socket.

00048 : m_socket( s ), m_sendLength( 0 ),
00049   m_pSession( i.getSession( sessionID, *this ) ),
00050   m_pMonitor( pMonitor ) 
00051 {
00052   FD_ZERO( &m_fds );
00053   FD_SET( m_socket, &m_fds );
00054   m_sessions.insert( sessionID );
00055 }

FIX::SocketConnection::~SocketConnection  )  [virtual]
 

Definition at line 57 of file SocketConnection.cpp.

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

00058 {
00059   if ( m_pSession )
00060     Session::unregisterSession( m_pSession->getSessionID() );
00061 }


Member Function Documentation

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

Implements FIX::Responder.

Definition at line 107 of file SocketConnection.cpp.

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

00108 { QF_STACK_PUSH(SocketConnection::disconnect)
00109 
00110   if ( m_pMonitor )
00111     m_pMonitor->drop( m_socket );
00112 
00113   QF_STACK_POP
00114 }

Session* FIX::SocketConnection::getSession  )  const [inline]
 

Definition at line 54 of file SocketConnection.h.

References m_pSession.

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

00054 { return m_pSession; }

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

Definition at line 53 of file SocketConnection.h.

References m_socket.

00053 { return m_socket; }

bool FIX::SocketConnection::isValidSession  )  [private]
 

Definition at line 181 of file SocketConnection.cpp.

References FIX::Session::getSessionID(), m_pSession, QF_STACK_POP, and QF_STACK_PUSH.

Referenced by read().

00182 { QF_STACK_PUSH(SocketConnection::isValidSession)
00183 
00184   if( m_pSession == 0 )
00185     return false;
00186   SessionID sessionID = m_pSession->getSessionID();
00187   if( Session::isSessionRegistered(sessionID) )
00188     return false;
00189   return !( m_sessions.find(sessionID) == m_sessions.end() );
00190 
00191   QF_STACK_POP
00192 }

void FIX::SocketConnection::onTimeout  ) 
 

Definition at line 237 of file SocketConnection.cpp.

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

00238 { QF_STACK_PUSH(SocketConnection::onTimeout)
00239   if ( m_pSession ) m_pSession->next();
00240   QF_STACK_POP
00241 }

bool FIX::SocketConnection::processQueue  ) 
 

Definition at line 76 of file SocketConnection.cpp.

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

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

00077 { QF_STACK_PUSH(SocketConnection::processQueue)
00078 
00079   Locker l( m_mutex );
00080 
00081   if( !m_sendQueue.size() ) return true;
00082 
00083   struct timeval timeout = { 0, 0 };
00084   fd_set writeset = m_fds;
00085   if( select( 1 + m_socket, 0, &writeset, 0, &timeout ) <= 0 )
00086     return false;
00087     
00088   const std::string& msg = m_sendQueue.front();
00089 
00090   int result = socket_send
00091     ( m_socket, msg.c_str() + m_sendLength, msg.length() - m_sendLength );
00092 
00093   if( result > 0 )
00094     m_sendLength += result;
00095 
00096   if( m_sendLength == msg.length() )
00097   {
00098     m_sendLength = 0;
00099     m_sendQueue.pop_front();
00100   }
00101 
00102   return !m_sendQueue.size();
00103 
00104   QF_STACK_POP
00105 }

bool FIX::SocketConnection::read SocketAcceptor ,
SocketServer
 

Definition at line 136 of file SocketConnection.cpp.

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

00137 { QF_STACK_PUSH(SocketConnection::read)
00138 
00139   std::string msg;
00140   try
00141   {
00142     readFromSocket();
00143 
00144     if ( !m_pSession )
00145     {
00146       if ( !readMessage( msg ) ) return false;
00147       m_pSession = Session::lookupSession( msg, true );
00148       if( !isValidSession() )
00149         m_pSession = 0;
00150       if( m_pSession )
00151         m_pSession = a.getSession( msg, *this );
00152       if( m_pSession )
00153         m_pSession->next( msg );
00154       if( !m_pSession )
00155       {
00156         s.getMonitor().drop( m_socket );
00157         return false;
00158       }
00159 
00160       Session::registerSession( m_pSession->getSessionID() );
00161     }
00162 
00163     readMessages( s.getMonitor() );
00164     return true;
00165   }
00166   catch ( SocketRecvFailed& e )
00167   {
00168     if( m_pSession )
00169       m_pSession->getLog()->onEvent( e.what() );
00170     s.getMonitor().drop( m_socket );
00171   }
00172   catch ( InvalidMessage& )
00173   {
00174     s.getMonitor().drop( m_socket );
00175   }
00176   return false;
00177 
00178   QF_STACK_POP
00179 }

bool FIX::SocketConnection::read SocketConnector s  ) 
 

Definition at line 116 of file SocketConnection.cpp.

References FIX::SocketMonitor::drop(), FIX::Session::getLog(), FIX::SocketConnector::getMonitor(), m_pSession, m_socket, FIX::Log::onEvent(), QF_STACK_POP, QF_STACK_PUSH, readFromSocket(), and readMessages().

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

00117 { QF_STACK_PUSH(SocketConnection::read)
00118 
00119   if ( !m_pSession ) return false;
00120 
00121   try
00122   {
00123     readFromSocket();
00124     readMessages( s.getMonitor() );
00125   }
00126   catch( SocketRecvFailed& e )
00127   {
00128     m_pSession->getLog()->onEvent( e.what() );
00129     s.getMonitor().drop( m_socket );
00130   }
00131   return true;
00132 
00133   QF_STACK_POP
00134 }

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

Definition at line 194 of file SocketConnection.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

Referenced by read().

00196 { QF_STACK_PUSH(SocketConnection::readFromSocket)
00197 
00198   int size = recv( m_socket, m_buffer, sizeof(m_buffer), 0 );
00199   if( size <= 0 ) throw SocketRecvFailed( size );
00200   m_parser.addToStream( m_buffer, size );
00201 
00202   QF_STACK_POP
00203 }

bool FIX::SocketConnection::readMessage std::string &  msg  )  [private]
 

Definition at line 205 of file SocketConnection.cpp.

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

Referenced by read(), and readMessages().

00206 { QF_STACK_PUSH(SocketConnection::readMessage)
00207 
00208   try
00209   {
00210     return m_parser.readFixMessage( msg );
00211   }
00212   catch ( MessageParseError& ) {}
00213   return true;
00214 
00215   QF_STACK_POP
00216 }

void FIX::SocketConnection::readMessages SocketMonitor s  )  [private]
 

Definition at line 218 of file SocketConnection.cpp.

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

Referenced by read().

00219 {
00220   if( !m_pSession ) return;
00221 
00222   std::string msg;
00223   while( readMessage( msg ) )
00224   {
00225     try
00226     {
00227       m_pSession->next( msg );
00228     }
00229     catch ( InvalidMessage& )
00230     {
00231       if( !m_pSession->isLoggedOn() )
00232         s.drop( m_socket );
00233     }
00234   }
00235 }

bool FIX::SocketConnection::send const std::string &   )  [private, virtual]
 

Implements FIX::Responder.

Definition at line 63 of file SocketConnection.cpp.

References m_sendQueue, processQueue(), QF_STACK_POP, QF_STACK_PUSH, and signal().

00064 { QF_STACK_PUSH(SocketConnection::send)
00065 
00066   Locker l( m_mutex );
00067 
00068   m_sendQueue.push_back( msg );
00069   processQueue();
00070   signal();
00071   return true;
00072 
00073   QF_STACK_POP
00074 }

void FIX::SocketConnection::signal  )  [inline]
 

Definition at line 60 of file SocketConnection.h.

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

Referenced by send().

00061   {
00062     Locker l( m_mutex );
00063     if( m_sendQueue.size() == 1 )
00064       m_pMonitor->signal( m_socket );
00065   }

void FIX::SocketConnection::unsignal  )  [inline]
 

Definition at line 67 of file SocketConnection.h.

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

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

00068   {
00069     Locker l( m_mutex );
00070     if( m_sendQueue.size() == 0 )
00071       m_pMonitor->unsignal( m_socket );
00072   }


Member Data Documentation

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

Definition at line 88 of file SocketConnection.h.

fd_set FIX::SocketConnection::m_fds [private]
 

Definition at line 97 of file SocketConnection.h.

Referenced by processQueue(), and SocketConnection().

Mutex FIX::SocketConnection::m_mutex [private]
 

Definition at line 96 of file SocketConnection.h.

Parser FIX::SocketConnection::m_parser [private]
 

Definition at line 90 of file SocketConnection.h.

Referenced by readMessage().

SocketMonitor* FIX::SocketConnection::m_pMonitor [private]
 

Definition at line 95 of file SocketConnection.h.

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

Session* FIX::SocketConnection::m_pSession [private]
 

Definition at line 94 of file SocketConnection.h.

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

unsigned FIX::SocketConnection::m_sendLength [private]
 

Definition at line 92 of file SocketConnection.h.

Referenced by processQueue().

Queue FIX::SocketConnection::m_sendQueue [private]
 

Definition at line 91 of file SocketConnection.h.

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

Sessions FIX::SocketConnection::m_sessions [private]
 

Definition at line 93 of file SocketConnection.h.

int FIX::SocketConnection::m_socket [private]
 

Definition at line 87 of file SocketConnection.h.

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


The documentation for this class was generated from the following files:
Generated on Mon Jul 24 19:36:51 2006 for QuickFIX by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2001