implement a max connections (default = 32) settings
This commit is contained in:
parent
e964a0a1f0
commit
6ac3bdb94a
@ -23,11 +23,16 @@ namespace ix
|
|||||||
const int WebSocketServer::kDefaultPort(8080);
|
const int WebSocketServer::kDefaultPort(8080);
|
||||||
const std::string WebSocketServer::kDefaultHost("127.0.0.1");
|
const std::string WebSocketServer::kDefaultHost("127.0.0.1");
|
||||||
const int WebSocketServer::kDefaultTcpBacklog(5);
|
const int WebSocketServer::kDefaultTcpBacklog(5);
|
||||||
|
const size_t WebSocketServer::kDefaultMaxConnections(32);
|
||||||
|
|
||||||
WebSocketServer::WebSocketServer(int port, const std::string& host, int backlog) :
|
WebSocketServer::WebSocketServer(int port,
|
||||||
|
const std::string& host,
|
||||||
|
int backlog,
|
||||||
|
size_t maxConnections) :
|
||||||
_port(port),
|
_port(port),
|
||||||
_host(host),
|
_host(host),
|
||||||
_backlog(backlog),
|
_backlog(backlog),
|
||||||
|
_maxConnections(maxConnections),
|
||||||
_stop(false)
|
_stop(false)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -194,6 +199,19 @@ namespace ix
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (getConnectedClientsCount() >= _maxConnections)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "WebSocketServer::run() reached max connections = "
|
||||||
|
<< _maxConnections << ". "
|
||||||
|
<< "Not accepting connection";
|
||||||
|
logError(ss.str());
|
||||||
|
|
||||||
|
::close(clientFd);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Launch the handleConnection work asynchronously in its own thread.
|
// Launch the handleConnection work asynchronously in its own thread.
|
||||||
//
|
//
|
||||||
// the destructor of a future returned by std::async blocks,
|
// the destructor of a future returned by std::async blocks,
|
||||||
@ -252,4 +270,9 @@ namespace ix
|
|||||||
std::lock_guard<std::mutex> lock(_clientsMutex);
|
std::lock_guard<std::mutex> lock(_clientsMutex);
|
||||||
return _clients;
|
return _clients;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t WebSocketServer::getConnectedClientsCount()
|
||||||
|
{
|
||||||
|
return getClients().size();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,11 @@
|
|||||||
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// pass to callback PATH
|
||||||
|
// pass connection success too to callback
|
||||||
|
//
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <utility> // pair
|
#include <utility> // pair
|
||||||
@ -25,7 +30,8 @@ namespace ix
|
|||||||
public:
|
public:
|
||||||
WebSocketServer(int port = WebSocketServer::kDefaultPort,
|
WebSocketServer(int port = WebSocketServer::kDefaultPort,
|
||||||
const std::string& host = WebSocketServer::kDefaultHost,
|
const std::string& host = WebSocketServer::kDefaultHost,
|
||||||
int backlog = WebSocketServer::kDefaultTcpBacklog);
|
int backlog = WebSocketServer::kDefaultTcpBacklog,
|
||||||
|
size_t maxConnections = WebSocketServer::kDefaultMaxConnections);
|
||||||
virtual ~WebSocketServer();
|
virtual ~WebSocketServer();
|
||||||
|
|
||||||
void setOnConnectionCallback(const OnConnectionCallback& callback);
|
void setOnConnectionCallback(const OnConnectionCallback& callback);
|
||||||
@ -43,6 +49,7 @@ namespace ix
|
|||||||
int _port;
|
int _port;
|
||||||
std::string _host;
|
std::string _host;
|
||||||
int _backlog;
|
int _backlog;
|
||||||
|
size_t _maxConnections;
|
||||||
|
|
||||||
OnConnectionCallback _onConnectionCallback;
|
OnConnectionCallback _onConnectionCallback;
|
||||||
|
|
||||||
@ -63,10 +70,12 @@ namespace ix
|
|||||||
const static int kDefaultPort;
|
const static int kDefaultPort;
|
||||||
const static std::string kDefaultHost;
|
const static std::string kDefaultHost;
|
||||||
const static int kDefaultTcpBacklog;
|
const static int kDefaultTcpBacklog;
|
||||||
|
const static size_t kDefaultMaxConnections;
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
void run();
|
void run();
|
||||||
void handleConnection(int fd);
|
void handleConnection(int fd);
|
||||||
|
size_t getConnectedClientsCount();
|
||||||
|
|
||||||
// Logging
|
// Logging
|
||||||
void logError(const std::string& str);
|
void logError(const std::string& str);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user