int main() { try { SocketAcceptor server; server.start(); } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; }
void initialize(Target& target) { broker::Broker* broker = dynamic_cast<broker::Broker*>(&target); // Only provide to a Broker if (broker) { if (!options.socketFds.empty()) { const broker::Broker::Options& opts = broker->getOptions(); SocketAcceptor* sa = new SocketAcceptor(opts.tcpNoDelay, false, opts.maxNegotiateTime, broker->getTimer()); for (unsigned i = 0; i<options.socketFds.size(); ++i) { int fd = options.socketFds[i]; if (!isSocket(fd)) { QPID_LOG(error, "Imported socket fd " << fd << ": isn't a socket"); continue; } Socket* s = new BSDSocket(fd); sa->addListener(s); QPID_LOG(notice, "Listening on imported socket: " << s->getLocalAddress()); } broker->registerTransport("socket", TransportAcceptor::shared_ptr(sa), TransportConnector::shared_ptr(), 0); } else { QPID_LOG(debug, "No Socket fd specified"); } } }
bool SocketConnection::read( SocketAcceptor& a, SocketServer& s ) { 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; }
bool SocketConnection::read( SocketAcceptor& a, SocketServer& s ) { std::string msg; try { if ( !m_pSession ) { int timeout = 1000; // 1000ms = 1 second struct pollfd pfd = { m_socket, POLLIN | POLLPRI, 0 }; while( !readMessage( msg ) ) { int result = poll( &pfd, 1, 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; }