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)
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;
}
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());
if (-1 == ::connect (sock, (const struct sockaddr *)&sa, sizeof(sa)))
{
- // TODO: On Windows, use WSAGetLastError()
- error.SetErrorToErrno();
+ SetLastError (error);
return error;
}
listen_sock = ::CreateSocket (family, socktype, protocol, child_processes_inherit);
if (listen_sock == kInvalidSocketValue)
{
- error.SetErrorToErrno();
+ SetLastError (error);
return error;
}
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;
}
if (sock == kInvalidSocketValue)
{
- // TODO: On Windows, use WSAGetLastError()
- error.SetErrorToErrno();
+ SetLastError (error);
break;
}
if (final_recv_fd == kInvalidSocketValue)
{
// Socket creation failed...
- // TODO: On Windows, use WSAGetLastError().
- error.SetErrorToErrno();
+ SetLastError (error);
}
else
{
if (::bind (final_recv_fd, addr, addr.GetLength()) == -1)
{
// Bind failed...
- // TODO: On Windows use WSAGetLastError()
- error.SetErrorToErrno();
+ SetLastError (error);
}
}
if (final_send_fd == kInvalidSocketValue)
{
- // TODO: On Windows, use WSAGetLastError().
- error.SetErrorToErrno();
+ SetLastError (error);
return error;
}
int fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit);
if (fd == kInvalidSocketValue)
{
- error.SetErrorToErrno();
+ SetLastError (error);
return error;
}
if (::connect (fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) < 0)
{
- error.SetErrorToErrno();
+ SetLastError (error);
return error;
}
listen_fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit);
if (listen_fd == kInvalidSocketValue)
{
- error.SetErrorToErrno();
+ SetLastError (error);
return error;
}
if (!success)
{
- error.SetErrorToErrno();
+ SetLastError (error);
return error;
}
// We are done with the listen port
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
}
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
m_socket = kInvalidSocketValue;
if (!success)
{
- // TODO: On Windows, use WSAGetLastError().
- error.SetErrorToErrno();
+ SetLastError (error);
}
return error;