From 499756bcf8ba63adff4debb29776e59848af5f7d Mon Sep 17 00:00:00 2001 From: Cathy Fitzpatrick Date: Sun, 24 Oct 2010 14:46:45 -0600 Subject: [PATCH] Added user limit. --- src/main/main.cpp | 10 +++++++--- src/network/network.cpp | 15 +++++++++++---- src/network/network.h | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/main.cpp b/src/main/main.cpp index 00efee9..722f946 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -55,7 +55,7 @@ const char *getPidFileName() { int initialise(int argc, char **argv, bool &daemon) { string configFile; - int port, databasePort, workerThreads, serverUid; + int port, databasePort, workerThreads, serverUid, userLimit; string serverName, welcomeFile, welcomeMessage; string databaseName, databaseHost, databaseUser, databasePassword; string authParameter, loginParameter, registerParameter; @@ -80,6 +80,10 @@ int initialise(int argc, char **argv, bool &daemon) { po::value(&port)->default_value( 8446), "server port") + ("server.limit", + po::value(&userLimit)->default_value( + -1), + "maximum number of users allowed") ("server.threads", po::value(&workerThreads)->default_value( 20), @@ -88,7 +92,7 @@ int initialise(int argc, char **argv, bool &daemon) { po::value(&serverUid), "UID to run the server process as") ("auth.salt", - "use simple simple authentication") + "use simple salt authentication") ("auth.vbulletin", po::value( &authParameter)->implicit_value("vbulletin"), @@ -278,7 +282,7 @@ int initialise(int argc, char **argv, bool &daemon) { } } - network::Server server(port); + network::Server server(port, userLimit); server.installSignalHandlers(); ScriptMachine *machine = server.getMachine(); diff --git a/src/network/network.cpp b/src/network/network.cpp index e2f12b9..a78c5c8 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -627,7 +627,7 @@ typedef pair CLAUSE_PAIR; class ServerImpl { public: - ServerImpl(Server *, const int); + ServerImpl(Server *, const int, const int); Server *getServer() const { return m_server; } void installSignalHandlers(); void run(); @@ -697,6 +697,7 @@ class ServerImpl { ChannelPtr m_mainChannel; CLIENT_LIST m_clients; int m_population; + const int m_userLimit; shared_mutex m_clientMutex; io_service m_service; tcp::acceptor m_acceptor; @@ -717,8 +718,8 @@ class ServerImpl { ServerImpl *ServerImpl::m_blockingServer = NULL; -Server::Server(const int port) { - m_impl = new ServerImpl(this, port); +Server::Server(const int port, const int userLimit) { + m_impl = new ServerImpl(this, port, userLimit); } void Server::installSignalHandlers() { @@ -2099,8 +2100,9 @@ ServerImpl::ClauseList::ClauseList(vector &clauses) finalise(); } -ServerImpl::ServerImpl(Server *server, const int port): +ServerImpl::ServerImpl(Server *server, const int port, const int userLimit): m_population(0), + m_userLimit(userLimit), m_acceptor(m_service, tcp::endpoint(tcp::v4(), port), true), m_server(server) { acceptClient(); @@ -2380,6 +2382,11 @@ void ServerImpl::handleAccept(ClientImplPtr client, << error.message() << endl; return; } + if (m_population > m_userLimit) { + boost::system::error_code ec; + client->getSocket().close(ec); + return; + } const boost::system::error_code startError = client->start(); if (startError) { Log::out() << "Error in ClientImpl::start(): " diff --git a/src/network/network.h b/src/network/network.h index 4ebd0d8..6e7d656 100644 --- a/src/network/network.h +++ b/src/network/network.h @@ -68,7 +68,7 @@ class Client { class Server { public: - Server(const int port); + Server(const int port, const int userLimit); ~Server(); void installSignalHandlers(); void run();