GSocket: Merge the unix and windows socket sources together

And remove the bits that were added to gasynchelper.c to support the
previous unix socket source.

part of https://bugzilla.gnome.org/show_bug.cgi?id=587898
This commit is contained in:
Dan Winship 2009-07-09 09:55:00 -04:00
parent 5b329c506a
commit 2c4a79c810
3 changed files with 77 additions and 132 deletions

View File

@ -46,7 +46,6 @@ typedef struct
GPollFD pollfd; GPollFD pollfd;
GCancellable *cancellable; GCancellable *cancellable;
gulong cancelled_tag; gulong cancelled_tag;
GObject *object;
} FDSource; } FDSource;
static gboolean static gboolean
@ -59,7 +58,7 @@ fd_source_prepare (GSource *source,
return g_cancellable_is_cancelled (fd_source->cancellable); return g_cancellable_is_cancelled (fd_source->cancellable);
} }
static gboolean static gboolean
fd_source_check (GSource *source) fd_source_check (GSource *source)
{ {
FDSource *fd_source = (FDSource *)source; FDSource *fd_source = (FDSource *)source;
@ -76,18 +75,14 @@ fd_source_dispatch (GSource *source,
{ {
GFDSourceFunc func = (GFDSourceFunc)callback; GFDSourceFunc func = (GFDSourceFunc)callback;
GFDSourceObjectFunc func2 = (GFDSourceObjectFunc)callback;
FDSource *fd_source = (FDSource *)source; FDSource *fd_source = (FDSource *)source;
g_warn_if_fail (func != NULL); g_warn_if_fail (func != NULL);
if (fd_source->object) return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd);
return (*func2) (fd_source->object, fd_source->pollfd.revents, user_data);
else
return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd);
} }
static void static void
fd_source_finalize (GSource *source) fd_source_finalize (GSource *source)
{ {
FDSource *fd_source = (FDSource *)source; FDSource *fd_source = (FDSource *)source;
@ -98,9 +93,6 @@ fd_source_finalize (GSource *source)
if (fd_source->cancellable) if (fd_source->cancellable)
g_object_unref (fd_source->cancellable); g_object_unref (fd_source->cancellable);
if (fd_source->object)
g_object_unref (fd_source->object);
} }
static GSourceFuncs fd_source_funcs = { static GSourceFuncs fd_source_funcs = {
@ -120,10 +112,9 @@ fd_source_cancelled_cb (GCancellable *cancellable,
} }
GSource * GSource *
_g_fd_source_new_with_object (GObject *object, _g_fd_source_new (int fd,
int fd, gushort events,
gushort events, GCancellable *cancellable)
GCancellable *cancellable)
{ {
GSource *source; GSource *source;
FDSource *fd_source; FDSource *fd_source;
@ -133,27 +124,16 @@ _g_fd_source_new_with_object (GObject *object,
if (cancellable) if (cancellable)
fd_source->cancellable = g_object_ref (cancellable); fd_source->cancellable = g_object_ref (cancellable);
if (object)
fd_source->object = g_object_ref (object);
fd_source->pollfd.fd = fd; fd_source->pollfd.fd = fd;
fd_source->pollfd.events = events; fd_source->pollfd.events = events;
g_source_add_poll (source, &fd_source->pollfd); g_source_add_poll (source, &fd_source->pollfd);
if (cancellable) if (cancellable)
fd_source->cancelled_tag = fd_source->cancelled_tag =
g_cancellable_connect (cancellable, g_cancellable_connect (cancellable,
(GCallback)fd_source_cancelled_cb, (GCallback)fd_source_cancelled_cb,
NULL, NULL); NULL, NULL);
return source; return source;
} }
GSource *
_g_fd_source_new (int fd,
gushort events,
GCancellable *cancellable)
{
return _g_fd_source_new_with_object (NULL, fd, events, cancellable);
}

View File

@ -24,37 +24,16 @@
#define __G_ASYNC_HELPER_H__ #define __G_ASYNC_HELPER_H__
#include <gio/gio.h> #include <gio/gio.h>
#include <glib-object.h>
G_BEGIN_DECLS G_BEGIN_DECLS
typedef struct
{
gpointer async_object;
GError * error;
gpointer user_data;
} GAsyncResultData;
typedef gboolean (*GFDSourceFunc) (gpointer user_data, typedef gboolean (*GFDSourceFunc) (gpointer user_data,
GIOCondition condition, GIOCondition condition,
int fd); int fd);
typedef gboolean (*GFDSourceObjectFunc) (GObject *object,
GIOCondition condition,
gpointer user_data);
void _g_queue_async_result (GAsyncResultData *result, GSource *_g_fd_source_new (int fd,
gpointer async_object, gushort events,
GError *error, GCancellable *cancellable);
gpointer user_data,
GSourceFunc source_func);
GSource *_g_fd_source_new_with_object (GObject *object,
int fd,
gushort events,
GCancellable *cancellable);
GSource *_g_fd_source_new (int fd,
gushort events,
GCancellable *cancellable);
G_END_DECLS G_END_DECLS

View File

@ -45,12 +45,14 @@
#include "gsocket.h" #include "gsocket.h"
#include "gcancellable.h" #include "gcancellable.h"
#include "gioenumtypes.h" #include "gioenumtypes.h"
#include "ginetaddress.h"
#include "ginitable.h" #include "ginitable.h"
#include "gasynchelper.h"
#include "gioerror.h" #include "gioerror.h"
#include "gioenums.h" #include "gioenums.h"
#include "gioerror.h" #include "gioerror.h"
#include "gnetworkingprivate.h" #include "gnetworkingprivate.h"
#include "gsocketaddress.h"
#include "gsocketcontrolmessage.h"
#include "glibintl.h" #include "glibintl.h"
#include "gioalias.h" #include "gioalias.h"
@ -2188,6 +2190,7 @@ update_condition (GSocket *socket)
return condition; return condition;
} }
#endif
typedef struct { typedef struct {
GSource source; GSource source;
@ -2196,103 +2199,87 @@ typedef struct {
GIOCondition condition; GIOCondition condition;
GCancellable *cancellable; GCancellable *cancellable;
GPollFD cancel_pollfd; GPollFD cancel_pollfd;
GIOCondition result_condition; } GSocketSource;
} GWinsockSource;
static gboolean static gboolean
winsock_prepare (GSource *source, socket_source_prepare (GSource *source,
gint *timeout) gint *timeout)
{ {
GWinsockSource *winsock_source = (GWinsockSource *)source; GSocketSource *socket_source = (GSocketSource *)source;
GIOCondition current_condition;
current_condition = update_condition (winsock_source->socket); #ifdef G_OS_WIN32
socket_source->pollfd.revents = update_condition (socket_source->socket);
#endif
*timeout = -1;
if (g_cancellable_is_cancelled (winsock_source->cancellable)) if (g_cancellable_is_cancelled (socket_source->cancellable))
{ return TRUE;
winsock_source->result_condition = current_condition;
return TRUE;
}
if ((winsock_source->condition & current_condition) != 0) if ((socket_source->condition & socket_source->pollfd.revents) != 0)
{ return TRUE;
winsock_source->result_condition = current_condition;
return TRUE;
}
return FALSE; return FALSE;
} }
static gboolean static gboolean
winsock_check (GSource *source) socket_source_check (GSource *source)
{ {
GWinsockSource *winsock_source = (GWinsockSource *)source; int timeout;
GIOCondition current_condition;
current_condition = update_condition (winsock_source->socket); return socket_source_prepare (source, &timeout);
if (g_cancellable_is_cancelled (winsock_source->cancellable))
{
winsock_source->result_condition = current_condition;
return TRUE;
}
if ((winsock_source->condition & current_condition) != 0)
{
winsock_source->result_condition = current_condition;
return TRUE;
}
return FALSE;
} }
static gboolean static gboolean
winsock_dispatch (GSource *source, socket_source_dispatch (GSource *source,
GSourceFunc callback, GSourceFunc callback,
gpointer user_data) gpointer user_data)
{ {
GSocketSourceFunc func = (GSocketSourceFunc)callback; GSocketSourceFunc func = (GSocketSourceFunc)callback;
GWinsockSource *winsock_source = (GWinsockSource *)source; GSocketSource *socket_source = (GSocketSource *)source;
return (*func) (winsock_source->socket, return (*func) (socket_source->socket,
winsock_source->result_condition & winsock_source->condition, socket_source->pollfd.revents & socket_source->condition,
user_data); user_data);
} }
static void static void
winsock_finalize (GSource *source) socket_source_finalize (GSource *source)
{ {
GWinsockSource *winsock_source = (GWinsockSource *)source; GSocketSource *socket_source = (GSocketSource *)source;
GSocket *socket; GSocket *socket;
socket = winsock_source->socket; socket = socket_source->socket;
#ifdef G_OS_WIN32
remove_condition_watch (socket, &socket_source->condition);
#endif
remove_condition_watch (socket, &winsock_source->condition);
g_object_unref (socket); g_object_unref (socket);
if (winsock_source->cancellable) if (socket_source->cancellable)
{ {
g_cancellable_release_fd (winsock_source->cancellable); g_cancellable_release_fd (socket_source->cancellable);
g_object_unref (winsock_source->cancellable); g_object_unref (socket_source->cancellable);
} }
} }
static GSourceFuncs winsock_funcs = static GSourceFuncs socket_source_funcs =
{ {
winsock_prepare, socket_source_prepare,
winsock_check, socket_source_check,
winsock_dispatch, socket_source_dispatch,
winsock_finalize socket_source_finalize
}; };
static GSource * static GSource *
winsock_source_new (GSocket *socket, socket_source_new (GSocket *socket,
GIOCondition condition, GIOCondition condition,
GCancellable *cancellable) GCancellable *cancellable)
{ {
GSource *source; GSource *source;
GWinsockSource *winsock_source; GSocketSource *socket_source;
#ifdef G_OS_WIN32
ensure_event (socket); ensure_event (socket);
if (socket->priv->event == WSA_INVALID_EVENT) if (socket->priv->event == WSA_INVALID_EVENT)
@ -2300,30 +2287,36 @@ winsock_source_new (GSocket *socket,
g_warning ("Failed to create WSAEvent"); g_warning ("Failed to create WSAEvent");
return g_source_new (&broken_funcs, sizeof (GSource)); return g_source_new (&broken_funcs, sizeof (GSource));
} }
#endif
condition |= G_IO_HUP | G_IO_ERR; condition |= G_IO_HUP | G_IO_ERR;
source = g_source_new (&winsock_funcs, sizeof (GWinsockSource)); source = g_source_new (&socket_source_funcs, sizeof (GSocketSource));
winsock_source = (GWinsockSource *)source; socket_source = (GSocketSource *)source;
winsock_source->socket = g_object_ref (socket); socket_source->socket = g_object_ref (socket);
winsock_source->condition = condition; socket_source->condition = condition;
add_condition_watch (socket, &winsock_source->condition);
if (g_cancellable_make_pollfd (cancellable, if (g_cancellable_make_pollfd (cancellable,
&winsock_source->cancel_pollfd)) &socket_source->cancel_pollfd))
{ {
winsock_source->cancellable = g_object_ref (cancellable); socket_source->cancellable = g_object_ref (cancellable);
g_source_add_poll (source, &winsock_source->cancel_pollfd); g_source_add_poll (source, &socket_source->cancel_pollfd);
} }
winsock_source->pollfd.fd = (gintptr) socket->priv->event; #ifdef G_OS_WIN32
winsock_source->pollfd.events = condition; add_condition_watch (socket, &socket_source->condition);
g_source_add_poll (source, &winsock_source->pollfd); socket_source->pollfd.fd = (gintptr) socket->priv->event;
#else
socket_source->pollfd.fd = socket->priv->fd;
#endif
socket_source->pollfd.events = condition;
socket_source->pollfd.revents = 0;
g_source_add_poll (source, &socket_source->pollfd);
return source; return source;
} }
#endif
/** /**
* g_socket_create_source: * g_socket_create_source:
@ -2336,7 +2329,7 @@ winsock_source_new (GSocket *socket,
* *
* The callback on the source is of the #GSocketSourceFunc type. * The callback on the source is of the #GSocketSourceFunc type.
* *
* It is meaningless to specify %G_IO_ERR or %G_IO_HUP in condition; * It is meaningless to specify %G_IO_ERR or %G_IO_HUP in @condition;
* these conditions will always be reported output if they are true. * these conditions will always be reported output if they are true.
* *
* @cancellable if not %NULL can be used to cancel the source, which will * @cancellable if not %NULL can be used to cancel the source, which will
@ -2354,16 +2347,9 @@ g_socket_create_source (GSocket *socket,
GIOCondition condition, GIOCondition condition,
GCancellable *cancellable) GCancellable *cancellable)
{ {
GSource *source;
g_return_val_if_fail (G_IS_SOCKET (socket) && (cancellable == NULL || G_IS_CANCELLABLE (cancellable)), NULL); g_return_val_if_fail (G_IS_SOCKET (socket) && (cancellable == NULL || G_IS_CANCELLABLE (cancellable)), NULL);
#ifdef G_OS_WIN32 return socket_source_new (socket, condition, cancellable);
source = winsock_source_new (socket, condition, cancellable);
#else
source =_g_fd_source_new_with_object (G_OBJECT (socket), socket->priv->fd,
condition, cancellable);
#endif
return source;
} }
/** /**