blob: c03774e4529e16a98e6e2375d916464dd4f00cdd [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
6#pragma once
7
Finn Williams9937f932020-04-29 12:00:24 +01008#include "common/include/NetworkSockets.hpp"
Finn Williams2ed809c2020-04-20 21:21:07 +01009#include "../../../../src/profiling/Packet.hpp"
10#include "common/include/SocketConnectionException.hpp"
11
Finn Williams9937f932020-04-29 12:00:24 +010012#include <string>
13#include <atomic>
14
Finn Williams2ed809c2020-04-20 21:21:07 +010015namespace armnnProfiling
16{
17
18enum class TargetEndianness
19{
20 BeWire,
21 LeWire
22};
23
24enum class PacketDirection
25{
26 Sending,
27 ReceivedHeader,
28 ReceivedData
29};
30class ConnectionHandler;
31
32class BasePipeServer
33{
34
35public:
36
37 BasePipeServer(armnnUtils::Sockets::Socket clientConnection, bool echoPackets)
38 : m_ClientConnection(clientConnection)
39 , m_EchoPackets(echoPackets)
40 {}
41
42 ~BasePipeServer()
43 {
44 // We have set SOCK_CLOEXEC on this socket but we'll close it to be good citizens.
45 armnnUtils::Sockets::Close(m_ClientConnection);
46 }
47
48 BasePipeServer(const BasePipeServer&) = delete;
49 BasePipeServer& operator=(const BasePipeServer&) = delete;
50
51 BasePipeServer(BasePipeServer&&) = delete;
52 BasePipeServer& operator=(BasePipeServer&&) = delete;
53
54 /// Close the client connection
55 /// @return 0 if successful
56 int Close()
57 {
58 return armnnUtils::Sockets::Close(m_ClientConnection);
59 }
60
61 /// Send a packet to the client
62 /// @return true if a valid packet has been sent.
63 bool SendPacket(uint32_t packetFamily, uint32_t packetId, const uint8_t* data, uint32_t dataLength);
64
65 /// Set the client socket to nonblocking
66 /// @return true if successful.
67 bool SetNonBlocking()
68 {
69 return armnnUtils::Sockets::SetNonBlocking(m_ClientConnection);
70 }
71
72 /// Block on the client connection until a complete packet has been received.
73 /// @return true if a valid packet has been received.
74 armnn::profiling::Packet WaitForPacket(uint32_t timeoutMs);
75
76 /// Once the connection is open wait to receive the stream meta data packet from the client. Reading this
77 /// packet differs from others as we need to determine endianness.
78 /// @return true only if a valid stream meta data packet has been received.
79 bool WaitForStreamMetaData();
80
81 uint32_t GetStreamMetadataVersion()
82 {
83 return m_StreamMetaDataVersion;
84 }
85
86 uint32_t GetStreamMetadataMaxDataLen()
87 {
88 return m_StreamMetaDataMaxDataLen;
89 }
90
91 uint32_t GetStreamMetadataPid()
92 {
93 return m_StreamMetaDataPid;
94 }
95
96private:
97
98 void EchoPacket(PacketDirection direction, uint8_t* packet, size_t lengthInBytes);
99 bool ReadFromSocket(uint8_t* packetData, uint32_t expectedLength);
100 bool ReadHeader(uint32_t headerAsWords[2]);
101
102 armnn::profiling::Packet ReceivePacket();
103
104 uint32_t ToUint32(uint8_t* data, TargetEndianness endianness);
105 void InsertU32(uint32_t value, uint8_t* data, TargetEndianness endianness);
106
107 armnnUtils::Sockets::Socket m_ClientConnection;
108 bool m_EchoPackets;
109 TargetEndianness m_Endianness;
110 static const uint32_t PIPE_MAGIC = 0x45495434;
111
112 uint32_t m_StreamMetaDataVersion;
113 uint32_t m_StreamMetaDataMaxDataLen;
114 uint32_t m_StreamMetaDataPid;
115};
116
117} // namespace armnnProfiling