Wrap socket error handling with SetLastError and IsInterrupted internal functions which can properly treat Windows and POSIX errors.

http://reviews.llvm.org/D8939

llvm-svn: 234559
diff --git a/lldb/source/Host/common/Socket.cpp b/lldb/source/Host/common/Socket.cpp
index ff1ca7d..2059fec 100644
--- a/lldb/source/Host/common/Socket.cpp
+++ b/lldb/source/Host/common/Socket.cpp
@@ -80,6 +80,25 @@
     return ::accept (sockfd, addr, addrlen);
 #endif
 }
+
+void SetLastError(Error &error)
+{
+#if defined(_WIN32)
+    error.SetError(::WSAGetLastError(), lldb::eErrorTypeWin32);
+#else
+    error.SetErrorToErrno();
+#endif
+}
+
+bool IsInterrupted()
+{
+#if defined(_WIN32)
+    return ::WSAGetLastError() == WSAEINTR;
+#else
+    return errno == EINTR;
+#endif
+}
+
 }
 
 Socket::Socket(NativeSocket socket, SocketProtocol protocol, bool should_close)
@@ -116,8 +135,7 @@
     sock = CreateSocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, child_processes_inherit);
     if (sock == kInvalidSocketValue)
     {
-        // TODO: On Windows, use WSAGetLastError().
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
 
@@ -143,9 +161,8 @@
         inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr);
         if (inet_pton_result <= 0)
         {
-            // TODO: On Windows, use WSAGetLastError()
             if (inet_pton_result == -1)
-                error.SetErrorToErrno();
+                SetLastError(error);
             else
                 error.SetErrorStringWithFormat("invalid host string: '%s'", host_str.c_str());
 
@@ -155,8 +172,7 @@
 
     if (-1 == ::connect (sock, (const struct sockaddr *)&sa, sizeof(sa)))
     {
-        // TODO: On Windows, use WSAGetLastError()
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
 
@@ -184,7 +200,7 @@
     listen_sock = ::CreateSocket (family, socktype, protocol, child_processes_inherit);
     if (listen_sock == kInvalidSocketValue)
     {
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
 
@@ -209,16 +225,14 @@
         int err = ::bind (listen_sock, anyaddr, anyaddr.GetLength());
         if (err == -1)
         {
-            // TODO: On Windows, use WSAGetLastError()
-            error.SetErrorToErrno();
+            SetLastError (error);
             return error;
         }
 
         err = ::listen (listen_sock, backlog);
         if (err == -1)
         {
-            // TODO: On Windows, use WSAGetLastError()
-            error.SetErrorToErrno();
+            SetLastError (error);
             return error;
         }
 
@@ -289,8 +303,7 @@
             
         if (sock == kInvalidSocketValue)
         {
-            // TODO: On Windows, use WSAGetLastError()
-            error.SetErrorToErrno();
+            SetLastError (error);
             break;
         }
     
@@ -354,8 +367,7 @@
     if (final_recv_fd == kInvalidSocketValue)
     {
         // Socket creation failed...
-        // TODO: On Windows, use WSAGetLastError().
-        error.SetErrorToErrno();
+        SetLastError (error);
     }
     else
     {
@@ -368,8 +380,7 @@
         if (::bind (final_recv_fd, addr, addr.GetLength()) == -1)
         {
             // Bind failed...
-            // TODO: On Windows use WSAGetLastError()
-            error.SetErrorToErrno();
+            SetLastError (error);
         }
     }
 
@@ -420,8 +431,7 @@
 
     if (final_send_fd == kInvalidSocketValue)
     {
-        // TODO: On Windows, use WSAGetLastError().
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
 
@@ -442,7 +452,7 @@
     int fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit);
     if (fd == kInvalidSocketValue)
     {
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
 
@@ -457,7 +467,7 @@
 
     if (::connect (fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) < 0) 
     {
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
 
@@ -481,7 +491,7 @@
     listen_fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit);
     if (listen_fd == kInvalidSocketValue)
     {
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
 
@@ -511,7 +521,7 @@
     
     if (!success)
     {
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
     // We are done with the listen port
@@ -585,12 +595,11 @@
     do
     {
         bytes_received = ::recv (m_socket, static_cast<char *>(buf), num_bytes, 0);
-        // TODO: Use WSAGetLastError on windows.
-    } while (bytes_received < 0 && errno == EINTR);
+    } while (bytes_received < 0 && IsInterrupted ());
 
     if (bytes_received < 0)
     {
-        error.SetErrorToErrno();
+        SetLastError (error);
         num_bytes = 0;
     }
     else
@@ -628,13 +637,11 @@
         }
         else
             bytes_sent = ::send (m_socket, static_cast<const char *>(buf), num_bytes, 0);
-        // TODO: Use WSAGetLastError on windows.
-    } while (bytes_sent < 0 && errno == EINTR);
+    } while (bytes_sent < 0 && IsInterrupted ());
 
     if (bytes_sent < 0)
     {
-        // TODO: On Windows, use WSAGEtLastError.
-        error.SetErrorToErrno();
+        SetLastError (error);
         num_bytes = 0;
     }
     else
@@ -680,8 +687,7 @@
     m_socket = kInvalidSocketValue;
     if (!success)
     {
-        // TODO: On Windows, use WSAGetLastError().
-        error.SetErrorToErrno();
+        SetLastError (error);
     }
 
     return error;