blob: c7635421fa89b62f377964b2b4f1f7d2761d864d [file] [log] [blame]
Finn Williams2ed809c2020-04-20 21:21:07 +01001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
Finn Williams2ed809c2020-04-20 21:21:07 +01003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Jim Flynnbbfe6032020-07-20 16:57:44 +01008#include <common/include/NetworkSockets.hpp>
9#include <common/include/Packet.hpp>
10#include <common/include/SocketConnectionException.hpp>
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000011#include <common/include/TargetEndianess.hpp>
Finn Williams2ed809c2020-04-20 21:21:07 +010012
Finn Williams9937f932020-04-29 12:00:24 +010013#include <string>
14#include <atomic>
15
Jim Flynnbbfe6032020-07-20 16:57:44 +010016namespace arm
17{
18
19namespace pipe
Finn Williams2ed809c2020-04-20 21:21:07 +010020{
21
Finn Williams2ed809c2020-04-20 21:21:07 +010022enum class PacketDirection
23{
24 Sending,
25 ReceivedHeader,
26 ReceivedData
27};
28class ConnectionHandler;
29
30class BasePipeServer
31{
32
33public:
34
Jim Flynnbbfe6032020-07-20 16:57:44 +010035 BasePipeServer(arm::pipe::Socket clientConnection, bool echoPackets)
Finn Williams2ed809c2020-04-20 21:21:07 +010036 : m_ClientConnection(clientConnection)
37 , m_EchoPackets(echoPackets)
38 {}
39
40 ~BasePipeServer()
41 {
42 // We have set SOCK_CLOEXEC on this socket but we'll close it to be good citizens.
Jim Flynnbbfe6032020-07-20 16:57:44 +010043 arm::pipe::Close(m_ClientConnection);
Finn Williams2ed809c2020-04-20 21:21:07 +010044 }
45
46 BasePipeServer(const BasePipeServer&) = delete;
47 BasePipeServer& operator=(const BasePipeServer&) = delete;
48
49 BasePipeServer(BasePipeServer&&) = delete;
50 BasePipeServer& operator=(BasePipeServer&&) = delete;
51
52 /// Close the client connection
53 /// @return 0 if successful
54 int Close()
55 {
Jim Flynnbbfe6032020-07-20 16:57:44 +010056 return arm::pipe::Close(m_ClientConnection);
Finn Williams2ed809c2020-04-20 21:21:07 +010057 }
58
59 /// Send a packet to the client
60 /// @return true if a valid packet has been sent.
61 bool SendPacket(uint32_t packetFamily, uint32_t packetId, const uint8_t* data, uint32_t dataLength);
62
63 /// Set the client socket to nonblocking
64 /// @return true if successful.
65 bool SetNonBlocking()
66 {
Jim Flynnbbfe6032020-07-20 16:57:44 +010067 return arm::pipe::SetNonBlocking(m_ClientConnection);
Finn Williams2ed809c2020-04-20 21:21:07 +010068 }
69
70 /// Block on the client connection until a complete packet has been received.
71 /// @return true if a valid packet has been received.
Jim Flynnbbfe6032020-07-20 16:57:44 +010072 arm::pipe::Packet WaitForPacket(uint32_t timeoutMs);
Finn Williams2ed809c2020-04-20 21:21:07 +010073
74 /// Once the connection is open wait to receive the stream meta data packet from the client. Reading this
75 /// packet differs from others as we need to determine endianness.
76 /// @return true only if a valid stream meta data packet has been received.
77 bool WaitForStreamMetaData();
78
79 uint32_t GetStreamMetadataVersion()
80 {
81 return m_StreamMetaDataVersion;
82 }
83
84 uint32_t GetStreamMetadataMaxDataLen()
85 {
86 return m_StreamMetaDataMaxDataLen;
87 }
88
89 uint32_t GetStreamMetadataPid()
90 {
91 return m_StreamMetaDataPid;
92 }
93
94private:
95
96 void EchoPacket(PacketDirection direction, uint8_t* packet, size_t lengthInBytes);
97 bool ReadFromSocket(uint8_t* packetData, uint32_t expectedLength);
98 bool ReadHeader(uint32_t headerAsWords[2]);
99
Jim Flynnbbfe6032020-07-20 16:57:44 +0100100 arm::pipe::Packet ReceivePacket();
Finn Williams2ed809c2020-04-20 21:21:07 +0100101
102 uint32_t ToUint32(uint8_t* data, TargetEndianness endianness);
103 void InsertU32(uint32_t value, uint8_t* data, TargetEndianness endianness);
104
Jim Flynnbbfe6032020-07-20 16:57:44 +0100105 arm::pipe::Socket m_ClientConnection;
Finn Williams2ed809c2020-04-20 21:21:07 +0100106 bool m_EchoPackets;
107 TargetEndianness m_Endianness;
Finn Williams2ed809c2020-04-20 21:21:07 +0100108
109 uint32_t m_StreamMetaDataVersion;
110 uint32_t m_StreamMetaDataMaxDataLen;
111 uint32_t m_StreamMetaDataPid;
112};
113
Jim Flynnbbfe6032020-07-20 16:57:44 +0100114} // namespace pipe
115} // namespace arm