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;
GCancellable *cancellable;
gulong cancelled_tag;
GObject *object;
} FDSource;
static gboolean
@ -59,7 +58,7 @@ fd_source_prepare (GSource *source,
return g_cancellable_is_cancelled (fd_source->cancellable);
}
static gboolean
static gboolean
fd_source_check (GSource *source)
{
FDSource *fd_source = (FDSource *)source;
@ -76,18 +75,14 @@ fd_source_dispatch (GSource *source,
{
GFDSourceFunc func = (GFDSourceFunc)callback;
GFDSourceObjectFunc func2 = (GFDSourceObjectFunc)callback;
FDSource *fd_source = (FDSource *)source;
g_warn_if_fail (func != NULL);
if (fd_source->object)
return (*func2) (fd_source->object, fd_source->pollfd.revents, user_data);
else
return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd);
return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd);
}
static void
static void
fd_source_finalize (GSource *source)
{
FDSource *fd_source = (FDSource *)source;
@ -98,9 +93,6 @@ fd_source_finalize (GSource *source)
if (fd_source->cancellable)
g_object_unref (fd_source->cancellable);
if (fd_source->object)
g_object_unref (fd_source->object);
}
static GSourceFuncs fd_source_funcs = {
@ -120,10 +112,9 @@ fd_source_cancelled_cb (GCancellable *cancellable,
}
GSource *
_g_fd_source_new_with_object (GObject *object,
int fd,
gushort events,
GCancellable *cancellable)
_g_fd_source_new (int fd,
gushort events,
GCancellable *cancellable)
{
GSource *source;
FDSource *fd_source;
@ -133,27 +124,16 @@ _g_fd_source_new_with_object (GObject *object,
if (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.events = events;
g_source_add_poll (source, &fd_source->pollfd);
if (cancellable)
fd_source->cancelled_tag =
g_cancellable_connect (cancellable,
g_cancellable_connect (cancellable,
(GCallback)fd_source_cancelled_cb,
NULL, NULL);
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__
#include <gio/gio.h>
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct
{
gpointer async_object;
GError * error;
gpointer user_data;
} GAsyncResultData;
typedef gboolean (*GFDSourceFunc) (gpointer user_data,
GIOCondition condition,
int fd);
typedef gboolean (*GFDSourceObjectFunc) (GObject *object,
GIOCondition condition,
gpointer user_data);
void _g_queue_async_result (GAsyncResultData *result,
gpointer async_object,
GError *error,
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);
GSource *_g_fd_source_new (int fd,
gushort events,
GCancellable *cancellable);
G_END_DECLS

View File

@ -45,12 +45,14 @@
#include "gsocket.h"
#include "gcancellable.h"
#include "gioenumtypes.h"
#include "ginetaddress.h"
#include "ginitable.h"
#include "gasynchelper.h"
#include "gioerror.h"
#include "gioenums.h"
#include "gioerror.h"
#include "gnetworkingprivate.h"
#include "gsocketaddress.h"
#include "gsocketcontrolmessage.h"
#include "glibintl.h"
#include "gioalias.h"
@ -2188,6 +2190,7 @@ update_condition (GSocket *socket)
return condition;
}
#endif
typedef struct {
GSource source;
@ -2196,103 +2199,87 @@ typedef struct {
GIOCondition condition;
GCancellable *cancellable;
GPollFD cancel_pollfd;
GIOCondition result_condition;
} GWinsockSource;
} GSocketSource;
static gboolean
winsock_prepare (GSource *source,
gint *timeout)
socket_source_prepare (GSource *source,
gint *timeout)
{
GWinsockSource *winsock_source = (GWinsockSource *)source;
GIOCondition current_condition;
GSocketSource *socket_source = (GSocketSource *)source;
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))
{
winsock_source->result_condition = current_condition;
return TRUE;
}
if (g_cancellable_is_cancelled (socket_source->cancellable))
return TRUE;
if ((winsock_source->condition & current_condition) != 0)
{
winsock_source->result_condition = current_condition;
return TRUE;
}
if ((socket_source->condition & socket_source->pollfd.revents) != 0)
return TRUE;
return FALSE;
}
static gboolean
winsock_check (GSource *source)
socket_source_check (GSource *source)
{
GWinsockSource *winsock_source = (GWinsockSource *)source;
GIOCondition current_condition;
int timeout;
current_condition = update_condition (winsock_source->socket);
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;
return socket_source_prepare (source, &timeout);
}
static gboolean
winsock_dispatch (GSource *source,
GSourceFunc callback,
gpointer user_data)
socket_source_dispatch (GSource *source,
GSourceFunc callback,
gpointer user_data)
{
GSocketSourceFunc func = (GSocketSourceFunc)callback;
GWinsockSource *winsock_source = (GWinsockSource *)source;
GSocketSource *socket_source = (GSocketSource *)source;
return (*func) (winsock_source->socket,
winsock_source->result_condition & winsock_source->condition,
return (*func) (socket_source->socket,
socket_source->pollfd.revents & socket_source->condition,
user_data);
}
static void
winsock_finalize (GSource *source)
socket_source_finalize (GSource *source)
{
GWinsockSource *winsock_source = (GWinsockSource *)source;
GSocketSource *socket_source = (GSocketSource *)source;
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);
if (winsock_source->cancellable)
if (socket_source->cancellable)
{
g_cancellable_release_fd (winsock_source->cancellable);
g_object_unref (winsock_source->cancellable);
g_cancellable_release_fd (socket_source->cancellable);
g_object_unref (socket_source->cancellable);
}
}
static GSourceFuncs winsock_funcs =
static GSourceFuncs socket_source_funcs =
{
winsock_prepare,
winsock_check,
winsock_dispatch,
winsock_finalize
socket_source_prepare,
socket_source_check,
socket_source_dispatch,
socket_source_finalize
};
static GSource *
winsock_source_new (GSocket *socket,
GIOCondition condition,
GCancellable *cancellable)
socket_source_new (GSocket *socket,
GIOCondition condition,
GCancellable *cancellable)
{
GSource *source;
GWinsockSource *winsock_source;
GSocketSource *socket_source;
#ifdef G_OS_WIN32
ensure_event (socket);
if (socket->priv->event == WSA_INVALID_EVENT)
@ -2300,30 +2287,36 @@ winsock_source_new (GSocket *socket,
g_warning ("Failed to create WSAEvent");
return g_source_new (&broken_funcs, sizeof (GSource));
}
#endif
condition |= G_IO_HUP | G_IO_ERR;
source = g_source_new (&winsock_funcs, sizeof (GWinsockSource));
winsock_source = (GWinsockSource *)source;
source = g_source_new (&socket_source_funcs, sizeof (GSocketSource));
socket_source = (GSocketSource *)source;
winsock_source->socket = g_object_ref (socket);
winsock_source->condition = condition;
add_condition_watch (socket, &winsock_source->condition);
socket_source->socket = g_object_ref (socket);
socket_source->condition = condition;
if (g_cancellable_make_pollfd (cancellable,
&winsock_source->cancel_pollfd))
&socket_source->cancel_pollfd))
{
winsock_source->cancellable = g_object_ref (cancellable);
g_source_add_poll (source, &winsock_source->cancel_pollfd);
socket_source->cancellable = g_object_ref (cancellable);
g_source_add_poll (source, &socket_source->cancel_pollfd);
}
winsock_source->pollfd.fd = (gintptr) socket->priv->event;
winsock_source->pollfd.events = condition;
g_source_add_poll (source, &winsock_source->pollfd);
#ifdef G_OS_WIN32
add_condition_watch (socket, &socket_source->condition);
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;
}
#endif
/**
* g_socket_create_source:
@ -2336,7 +2329,7 @@ winsock_source_new (GSocket *socket,
*
* 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.
*
* @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,
GCancellable *cancellable)
{
GSource *source;
g_return_val_if_fail (G_IS_SOCKET (socket) && (cancellable == NULL || G_IS_CANCELLABLE (cancellable)), NULL);
#ifdef G_OS_WIN32
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;
return socket_source_new (socket, condition, cancellable);
}
/**