Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle event_cb being called after the socket is closed. #1318

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 14 additions & 8 deletions docs/examples/asiohiper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,19 @@ static void check_multi_info(GlobalInfo *g)
}

/* Called by asio when there is an action on a socket */
static void event_cb(GlobalInfo *g, boost::asio::ip::tcp::socket *tcp_socket,
static void event_cb(GlobalInfo *g, curl_socket_t s,
int action, const boost::system::error_code & error,
int *fdp)
{
fprintf(MSG_OUT, "\nevent_cb: action=%d", action);

if (socket_map.find(s) == socket_map.end()) {
fprintf(MSG_OUT, "\nevent_cb: socket already closed");
return;
}

/* make sure the event matches what are wanted */
if(*fdp == action || *fdp == CURL_POLL_INOUT) {
curl_socket_t s = tcp_socket->native_handle();
CURLMcode rc;
if(error)
action = CURL_CSELECT_ERR;
Expand All @@ -197,14 +201,16 @@ static void event_cb(GlobalInfo *g, boost::asio::ip::tcp::socket *tcp_socket,
* in curl_multi_socket_action(), so check them both */
if(!error && socket_map.find(s) != socket_map.end() &&
(*fdp == action || *fdp == CURL_POLL_INOUT)) {
boost::asio::ip::tcp::socket *tcp_socket = socket_map.find(s)->second;

if(action == CURL_POLL_IN) {
tcp_socket->async_read_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket,
boost::bind(&event_cb, g, s,
action, _1, fdp));
}
if(action == CURL_POLL_OUT) {
tcp_socket->async_write_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket,
boost::bind(&event_cb, g, s,
action, _1, fdp));
}
}
Expand Down Expand Up @@ -257,28 +263,28 @@ static void setsock(int *fdp, curl_socket_t s, CURL *e, int act, int oldact,
fprintf(MSG_OUT, "\nwatching for socket to become readable");
if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) {
tcp_socket->async_read_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket,
boost::bind(&event_cb, g, s,
CURL_POLL_IN, _1, fdp));
}
}
else if(act == CURL_POLL_OUT) {
fprintf(MSG_OUT, "\nwatching for socket to become writable");
if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) {
tcp_socket->async_write_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket,
boost::bind(&event_cb, g, s,
CURL_POLL_OUT, _1, fdp));
}
}
else if(act == CURL_POLL_INOUT) {
fprintf(MSG_OUT, "\nwatching for socket to become readable & writable");
if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) {
tcp_socket->async_read_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket,
boost::bind(&event_cb, g, s,
CURL_POLL_IN, _1, fdp));
}
if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) {
tcp_socket->async_write_some(boost::asio::null_buffers(),
boost::bind(&event_cb, g, tcp_socket,
boost::bind(&event_cb, g, s,
CURL_POLL_OUT, _1, fdp));
}
}
Expand Down