ns_error basis_iostream::write(const std::string& vl, timeouttype tmout) { checkconnect(); if (state_ == connected) { is_timout = false; is_data_ready = false; is_error = false; is_connect = true; error_cod = 0; try { set_wb(vl); io_service_.reset(); //DEBUG_STR_DVNCI(SET ASYNCWRITE) write_impl(); //DEBUG_STR_DVNCI(SET ASYNCTIME) tmout_timer_.expires_from_now(boost::posix_time::milliseconds(tmout ? tmout : timout())); tmout_timer_.async_wait(boost::bind( &basis_iostream::io_handle_timout_expire, shared_from_this(), boost::asio::placeholders::error)); io_service_.run(); } catch (dvncierror& errd) { if (errd.code() == ERROR_IO_SERVICE_LOCK) throw errd; return (errd.code()); } catch (...) { DEBUG_STR_DVNCI(ERROR RESULT); return NS_ERROR_NODEF; } //DEBUG_STR_DVNCI(SET RESULT) if (is_timout) { error_cod = ERROR_IO_TIMOUT_EXPIRE; return error_cod; } if (is_error) { error_cod = ERROR_FAILNET_CONNECTED; return error_cod; } return 0; } return ERROR_IO_LINK_NOT_CONNECTION; }
CURLcode Curl_is_connected(struct connectdata *conn, int sockindex, bool *connected) { struct SessionHandle *data = conn->data; CURLcode code = CURLE_OK; curl_socket_t sockfd = conn->sock[sockindex]; long allow = DEFAULT_CONNECT_TIMEOUT; int error = 0; struct timeval now; enum chkconn_t chk; DEBUGASSERT(sockindex >= FIRSTSOCKET && sockindex <= SECONDARYSOCKET); *connected = FALSE; /* a very negative world view is best */ if(conn->bits.tcpconnect[sockindex]) { /* we are connected already! */ *connected = TRUE; return CURLE_OK; } now = Curl_tvnow(); /* figure out how long time we have left to connect */ allow = Curl_timeleft(data, &now, TRUE); if(allow < 0) { /* time-out, bail out, go home */ failf(data, "Connection time-out"); return CURLE_OPERATION_TIMEDOUT; } /* check socket for connect */ chk = checkconnect(sockfd); if(CHKCONN_IDLE == chk) { if(curlx_tvdiff(now, conn->connecttime) >= conn->timeoutms_per_addr) { infof(data, "After %ldms connect time, move on!\n", conn->timeoutms_per_addr); goto next; } /* not an error, but also no connection yet */ return code; } if(CHKCONN_CONNECTED == chk) { if(verifyconnect(sockfd, &error)) { /* we are connected with TCP, awesome! */ /* see if we need to do any proxy magic first once we connected */ code = Curl_connected_proxy(conn); if(code) return code; conn->bits.tcpconnect[sockindex] = TRUE; *connected = TRUE; if(sockindex == FIRSTSOCKET) Curl_pgrsTime(data, TIMER_CONNECT); /* connect done */ Curl_verboseconnect(conn); Curl_updateconninfo(conn, sockfd); return CURLE_OK; } /* nope, not connected for real */ } else { /* nope, not connected */ if(CHKCONN_FDSET_ERROR == chk) { (void)verifyconnect(sockfd, &error); infof(data, "%s\n",Curl_strerror(conn, error)); } else infof(data, "Connection failed\n"); } /* * The connection failed here, we should attempt to connect to the "next * address" for the given host. But first remember the latest error. */ if(error) { data->state.os_errno = error; SET_SOCKERRNO(error); } next: conn->timeoutms_per_addr = conn->ip_addr->ai_next == NULL ? allow : allow / 2; code = trynextip(conn, sockindex, connected); if(code) { error = SOCKERRNO; data->state.os_errno = error; failf(data, "Failed connect to %s:%ld; %s", conn->host.name, conn->port, Curl_strerror(conn, error)); } return code; }
ns_error basis_iostream::read(std::string& vl, size_t cnt, timeouttype tmout) { checkconnect(); if (state_ == connected) { is_timout = false; is_data_ready = false; is_error = false; is_connect = true; error_cod = 0; size_t need_cnt = cnt; if (need_cnt) { if (size_rb() > 0) { if (size_rb() < need_cnt) { need_cnt = need_cnt - size_rb(); get_rb(vl); } else { get_rb(vl, need_cnt); return 0; } } } try { io_service_.reset(); //DEBUG_STR_DVNCI(SET ASYNCWRITE) read_impl(need_cnt); //DEBUG_STR_DVNCI(SET ASYNCTIME) tmout_timer_.expires_from_now(boost::posix_time::milliseconds(tmout ? tmout : timout())); tmout_timer_.async_wait(boost::bind( &basis_iostream::io_handle_timout_expire, shared_from_this(), boost::asio::placeholders::error)); //DEBUG_STR_DVNCI(SET RUN) io_service_.run(); } catch (dvncierror& errd) { if (errd.code() == ERROR_IO_SERVICE_LOCK) throw errd; return (errd.code()); } catch (...) { DEBUG_STR_DVNCI(ERROR RESULT); return NS_ERROR_NODEF; } //DEBUG_STR_DVNCI(SET RESULT) if (is_timout) { error_cod = ERROR_IO_TIMOUT_EXPIRE; return error_cod; } if (is_error) { error_cod = ERROR_FAILNET_CONNECTED; return error_cod; } if (!is_data_ready) return ERROR_NULLRESPONSE; if (size_rb() < need_cnt) return ERROR_NULLRESPONSE; if (need_cnt) get_rb(vl, need_cnt); else get_rb(vl); return 0; } return ERROR_IO_LINK_NOT_CONNECTION; }