Updates to epee HTTP client code

- http_simple_client now uses std::chrono for timeouts
  - http_simple_client accepts timeouts per connect / invoke call
  - shortened names of epee http invoke functions
  - invoke command functions only take relative path, connection
    is not automatically performed
This commit is contained in:
Lee Clagett 2017-01-25 00:16:05 -05:00
parent ad91ffe7e5
commit c02e1cb943
15 changed files with 172 additions and 323 deletions

View file

@ -37,6 +37,7 @@
#include <ostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/preprocessor/selection/min.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
@ -98,7 +99,7 @@ namespace net_utils
// No deadline is required until the first socket operation is started. We
// set the deadline to positive infinity so that the actor takes no action
// until a specific deadline is set.
m_deadline.expires_at(boost::posix_time::pos_infin);
m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
// Start the persistent actor that checks for deadline expiry.
check_deadline();
@ -111,26 +112,16 @@ namespace net_utils
shutdown();
}
inline void set_recv_timeout(int reciev_timeout)
{
m_reciev_timeout = reciev_timeout;
}
inline
bool connect(const std::string& addr, int port, unsigned int connect_timeout, unsigned int reciev_timeout, const std::string& bind_ip = "0.0.0.0")
bool connect(const std::string& addr, int port, std::chrono::milliseconds timeout, const std::string& bind_ip = "0.0.0.0")
{
return connect(addr, std::to_string(port), connect_timeout, reciev_timeout, bind_ip);
return connect(addr, std::to_string(port), timeout, bind_ip);
}
inline
bool connect(const std::string& addr, const std::string& port, unsigned int connect_timeout, unsigned int reciev_timeout, const std::string& bind_ip = "0.0.0.0")
bool connect(const std::string& addr, const std::string& port, std::chrono::milliseconds timeout, const std::string& bind_ip = "0.0.0.0")
{
m_connect_timeout = connect_timeout;
m_reciev_timeout = reciev_timeout;
m_connected = false;
if(!m_reciev_timeout)
m_reciev_timeout = m_connect_timeout;
try
{
m_socket.close();
@ -164,7 +155,7 @@ namespace net_utils
}
m_deadline.expires_from_now(boost::posix_time::milliseconds(m_connect_timeout));
m_deadline.expires_from_now(timeout);
boost::system::error_code ec = boost::asio::error::would_block;
@ -179,7 +170,7 @@ namespace net_utils
if (!ec && m_socket.is_open())
{
m_connected = true;
m_deadline.expires_at(boost::posix_time::pos_infin);
m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
return true;
}else
{
@ -231,12 +222,12 @@ namespace net_utils
inline
bool send(const std::string& buff)
bool send(const std::string& buff, std::chrono::milliseconds timeout)
{
try
{
m_deadline.expires_from_now(boost::posix_time::milliseconds(m_reciev_timeout));
m_deadline.expires_from_now(timeout);
// Set up the variable that receives the result of the asynchronous
// operation. The error code is set to would_block to signal that the
@ -264,7 +255,7 @@ namespace net_utils
return false;
}else
{
m_deadline.expires_at(boost::posix_time::pos_infin);
m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
}
}
@ -322,7 +313,7 @@ namespace net_utils
return false;
}else
{
m_deadline.expires_at(boost::posix_time::pos_infin);
m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
}
}
@ -350,7 +341,7 @@ namespace net_utils
}
inline
bool recv(std::string& buff)
bool recv(std::string& buff, std::chrono::milliseconds timeout)
{
try
@ -358,7 +349,7 @@ namespace net_utils
// Set a deadline for the asynchronous operation. Since this function uses
// a composed operation (async_read_until), the deadline applies to the
// entire operation, rather than individual reads from the socket.
m_deadline.expires_from_now(boost::posix_time::milliseconds(m_reciev_timeout));
m_deadline.expires_from_now(timeout);
// Set up the variable that receives the result of the asynchronous
// operation. The error code is set to would_block to signal that the
@ -404,7 +395,7 @@ namespace net_utils
}else
{
MTRACE("READ ENDS: Success. bytes_tr: " << bytes_transfered);
m_deadline.expires_at(boost::posix_time::pos_infin);
m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
}
/*if(!bytes_transfered)
@ -432,7 +423,7 @@ namespace net_utils
}
inline bool recv_n(std::string& buff, int64_t sz)
inline bool recv_n(std::string& buff, int64_t sz, std::chrono::milliseconds timeout)
{
try
@ -440,7 +431,7 @@ namespace net_utils
// Set a deadline for the asynchronous operation. Since this function uses
// a composed operation (async_read_until), the deadline applies to the
// entire operation, rather than individual reads from the socket.
m_deadline.expires_from_now(boost::posix_time::milliseconds(m_reciev_timeout));
m_deadline.expires_from_now(timeout);
// Set up the variable that receives the result of the asynchronous
// operation. The error code is set to would_block to signal that the
@ -477,7 +468,7 @@ namespace net_utils
return false;
}else
{
m_deadline.expires_at(boost::posix_time::pos_infin);
m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
}
if(bytes_transfered != buff.size())
@ -539,7 +530,7 @@ namespace net_utils
// Check whether the deadline has passed. We compare the deadline against
// the current time since a new asynchronous operation may have moved the
// deadline before this actor had a chance to run.
if (m_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now())
if (m_deadline.expires_at() <= std::chrono::steady_clock::now())
{
// The deadline has passed. The socket is closed so that any outstanding
// asynchronous operations are cancelled. This allows the blocked
@ -550,7 +541,7 @@ namespace net_utils
// There is no longer an active deadline. The expiry is set to positive
// infinity so that the actor takes no action until a new deadline is set.
m_deadline.expires_at(boost::posix_time::pos_infin);
m_deadline.expires_at(std::chrono::steady_clock::time_point::max());
}
// Put the actor back to sleep.
@ -562,11 +553,9 @@ namespace net_utils
protected:
boost::asio::io_service m_io_service;
boost::asio::ip::tcp::socket m_socket;
int m_connect_timeout;
int m_reciev_timeout;
bool m_initialized;
bool m_connected;
boost::asio::deadline_timer m_deadline;
boost::asio::steady_timer m_deadline;
volatile uint32_t m_shutdowned;
};