diff options
| author | Miguel Gaio <miguel.gaio@renault.com> | 2018-12-04 13:50:58 +0100 |
|---|---|---|
| committer | Eric Laurent <elaurent@google.com> | 2019-04-26 10:59:31 -0700 |
| commit | f7bf29332b17034e1a2d32d1f527ad170a0c46ad (patch) | |
| tree | 6f0fe88488d36cf7f6ac9761bee33d00b3123f6b | |
| parent | 8a68f31307cfcffa4497078430bf3c59a5c2571c (diff) | |
| download | platform_external_parameter-framework-f7bf29332b17034e1a2d32d1f527ad170a0c46ad.tar.gz platform_external_parameter-framework-f7bf29332b17034e1a2d32d1f527ad170a0c46ad.tar.bz2 platform_external_parameter-framework-f7bf29332b17034e1a2d32d1f527ad170a0c46ad.zip | |
AF_UNIX socket enablement: use asio::generic::stream_protocol socket type
ASIO provides generic asio::stream_protocol scoket type which handles
both AF_INET6 and AF_UNIX domains.
Convert asio::ip::tcp to asio::generic::stream_protocol
Bug: 130284799
Test: make
Change-Id: I39dc95a0c225b8e5cd9c278cfcfb37d22ff4f54d
Signed-off-by: Miguel Gaio <miguel.gaio@renault.com>
| -rw-r--r-- | upstream/asio/stub/asio.hpp | 56 | ||||
| -rw-r--r-- | upstream/parameter/ParameterMgr.cpp | 3 | ||||
| -rw-r--r-- | upstream/remote-process/main.cpp | 12 | ||||
| -rw-r--r-- | upstream/remote-processor/Message.cpp | 2 | ||||
| -rw-r--r-- | upstream/remote-processor/RemoteProcessorServer.cpp | 19 | ||||
| -rw-r--r-- | upstream/remote-processor/RemoteProcessorServer.h | 4 | ||||
| -rw-r--r-- | upstream/remote-processor/Socket.h | 10 |
7 files changed, 72 insertions, 34 deletions
diff --git a/upstream/asio/stub/asio.hpp b/upstream/asio/stub/asio.hpp index 84a0ea4..1fd3b0c 100644 --- a/upstream/asio/stub/asio.hpp +++ b/upstream/asio/stub/asio.hpp @@ -37,6 +37,10 @@ #include <system_error> +#include <netinet/in.h> + +#define ASIO_OS_DEF(x) (x) + namespace asio { struct dummy_base @@ -46,7 +50,27 @@ struct dummy_base { } void set_option(const dummy_base &) const {}; + int protocol() const { return 0; } +}; +struct endpoint_base : dummy_base +{ + using dummy_base::dummy_base; + + dummy_base protocol() const { return {}; }; }; + +template <typename T> +struct basic_socket_acceptor : dummy_base +{ + using dummy_base::dummy_base; + + using reuse_address = dummy_base; + void open(const dummy_base &) const {}; + void bind(const dummy_base &) const {}; + void listen() const {}; + void async_accept(const dummy_base &, const dummy_base &) const {}; +}; + inline bool write(const dummy_base &, const dummy_base &, const dummy_base &) { return true; @@ -76,6 +100,10 @@ struct socket_base : dummy_base using linger = dummy_base; using enable_connection_aborted = dummy_base; void close() const {}; + + const endpoint_base &local_endpoint() const { return base; } + + endpoint_base base; }; bool write(const dummy_base &, const dummy_base &, const dummy_base &); @@ -95,23 +123,23 @@ namespace tcp { using v6 = dummy_base; using no_delay = dummy_base; +using endpoint = endpoint_base; using socket = socket_base; -struct endpoint : dummy_base -{ - using dummy_base::dummy_base; +using acceptor = basic_socket_acceptor<void>; +} +} - dummy_base protocol() const { return {}; }; -}; -struct acceptor : dummy_base +namespace generic { - using dummy_base::dummy_base; - - using reuse_address = dummy_base; - void open(const dummy_base &) const {}; - void bind(const dummy_base &) const {}; - void listen() const {}; - void async_accept(const dummy_base &, const dummy_base &) const {}; +struct stream_protocol +{ + using endpoint = endpoint_base; + using socket = socket_base; }; -} +}; + +namespace local +{ +using stream_protocol = generic::stream_protocol; } } diff --git a/upstream/parameter/ParameterMgr.cpp b/upstream/parameter/ParameterMgr.cpp index 91dd329..d515af0 100644 --- a/upstream/parameter/ParameterMgr.cpp +++ b/upstream/parameter/ParameterMgr.cpp @@ -2856,7 +2856,8 @@ bool CParameterMgr::handleRemoteProcessingInterface(string &strError) try { // The ownership of remoteComandHandler is given to Bg remote processor server. - _pRemoteProcessorServer = new BackgroundRemoteProcessorServer(bindAddress, createCommandHandler()); + _pRemoteProcessorServer = + new BackgroundRemoteProcessorServer(bindAddress, createCommandHandler()); } catch (std::runtime_error &e) { strError = string("ParameterMgr: Unable to create Remote Processor Server: ") + e.what(); return false; diff --git a/upstream/remote-process/main.cpp b/upstream/remote-process/main.cpp index 3ebaa86..00fde23 100644 --- a/upstream/remote-process/main.cpp +++ b/upstream/remote-process/main.cpp @@ -40,7 +40,8 @@ using namespace std; -bool sendAndDisplayCommand(asio::ip::tcp::socket &socket, CRequestMessage &requestMessage) +bool sendAndDisplayCommand(asio::generic::stream_protocol::socket &socket, + CRequestMessage &requestMessage) { string strError; @@ -87,16 +88,17 @@ int main(int argc, char *argv[]) return 1; } - using asio::ip::tcp; asio::io_service io_service; - tcp::resolver resolver(io_service); + asio::ip::tcp::resolver resolver(io_service); - tcp::socket connectionSocket(io_service); + asio::generic::stream_protocol::socket connectionSocket(io_service); + asio::ip::tcp::socket tcpSocket(io_service); string host{argv[1]}; string port{argv[2]}; try { - asio::connect(connectionSocket, resolver.resolve(tcp::resolver::query(host, port))); + asio::connect(tcpSocket, resolver.resolve(asio::ip::tcp::resolver::query(host, port))); + connectionSocket = std::move(tcpSocket); } catch (const asio::system_error &e) { cerr << "Connection to '" << host << ":" << port << "' failed: " << e.what() << endl; return 1; diff --git a/upstream/remote-processor/Message.cpp b/upstream/remote-processor/Message.cpp index 66154ec..5faecc0 100644 --- a/upstream/remote-processor/Message.cpp +++ b/upstream/remote-processor/Message.cpp @@ -129,7 +129,7 @@ size_t CMessage::getRemainingDataSize() const // Send/Receive CMessage::Result CMessage::serialize(Socket &&socket, bool bOut, string &strError) { - asio::ip::tcp::socket &asioSocket = socket.get(); + auto &asioSocket = socket.get(); if (bOut) { asio::error_code ec; diff --git a/upstream/remote-processor/RemoteProcessorServer.cpp b/upstream/remote-processor/RemoteProcessorServer.cpp index 8aa753c..e28bebe 100644 --- a/upstream/remote-processor/RemoteProcessorServer.cpp +++ b/upstream/remote-processor/RemoteProcessorServer.cpp @@ -56,23 +56,27 @@ bool CRemoteProcessorServer::start(string &error) using namespace asio; try { + generic::stream_protocol::endpoint endpoint; uint16_t port; - if (!convertTo(_bindAddress, port)) { + if (convertTo(_bindAddress, port)) { + endpoint = ip::tcp::endpoint(ip::tcp::v6(), port); + } else { throw std::invalid_argument("unable to convert bind Address: " + _bindAddress); } - ip::tcp::endpoint endpoint(ip::tcp::v6(), port); _acceptor.open(endpoint.protocol()); - _acceptor.set_option(ip::tcp::acceptor::reuse_address(true)); - _acceptor.set_option(asio::socket_base::linger(true, 0)); + if (endpoint.protocol().protocol() == ASIO_OS_DEF(IPPROTO_TCP)) { + _acceptor.set_option(ip::tcp::acceptor::reuse_address(true)); + } + _acceptor.set_option(socket_base::linger(true, 0)); _acceptor.set_option(socket_base::enable_connection_aborted(true)); _acceptor.bind(endpoint); _acceptor.listen(); } catch (std::exception &e) { - error = "Unable to listen on port " + _bindAddress + ": " + e.what(); + error = "Unable to listen on " + _bindAddress + ": " + e.what(); return false; } @@ -94,7 +98,10 @@ void CRemoteProcessorServer::acceptRegister(IRemoteCommandHandler &commandHandle return; } - _socket.set_option(asio::ip::tcp::no_delay(true)); + const auto &endpoint = _socket.local_endpoint(); + if (endpoint.protocol().protocol() == ASIO_OS_DEF(IPPROTO_TCP)) { + _socket.set_option(asio::ip::tcp::no_delay(true)); + } handleNewConnection(commandHandler); _socket.close(); diff --git a/upstream/remote-processor/RemoteProcessorServer.h b/upstream/remote-processor/RemoteProcessorServer.h index ab70a24..7a2f739 100644 --- a/upstream/remote-processor/RemoteProcessorServer.h +++ b/upstream/remote-processor/RemoteProcessorServer.h @@ -58,6 +58,6 @@ private: std::string _bindAddress; asio::io_service _io_service; - asio::ip::tcp::acceptor _acceptor; - asio::ip::tcp::socket _socket; + asio::basic_socket_acceptor<asio::generic::stream_protocol> _acceptor; + asio::generic::stream_protocol::socket _socket; }; diff --git a/upstream/remote-processor/Socket.h b/upstream/remote-processor/Socket.h index 020750b..091d1cb 100644 --- a/upstream/remote-processor/Socket.h +++ b/upstream/remote-processor/Socket.h @@ -29,19 +29,19 @@ */ #include <asio.hpp> -/** Wraps and hides asio::ip::tcp::socket +/** Wraps and hides asio::generic::stream_protocol::socket * - * asio::ip::tcp::socket cannot be forward-declared because it is an + * asio::generic::stream_protocol::socket cannot be forward-declared because it is an * inner-class. This class wraps the asio class in order for it to be * forward-declared and avoid it to leak in client interfaces. */ class Socket { public: - Socket(asio::ip::tcp::socket &socket) : mSocket(socket) {} + Socket(asio::generic::stream_protocol::socket &socket) : mSocket(socket) {} - asio::ip::tcp::socket &get() { return mSocket; } + asio::generic::stream_protocol::socket &get() { return mSocket; } private: - asio::ip::tcp::socket &mSocket; + asio::generic::stream_protocol::socket &mSocket; }; |
