diff options
author | Finn Williams <Finn.Williams@arm.com> | 2020-04-20 21:21:07 +0100 |
---|---|---|
committer | Finn Williams <Finn.Williams@arm.com> | 2020-04-27 20:50:53 +0100 |
commit | 2ed809cb4765306b7af9b6968e2ec609d143979b (patch) | |
tree | a26b4d4e841434802c01b11a202ec58acf3cd61f /profiling/server/src/basePipeServer/ConnectionHandler.cpp | |
parent | 4e755a50e35a1f5ac1b011dc4baf89e6d97f116e (diff) | |
download | armnn-2ed809cb4765306b7af9b6968e2ec609d143979b.tar.gz |
IVGCVSW-4594 Refactor the GatordMockService and GatordMockMain to extract a BasePipeServer
Signed-off-by: Finn Williams <Finn.Williams@arm.com>
Change-Id: I03c1b46104dadc491dba6075865e486f78aa60fa
Diffstat (limited to 'profiling/server/src/basePipeServer/ConnectionHandler.cpp')
-rw-r--r-- | profiling/server/src/basePipeServer/ConnectionHandler.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/profiling/server/src/basePipeServer/ConnectionHandler.cpp b/profiling/server/src/basePipeServer/ConnectionHandler.cpp new file mode 100644 index 0000000000..69ccd01050 --- /dev/null +++ b/profiling/server/src/basePipeServer/ConnectionHandler.cpp @@ -0,0 +1,55 @@ +// +// Copyright © 2020 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// +#include "ConnectionHandler.hpp" + +using namespace armnnUtils; + +namespace armnnProfiling +{ +ConnectionHandler::ConnectionHandler(const std::string& udsNamespace, const bool setNonBlocking) +{ + Sockets::Initialize(); + m_ListeningSocket = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + + if (-1 == m_ListeningSocket) + { + throw SocketConnectionException(": Socket construction failed: ", 1, 1); + } + + sockaddr_un udsAddress; + memset(&udsAddress, 0, sizeof(sockaddr_un)); + // We've set the first element of sun_path to be 0, skip over it and copy the namespace after it. + memcpy(udsAddress.sun_path + 1, udsNamespace.c_str(), strlen(udsNamespace.c_str())); + udsAddress.sun_family = AF_UNIX; + + // Bind the socket to the UDS namespace. + if (-1 == bind(m_ListeningSocket, reinterpret_cast<const sockaddr*>(&udsAddress), sizeof(sockaddr_un))) + { + throw SocketConnectionException(": Binding on socket failed: ", m_ListeningSocket, errno); + } + // Listen for connections. + if (-1 == listen(m_ListeningSocket, 1)) + { + throw SocketConnectionException(": Listen call on socket failed: ", m_ListeningSocket, errno); + } + + if (setNonBlocking) + { + Sockets::SetNonBlocking(m_ListeningSocket); + } +} + +std::unique_ptr<BasePipeServer> ConnectionHandler::GetNewBasePipeServer(const bool echoPackets) +{ + armnnUtils::Sockets::Socket clientConnection = armnnUtils::Sockets::Accept(m_ListeningSocket, nullptr, nullptr, + SOCK_CLOEXEC); + if (clientConnection < 1) + { + return nullptr; + } + return std::make_unique<BasePipeServer>(clientConnection, echoPackets); +} + +} // namespace armnnProfiling
\ No newline at end of file |