gio: move Winsock error mapping to g_io_error_from_win32_error()
authorDan Winship <danw@gnome.org>
Tue, 25 Mar 2014 17:52:45 +0000 (13:52 -0400)
committerDan Winship <danw@gnome.org>
Sun, 30 Mar 2014 15:55:47 +0000 (11:55 -0400)
Rather than having special code in gsocket.c, handle Winsock errors
along with other Win32 errors in gioerror.c

Also, reference g_win32_error_message() from the
g_io_error_from_win32_error() docs, and update the
g_win32_error_message() docs to clarify that it works with Winsock
error codes too.

gio/gioerror.c
gio/gsocket.c
glib/gwin32.c

index cce1dfe..fbae67d 100644 (file)
@@ -22,6 +22,9 @@
 #include <errno.h>
 #include "gioerror.h"
 
+#ifdef G_OS_WIN32
+#include <winsock2.h>
+#endif
 
 /**
  * SECTION:gioerror
@@ -251,11 +254,16 @@ g_io_error_from_errno (gint err_no)
  * g_io_error_from_win32_error:
  * @error_code: Windows error number.
  *
- * Converts some common error codes into GIO error codes. The fallback
- * value %G_IO_ERROR_FAILED is returned for error codes not currently
+ * Converts some common error codes (as returned from GetLastError()
+ * or WSAGetLastError()) into GIO error codes. The fallback value
+ * %G_IO_ERROR_FAILED is returned for error codes not currently
  * handled (but note that future GLib releases may return a more
  * specific value instead).
  *
+ * You can use g_win32_error_message() to get a localized string
+ * corresponding to @error_code. (But note that unlike g_strerror(),
+ * g_win32_error_message() returns a string that must be freed.)
+ *
  * Returns: #GIOErrorEnum value for the given error number.
  *
  * Since: 2.26
@@ -263,11 +271,42 @@ g_io_error_from_errno (gint err_no)
 GIOErrorEnum
 g_io_error_from_win32_error (gint error_code)
 {
+  /* Note: Winsock errors are a subset of Win32 error codes as a
+   * whole. (The fact that the Winsock API makes them look like they
+   * aren't is just because the API predates Win32.)
+   */
+
   switch (error_code)
     {
+    case WSAEADDRINUSE:
+      return G_IO_ERROR_ADDRESS_IN_USE;
+
+    case WSAEWOULDBLOCK:
+      return G_IO_ERROR_WOULD_BLOCK;
+
+    case WSAEACCES:
+      return G_IO_ERROR_PERMISSION_DENIED;
+
+    case WSA_INVALID_HANDLE:
+    case WSA_INVALID_PARAMETER:
+    case WSAEBADF:
+    case WSAENOTSOCK:
+      return G_IO_ERROR_INVALID_ARGUMENT;
+
+    case WSAEPROTONOSUPPORT:
+      return G_IO_ERROR_NOT_SUPPORTED;
+
+    case WSAECANCELLED:
+      return G_IO_ERROR_CANCELLED;
+
+    case WSAESOCKTNOSUPPORT:
+    case WSAEOPNOTSUPP:
+    case WSAEPFNOSUPPORT:
+    case WSAEAFNOSUPPORT:
+      return G_IO_ERROR_NOT_SUPPORTED;
+
     default:
       return G_IO_ERROR_FAILED;
-      break;
     }
 }
 
index b9c02e7..067431b 100644 (file)
@@ -201,34 +201,10 @@ get_socket_errno (void)
 static GIOErrorEnum
 socket_io_error_from_errno (int err)
 {
-#ifndef G_OS_WIN32
-  return g_io_error_from_errno (err);
+#ifdef G_OS_WIN32
+  return g_io_error_from_win32_error (err);
 #else
-  switch (err)
-    {
-    case WSAEADDRINUSE:
-      return G_IO_ERROR_ADDRESS_IN_USE;
-    case WSAEWOULDBLOCK:
-      return G_IO_ERROR_WOULD_BLOCK;
-    case WSAEACCES:
-      return G_IO_ERROR_PERMISSION_DENIED;
-    case WSA_INVALID_HANDLE:
-    case WSA_INVALID_PARAMETER:
-    case WSAEBADF:
-    case WSAENOTSOCK:
-      return G_IO_ERROR_INVALID_ARGUMENT;
-    case WSAEPROTONOSUPPORT:
-      return G_IO_ERROR_NOT_SUPPORTED;
-    case WSAECANCELLED:
-      return G_IO_ERROR_CANCELLED;
-    case WSAESOCKTNOSUPPORT:
-    case WSAEOPNOTSUPP:
-    case WSAEPFNOSUPPORT:
-    case WSAEAFNOSUPPORT:
-      return G_IO_ERROR_NOT_SUPPORTED;
-    default:
-      return G_IO_ERROR_FAILED;
-    }
+  return g_io_error_from_errno (err);
 #endif
 }
 
index ea26bf4..3c60df4 100644 (file)
@@ -161,12 +161,12 @@ g_win32_getlocale (void)
  * g_win32_error_message:
  * @error: error code.
  *
- * Translate a Win32 error code (as returned by GetLastError()) into
- * the corresponding message. The message is either language neutral,
- * or in the thread's language, or the user's language, the system's
- * language, or US English (see docs for FormatMessage()). The
- * returned string is in UTF-8. It should be deallocated with
- * g_free().
+ * Translate a Win32 error code (as returned by GetLastError() or
+ * WSAGetLastError()) into the corresponding message. The message is
+ * either language neutral, or in the thread's language, or the user's
+ * language, the system's language, or US English (see docs for
+ * FormatMessage()). The returned string is in UTF-8. It should be
+ * deallocated with g_free().
  *
  * Returns: newly-allocated error message
  **/