[solved] QTcpServer pause listening
how can I pause listening fpor new incoming connections? I like to set a limit for the maximum number of concurrent connections. When this limit is reached, I want to pause listening, so that more incoming connections get ignored or rejected. I want to resume listening, when resources become free again.
Calling QTcpServer::etMaxPendingConnections(1) did not help. I was able to open 4 simultaneous connections, even when the max value was 1.
I tried to pause listening by calling QTcpServer::close() but this closed all open connection which was not what I wanted.
I also tried to simply ignore the QTcpServer::incomingConnection() signal, but then the connections will be accepted causing the connected client to report a read timeout instead of a connection timeout or connection failure.
So, how can I pause accepting incoming connection while keeping current connections connected?
I think I need to call addPendingConnection() in my incomingConnection() method but that's difficult because I want to create the QTcpSocket object in another thread, which has no access to that addPendingConnection() method. I understood that I cannot share a QSocket object between multiple threads. My QTcpSocket is executed by the main thread, while I pass new connections to worker threads.
I checked the source code of QTcpServer a litte. It apperas that I could simply create dummy QTcpSocket object by calling addPendingConnection(new QTcpSocket) whenever I accept a new connection and pass it to the worker thread, which created the "real" QTcpSocket instance bound to the connection.
This way, the pending queue indicates, how many connections are currently open. And if the maximum gets reachted, the QTcpServer pauses listening. Whenever a worker thread finished processing a connection (connection is closed then), I remove one dummy QTcpSocket from the pending queue by calling nextPendingConnection(). This re-enables listening.
That's a bit dirty hack, isn't it? How can I make it better?
I tried that but it does also not work. Even when the pending queue is full, I can still open additional connections. So that does not do what I expected.
That means, my initial question is still open: How can I pause accpting new incoming connections? There must be a way how the server can singlat to the clients that it is busy by either rejecting the connection or (as a fall-back) by ignoring them.
After thinking more about what I really want to do, I came to the conclusion that I do not want to pause listening anymore :-)
Reading the HTTP specification, I found out that the HTTP server shall never ignore or reject connections. In case of overload, it shall send a HTTP 503 response, which is pretty small and does not costs much resources.