Add thin abstraction layer for network sockets

This makes SocketProfilingConnection and GatordMock work on Windows as
well as Linux

Change-Id: I4b10c079b653a1c3f61eb20694e5b5f8a6f5fdfb
Signed-off-by: Robert Hughes <robert.hughes@arm.com>
diff --git a/tests/profiling/gatordmock/CommandFileParser.cpp b/tests/profiling/gatordmock/CommandFileParser.cpp
index 4a8a19b..7c746f1 100644
--- a/tests/profiling/gatordmock/CommandFileParser.cpp
+++ b/tests/profiling/gatordmock/CommandFileParser.cpp
@@ -54,7 +54,7 @@
             // 500000       polling period in micro seconds
             // 1 2 5 10     counter list
 
-            uint period = static_cast<uint>(std::stoul(tokens[1]));
+            uint32_t period = static_cast<uint32_t>(std::stoul(tokens[1]));
 
             std::vector<uint16_t> counters;
 
@@ -73,7 +73,7 @@
             // WAIT         command
             // 11000000     timeout period in micro seconds
 
-            uint timeout = static_cast<uint>(std::stoul(tokens[1]));
+            uint32_t timeout = static_cast<uint32_t>(std::stoul(tokens[1]));
 
             mockService.WaitCommand(timeout);
         }
diff --git a/tests/profiling/gatordmock/GatordMockService.cpp b/tests/profiling/gatordmock/GatordMockService.cpp
index 529ef06..c521196 100644
--- a/tests/profiling/gatordmock/GatordMockService.cpp
+++ b/tests/profiling/gatordmock/GatordMockService.cpp
@@ -8,17 +8,15 @@
 #include <CommandHandlerRegistry.hpp>
 #include <PacketVersionResolver.hpp>
 #include <ProfilingUtils.hpp>
+#include <NetworkSockets.hpp>
 
 #include <cerrno>
 #include <fcntl.h>
 #include <iomanip>
 #include <iostream>
-#include <poll.h>
 #include <string>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <unistd.h>
