Search code examples
c++websocketpocopoco-libraries

Aysnc handler for incoming messages in POCO Websocket


I am trying to design Websocket Server using POCO libraries. I have implemented a simple class WebSocketRequestHandler: public HTTPRequestHandler which accepts connection and perform tasks and so on The code snippet is below:

class WebSocketRequestHandler: public HTTPRequestHandler
// Handle a WebSocket connection.
{
public:
    WebSocketRequestHandler()
    {
    }

    void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
    {
        Application& app = Application::instance();
        try
        {
            std::cout << "Waiting for connection.." << std::endl;
            WebSocket ws(request, response);
            ws.setReceiveTimeout(Poco::Timespan(10, 0, 0, 0, 0)); 
            app.logger().information("WebSocket connection established!");
            int flags;
            int n;
            do
            {
                std::cout << "Waiting for incoming frame" << std::endl;
                n = ws.receiveFrame(buffer, sizeof(buffer), flags);
                std::cout << "Frame received" << std::endl;
                //Parse the frame sequentially and so on..
            }
}

I don't want sequential operation for the server. It should be async. I tried finding in POCO resources but couldn't find anything related to it. So does POCO provide async api? Like pushing the incoming messages in a queue and having a separate thread handle the messages separately while the main thread keeps on receiving messages from clients? Or May be Something like boost asio api, registering function via async_handle(socket, func*) and handling each message in a separate thread? Or any other better solution?


Solution

  • There is SocketReactor for TCP, but there is no async HTTPServer, every HTTP requests is handled in its own thread, which is obtained from ThreadPool. While this works fine for a low request frequency, it will not scale. To scale it, you can use NotificationQueue for async notifications, or PollSet to react to sockets readable/writeable states - just make sure the queue or pollset are long-lived; handlers are short lived and created for every request, so you should create your processing facility in the HTTP request factory.

    Note that PollSet has a windows bug, which was fixed for the next release.

    EDIT: to clarify further, strictly speaking, HTTPServer is not sequential, because every handler uses a (pre-created) thread; it is best to do as little work as possible in the handling function and "offload" the bulk of the workload elsewhere.