[Lldb-commits] [lldb] r234559 - Wrap socket error handling with SetLastError and IsInterrupted internal functions which can properly treat Windows and POSIX errors.

Oleksiy Vyalov ovyalov at google.com
Thu Apr 9 19:31:37 PDT 2015


Author: ovyalov
Date: Thu Apr  9 21:31:37 2015
New Revision: 234559

URL: http://llvm.org/viewvc/llvm-project?rev=234559&view=rev
Log:
Wrap socket error handling with SetLastError and IsInterrupted internal functions which can properly treat Windows and POSIX errors.

http://reviews.llvm.org/D8939


Modified:
    lldb/trunk/source/Host/common/Socket.cpp

Modified: lldb/trunk/source/Host/common/Socket.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Socket.cpp?rev=234559&r1=234558&r2=234559&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Socket.cpp (original)
+++ lldb/trunk/source/Host/common/Socket.cpp Thu Apr  9 21:31:37 2015
@@ -80,6 +80,25 @@ NativeSocket Accept(NativeSocket sockfd,
     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 @@ Error Socket::TcpConnect(llvm::StringRef
     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 @@ Error Socket::TcpConnect(llvm::StringRef
         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 @@ Error Socket::TcpConnect(llvm::StringRef
 
     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 @@ Error Socket::TcpListen(
     listen_sock = ::CreateSocket (family, socktype, protocol, child_processes_inherit);
     if (listen_sock == kInvalidSocketValue)
     {
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
 
@@ -209,16 +225,14 @@ Error Socket::TcpListen(
         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 @@ Error Socket::BlockingAccept(llvm::Strin
             
         if (sock == kInvalidSocketValue)
         {
-            // TODO: On Windows, use WSAGetLastError()
-            error.SetErrorToErrno();
+            SetLastError (error);
             break;
         }
     
@@ -354,8 +367,7 @@ Error Socket::UdpConnect(llvm::StringRef
     if (final_recv_fd == kInvalidSocketValue)
     {
         // Socket creation failed...
-        // TODO: On Windows, use WSAGetLastError().
-        error.SetErrorToErrno();
+        SetLastError (error);
     }
     else
     {
@@ -368,8 +380,7 @@ Error Socket::UdpConnect(llvm::StringRef
         if (::bind (final_recv_fd, addr, addr.GetLength()) == -1)
         {
             // Bind failed...
-            // TODO: On Windows use WSAGetLastError()
-            error.SetErrorToErrno();
+            SetLastError (error);
         }
     }
 
@@ -420,8 +431,7 @@ Error Socket::UdpConnect(llvm::StringRef
 
     if (final_send_fd == kInvalidSocketValue)
     {
-        // TODO: On Windows, use WSAGetLastError().
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
 
@@ -442,7 +452,7 @@ Error Socket::UnixDomainConnect(llvm::St
     int fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit);
     if (fd == kInvalidSocketValue)
     {
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
 
@@ -457,7 +467,7 @@ Error Socket::UnixDomainConnect(llvm::St
 
     if (::connect (fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) < 0) 
     {
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
 
@@ -481,7 +491,7 @@ Error Socket::UnixDomainAccept(llvm::Str
     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 @@ Error Socket::UnixDomainAccept(llvm::Str
     
     if (!success)
     {
-        error.SetErrorToErrno();
+        SetLastError (error);
         return error;
     }
     // We are done with the listen port
@@ -585,12 +595,11 @@ Error Socket::Read (void *buf, size_t &n
     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 @@ Error Socket::Write (const void *buf, si
         }
         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 @@ Error Socket::Close()
     m_socket = kInvalidSocketValue;
     if (!success)
     {
-        // TODO: On Windows, use WSAGetLastError().
-        error.SetErrorToErrno();
+        SetLastError (error);
     }
 
     return error;





More information about the lldb-commits mailing list