Return correct value for g_socket_get_available_bytes() on Windows and OSX

https://bugzilla.gnome.org/show_bug.cgi?id=686786
This commit is contained in:
Sebastian Dröge 2012-10-24 14:25:01 +02:00 committed by Matthias Clasen
parent b65dac802e
commit 1e598600a1
2 changed files with 64 additions and 4 deletions

View File

@ -2368,7 +2368,10 @@ g_socket_check_connect_result (GSocket *socket,
* g_socket_get_available_bytes:
* @socket: a #GSocket
*
* Get the amount of data pending in the OS input buffer.
* Get the amount of data that can be read from the socket without
* blocking. In the case of datagram sockets this returns the size
* of the first datagram and not the sum of the sizes of all currently
* queued datagrams.
*
* Returns: the number of bytes that can be read from the socket
* without blocking or -1 on error.
@ -2382,15 +2385,19 @@ g_socket_get_available_bytes (GSocket *socket)
gulong avail = 0;
#else
gint avail = 0;
gsize avail_len = sizeof (avail);
#endif
g_return_val_if_fail (G_IS_SOCKET (socket), -1);
#ifndef G_OS_WIN32
if (ioctl (socket->priv->fd, FIONREAD, &avail) < 0)
#if defined(G_OS_WIN32)
if (WSAIoctl (socket->priv->fd, FIONREAD, NULL, 0, &avail, sizeof (avail), 0, 0) == SOCKET_ERROR)
return -1;
#elif defined(SO_NREAD)
if (getsockopt (socket->priv->fd, SOL_SOCKET, SO_NREAD, &avail, &avail_len) < 0)
return -1;
#else
if (ioctlsocket (socket->priv->fd, FIONREAD, &avail) == SOCKET_ERROR)
if (ioctl (socket->priv->fd, FIONREAD, &avail) < 0)
return -1;
#endif

View File

@ -813,6 +813,58 @@ test_unix_connection_ancillary_data (void)
}
#endif /* G_OS_UNIX */
static void
test_datagram_get_available (void)
{
GError *err = NULL;
GSocket *server, *client;
GInetAddress *addr;
GSocketAddress *saddr;
gchar data[] = "0123456789abcdef";
server = g_socket_new (G_SOCKET_FAMILY_IPV4,
G_SOCKET_TYPE_DATAGRAM,
G_SOCKET_PROTOCOL_DEFAULT,
&err);
g_assert_no_error (err);
g_assert (G_IS_SOCKET (server));
client = g_socket_new (G_SOCKET_FAMILY_IPV4,
G_SOCKET_TYPE_DATAGRAM,
G_SOCKET_PROTOCOL_DEFAULT,
&err);
g_assert_no_error (err);
g_assert (G_IS_SOCKET (client));
addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
saddr = g_inet_socket_address_new (addr, 0);
g_socket_bind (server, saddr, TRUE, &err);
g_assert_no_error (err);
g_object_unref (saddr);
g_object_unref (addr);
saddr = g_socket_get_local_address (server, &err);
g_assert_no_error (err);
g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
g_assert_no_error (err);
g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
g_assert_no_error (err);
g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
g_socket_close (server, &err);
g_assert_no_error (err);
g_object_unref (saddr);
g_object_unref (server);
g_object_unref (client);
}
int
main (int argc,
char *argv[])
@ -834,6 +886,7 @@ main (int argc,
g_test_add_func ("/socket/unix-connection", test_unix_connection);
g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
#endif
g_test_add_func ("/socket/datagram_get_available", test_datagram_get_available);
return g_test_run();
}