GSocket: Add function to get the currently available bytes for reading
authorOle André Vadla Ravnås <oravnas@cisco.com>
Mon, 16 Jan 2012 13:00:49 +0000 (14:00 +0100)
committerSebastian Dröge <sebastian.droege@collabora.co.uk>
Mon, 16 Jan 2012 17:41:40 +0000 (18:41 +0100)
https://bugzilla.gnome.org/show_bug.cgi?id=668009

docs/reference/gio/gio-sections.txt
gio/gio.symbols
gio/gsocket.c
gio/gsocket.h

index 3da75ef4b93ce4ab53835f7a7fb54554a2af5ad9..96b8b321b082e4bf9ab98127851996bb6ce1ce82 100644 (file)
@@ -1806,6 +1806,7 @@ g_socket_is_connected
 g_socket_create_source
 g_socket_condition_check
 g_socket_condition_wait
+g_socket_get_available_bytes
 g_socket_set_listen_backlog
 g_socket_get_listen_backlog
 g_socket_get_blocking
index d1e3ff8f031a3ff0157150026d090127d854753a..94391d55271f4c3aa7e00d799ac1f0712f77610e 100644 (file)
@@ -949,6 +949,7 @@ g_socket_condition_check
 g_socket_condition_wait
 g_socket_connect
 g_socket_create_source
+g_socket_get_available_bytes
 g_socket_get_blocking
 g_socket_get_broadcast
 g_socket_get_family
index 196c84295bec27605d6f03a75e68ab33c45acfca..d985e9eaab1559db5baa682058a0ee2c0a73a033 100644 (file)
@@ -41,6 +41,7 @@
 #ifndef G_OS_WIN32
 # include <fcntl.h>
 # include <unistd.h>
+# include <sys/ioctl.h>
 #endif
 
 #ifdef HAVE_SYS_UIO_H
@@ -2335,6 +2336,39 @@ g_socket_check_connect_result (GSocket  *socket,
   return TRUE;
 }
 
+/**
+ * g_socket_get_available_bytes:
+ * @socket: a #GSocket
+ *
+ * Get the amount of data pending in the OS input buffer.
+ *
+ * Returns: the number of bytes that can be read from the socket
+ * without blocking or -1 on error.
+ *
+ * Since: 2.32
+ */
+gssize
+g_socket_get_available_bytes (GSocket *socket)
+{
+#ifndef G_OS_WIN32
+  gulong avail = 0;
+#else
+  gint avail = 0;
+#endif
+
+  g_return_val_if_fail (G_IS_SOCKET (socket), -1);
+
+#ifndef G_OS_WIN32
+  if (ioctl (socket->priv->fd, FIONREAD, &avail) < 0)
+    return -1;
+#else
+  if (ioctlsocket (socket->priv->fd, FIONREAD, &avail) == SOCKET_ERROR)
+    return -1;
+#endif
+
+  return avail;
+}
+
 /**
  * g_socket_receive:
  * @socket: a #GSocket
index 94b4ce1a710c23b24178773567790164bdec0c57..ae7673ceea31aaf99685e5ff1a633e7f51c9264a 100644 (file)
@@ -137,6 +137,8 @@ gboolean               g_socket_connect                 (GSocket
 gboolean               g_socket_check_connect_result    (GSocket                 *socket,
                                                         GError                 **error);
 
+gssize                 g_socket_get_available_bytes     (GSocket                 *socket);
+
 GIOCondition           g_socket_condition_check         (GSocket                 *socket,
                                                         GIOCondition             condition);
 gboolean               g_socket_condition_wait          (GSocket                 *socket,