+
+using namespace armnnUtils;
 
 namespace armnn
 {
@@ -28,6 +26,7 @@
 
 bool GatordMockService::OpenListeningSocket(std::string udsNamespace)
 {
+    Sockets::Initialize();
     m_ListeningSocket = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
     if (-1 == m_ListeningSocket)
     {
@@ -56,9 +55,9 @@
     return true;
 }
 
-int GatordMockService::BlockForOneClient()
+Sockets::Socket GatordMockService::BlockForOneClient()
 {
-    m_ClientConnection = accept4(m_ListeningSocket, nullptr, nullptr, SOCK_CLOEXEC);
+    m_ClientConnection = Sockets::Accept(m_ListeningSocket, nullptr, nullptr, SOCK_CLOEXEC);
     if (-1 == m_ClientConnection)
     {
         std::cerr << ": Failure when waiting for a client connection: " << strerror(errno) << std::endl;
@@ -112,13 +111,14 @@
     // Remember we already read the pipe magic 4 bytes.
     uint32_t metaDataLength = ToUint32(&header[4], m_Endianness) - 4;
     // Read the entire packet.
-    uint8_t packetData[metaDataLength];
-    if (metaDataLength != boost::numeric_cast<uint32_t>(read(m_ClientConnection, &packetData, metaDataLength)))
+    std::vector<uint8_t> packetData(metaDataLength);
+    if (metaDataLength !=
+        boost::numeric_cast<uint32_t>(Sockets::Read(m_ClientConnection, packetData.data(), metaDataLength)))
     {
         std::cerr << ": Protocol read error. Data length mismatch." << std::endl;
         return false;
     }
-    EchoPacket(PacketDirection::ReceivedData, packetData, metaDataLength);
+    EchoPacket(PacketDirection::ReceivedData, packetData.data(), metaDataLength);
     m_StreamMetaDataVersion    = ToUint32(&packetData[0], m_Endianness);
     m_StreamMetaDataMaxDataLen = ToUint32(&packetData[4], m_Endianness);
     m_StreamMetaDataPid        = ToUint32(&packetData[8], m_Endianness);
@@ -153,10 +153,9 @@
         std::cout << "Launching receiving thread." << std::endl;
     }
     // At this point we want to make the socket non blocking.
-    const int currentFlags = fcntl(m_ClientConnection, F_GETFL);
-    if (0 != fcntl(m_ClientConnection, F_SETFL, currentFlags | O_NONBLOCK))
+    if (!Sockets::SetNonBlocking(m_ClientConnection))
     {
-        close(m_ClientConnection);
+        Sockets::Close(m_ClientConnection);
         std::cerr << "Failed to set socket as non blocking: " << strerror(errno) << std::endl;
         return false;
     }
@@ -212,13 +211,13 @@
     // should deal with it.
 }
 
-void GatordMockService::WaitCommand(uint timeout)
+void GatordMockService::WaitCommand(uint32_t timeout)
 {
     // Wait for a maximum of timeout microseconds or if the receive thread has closed.
     // There is a certain level of rounding involved in this timing.
-    uint iterations = timeout / 1000;
+    uint32_t iterations = timeout / 1000;
     std::cout << std::dec << "Wait command with timeout of " << timeout << " iterations =  " << iterations << std::endl;
-    uint count = 0;
+    uint32_t count = 0;
     while ((this->ReceiveThreadRunning() && (count < iterations)))
     {
         std::this_thread::sleep_for(std::chrono::microseconds(1000));
@@ -261,7 +260,7 @@
 {
     // Is there currently more than a headers worth of data waiting to be read?
     int bytes_available;
-    ioctl(m_ClientConnection, FIONREAD, &bytes_available);
+    Sockets::Ioctl(m_ClientConnection, FIONREAD, &bytes_available);
     if (bytes_available > 8)
     {
         // Yes there is. Read it:
@@ -272,7 +271,7 @@
         // No there's not. Poll for more data.
         struct pollfd pollingFd[1]{};
         pollingFd[0].fd = m_ClientConnection;
-        int pollResult  = poll(pollingFd, 1, static_cast<int>(timeoutMs));
+        int pollResult  = Sockets::Poll(pollingFd, 1, static_cast<int>(timeoutMs));
 
         switch (pollResult)
         {
@@ -362,16 +361,16 @@
     header[0] = packetFamily << 26 | packetId << 16;
     header[1] = dataLength;
     // Add the header to the packet.
-    uint8_t packet[8 + dataLength];
-    InsertU32(header[0], packet, m_Endianness);
-    InsertU32(header[1], packet + 4, m_Endianness);
+    std::vector<uint8_t> packet(8 + dataLength);
+    InsertU32(header[0], packet.data(), m_Endianness);
+    InsertU32(header[1], packet.data() + 4, m_Endianness);
     // And the rest of the data if there is any.
     if (dataLength > 0)
     {
-        memcpy((packet + 8), data, dataLength);
+        memcpy((packet.data() + 8), data, dataLength);
     }
-    EchoPacket(PacketDirection::Sending, packet, sizeof(packet));
-    if (-1 == write(m_ClientConnection, packet, sizeof(packet)))
+    EchoPacket(PacketDirection::Sending, packet.data(), packet.size());
+    if (-1 == Sockets::Write(m_ClientConnection, packet.data(), packet.size()))
     {
         std::cerr << ": Failure when writing to client socket: " << strerror(errno) << std::endl;
         return false;
@@ -396,10 +395,10 @@
 bool GatordMockService::ReadFromSocket(uint8_t* packetData, uint32_t expectedLength)
 {
     // This is a blocking read until either expectedLength has been received or an error is detected.
-    ssize_t totalBytesRead = 0;
+    long totalBytesRead = 0;
     while (boost::numeric_cast<uint32_t>(totalBytesRead) < expectedLength)
     {
-        ssize_t bytesRead = recv(m_ClientConnection, packetData, expectedLength, 0);
+        long bytesRead = Sockets::Read(m_ClientConnection, packetData, expectedLength);
         if (bytesRead < 0)
         {
             std::cerr << ": Failure when reading from client socket: " << strerror(errno) << std::endl;
diff --git a/tests/profiling/gatordmock/GatordMockService.hpp b/tests/profiling/gatordmock/GatordMockService.hpp
index c3afc33..f91e902 100644
--- a/tests/profiling/gatordmock/GatordMockService.hpp
+++ b/tests/profiling/gatordmock/GatordMockService.hpp
@@ -7,6 +7,7 @@
 
 #include <CommandHandlerRegistry.hpp>
 #include <Packet.hpp>
+#include <NetworkSockets.hpp>
 
 #include <atomic>
 #include <string>
@@ -49,8 +50,8 @@
     ~GatordMockService()
     {
         // We have set SOCK_CLOEXEC on these sockets but we'll close them to be good citizens.
-        close(m_ClientConnection);
-        close(m_ListeningSocket);
+        armnnUtils::Sockets::Close(m_ClientConnection);
+        armnnUtils::Sockets::Close(m_ListeningSocket);
     }
 
     /// Establish the Unix domain socket and set it to listen for connections.
@@ -60,7 +61,7 @@
 
     /// Block waiting to accept one client to connect to the UDS.
     /// @return the file descriptor of the client connection.
-    int BlockForOneClient();
+    armnnUtils::Sockets::Socket BlockForOneClient();
 
     /// Once the connection is open wait to receive the stream meta data packet from the client. Reading this
     /// packet differs from others as we need to determine endianness.
@@ -147,8 +148,8 @@
     armnn::profiling::CommandHandlerRegistry& m_HandlerRegistry;
 
     bool m_EchoPackets;
-    int m_ListeningSocket;
-    int m_ClientConnection;
+    armnnUtils::Sockets::Socket m_ListeningSocket;
+    armnnUtils::Sockets::Socket m_ClientConnection;
     std::thread m_ListeningThread;
     std::atomic<bool> m_CloseReceivingThread;
 };
diff --git a/tests/profiling/timelineDecoder/TimelineCaptureCommandHandler.cpp b/tests/profiling/timelineDecoder/TimelineCaptureCommandHandler.cpp
index bdceca6..78b1300 100644
--- a/tests/profiling/timelineDecoder/TimelineCaptureCommandHandler.cpp
+++ b/tests/profiling/timelineDecoder/TimelineCaptureCommandHandler.cpp
@@ -122,7 +122,7 @@
     event.m_TimeStamp = profiling::ReadUint64(data, offset);
     offset += uint64_t_size;
 
-    event.m_ThreadId = new u_int8_t[threadId_size];
+    event.m_ThreadId = new uint8_t[threadId_size];
     profiling::ReadBytes(data, offset, threadId_size, event.m_ThreadId);
     offset += threadId_size;