PDA

View Full Version : QWebSocketServer howto move new connections to a own thread



M4chin3
5th November 2014, 18:45
Hi,

I’m developing a client-server application. Therefore I’m using the QWebSocket class to achieve binary transparency and bidirectional data transfer. To listen for incoming connections I’m using the QWebSocketServer class which works pretty nice so far. (basically the echoclient and echoserver example)

Now my question:
I would like to have a thread for every incoming connection (the QTcpServer class does support this) but the documentation says the returned QWebSocket* pointer by nextPendingConnection() cannot be used from another thead.
Is this somehow possible and how?

wysota
5th November 2014, 20:51
I would like to have a thread for every incoming connection
Bad idea. I could crash your app in seconds.


Is this somehow possible and how?
If the docs says it can't be used from another thread then it can't. If you really want to, you can make a proxy that will talk to the socket over cross-thread signals and slots. However I really can't understand why so many people try to force using threads for network communication. There is no blocking operation here, so there is simply no benefit from using threads and there are usually many problems with thread synchronization.

M4chin3
5th November 2014, 21:15
Bad idea. I could crash your app in seconds.

Why?


If the docs says it can't be used from another thread then it can't......

The same thing is mentioned for QTcpServer for the function QTcpServer::nextPendingConnection but it is possible to overwrite QTcpServer::incomingConnection for this.
I just wanted to know if there is a possiblity to do it.

Why threads? -> Maybe every connection can do a blocking operation in my application and therefore I want to decouple them from the rest of the application/listener.

wysota
5th November 2014, 23:21
Why?
Because usually the number of threads you can spawn per process or per user is limited. By opening a large number of concurrent connections I'd either force your app to run out of threads or starve it by flooding it with context switching.


Why threads? -> Maybe every connection can do a blocking operation in my application and therefore I want to decouple them from the rest of the application/listener.
Then you want to perform that blocking operation in another thread (or better yet using a limited thread pool) and not push the whole communication there. You can decouple whatever you want decoupled by encapsulating it in an object. You don't require threads for that.

M4chin3
6th November 2014, 00:32
By decoupling I meant moving the task to another thread to avoid blocking the whole program. But basically you're right, I can move the blocking tasks to threads and keep the connections all together within another thread. I'm not worrying about the number of threads shouldn't be so many, but I'm giving the limited thread pool a thought.

Thanks!

wysota
6th November 2014, 00:56
I'm not worrying about the number of threads shouldn't be so many

Right... and 640kB or RAM should be enough for anybody :)

anda_skoa
6th November 2014, 20:21
The same thing is mentioned for QTcpServer for the function QTcpServer::nextPendingConnection but it is possible to overwrite QTcpServer::incomingConnection for this.


I think that part of the docs is actually outdated. Somewhen during Qt5 it became possible to move QTcpSocket instances to different threads.

Cheers,
_