8 #include "common/include/SocketConnectionException.hpp" 22 arm::pipe::Initialize();
23 memset(m_Socket, 0,
sizeof(m_Socket));
25 m_Socket[0].fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
26 if (m_Socket[0].fd == -1)
28 throw arm::pipe::SocketConnectionException(
29 std::string(
"SocketProfilingConnection: Socket construction failed: ") + strerror(errno),
36 memset(&server, 0,
sizeof(sockaddr_un));
38 memcpy(server.sun_path, m_GatorNamespace, strlen(m_GatorNamespace + 1) + 1);
39 server.sun_family = AF_UNIX;
40 if (0 != connect(m_Socket[0].fd, reinterpret_cast<const sockaddr*>(&server),
sizeof(sockaddr_un)))
43 throw arm::pipe::SocketConnectionException(
44 std::string(
"SocketProfilingConnection: Cannot connect to stream socket: ") + strerror(errno),
50 m_Socket[0].events = POLLIN;
53 if (!arm::pipe::SetNonBlocking(m_Socket[0].fd))
56 throw arm::pipe::SocketConnectionException(
57 std::string(
"SocketProfilingConnection: Failed to set socket as non blocking: ") + strerror(errno),
65 return m_Socket[0].fd > 0;
70 if (arm::pipe::Close(m_Socket[0].fd) != 0)
72 throw arm::pipe::SocketConnectionException(
73 std::string(
"SocketProfilingConnection: Cannot close stream socket: ") + strerror(errno),
78 memset(m_Socket, 0,
sizeof(m_Socket));
83 if (buffer ==
nullptr || length == 0)
88 return arm::pipe::Write(m_Socket[0].fd, buffer, length) != -1;
94 int bytes_available = 0;
95 arm::pipe::Ioctl(m_Socket[0].fd, FIONREAD, &bytes_available);
96 if (bytes_available >= 8)
99 return ReceivePacket();
103 int pollResult = arm::pipe::Poll(&m_Socket[0], 1, static_cast<int>(timeout));
108 throw arm::pipe::SocketConnectionException(
109 std::string(
"SocketProfilingConnection: Error occured while reading from socket: ") + strerror(errno),
114 throw arm::pipe::TimeoutException(
"SocketProfilingConnection: Timeout while reading from socket");
118 if (m_Socket[0].revents & (POLLNVAL | POLLERR | POLLHUP))
120 if (m_Socket[0].revents == POLLNVAL)
124 throw arm::pipe::SocketConnectionException(
125 std::string(
"SocketProfilingConnection: Error occured while polling receiving socket: POLLNVAL."),
128 if (m_Socket[0].revents == POLLERR)
130 throw arm::pipe::SocketConnectionException(
132 "SocketProfilingConnection: Error occured while polling receiving socket: POLLERR: ")
137 if (m_Socket[0].revents == POLLHUP)
141 throw arm::pipe::SocketConnectionException(
142 std::string(
"SocketProfilingConnection: Connection closed by remote client: POLLHUP."),
148 if (!(m_Socket[0].revents & (POLLIN)))
153 "SocketProfilingConnection: File descriptor was polled but no data was available to receive.");
156 return ReceivePacket();
160 arm::pipe::Packet SocketProfilingConnection::ReceivePacket()
163 long receiveResult = arm::pipe::Read(m_Socket[0].fd, &header,
sizeof(header));
165 switch( receiveResult )
170 throw arm::pipe::SocketConnectionException(
171 std::string(
"SocketProfilingConnection: Remote socket has closed the connection."),
176 throw arm::pipe::SocketConnectionException(
177 std::string(
"SocketProfilingConnection: Error occured while reading the packet: ") + strerror(errno),
181 if (receiveResult < 8)
183 throw arm::pipe::SocketConnectionException(
185 "SocketProfilingConnection: The received packet did not contains a valid PIPE header."),
192 uint32_t metadataIdentifier = 0;
193 std::memcpy(&metadataIdentifier, header,
sizeof(metadataIdentifier));
196 uint32_t dataLength = 0;
197 std::memcpy(&dataLength, header + 4u,
sizeof(dataLength));
199 std::unique_ptr<unsigned char[]> packetData;
202 packetData = std::make_unique<unsigned char[]>(dataLength);
203 long receivedLength = arm::pipe::Read(m_Socket[0].fd, packetData.get(), dataLength);
204 if (receivedLength < 0)
206 throw arm::pipe::SocketConnectionException(
207 std::string(
"SocketProfilingConnection: Error occured while reading the packet: ") + strerror(errno),
211 if (dataLength != static_cast<uint32_t>(receivedLength))
214 throw arm::pipe::SocketConnectionException(
215 std::string(
"SocketProfilingConnection: Invalid PIPE packet."),
220 return arm::pipe::Packet(metadataIdentifier, dataLength, packetData);
bool IsOpen() const final
Copyright (c) 2021 ARM Limited and Contributors.
arm::pipe::Packet ReadPacket(uint32_t timeout) final
SocketProfilingConnection()
bool WritePacket(const unsigned char *buffer, uint32_t length) final