[glib] gio: move Winsock error mapping to g_io_error_from_win32_error()



commit 3da5d59078b135ee224dae223b7ebb3e873e980a
Author: Dan Winship <danw gnome org>
Date:   Tue Mar 25 13:52:45 2014 -0400

    gio: move Winsock error mapping to g_io_error_from_win32_error()
    
    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 |   45 ++++++++++++++++++++++++++++++++++++++++++---
 gio/gsocket.c  |   30 +++---------------------------
 glib/gwin32.c  |   12 ++++++------
 3 files changed, 51 insertions(+), 36 deletions(-)
---
diff --git a/gio/gioerror.c b/gio/gioerror.c
index cce1dfe..fbae67d 100644
--- a/gio/gioerror.c
+++ b/gio/gioerror.c
@@ -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;
     }
 }
 
diff --git a/gio/gsocket.c b/gio/gsocket.c
index b9c02e7..067431b 100644
--- a/gio/gsocket.c
+++ b/gio/gsocket.c
@@ -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
 }
 
diff --git a/glib/gwin32.c b/glib/gwin32.c
index ea26bf4..3c60df4 100644
--- a/glib/gwin32.c
+++ b/glib/gwin32.c
@@ -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
  **/


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]