void ThreadedSocketAcceptor::onInitialize( const SessionSettings& s ) throw ( RuntimeError ) { short port = 0; std::set<int> ports; std::set<SessionID> sessions = s.getSessions(); std::set<SessionID>::iterator i = sessions.begin(); for( ; i != sessions.end(); ++i ) { const Dictionary& settings = s.get( *i ); port = (short)settings.getInt( SOCKET_ACCEPT_PORT ); m_portToSessions[port].insert( *i ); if( ports.find(port) != ports.end() ) continue; ports.insert( port ); const bool reuseAddress = settings.has( SOCKET_REUSE_ADDRESS ) ? settings.getBool( SOCKET_REUSE_ADDRESS ) : true; const bool noDelay = settings.has( SOCKET_NODELAY ) ? settings.getBool( SOCKET_NODELAY ) : false; const int sendBufSize = settings.has( SOCKET_SEND_BUFFER_SIZE ) ? settings.getInt( SOCKET_SEND_BUFFER_SIZE ) : 0; const int rcvBufSize = settings.has( SOCKET_RECEIVE_BUFFER_SIZE ) ? settings.getInt( SOCKET_RECEIVE_BUFFER_SIZE ) : 0; int socket = socket_createAcceptor( port, reuseAddress ); if( socket < 0 ) { SocketException e; socket_close( socket ); throw RuntimeError( "Unable to create, bind, or listen to port " + IntConvertor::convert( (unsigned short)port ) + " (" + e.what() + ")" ); } if( noDelay ) socket_setsockopt( socket, TCP_NODELAY ); if( sendBufSize ) socket_setsockopt( socket, SO_SNDBUF, sendBufSize ); if( rcvBufSize ) socket_setsockopt( socket, SO_RCVBUF, rcvBufSize ); m_socketToPort[socket] = port; m_sockets.insert( socket ); } }
int SocketServer::add( int port, bool reuse, bool noDelay, int sendBufSize, int rcvBufSize ) throw( SocketException& ) { if( m_portToInfo.find(port) != m_portToInfo.end() ) return m_portToInfo[port].m_socket; int socket = socket_createAcceptor( port, reuse ); if( socket < 0 ) throw SocketException(); if( noDelay ) socket_setsockopt( socket, TCP_NODELAY ); if( sendBufSize ) socket_setsockopt( socket, SO_SNDBUF, sendBufSize ); if( rcvBufSize ) socket_setsockopt( socket, SO_RCVBUF, rcvBufSize ); m_monitor.addRead( socket ); SocketInfo info( socket, port, noDelay, sendBufSize, rcvBufSize ); m_socketToInfo[socket] = info; m_portToInfo[port] = info; return socket; }