zaphoyd / websocketpp

C++ websocket client/server library
http://www.zaphoyd.com/websocketpp
Other
7.01k stars 1.97k forks source link

The close handler will never be called for iostream client #290

Open yinpengji opened 11 years ago

yinpengji commented 11 years ago

I'm using the iostream client as an engine to generate/access payload from sockets. Everything works fine except that after close is called, the on_close callback will never be called.

I debugged into the code and found the code below: websocketpp_0_3_x\websocketpp\impl\connection_impl.hpp at line 1756

    } else if (m_state == session::state::closing && !m_was_clean) {
        // ack of our close
        m_alog.write(log::alevel::devel,"Got acknowledgement of close");

        m_was_clean = true;

        // If we are a server terminate the connection now. Clients should
        // leave the connection open to give the server an opportunity to
        // initiate the TCP close. The client's timer will handle closing
        // its side of the connection if the server misbehaves.
        //
        // TODO: different behavior if the underlying transport doesn't
        // support timers?
        if (m_is_server) {
            terminate(lib::error_code());
        }
    } else {
zaphoyd commented 11 years ago

The iostream transport now includes eof() and fatal_error() methods that can be used by the part of your program feeding bytes to the connection to signal these conditions to the library as appropriate. In general if you are reading from a socket and you get EOF on your socket you should signal EOF to the transport. Same with another type of socket error. If you are not actually backed by a socket but by something else (reading a canned connection from a file, or other stream) you may need to play around with exactly when it would be appropriate to "simulate" an EOF or transport error. This should enable the full proper close behavior for the library.

I'm working on some additional documentation about how to use the iostream transport to full effect.

yinpengji commented 11 years ago

If I understand correctly, if I feed all the data from the socket to the websocket++ lib, if the data contains EOF, the websocket++ will call eof() by itself, and I do not need to call eof() explicitly, right? The same with fatal_error.

If no error happens from the socket during the connection, I just want to clean up the resource before the websocket++ lib get EOF data, I just need to call eof() manually, right?