Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #include "ConnectionHandler.hpp" |
| 6 | |
Finn Williams | badcc3f | 2020-05-22 14:28:15 +0100 | [diff] [blame] | 7 | #include <string.h> |
| 8 | |
Finn Williams | 2ed809c | 2020-04-20 21:21:07 +0100 | [diff] [blame] | 9 | using namespace armnnUtils; |
| 10 | |
| 11 | namespace armnnProfiling |
| 12 | { |
| 13 | ConnectionHandler::ConnectionHandler(const std::string& udsNamespace, const bool setNonBlocking) |
| 14 | { |
| 15 | Sockets::Initialize(); |
| 16 | m_ListeningSocket = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 17 | |
| 18 | if (-1 == m_ListeningSocket) |
| 19 | { |
| 20 | throw SocketConnectionException(": Socket construction failed: ", 1, 1); |
| 21 | } |
| 22 | |
| 23 | sockaddr_un udsAddress; |
| 24 | memset(&udsAddress, 0, sizeof(sockaddr_un)); |
| 25 | // We've set the first element of sun_path to be 0, skip over it and copy the namespace after it. |
| 26 | memcpy(udsAddress.sun_path + 1, udsNamespace.c_str(), strlen(udsNamespace.c_str())); |
| 27 | udsAddress.sun_family = AF_UNIX; |
| 28 | |
| 29 | // Bind the socket to the UDS namespace. |
| 30 | if (-1 == bind(m_ListeningSocket, reinterpret_cast<const sockaddr*>(&udsAddress), sizeof(sockaddr_un))) |
| 31 | { |
| 32 | throw SocketConnectionException(": Binding on socket failed: ", m_ListeningSocket, errno); |
| 33 | } |
| 34 | // Listen for connections. |
| 35 | if (-1 == listen(m_ListeningSocket, 1)) |
| 36 | { |
| 37 | throw SocketConnectionException(": Listen call on socket failed: ", m_ListeningSocket, errno); |
| 38 | } |
| 39 | |
| 40 | if (setNonBlocking) |
| 41 | { |
| 42 | Sockets::SetNonBlocking(m_ListeningSocket); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | std::unique_ptr<BasePipeServer> ConnectionHandler::GetNewBasePipeServer(const bool echoPackets) |
| 47 | { |
| 48 | armnnUtils::Sockets::Socket clientConnection = armnnUtils::Sockets::Accept(m_ListeningSocket, nullptr, nullptr, |
| 49 | SOCK_CLOEXEC); |
| 50 | if (clientConnection < 1) |
| 51 | { |
| 52 | return nullptr; |
| 53 | } |
| 54 | return std::make_unique<BasePipeServer>(clientConnection, echoPackets); |
| 55 | } |
| 56 | |
| 57 | } // namespace armnnProfiling |