System: Ubuntu 16.0.4, BOOST
First problem:
1, set to a non-blocking socket. Connect (... ) should be how to set the timeout? Physical broken network, timeout here doesn't work;
2, the first boost: : an asio: : read_until (... ) timeout can capture and processing normally, but the second and subsequent read_until (... ) a non-blocking read the data, to set up a block to read the data, I want to ask, in this case, I need to set the timeout, behind read_until (... ) should be how to set the timeout?
The code is as follows:
Int HttpCommandInterface: : httpGet (const STD: : string request_path, STD: : string & amp; The header, STD: : string & amp; The content)
{
The header="";
The content="";
IP using boost: : an asio: : : : TCP;
Try
{
Boost: : an asio: : io_service io_service;
//Lookup the endpoint
TCP: : resolver resolver (io_service);
TCP: : resolver: : query query (http_host_, STD: : to_string (http_port_));
TCP: : resolver: : iterator endpoint_iterator=resolver. Resolve (query);
TCP: : resolver: : iterator end;
//Create a socket
TCP: : socket socket (io_service);
Boost: : system: : error_code error=boost: : an asio: : error: : host_not_found;
Fd_set fdSelect;
Struct timeval tvSocket;
Boost: : an asio: : socket_base: : non_blocking_io io_option (true);
//to Iterate over endpoints and etablish connection
While (error & amp; & Endpoint_iterator!=end)
{
socket.close();
TvSocket. Tv_sec=10;
TvSocket. Tv_usec=0;
Socket. The open (TCP: : v4 ());
The setsockopt (socket. Native (), SOL_SOCKET, SO_RCVTIMEO, & amp; TvSocket, sizeof (tvSocket));
The setsockopt (socket. Native (), SOL_SOCKET, SO_SNDTIMEO, & amp; TvSocket, sizeof (tvSocket));
Io_option. Set (true);
Socket. Io_control (io_option);//all set to not block with a select to test
Socket. Connect (* endpoint_iterator + +, the error).
FD_ZERO (& amp; FdSelect);
FD_SET (socket. Native (), & amp; FdSelect);
TvSocket. Tv_sec=10;
TvSocket. Tv_usec=0;
If (select (socket. Native () + 1, NULL, & amp; FdSelect, NULL, & amp; TvSocket) & lt;=0 | |! FD_ISSET (socket. Native (), & amp; FdSelect))
{
Error=boost: : an asio: : error: : host_not_found;
}
The else
{
Error=boost: : system: : errc: : make_error_code (boost: : system: : errc: : success);
break;
}
}
If (error)
Throw the boost: : system: : system_error (error);
//Prepare request
Boost: : an asio: : streambuf request;
STD: : ostream request_stream (& amp; Request);
Request_stream & lt; <"GET" & lt;
Boost: : an asio: : write (socket, request);
//Read the response status line. The response streambuf will automatically
//turns to accommodate the - line. The growth may be limited by passing
//a maximum size to the streambuf constructor.
TvSocket. Tv_sec=10;
TvSocket. Tv_usec=0;
FD_ZERO (& amp; FdSelect);
FD_SET (socket. Native (), & amp; FdSelect);
TvSocket. Tv_sec=10;
TvSocket. Tv_usec=0;
If (select (socket. Native () + 1, & amp; FdSelect, NULL, NULL, & amp; TvSocket) & lt;=0 | |! FD_ISSET (socket. Native (), & amp; FdSelect))
{
MYLOG_PRINT (wayToPrint, "RevTimeOut");
return 0;
}
Boost: : an asio: : streambuf response;
Boost: : an asio: : read_until (socket, the response, "\ r \ n");
Io_option. Set (false);//ifdown to also won't read here wouldn't block must be to restore to block or can't read data
Socket. Io_control (io_option);
//Check that the response is OK.
STD: : cost response_stream (& amp; The response);
STD: : string http_version;
Response_stream & gt;> Http_version;
Unsigned int status_code;
Response_stream & gt;> Status_code;
STD: : string status_message;
STD: : getline (response_stream status_message);
if (! Response_stream | | http_version. Substr (0, 5).
="HTTP/"){
MYLOG_PRINT (wayToPrint, "Invalid response");
return 0;
}
//Read the response headers, which are terminated by a blank line.
Boost: : an asio: : read_until (socket, the response, "\ r \ n \ r \ n");
//Process the response headers.
STD: : string TMP.
While (STD: : getline (response_stream, TMP) & amp; & TMP!="/r")
The header +=TMP + "\ n";
//Write whatever the content we already have the to the output.
While (STD: : getline (response_stream, TMP))
Content +=TMP;
//Read until EOF, writing data to the output as we go.
While (boost: : an asio: : read (socket, the response, boost: : an asio: : transfer_at_least (1), the error))
{
Response_stream. The clear ();
While (STD: : getline (response_stream, TMP))
Content +=TMP;
}
If (error!=boost: : an asio: : error: : eof)
Throw the boost: : system: : system_error (error);
//Substitute CRs by a space
For (STD: : size_t I=0; i
The header [I]=' ';
For (STD: : size_t I=0; i
The content [I]=' ';
Return status_code;
}
The catch (STD: : exception & amp; E)
{
MYLOG_PRINT (wayToPrint, "Exception: {}", e.w hat ());
//STD: : cerr & lt; <"The Exception:" & lt;
}
}
CodePudding user response:
null