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