diff options
author | eidheim <eidheim@gmail.com> | 2020-09-19 21:58:07 +0300 |
---|---|---|
committer | eidheim <eidheim@gmail.com> | 2020-09-19 21:58:07 +0300 |
commit | c6df27aae15b1b695003e32ab310f71cc6ee3ee8 (patch) | |
tree | 8b0df293a961636e28c79eac1cbe61b227097166 | |
parent | 5cc9879c4f9196221717bc1d639a5221893fa3cc (diff) |
Added SocketClient::Connection::remote_endpoint
-rw-r--r-- | client_ws.hpp | 12 | ||||
-rw-r--r-- | server_ws.hpp | 72 |
2 files changed, 48 insertions, 36 deletions
diff --git a/client_ws.hpp b/client_ws.hpp index 72d8d9e..e4a26f6 100644 --- a/client_ws.hpp +++ b/client_ws.hpp @@ -86,6 +86,8 @@ namespace SimpleWeb { std::atomic<bool> closed; + asio::ip::tcp::endpoint endpoint; // The endpoint is read in SocketClient::upgrade and must be stored so that it can be read reliably in all handlers, including on_error + void close() noexcept { error_code ec; socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec); @@ -252,6 +254,10 @@ namespace SimpleWeb { // fin_rsv_opcode=136: message close send(out_message, std::move(callback), 136); } + + const asio::ip::tcp::endpoint &remote_endpoint() const noexcept { + return endpoint; + } }; class Config { @@ -409,6 +415,12 @@ namespace SimpleWeb { ostream << header_field.first << ": " << header_field.second << "\r\n"; ostream << "\r\n"; + try { + connection->endpoint = connection->socket->lowest_layer().remote_endpoint(); + } + catch(...) { + } + connection->in_message = std::shared_ptr<InMessage>(new InMessage()); connection->set_timeout(config.timeout_request); diff --git a/server_ws.hpp b/server_ws.hpp index 071a492..2256d55 100644 --- a/server_ws.hpp +++ b/server_ws.hpp @@ -87,42 +87,8 @@ namespace SimpleWeb { regex::smatch path_match; - const asio::ip::tcp::endpoint &remote_endpoint() const noexcept { - return endpoint; - } - - asio::ip::tcp::endpoint local_endpoint() const noexcept { - try { - if(auto connection = this->connection.lock()) - return connection->socket->lowest_layer().local_endpoint(); - } - catch(...) { - } - return asio::ip::tcp::endpoint(); - } - - /// Deprecated, please use remote_endpoint().address().to_string() instead. - DEPRECATED std::string remote_endpoint_address() const noexcept { - try { - return endpoint.address().to_string(); - } - catch(...) { - } - return std::string(); - } - - /// Deprecated, please use remote_endpoint().port() instead. - DEPRECATED unsigned short remote_endpoint_port() const noexcept { - try { - return endpoint.port(); - } - catch(...) { - } - return 0; - } - private: - /// Used to call Server::upgrade. + /// Used to call SocketServer::upgrade. template <typename... Args> Connection(std::shared_ptr<ScopeRunner> handler_runner_, long timeout_idle, Args &&... args) noexcept : handler_runner(std::move(handler_runner_)), socket(new socket_type(std::forward<Args>(args)...)), timeout_idle(timeout_idle), closed(false) {} @@ -141,7 +107,7 @@ namespace SimpleWeb { std::atomic<bool> closed; - asio::ip::tcp::endpoint endpoint; // The endpoint is read in Server::write_handshake and must be stored so that it can be read reliably in all handlers, including on_error + asio::ip::tcp::endpoint endpoint; // The endpoint is read in SocketServer::write_handshake and must be stored so that it can be read reliably in all handlers, including on_error void close() noexcept { error_code ec; @@ -292,6 +258,40 @@ namespace SimpleWeb { // fin_rsv_opcode=136: message close send(std::move(send_stream), std::move(callback), 136); } + + const asio::ip::tcp::endpoint &remote_endpoint() const noexcept { + return endpoint; + } + + asio::ip::tcp::endpoint local_endpoint() const noexcept { + try { + if(auto connection = this->connection.lock()) + return connection->socket->lowest_layer().local_endpoint(); + } + catch(...) { + } + return asio::ip::tcp::endpoint(); + } + + /// Deprecated, please use remote_endpoint().address().to_string() instead. + DEPRECATED std::string remote_endpoint_address() const noexcept { + try { + return endpoint.address().to_string(); + } + catch(...) { + } + return std::string(); + } + + /// Deprecated, please use remote_endpoint().port() instead. + DEPRECATED unsigned short remote_endpoint_port() const noexcept { + try { + return endpoint.port(); + } + catch(...) { + } + return 0; + } }; class Endpoint { |