piclouddispatcher patch

This commit is contained in:
2021-08-06 10:49:14 +03:00
parent 25def958a1
commit ca09b9d14b
6 changed files with 83 additions and 339 deletions

View File

@@ -17,7 +17,7 @@ void DispatcherClient::start() {
DispatcherClient::~DispatcherClient() {
// delete eth;
delete eth;
}

View File

@@ -4,6 +4,7 @@
DispatcherServer::DispatcherServer(PIEthernet::Address addr) : eth(PIEthernet::TCP_Server) {
client_gid = 0;
max_connections = 1000;
eth.setParameter(PIEthernet::ReuseAddress);
eth.setReadAddress(addr);
CONNECTU(&eth, newConnection, this, newConnection);
@@ -123,11 +124,9 @@ const DispatcherClient * DispatcherServer::getConnection(int index) {
const DispatcherClient * DispatcherServer::getServer(int index) {
const DispatcherClient * ret = nullptr;
const DispatcherClient * tmp;
map_mutex.lock();
if (index >=0 && index < clients.size_s()) {
tmp = clients[index];
if (index_c_servers.contains(tmp)) ret = tmp;
if (index_c_servers.contains(clients[index])) ret = clients[index];
}
map_mutex.unlock();
return ret;
@@ -149,6 +148,11 @@ PISet<const DispatcherClient *> DispatcherServer::getServers(PISet<int> ids) {
}
void DispatcherServer::setMaxConnections(uint max_count) {
max_connections = max_count;
}
void DispatcherServer::disconnectClient(DispatcherClient *client) {
if (!clients.contains(client)) {
//piCoutObj << "INVALID client" << client;
@@ -183,6 +187,11 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) {
void DispatcherServer::newConnection(PIEthernet *cl) {
if (clients.size_s() >= max_connections) {
cl->close();
delete cl;
return;
}
DispatcherClient * client = new DispatcherClient(cl, client_gid++);
CONNECTU(client, disconnectEvent, this, disconnectClient);
CONNECTL(client, registerServer, [this](const PIByteArray & sname, DispatcherClient * c){

View File

@@ -19,6 +19,8 @@ public:
const DispatcherClient * getConnection(int index);
const DispatcherClient * getServer(int index);
PISet<const DispatcherClient *> getServers(PISet<int> ids);
void setMaxConnections(uint max_count);
uint maxConnections() const {return max_connections;}
EVENT_HANDLER0(void, picoutStatus);
private:
@@ -35,6 +37,7 @@ private:
PITimer timeout_timer;
PIMutex map_mutex;
uint client_gid;
uint max_connections;
};
#endif // DISPATCHERSERVER_H

View File

@@ -69,15 +69,18 @@ int main (int argc, char * argv[]) {
PIString conf_path = confDir();
PIDir::make(conf_path);
conf_path += "/picloud.conf";
uint max_connections = 1000;
if (!PIFile::isExists(conf_path)) {
PIFile f(conf_path, PIIODevice::ReadWrite);
f << "ip = " << addr.ipString() << "\n"
<< "port = " << addr.port() << "\n";
<< "port = " << addr.port() << "\n"
<< "max_connections = " << max_connections << "\n";
}
{
PIConfig conf(conf_path, PIIODevice::ReadOnly);
addr.setIP(conf.getValue("ip", addr.ipString()).toString());
addr.setPort(conf.getValue("port", addr.port()).toUShort());
PIConfig conf(conf_path, PIIODevice::ReadOnly);
addr.setIP(conf.getValue("ip", addr.ipString()).toString());
addr.setPort(conf.getValue("port", addr.port()).toUShort());
max_connections = conf.getValue("max_connections", max_connections).toUInt();
}
PITimer status_timer;
@@ -86,6 +89,7 @@ int main (int argc, char * argv[]) {
if (cli.hasArgument("port"))
addr.setPort(cli.argumentValue("port").toInt());
DispatcherServer server(addr);
server.setMaxConnections(max_connections);
if (cli.hasArgument("screen")) {
PISet<const DispatcherClient *> sel_servers;
PIScreen screen(false);