int SocketConnector::connect( const std::string& address, int port, bool noDelay, int sendBufSize, int rcvBufSize , int mode) { int socket = socket_createConnector(mode); if ( socket != -1 ) { if( noDelay ) socket_setsockopt( socket, TCP_NODELAY ); if( sendBufSize ) socket_setsockopt( socket, SO_SNDBUF, sendBufSize ); if( rcvBufSize ) socket_setsockopt( socket, SO_RCVBUF, rcvBufSize ); if( socket_connect( socket, address.c_str(), port ) < 0 ) { DWORD code = GetLastError(); socket_close( socket ); SetLastError(code); socket = -1; } else { m_monitor.addConnect( socket ); } } return socket; }
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& ) {} }
void ThreadedSSLSocketInitiator::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; getHost(s, d, address, port); int 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)); SSL *ssl = SSL_new(m_ctx); if (ssl == 0) { log->onEvent("Failed to create ssl object"); return; } SSL_clear(ssl); BIO *sbio = BIO_new_socket(socket, BIO_CLOSE); SSL_set_bio(ssl, sbio, sbio); ThreadedSSLSocketConnection *pConnection = new ThreadedSSLSocketConnection( s, socket, ssl, address, port, getLog()); ThreadPair *pair = new ThreadPair(this, pConnection); { Locker l(m_mutex); thread_id thread; if (thread_spawn(&socketThread, pair, thread)) { addThread(SocketKey(socket, ssl), thread); } else { delete pair; pConnection->disconnect(); delete pConnection; SSL_free(ssl); setDisconnected(s); } } } catch (std::exception &) { } }