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