[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] Handling the connection close



Hi all,

I want to ask what should I do when the connection is closed while I am sending or recving data. For example the connection is closed by the server explicitly or because the server is down, when the client is still sending data or recving data. For libevent, if the connection is closed, event callback is invoked. But in this callback what should we do? Is there any common practice?

(Any useful code can be directly filled in the following function)

Thanks!

void Connection::EventCb(struct bufferevent *bev, short events, void *ctx) {

    Connection* conn = (Connection*)ctx;
    assert(conn != NULL && bev != NULL);

    if (events & BEV_EVENT_ERROR) {

LOG_TRACE("Error from client bufferevent: %s",evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()));

// tell the client send error, and the client should send again or do some other things
        if (conn->GetStatus() == SENDING) {
            LOG_TRACE("Send error");
            // TODO: let the client know
        }

        conn->Close();
    }

    // The other side explicitly closes the connection
    if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {

        // This means server explicitly closes the connection
LOG_TRACE("ClientEventCb: %s",evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())); // the error string is "SUCCESS"

        // free the buffer event
        conn->Close();
    }
}
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.