2015-01-27 06:35:29 -05:00
|
|
|
#ifndef CONNECTION_HPP
|
|
|
|
#define CONNECTION_HPP
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "server/http/compression_type.hpp"
|
|
|
|
#include "server/http/reply.hpp"
|
|
|
|
#include "server/http/request.hpp"
|
|
|
|
#include "server/request_parser.hpp"
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2015-01-26 11:37:37 -05:00
|
|
|
#include <boost/array.hpp>
|
2013-09-19 05:54:24 -04:00
|
|
|
#include <boost/asio.hpp>
|
2014-05-11 10:51:14 -04:00
|
|
|
#include <boost/config.hpp>
|
|
|
|
#include <boost/version.hpp>
|
|
|
|
|
2015-01-23 12:53:37 -05:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
2014-05-11 10:51:14 -04:00
|
|
|
|
2015-01-23 12:53:37 -05:00
|
|
|
// workaround for incomplete std::shared_ptr compatibility in old boost versions
|
2014-05-11 10:51:14 -04:00
|
|
|
#if BOOST_VERSION < 105300 || defined BOOST_NO_CXX11_SMART_PTR
|
|
|
|
|
2015-01-23 12:53:37 -05:00
|
|
|
namespace boost
|
2014-05-11 10:51:14 -04:00
|
|
|
{
|
2015-01-23 12:53:37 -05:00
|
|
|
template <class T> const T *get_pointer(std::shared_ptr<T> const &p) { return p.get(); }
|
2014-05-11 10:51:14 -04:00
|
|
|
|
2015-01-23 12:53:37 -05:00
|
|
|
template <class T> T *get_pointer(std::shared_ptr<T> &p) { return p.get(); }
|
2014-05-11 10:51:14 -04:00
|
|
|
} // namespace boost
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace server
|
2014-05-07 11:08:30 -04:00
|
|
|
{
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
class RequestHandler;
|
|
|
|
|
2011-01-09 16:42:27 -05:00
|
|
|
/// Represents a single connection from a client.
|
2014-05-09 11:04:55 -04:00
|
|
|
class Connection : public std::enable_shared_from_this<Connection>
|
2014-05-07 11:06:28 -04:00
|
|
|
{
|
2014-05-07 11:08:30 -04:00
|
|
|
public:
|
|
|
|
explicit Connection(boost::asio::io_service &io_service, RequestHandler &handler);
|
|
|
|
Connection(const Connection &) = delete;
|
Takes care of proper special member generation globally, fixes #1689
Phew, a lot of classes were affected by this. The rationale for the
changes are as follows:
- When a type X declares any constructor, the default constructor is
not declared, so there is no need for X() = delete there. In fact,
there is brutal difference between those two: deleted members
participate in overload resolution, but not-declared members do not!
- When a type X wants to be non-copyable (e.g. to be only movable, like
threads, unique_ptrs, and so on), you can either do it by inheriting
from boost::noncopyable (the old way), or better declare both (!) the
copy constructor _and_ the copy assignment operator as deleted:
X(X const&) = delete;
X& operator=(X const&) = delete;
We had tons of types with deleted copy constructors that were lacking
a corresponding deleted copy assignment operator, making them still
copyable and you wouldn't even notice (read: scary)!
References:
- http://accu.org/content/conf2014/Howard_Hinnant_Accu_2014.pdf
- http://www.boost.org/doc/libs/master/libs/core/doc/html/core/noncopyable.html
Note: I know, I'm quoting Hinnant's extraordinary slides a lot, but
getting the sematic right here is so incredibly important.
2016-01-27 05:20:55 -05:00
|
|
|
Connection &operator=(const Connection &) = delete;
|
2014-05-07 11:08:30 -04:00
|
|
|
|
|
|
|
boost::asio::ip::tcp::socket &socket();
|
|
|
|
|
|
|
|
/// Start the first asynchronous operation for the connection.
|
|
|
|
void start();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void handle_read(const boost::system::error_code &e, std::size_t bytes_transferred);
|
|
|
|
|
|
|
|
/// Handle completion of a write operation.
|
|
|
|
void handle_write(const boost::system::error_code &e);
|
|
|
|
|
2019-08-19 09:15:56 -04:00
|
|
|
/// Handle read timeout
|
2019-08-20 05:04:58 -04:00
|
|
|
void handle_timeout(boost::system::error_code);
|
|
|
|
|
|
|
|
void handle_shutdown();
|
2019-08-19 09:15:56 -04:00
|
|
|
|
2015-01-26 07:35:24 -05:00
|
|
|
std::vector<char> compress_buffers(const std::vector<char> &uncompressed_data,
|
2016-01-05 10:51:13 -05:00
|
|
|
const http::compression_type compression_type);
|
2014-05-07 11:08:30 -04:00
|
|
|
|
|
|
|
boost::asio::io_service::strand strand;
|
|
|
|
boost::asio::ip::tcp::socket TCP_socket;
|
2019-08-19 09:15:56 -04:00
|
|
|
boost::asio::deadline_timer timer;
|
2014-05-07 11:08:30 -04:00
|
|
|
RequestHandler &request_handler;
|
2015-02-12 03:01:46 -05:00
|
|
|
RequestParser request_parser;
|
2015-01-26 11:37:37 -05:00
|
|
|
boost::array<char, 8192> incoming_data_buffer;
|
2016-01-05 10:51:13 -05:00
|
|
|
http::request current_request;
|
|
|
|
http::reply current_reply;
|
Extend compressed output lifetime till the async write function finishes.
This extends the compressed output vector's lifetime, as we issue an
asynchronous write operation that only receives a non-owning buffer to
the compressed data.
When the compressed output vector then goes out of scope, its destructor
is called and the data gets (potentially) destroyed. If the asynchronous
write happens afterwards, it's accessing data that is no longer there.
This is the reason for race conditions --- well, for undefined behavior
in general, but it manifests in the routed _sometimes_ not responding at
all.
The fix works like this: keep the compressed output associated with a
connection. Connections inherit from `std::enable_shared_from_this` and
issues a `shared_from_this()` call, passing a `std::shared_ptr` to the
asynchronous write function, thus extending their lifetime.
Connecitons thus manage their lifetime by themselves, extending it when
needed (and of course via the `std::shared_pointers` pointing to it).
Buffer's non owning property, from the `async_write` documentation:
> One or more buffers containing the data to be written. Although
> the buffers object may be copied as necessary, ownership of the
> underlying memory blocks is retained by the caller, which must
> guarantee that they remain valid until the handler is called.
Reference:
- http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/reference/async_write/overload1.html
2015-09-15 11:59:39 -04:00
|
|
|
std::vector<char> compressed_output;
|
2016-01-13 12:36:11 -05:00
|
|
|
// Header compression_header;
|
|
|
|
std::vector<boost::asio::const_buffer> output_buffer;
|
2019-08-19 09:27:45 -04:00
|
|
|
// Keep alive support
|
2019-08-19 08:45:20 -04:00
|
|
|
bool keep_alive = false;
|
|
|
|
short processed_requests = 512;
|
2019-08-19 09:15:56 -04:00
|
|
|
short keepalive_timeout = 5; // In seconds
|
2011-01-09 16:42:27 -05:00
|
|
|
};
|
2016-01-05 10:51:13 -05:00
|
|
|
}
|
|
|
|
}
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2015-01-27 06:35:29 -05:00
|
|
|
#endif // CONNECTION_HPP
|