Search code examples
libwebsockets

libwebsocket: How to disconnect client if there is no server response for a while (with timeout)?


I have a client using libwebsocket to establish a connection to a server. Whenever the client sends a request, the server sends a response and after receiving the response the client closes the connection. Works fine.

But when the server does not answer to the request i have the problem that the client keeps waiting for a response forever. When nothing happens the callback is never called and its not possible to close connection with returning -1 from callback function.

Is there any way to enable a timeout for the connection to close? Or any possibility to close connection from outside the callback function?

Here is my code so far:

int callback_function(libwebsocket_context* context, libwebsocket* wsi, enum libwebsocket_callback_reasons reason, void* user, void* in, size_t len) {

    switch (reason) {

        case LWS_CALLBACK_CLIENT_ESTABLISHED: {
            std::cout << "LWS_CALLBACK_CLIENT_ESTABLISHED" << std::endl;
            libwebsocket_callback_on_writable(context, wsi);
        }
        break;

        case LWS_CALLBACK_CLOSED:{
            std::cout << "LWS_CALLBACK_CLOSED" << std::endl;
        }
        break;

        case LWS_CALLBACK_CLIENT_RECEIVE_PONG:
        case LWS_CALLBACK_CLIENT_RECEIVE:{
            std::cout << "LWS_CALLBACK_CLIENT_RECEIVE" << endl;
            ((char *)in)[len] = '\0';
            answers_[current_request] = answers_[current_request] + string((char *)in);
            if (libwebsocket_is_final_fragment(wsi)){
                std::cout << "request:" << requests_[current_request] << std::endl;
                std::cout << "answer:" << answers_[current_request] << std::endl;
                current_request++;
                if(current_request >= answers_.size()) {
                    ready = true;

                    return -1;
                }
                libwebsocket_callback_on_writable(context, wsi);
            }
        }
        break;

        case LWS_CALLBACK_CLIENT_WRITEABLE:{
            std::cout << "LWS_CALLBACK_CLIENT_WRITEABLE" << endl;
            unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 + LWS_SEND_BUFFER_POST_PADDING];

            const std::string message = std::string(requests_[current_request]);

            std::copy(message.begin(), message.end(), &buf[LWS_SEND_BUFFER_PRE_PADDING]);
            buf[LWS_SEND_BUFFER_PRE_PADDING+(int)message.size()]='\0';

            int n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], (size_t)message.size(), static_cast<libwebsocket_write_protocol>(LWS_WRITE_BINARY));

            if (n < 0){
                std::cout << kLogErr << "bad things are happening" << std::endl;
                return -1;
            }
            if (n < (int)message.size()) {
                std::cout << kLogErr << "Partial write LWS_CALLBACK_CLIENT_WRITEABLE" << std::endl;
                return -1;
            }
        }
        break;

        default:
            std::cout << "CALLBACK_DEFAULT: " << reason << endl;
        break;
    }

    return 0;
}

vector<string> sendMessage(const string& server, int port, const string& path, const vector<string>& messages, bool& error) {

    ready = error = false;
    current_request = 0;

    requests_ = vector<string>(messages);
    answers_ = vector<string>(requests_.size(), "");
    int ietf_version = -1; /* latest */
    wsi_ = libwebsocket_client_connect(context_, server.c_str(), port, 2,  path.c_str(), server.c_str(), "origin", NULL, ietf_version);

    if (wsi_ == NULL) {
        std::cout << kLogErr << "libwebsocket connect failed server:" << server  << " port: " << port << " path: " << path << std::endl;
        error = true;
        return vector<string>();
    }

    bool first_time = true;
    int n = 0;
    while (n >= 0 && !force_exit && !ready) {
        n = libwebsocket_service(context_, 0);
        if(first_time) {
            libwebsocket_callback_on_writable(context_, wsi_);
            first_time = false;
        }
        if (n < 0){
            continue;
        }
        if (wsi_ == NULL) {
            break;
        }
    }
    error = !ready;
    wsi_ = NULL;
    return vector<string>(answers_);
}

Solution

  • I solved the problem.

    I programmed a timer in

    vector<string> sendMessage(const string& server, int port, const string& path, const vector<string>& messages, bool& error) 
    

    and when the timeout is reaches, the timer sets a flag and triggers

    libwebsocket_callback_on_writable(context_, wsi_);
    

    again. Then in

    int callback_function(libwebsocket_context* context, libwebsocket* wsi, enum libwebsocket_callback_reasons reason, void* user, void* in, size_t len)
    

    in case

    LWS_CALLBACK_CLIENT_WRITEABLE
    

    i check the flag and if it is set the callback is aborted with

    return -1;
    

    Works fine!