void ThreadedSocketInitiator::doConnect( const SessionID& s, const Dictionary& d ) { try { Session* session = Session::lookupSession( s ); if( !session->isSessionTime(UtcTimeStamp()) ) return; Log* log = session->getLog(); std::string address; short port = 0; std::string sourceAddress; short sourcePort = 0; getHost( s, d, address, port, sourceAddress, sourcePort ); socket_handle socket = socket_createConnector(); if( m_noDelay ) socket_setsockopt( socket, TCP_NODELAY ); if( m_sendBufSize ) socket_setsockopt( socket, SO_SNDBUF, m_sendBufSize ); if( m_rcvBufSize ) socket_setsockopt( socket, SO_RCVBUF, m_rcvBufSize ); setPending( s ); log->onEvent( "Connecting to " + address + " on port " + IntConvertor::convert((unsigned short)port) + " (Source " + sourceAddress + ":" + IntConvertor::convert((unsigned short)sourcePort) + ")"); ThreadedSocketConnection* pConnection = new ThreadedSocketConnection( s, socket, address, port, getLog(), sourceAddress, sourcePort ); ThreadPair* pair = new ThreadPair( this, pConnection ); { Locker l( m_mutex ); thread_id thread; if ( thread_spawn( &socketThread, pair, thread ) ) { addThread( socket, thread ); } else { delete pair; pConnection->disconnect(); delete pConnection; setDisconnected( s ); } } } catch ( std::exception& ) {} }
THREAD_PROC ThreadedSocketAcceptor::socketConnectionThread( void* p ) { ConnectionThreadInfo * info = reinterpret_cast < ConnectionThreadInfo* > ( p ); ThreadedSocketAcceptor* pAcceptor = info->m_pAcceptor; ThreadedSocketConnection* pConnection = info->m_pConnection; delete info; int socket = pConnection->getSocket(); while ( pConnection->read() ) {} delete pConnection; if( !pAcceptor->isStopped() ) pAcceptor->removeThread( socket ); return 0; }
THREAD_PROC ThreadedSocketInitiator::socketThread( void* p ) { ThreadPair * pair = reinterpret_cast < ThreadPair* > ( p ); ThreadedSocketInitiator* pInitiator = pair->first; ThreadedSocketConnection* pConnection = pair->second; FIX::SessionID sessionID = pConnection->getSession()->getSessionID(); FIX::Session* pSession = FIX::Session::lookupSession( sessionID ); int socket = pConnection->getSocket(); delete pair; pInitiator->lock(); if( !pConnection->connect() ) { pInitiator->getLog()->onEvent( "Connection failed" ); pConnection->disconnect(); delete pConnection; pInitiator->removeThread( socket ); pInitiator->setDisconnected( sessionID ); return 0; } pInitiator->setConnected( sessionID ); pInitiator->getLog()->onEvent( "Connection succeeded" ); pSession->next(); while ( pConnection->read() ) {} delete pConnection; if( !pInitiator->isStopped() ) pInitiator->removeThread( socket ); pInitiator->setDisconnected( sessionID ); return 0; }