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.
This commit is contained in:
Dan Winship 2014-03-25 13:52:45 -04:00
parent befc111c43
commit 3da5d59078
3 changed files with 51 additions and 36 deletions

View 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;
}
}

View 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
}

View 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
**/