* against the currently-satisfied conditions on @socket. The result
* is returned.
*
+ * Note that on Windows, it is possible for an operation to return
+ * %G_IO_ERROR_WOULD_BLOCK even immediately after
+ * g_socket_condition_check() has claimed that the socket is ready for
+ * writing. Rather than calling g_socket_condition_check() and then
+ * writing to the socket if it succeeds, it is generally better to
+ * simply try writing to the socket right away, and try again later if
+ * the initial attempt returns %G_IO_ERROR_WOULD_BLOCK.
+ *
* It is meaningless to specify %G_IO_ERR or %G_IO_HUP in condition;
* these conditions will always be set in the output if they are true.
*
#include <gio/gsimpleasyncresult.h>
#include <gio/gcancellable.h>
+#include <gio/gioerror.h>
#include "gioalias.h"
/* pending operation metadata */
GSimpleAsyncResult *result;
GCancellable *cancellable;
- gboolean from_mainloop;
gpointer buffer;
gsize count;
};
GError *error = NULL;
gssize result;
- simple = stream->priv->result;
- stream->priv->result = NULL;
-
result = g_socket_receive (stream->priv->socket,
stream->priv->buffer,
stream->priv->count,
stream->priv->cancellable,
&error);
+
+ if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
+ return TRUE;
+
+ simple = stream->priv->result;
+ stream->priv->result = NULL;
+
if (result >= 0)
g_simple_async_result_set_op_res_gssize (simple, result);
if (stream->priv->cancellable)
g_object_unref (stream->priv->cancellable);
- if (stream->priv->from_mainloop)
- g_simple_async_result_complete (simple);
- else
- g_simple_async_result_complete_in_idle (simple);
-
+ g_simple_async_result_complete (simple);
g_object_unref (simple);
return FALSE;
gpointer user_data)
{
GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
+ GSource *source;
g_assert (input_stream->priv->result == NULL);
input_stream->priv->buffer = buffer;
input_stream->priv->count = count;
- if (!g_socket_condition_check (input_stream->priv->socket, G_IO_IN))
- {
- GSource *source;
-
- input_stream->priv->from_mainloop = TRUE;
- source = g_socket_create_source (input_stream->priv->socket,
- G_IO_IN | G_IO_HUP | G_IO_ERR,
- cancellable);
- g_source_set_callback (source,
- (GSourceFunc) g_socket_input_stream_read_ready,
- g_object_ref (input_stream), g_object_unref);
- g_source_attach (source, g_main_context_get_thread_default ());
- g_source_unref (source);
- }
- else
- {
- input_stream->priv->from_mainloop = FALSE;
- g_socket_input_stream_read_ready (input_stream->priv->socket, G_IO_IN, input_stream);
- }
+ source = g_socket_create_source (input_stream->priv->socket,
+ G_IO_IN | G_IO_HUP | G_IO_ERR,
+ cancellable);
+ g_source_set_callback (source,
+ (GSourceFunc) g_socket_input_stream_read_ready,
+ g_object_ref (input_stream), g_object_unref);
+ g_source_attach (source, g_main_context_get_thread_default ());
+ g_source_unref (source);
}
static gssize
#include <gio/gsimpleasyncresult.h>
#include <gio/gcancellable.h>
+#include <gio/gioerror.h>
#include "glibintl.h"
#include "gioalias.h"
/* pending operation metadata */
GSimpleAsyncResult *result;
GCancellable *cancellable;
- gboolean from_mainloop;
gconstpointer buffer;
gsize count;
};
GError *error = NULL;
gssize result;
- simple = stream->priv->result;
- stream->priv->result = NULL;
-
result = g_socket_send (stream->priv->socket,
stream->priv->buffer,
stream->priv->count,
stream->priv->cancellable,
&error);
+
+ if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
+ return TRUE;
+
+ simple = stream->priv->result;
+ stream->priv->result = NULL;
+
if (result >= 0)
g_simple_async_result_set_op_res_gssize (simple, result);
if (stream->priv->cancellable)
g_object_unref (stream->priv->cancellable);
- if (stream->priv->from_mainloop)
- g_simple_async_result_complete (simple);
- else
- g_simple_async_result_complete_in_idle (simple);
-
+ g_simple_async_result_complete (simple);
g_object_unref (simple);
return FALSE;
gpointer user_data)
{
GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (stream);
+ GSource *source;
g_assert (output_stream->priv->result == NULL);
output_stream->priv->buffer = buffer;
output_stream->priv->count = count;
- if (!g_socket_condition_check (output_stream->priv->socket, G_IO_OUT))
- {
- GSource *source;
-
- output_stream->priv->from_mainloop = TRUE;
- source = g_socket_create_source (output_stream->priv->socket,
- G_IO_OUT | G_IO_HUP | G_IO_ERR,
- cancellable);
- g_source_set_callback (source,
- (GSourceFunc) g_socket_output_stream_write_ready,
- g_object_ref (output_stream), g_object_unref);
- g_source_attach (source, g_main_context_get_thread_default ());
- g_source_unref (source);
- }
- else
- {
- output_stream->priv->from_mainloop = FALSE;
- g_socket_output_stream_write_ready (output_stream->priv->socket, G_IO_OUT, output_stream);
- }
+ source = g_socket_create_source (output_stream->priv->socket,
+ G_IO_OUT | G_IO_HUP | G_IO_ERR,
+ cancellable);
+ g_source_set_callback (source,
+ (GSourceFunc) g_socket_output_stream_write_ready,
+ g_object_ref (output_stream), g_object_unref);
+ g_source_attach (source, g_main_context_get_thread_default ());
+ g_source_unref (source);
}
static gssize