Import GInitable, GSocket and dependencies from gnio

This adds:
GInitable - failable object constructor interface
GAsyncInitable - async failable object constructor interface
GSocket - Platform independent lowlevel berkely socket style object
GSocketControlMessage - For passing control messages over GSocket
GUnixFDMessage - unix fd passing socket control message

Some changes were done during the import from gnio to make things
work in glib. For instance, types were moved to other headers, header
file boiler plate were updated to glib style and gio.symbols stuff
was added.
This commit is contained in:
Alexander Larsson 2009-05-14 15:26:37 +02:00
parent 33c00e5c33
commit 145cec3c93
17 changed files with 4833 additions and 1 deletions

View File

@ -975,6 +975,14 @@ if $glib_failed ; then
AC_MSG_ERROR([Could not determine values for AF_INET* constants])
fi
glib_failed=false
GLIB_CHECK_VALUE(MSG_PEEK, $glib_inet_includes, glib_failed=true)
GLIB_CHECK_VALUE(MSG_OOB, $glib_inet_includes, glib_failed=true)
GLIB_CHECK_VALUE(MSG_DONTROUTE, $glib_inet_includes, glib_failed=true)
if $glib_failed ; then
AC_MSG_ERROR([Could not determine values for MSG_* constants])
fi
AC_CHECK_FUNCS(getprotobyname_r endservent)
AC_CHECK_HEADERS([netdb.h winsock2.h mswsock.h])
@ -3038,6 +3046,10 @@ typedef $g_pid_type GPid;
#define GLIB_SYSDEF_AF_INET $g_af_inet
#define GLIB_SYSDEF_AF_INET6 $g_af_inet6
#define GLIB_SYSDEF_MSG_OOB $g_msg_oob
#define GLIB_SYSDEF_MSG_PEEK $g_msg_peek
#define GLIB_SYSDEF_MSG_DONTROUTE $g_msg_dontroute
G_END_DECLS
#endif /* GLIBCONFIG_H */
@ -3328,6 +3340,10 @@ g_af_unix=$glib_cv_value_AF_UNIX
g_af_inet=$glib_cv_value_AF_INET
g_af_inet6=$glib_cv_value_AF_INET6
g_msg_peek=$glib_cv_value_MSG_PEEK
g_msg_oob=$glib_cv_value_MSG_OOB
g_msg_dontroute=$glib_cv_value_MSG_DONTROUTE
g_stack_grows=$glib_cv_stack_grows
g_have_eilseq=$have_eilseq

View File

@ -130,6 +130,7 @@ appinfo_sources += gdesktopappinfo.c gdesktopappinfo.h
platform_libadd += libasyncns/libasyncns.la xdgmime/libxdgmime.la
platform_deps += libasyncns/libasyncns.la xdgmime/libxdgmime.la
unix_sources = \
gunixfdmessage.c \
gunixmount.c \
gunixmount.h \
gunixmounts.c \
@ -150,6 +151,7 @@ giounixincludedir=$(includedir)/gio-unix-2.0/gio
giounixinclude_HEADERS = \
gdesktopappinfo.h \
gunixmounts.h \
gunixfdmessage.h \
gunixinputstream.h \
gunixoutputstream.h \
gunixsocketaddress.h \
@ -176,6 +178,7 @@ libgio_2_0_la_SOURCES = \
gappinfo.c \
gasynchelper.c \
gasynchelper.h \
gasyncinitable.c \
gasyncresult.c \
gbufferedinputstream.c \
gbufferedoutputstream.c \
@ -207,6 +210,7 @@ libgio_2_0_la_SOURCES = \
gicon.c \
ginetaddress.c \
ginetsocketaddress.c \
ginitable.c \
ginputstream.c \
gioenums.h \
gioerror.c \
@ -230,6 +234,8 @@ libgio_2_0_la_SOURCES = \
gresolver.c \
gseekable.c \
gsimpleasyncresult.c \
gsocket.c \
gsocketcontrolmessage.c \
gsocketaddress.c \
gsocketaddressenumerator.c \
gsocketconnectable.c \
@ -299,6 +305,7 @@ libgio_2_0_la_DEPENDENCIES = $(gio_def) $(platform_deps)
gio_headers = \
gappinfo.h \
gasyncinitable.h \
gasyncresult.h \
gbufferedinputstream.h \
gbufferedoutputstream.h \
@ -325,6 +332,7 @@ gio_headers = \
ginetaddress.h \
ginetsocketaddress.h \
ginputstream.h \
ginitable.h \
gio.h \
giotypes.h \
gioenums.h \
@ -344,6 +352,8 @@ gio_headers = \
gresolver.h \
gseekable.h \
gsimpleasyncresult.h \
gsocket.h \
gsocketcontrolmessage.h \
gsocketaddress.h \
gsocketaddressenumerator.h \
gsocketconnectable.h \

383
gio/gasyncinitable.c Normal file
View File

@ -0,0 +1,383 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2009 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#include "config.h"
#include "gasyncinitable.h"
#include "gasyncresult.h"
#include "gsimpleasyncresult.h"
#include "glibintl.h"
#include "gioalias.h"
/**
* SECTION:gasyncinitable
* @short_description: Asynchronously failable object initialization interface
* @include: gio/gio.h
* @see_also: #GInitable
*
* This is the asynchronous version of #GInitable, it behaves the same
* in all ways except that initialization is asynchronous. For more details
* see the descriptions on #GInitable.
*
* A class may implement both the #GInitable and #GAsyncInitable interfaces
* or both.
*
* Users of objects implementing this are not intended to use
* the interface method directly, instead it will be used automatically
* in various ways. For C applications you generally just call
* g_async_initable_new() directly, or indirectly via a foo_thing_new_async() wrapper.
* This will call g_async_initable_init() under the cover, calling back with %NULL and
* a set %GError on failure.
**/
static void g_async_initable_base_init (gpointer g_iface);
static void g_async_initable_real_init_async (GAsyncInitable *initable,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static gboolean g_async_initable_real_init_finish (GAsyncInitable *initable,
GAsyncResult *res,
GError **error);
GType
g_async_initable_get_type (void)
{
static volatile gsize g_define_type_id__volatile = 0;
if (g_once_init_enter (&g_define_type_id__volatile))
{
const GTypeInfo initable_info =
{
sizeof (GAsyncInitableIface), /* class_size */
g_async_initable_base_init, /* base_init */
NULL, /* base_finalize */
NULL,
NULL, /* class_finalize */
NULL, /* class_data */
0,
0, /* n_preallocs */
NULL
};
GType g_define_type_id =
g_type_register_static (G_TYPE_INTERFACE, I_("GAsyncInitable"),
&initable_info, 0);
g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
}
return g_define_type_id__volatile;
}
static void
g_async_initable_base_init (gpointer g_iface)
{
GAsyncInitableIface *iface = g_iface;
iface->init_async = g_async_initable_real_init_async;
iface->init_finish = g_async_initable_real_init_finish;
}
/**
* g_async_initable_init_async:
* @initable: a #GAsyncInitable.
* @io_priority: the <link linkend="io-priority">I/O priority</link>
* of the operation.
* @cancellable: optional #GCancellable object, %NULL to ignore.
* @callback: a #GAsyncReadyCallback to call when the request is satisfied
* @user_data: the data to pass to callback function
*
* Starts asynchronous initialization of the object implementing the interface.
* This must be done before any real use of the object after initial construction.
* If the object also implements #GInitable you can optionally call g_initable_init()
* instead.
*
* When the initialization is finished, @callback will be called. You can then call
* g_async_initable_init_finish() to get the result of the initialization.
*
* Implementations may also support cancellation. If @cancellable is not %NULL,
* then initialization can be cancelled by triggering the cancellable object
* from another thread. If the operation was cancelled, the error
* %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL and
* the object doesn't support cancellable initialization the error
* %G_IO_ERROR_NOT_SUPPORTED will be returned.
*
* If this function is not called, or returns with an error then all
* operations on the object should fail, generally returning the
* error %G_IO_ERROR_NOT_INITIALIZED.
*
* Implementations of this method must be idempotent, i.e. multiple calls
* to this function with the same argument should return the same results.
* Only the first call initializes the object, further calls return the result
* of the first call. This is so that its safe to implement the singleton
* pattern in the GObject constructor function.
*
* For classes that also support the #GInitable interface the default
* implementation of this method will run the g_initable_init() function
* in a thread, so if you want to support asynchronous initialization via
* threads, just implement the #GAsyncInitable interface without overriding
* any interface methods.
*
* Since: 2.22
**/
void
g_async_initable_init_async (GAsyncInitable *initable,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GAsyncInitableIface *iface;
g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
iface = G_ASYNC_INITABLE_GET_IFACE (initable);
return (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
}
/**
* g_async_initable_init_finish:
* @initable: a #GAsyncInitable.
* @res: a #GAsyncResult.
* @error: a #GError location to store the error occuring, or %NULL to
* ignore.
*
* Finishes asynchronous initialization and returns the result.
* See g_async_initable_init_async().
*
* Returns: %TRUE if successful. If an error
* has occurred, this function will return %FALSE and set @error
* appropriately if present.
*
* Since: 2.22
**/
gboolean
g_async_initable_init_finish (GAsyncInitable *initable,
GAsyncResult *res,
GError **error)
{
GAsyncInitableIface *iface;
g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
if (G_IS_SIMPLE_ASYNC_RESULT (res))
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
if (g_simple_async_result_propagate_error (simple, error))
return FALSE;
}
iface = G_ASYNC_INITABLE_GET_IFACE (initable);
return (* iface->init_finish) (initable, res, error);
}
static void
async_init_thread (GSimpleAsyncResult *res,
GObject *object,
GCancellable *cancellable)
{
GError *error = NULL;
if (!g_initable_init (G_INITABLE (res), cancellable, &error))
{
g_simple_async_result_set_from_error (res, error);
g_error_free (error);
}
}
static void
g_async_initable_real_init_async (GAsyncInitable *initable,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *res;
g_return_if_fail (G_IS_INITABLE (initable));
res = g_simple_async_result_new (G_OBJECT (initable), callback, user_data,
g_async_initable_real_init_async);
g_simple_async_result_run_in_thread (res, async_init_thread,
io_priority, cancellable);
g_object_unref (res);
}
static gboolean
g_async_initable_real_init_finish (GAsyncInitable *initable,
GAsyncResult *res,
GError **error)
{
return TRUE; /* Errors handled by base impl */
}
/**
* g_async_initable_new_async:
* @object_type: a #GType supporting #GAsyncInitable.
* @io_priority: the <link linkend="io-priority">I/O priority</link>
* of the operation.
* @cancellable: optional #GCancellable object, %NULL to ignore.
* @callback: a #GAsyncReadyCallback to call when the initialization is finished
* @user_data: the data to pass to callback function
* @first_property_name: the name of the first property, followed by
* the value, and other property value pairs, and ended by %NULL.
*
* Helper function for constructing #GAsyncInitiable object. This is
* similar to g_object_new() but also initializes the object asyncronously.
*
* When the initialization is finished, @callback will be called. You can then call
* g_async_initable_new_finish() to get new object and check for any errors.
*
* Since: 2.22
**/
void
g_async_initable_new_async (GType object_type,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data,
const gchar *first_property_name,
...)
{
va_list var_args;
va_start (var_args, first_property_name);
g_async_initable_new_valist_async (object_type,
first_property_name, var_args,
io_priority, cancellable,
callback, user_data);
va_end (var_args);
}
/**
* g_async_initable_newv_async:
* @object_type: a #GType supporting #GAsyncInitable.
* @n_parameters: the number of parameters in @parameters
* @parameters: the parameters to use to construct the object
* @io_priority: the <link linkend="io-priority">I/O priority</link>
* of the operation.
* @cancellable: optional #GCancellable object, %NULL to ignore.
* @callback: a #GAsyncReadyCallback to call when the initialization is finished
* @user_data: the data to pass to callback function
*
* Helper function for constructing #GAsyncInitiable object. This is
* similar to g_object_newv() but also initializes the object asyncronously.
*
* When the initialization is finished, @callback will be called. You can then call
* g_async_initable_new_finish() to get new object and check for any errors.
*
* Since: 2.22
**/
void
g_async_initable_newv_async (GType object_type,
guint n_parameters,
GParameter *parameters,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GObject *obj;
g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
obj = g_object_newv (object_type, n_parameters, parameters);
g_async_initable_init_async (G_ASYNC_INITABLE (obj),
io_priority, cancellable,
callback, user_data);
}
/**
* g_async_initable_new_async:
* @object_type: a #GType supporting #GAsyncInitable.
* @first_property_name: the name of the first property, followed by
* the value, and other property value pairs, and ended by %NULL.
* @var_args: The var args list generated from @first_property_name.
* @io_priority: the <link linkend="io-priority">I/O priority</link>
* of the operation.
* @cancellable: optional #GCancellable object, %NULL to ignore.
* @callback: a #GAsyncReadyCallback to call when the initialization is finished
* @user_data: the data to pass to callback function
*
* Helper function for constructing #GAsyncInitiable object. This is
* similar to g_object_new_valist() but also initializes the object asyncronously.
*
* When the initialization is finished, @callback will be called. You can then call
* g_async_initable_new_finish() to get new object and check for any errors.
*
* Since: 2.22
**/
void
g_async_initable_new_valist_async (GType object_type,
const gchar *first_property_name,
va_list var_args,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GObject *obj;
g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
obj = g_object_new_valist (object_type,
first_property_name,
var_args);
g_async_initable_init_async (G_ASYNC_INITABLE (obj),
io_priority, cancellable,
callback, user_data);
g_object_unref (obj); /* Passed ownership to async call */
}
/**
* g_async_initable_new_finish:
* @initable: the #GAsyncInitable from the callback
* @res: the #GAsyncResult.from the callback
* @error: a #GError location to store the error occuring, or %NULL to
* ignore.
*
* Finishes the async construction for the various g_async_initable_new calls,
* returning the created object or %NULL on error.
*
* Returns: a newly created #GObject, or %NULL on error. Free with g_object_unref().
*
* Since: 2.22
**/
GObject *
g_async_initable_new_finish (GAsyncInitable *initable,
GAsyncResult *res,
GError **error)
{
if (g_async_initable_init_finish (initable, res, error))
return g_object_ref (initable);
else
return NULL;
}
#define __G_ASYNC_INITABLE_C__
#include "gioaliasdef.c"

119
gio/gasyncinitable.h Normal file
View File

@ -0,0 +1,119 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2009 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#ifndef __G_ASYNC_INITABLE_H__
#define __G_ASYNC_INITABLE_H__
#include <gio/giotypes.h>
#include <gio/ginitable.h>
G_BEGIN_DECLS
#define G_TYPE_ASYNC_INITABLE (g_async_initable_get_type ())
#define G_ASYNC_INITABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_ASYNC_INITABLE, GAsyncInitable))
#define G_IS_ASYNC_INITABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_TYPE_ASYNC_INITABLE))
#define G_ASYNC_INITABLE_GET_IFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), G_TYPE_ASYNC_INITABLE, GAsyncInitableIface))
#define G_TYPE_IS_ASYNC_INITABLE(type) (g_type_is_a ((type), G_TYPE_ASYNC_INITABLE))
/**
* GAsyncInitable:
*
* Interface for asynchronously initializable objects.
*
* Since: 2.22
**/
typedef struct _GAsyncInitableIface GAsyncInitableIface;
/**
* GAsyncInitableIface:
* @g_iface: The parent interface.
* @init_async: Starts initialization of the object.
* @init_finish: Finishes initialization of the object.
*
* Provides an interface for asynchronous initializing object such that
* initialization may fail.
*
* Since: 2.22
**/
struct _GAsyncInitableIface
{
GTypeInterface g_iface;
/* Virtual Table */
void (* init_async) (GAsyncInitable *initable,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
gboolean (* init_finish) (GAsyncInitable *initable,
GAsyncResult *res,
GError **error);
};
GType g_async_initable_get_type (void) G_GNUC_CONST;
void g_async_initable_init_async (GAsyncInitable *initable,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
gboolean g_async_initable_init_finish (GAsyncInitable *initable,
GAsyncResult *res,
GError **error);
void g_async_initable_new_async (GType object_type,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data,
const gchar *first_property_name,
...);
void g_async_initable_newv_async (GType object_type,
guint n_parameters,
GParameter *parameters,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
void g_async_initable_new_valist_async (GType object_type,
const gchar *first_property_name,
va_list var_args,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
GObject *g_async_initable_new_finish (GAsyncInitable *initable,
GAsyncResult *res,
GError **error);
G_END_DECLS
#endif /* __G_ASYNC_INITABLE_H__ */

251
gio/ginitable.c Normal file
View File

@ -0,0 +1,251 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2009 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#include "config.h"
#include "ginitable.h"
#include "glibintl.h"
#include "gioalias.h"
/**
* SECTION:ginitable
* @short_description: Failable object initialization interface
* @include: gio/gio.h
* @see_also: #GAsyncInitable
*
* #GInitable is implemented by objects that can fail during
* initialization. If an object implements this interface the
* g_initable_init() function must be called as the first thing
* after construction. If g_initable_init() is not called, or if
* it returns an error, all further operations on the object
* should fail, generally with a %G_IO_ERROR_NOT_INITIALIZED error.
*
* Users of objects implementing this are not intended to use
* the interface method directly, instead it will be used automatically
* in various ways. For C applications you generally just call
* g_initable_new() directly, or indirectly via a foo_thing_new() wrapper.
* This will call g_initable_init() under the cover, returning %NULL and
* setting a %GError on failure.
*
* For bindings in languages where the native constructor supports
* exceptions the binding could check for objects implemention %GInitable
* during normal construction and automatically initialize them, throwing
* an exception on failure.
**/
GType
g_initable_get_type (void)
{
static volatile gsize g_define_type_id__volatile = 0;
if (g_once_init_enter (&g_define_type_id__volatile))
{
const GTypeInfo initable_info =
{
sizeof (GInitableIface), /* class_size */
NULL, /* base_init */
NULL, /* base_finalize */
NULL,
NULL, /* class_finalize */
NULL, /* class_data */
0,
0, /* n_preallocs */
NULL
};
GType g_define_type_id =
g_type_register_static (G_TYPE_INTERFACE, I_("GInitable"),
&initable_info, 0);
g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
}
return g_define_type_id__volatile;
}
/**
* g_initable_init:
* @initable: a #GInitable.
* @cancellable: optional #GCancellable object, %NULL to ignore.
* @error: a #GError location to store the error occuring, or %NULL to
* ignore.
*
* Initializes the object implementing the interface. This must be
* done before any real use of the object after initial construction.
*
* Implementations may also support cancellation. If @cancellable is not %NULL,
* then initialization can be cancelled by triggering the cancellable object
* from another thread. If the operation was cancelled, the error
* %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL and
* the object doesn't support cancellable initialization the error
* %G_IO_ERROR_NOT_SUPPORTED will be returned.
*
* If this function is not called, or returns with an error then all
* operations on the object should fail, generally returning the
* error %G_IO_ERROR_NOT_INITIALIZED.
*
* Implementations of this method must be idempotent, i.e. multiple calls
* to this function with the same argument should return the same results.
* Only the first call initializes the object, further calls return the result
* of the first call. This is so that its safe to implement the singleton
* pattern in the GObject constructor function.
*
* Returns: %TRUE if successful. If an error
* has occurred, this function will return %FALSE and set @error
* appropriately if present.
*
* Since: 2.22
**/
gboolean
g_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
GInitableIface *iface;
g_return_val_if_fail (G_IS_INITABLE (initable), FALSE);
iface = G_INITABLE_GET_IFACE (initable);
return (* iface->init) (initable, cancellable, error);
}
/**
* g_initable_new:
* @object_type: a #GType supporting #GInitable.
* @cancellable: optional #GCancellable object, %NULL to ignore.
* @error: a #GError location to store the error occuring, or %NULL to
* ignore.
* @first_property_name: the name of the first property, followed by
* the value, and other property value pairs, and ended by %NULL.
*
* Helper function for constructing #GInitiable object. This is
* similar to g_object_new() but also initializes the object
* and returns %NULL, setting an error on failure.
*
* Return value: a newly allocated #GObject, or %NULL on error
*
* Since: 2.22
**/
gpointer
g_initable_new (GType object_type,
GCancellable *cancellable,
GError **error,
const gchar *first_property_name,
...)
{
GObject *object;
va_list var_args;
va_start (var_args, first_property_name);
object = g_initable_new_valist (object_type,
first_property_name, var_args,
cancellable, error);
va_end (var_args);
return object;
}
/**
* g_initable_new:
* @object_type: a #GType supporting #GInitable.
* @n_parameters: the number of parameters in @parameters
* @parameters: the parameters to use to construct the object
* @cancellable: optional #GCancellable object, %NULL to ignore.
* @error: a #GError location to store the error occuring, or %NULL to
* ignore.
*
* Helper function for constructing #GInitiable object. This is
* similar to g_object_newv() but also initializes the object
* and returns %NULL, setting an error on failure.
*
* Return value: a newly allocated #GObject, or %NULL on error
*
* Since: 2.22
**/
gpointer
g_initable_newv (GType object_type,
guint n_parameters,
GParameter *parameters,
GCancellable *cancellable,
GError **error)
{
GObject *obj;
g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
obj = g_object_newv (object_type, n_parameters, parameters);
if (!g_initable_init (G_INITABLE (obj), cancellable, error))
{
g_object_unref (obj);
return NULL;
}
return (gpointer)obj;
}
/**
* g_initable_new_valist:
* @object_type: a #GType supporting #GInitable.
* @first_property_name: the name of the first property, followed by
* the value, and other property value pairs, and ended by %NULL.
* @var_args: The var args list generated from @first_property_name.
* @cancellable: optional #GCancellable object, %NULL to ignore.
* @error: a #GError location to store the error occuring, or %NULL to
* ignore.
*
* Helper function for constructing #GInitiable object. This is
* similar to g_object_new_valist() but also initializes the object
* and returns %NULL, setting an error on failure.
*
* Return value: a newly allocated #GObject, or %NULL on error
*
* Since: 2.22
**/
GObject*
g_initable_new_valist (GType object_type,
const gchar *first_property_name,
va_list var_args,
GCancellable *cancellable,
GError **error)
{
GObject *obj;
g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
obj = g_object_new_valist (object_type,
first_property_name,
var_args);
if (!g_initable_init (G_INITABLE (obj), cancellable, error))
{
g_object_unref (obj);
return NULL;
}
return obj;
}
#define __G_INITABLE_C__
#include "gioaliasdef.c"

95
gio/ginitable.h Normal file
View File

@ -0,0 +1,95 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2009 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#ifndef __G_INITABLE_H__
#define __G_INITABLE_H__
#include <gio/giotypes.h>
G_BEGIN_DECLS
#define G_TYPE_INITABLE (g_initable_get_type ())
#define G_INITABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_INITABLE, GInitable))
#define G_IS_INITABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_TYPE_INITABLE))
#define G_INITABLE_GET_IFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), G_TYPE_INITABLE, GInitableIface))
#define G_TYPE_IS_INITABLE(type) (g_type_is_a ((type), G_TYPE_INITABLE))
/**
* GInitable:
*
* Interface for initializable objects.
*
* Since: 2.22
**/
typedef struct _GInitableIface GInitableIface;
/**
* GInitableIface:
* @g_iface: The parent interface.
* @init: Initializes the object.
*
* Provides an interface for initializing object such that initialization may fail.
*
* Since: 2.22
**/
struct _GInitableIface
{
GTypeInterface g_iface;
/* Virtual Table */
gboolean (* init) (GInitable *initable,
GCancellable *cancellable,
GError **error);
};
GType g_initable_get_type (void) G_GNUC_CONST;
gboolean g_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error);
gpointer g_initable_new (GType object_type,
GCancellable *cancellable,
GError **error,
const gchar *first_property_name,
...);
gpointer g_initable_newv (GType object_type,
guint n_parameters,
GParameter *parameters,
GCancellable *cancellable,
GError **error);
GObject* g_initable_new_valist (GType object_type,
const gchar *first_property_name,
va_list var_args,
GCancellable *cancellable,
GError **error);
G_END_DECLS
#endif /* __G_INITABLE_H__ */

View File

@ -29,6 +29,7 @@
#include <gio/gappinfo.h>
#include <gio/gasyncresult.h>
#include <gio/gasyncinitable.h>
#include <gio/gbufferedinputstream.h>
#include <gio/gbufferedoutputstream.h>
#include <gio/gcancellable.h>
@ -56,6 +57,7 @@
#include <gio/gioenumtypes.h>
#include <gio/gioerror.h>
#include <gio/giomodule.h>
#include <gio/ginitable.h>
#include <gio/gioscheduler.h>
#include <gio/giostream.h>
#include <gio/gfileiostream.h>
@ -71,6 +73,8 @@
#include <gio/gresolver.h>
#include <gio/gseekable.h>
#include <gio/gsimpleasyncresult.h>
#include <gio/gsocket.h>
#include <gio/gsocketcontrolmessage.h>
#include <gio/gsocketaddress.h>
#include <gio/gsocketaddressenumerator.h>
#include <gio/gsocketconnectable.h>

View File

@ -868,6 +868,8 @@ g_ask_password_flags_get_type G_GNUC_CONST
g_password_save_get_type G_GNUC_CONST
g_emblem_origin_get_type G_GNUC_CONST
g_socket_family_get_type G_GNUC_CONST
g_socket_type_get_type G_GNUC_CONST
g_socket_msg_flags_get_type G_GNUC_CONST
g_resolver_error_get_type G_GNUC_CONST
#endif
#endif
@ -1035,3 +1037,82 @@ g_socket_address_enumerator_next_async
g_socket_address_enumerator_next_finish
#endif
#endif
#if IN_HEADER(__G_ASYNC_INITABLE_H__)
#if IN_FILE(__G_ASYNC_INITABLE_C__)
g_async_initable_get_type G_GNUC_CONST
g_async_initable_init_async
g_async_initable_init_finish
g_async_initable_new_async
g_async_initable_new_finish
g_async_initable_new_valist_async
g_async_initable_newv_async
#endif
#endif
#if IN_HEADER(__G_INITABLE_H__)
#if IN_FILE(__G_INITABLE_C__)
g_initable_get_type G_GNUC_CONST
g_initable_init
g_initable_new
g_initable_newv
g_initable_new_valist
#endif
#endif
#if IN_HEADER(__G_SOCKET_H__)
#if IN_FILE(__G_SOCKET_C__)
g_socket_get_type G_GNUC_CONST
g_socket_accept
g_socket_bind
g_socket_check_pending_error
g_socket_close
g_socket_condition_check
g_socket_condition_wait
g_socket_connect
g_socket_create_source
g_socket_get_blocking
g_socket_get_family
g_socket_get_fd
g_socket_get_keepalive
g_socket_get_listen_backlog
g_socket_get_local_address
g_socket_get_protocol
g_socket_get_remote_address
g_socket_get_socket_type
g_socket_is_closed
g_socket_is_connected
g_socket_listen
g_socket_new
g_socket_new_from_fd
g_socket_receive
g_socket_receive_from
g_socket_receive_message
g_socket_send
g_socket_send_message
g_socket_send_to
g_socket_set_blocking
g_socket_set_keepalive
g_socket_set_listen_backlog
#endif
#endif
#if IN_HEADER(__G_SOCKET_CONTROL_MESSAGE_H__)
#if IN_FILE(__G_SOCKET_CONTROL_MESSAGE_C__)
g_socket_control_message_get_type G_GNUC_CONST
g_socket_control_message_deserialize
g_socket_control_message_get_level
g_socket_control_message_get_msg_type
g_socket_control_message_get_size
g_socket_control_message_serialize
#endif
#endif
#if IN_HEADER(__G_UNIX_FD_MESSAGE_H__)
#if IN_FILE(__G_UNIX_FD_MESSAGE_C__)
g_unix_fd_message_get_type G_GNUC_CONST
g_unix_fd_message_append_fd
g_unix_fd_message_new
g_unix_fd_message_steal_fds
#endif
#endif

View File

@ -342,6 +342,8 @@ typedef enum {
* @G_IO_ERROR_TOO_MANY_OPEN_FILES: The current process has too many files
* open and can't open any more. Duplicate descriptors do count toward
* this limit. Since 2.20
* @G_IO_ERROR_NOT_INITIALIZED: The object has not been initialized. Since 2.22
* @G_IO_ERROR_ADDRESS_IN_USE: The requested address is already in use. Since 2.22
*
* Error codes returned by GIO functions.
*
@ -378,7 +380,9 @@ typedef enum {
G_IO_ERROR_HOST_NOT_FOUND,
G_IO_ERROR_WOULD_MERGE,
G_IO_ERROR_FAILED_HANDLED,
G_IO_ERROR_TOO_MANY_OPEN_FILES
G_IO_ERROR_TOO_MANY_OPEN_FILES,
G_IO_ERROR_NOT_INITIALIZED,
G_IO_ERROR_ADDRESS_IN_USE
} GIOErrorEnum;
@ -511,6 +515,46 @@ typedef enum {
G_SOCKET_FAMILY_IPV6 = GLIB_SYSDEF_AF_INET6
} GSocketFamily;
/**
* GSocketType:
* @G_SOCKET_TYPE_INVALID: Type unknown or wrong
* @G_SOCKET_TYPE_STREAM: Reliable connection-based byte streams (e.g. TCP).
* @G_SOCKET_TYPE_DATAGRAM: Connectionless, unreliable datagram passing. (e.g. UDP)
* @G_SOCKET_TYPE_SEQPACKET: Reliable connection-based passing of datagrams of fixed maximum length (e.g. SCTP).
*
* Flags used when creating a #GSocket. Some protocols may not implement all the socket types.
*
* Since: 2.22
*/
typedef enum
{
G_SOCKET_TYPE_INVALID,
G_SOCKET_TYPE_STREAM,
G_SOCKET_TYPE_DATAGRAM,
G_SOCKET_TYPE_SEQPACKET,
} GSocketType;
/**
* GSocketMsgFlags:
* @G_SOCKET_MSG_OOB: Request to send/receive out of band data.
* @G_SOCKET_MSG_PEEK: Read data from the socket without removing it from the queue.
* @G_SOCKET_MSG_DONTROUTE: Don't use a gateway to send out the packet, only send to hosts on directly connected networks.
*
* Flags used in g_socket_receive_message() and g_socket_send_message(). The flags listed in the enum are
* some commonly available flags, but the values used for them are the same as on the platform, and any other
* flags are passed in/out as is. So to use a platform specific flag, just include the right system header and
* pass in the flag.
*
* Since: 2.22
*/
typedef enum
{
G_SOCKET_MSG_INVALID,
G_SOCKET_MSG_OOB = GLIB_SYSDEF_MSG_OOB,
G_SOCKET_MSG_PEEK = GLIB_SYSDEF_MSG_PEEK,
G_SOCKET_MSG_DONTROUTE = GLIB_SYSDEF_MSG_DONTROUTE
} GSocketMsgFlags;
G_END_DECLS
#endif /* __GIO_ENUMS_H__ */

View File

@ -194,6 +194,12 @@ g_io_error_from_errno (gint err_no)
break;
#endif
#ifdef EADDRINUSE
case EADDRINUSE:
return G_IO_ERROR_ADDRESS_IN_USE;
break;
#endif
default:
return G_IO_ERROR_FAILED;
break;

View File

@ -34,6 +34,7 @@ G_BEGIN_DECLS
typedef struct _GAppLaunchContext GAppLaunchContext;
typedef struct _GAppInfo GAppInfo; /* Dummy typedef */
typedef struct _GAsyncResult GAsyncResult; /* Dummy typedef */
typedef struct _GAsyncInitable GAsyncInitable;
typedef struct _GBufferedInputStream GBufferedInputStream;
typedef struct _GBufferedOutputStream GBufferedOutputStream;
typedef struct _GCancellable GCancellable;
@ -79,6 +80,7 @@ typedef struct _GIcon GIcon; /* Dummy typedef */
typedef struct _GInetAddress GInetAddress;
typedef struct _GInetSocketAddress GInetSocketAddress;
typedef struct _GInputStream GInputStream;
typedef struct _GInitable GInitable;
typedef struct _GIOModule GIOModule;
typedef struct _GIOExtensionPoint GIOExtensionPoint;
typedef struct _GIOExtension GIOExtension;
@ -107,6 +109,23 @@ typedef struct _GIOStream GIOStream;
typedef struct _GResolver GResolver;
typedef struct _GSeekable GSeekable;
typedef struct _GSimpleAsyncResult GSimpleAsyncResult;
/**
* GSocket:
*
* A lowlevel network socket object.
*
* Since: 2.22
**/
typedef struct _GSocket GSocket;
/**
* GSocketControlMessage:
*
* Base class for socket-type specific control messages that can be sent and
* received over #GSocket.
**/
typedef struct _GSocketControlMessage GSocketControlMessage;
typedef struct _GSocketAddress GSocketAddress;
typedef struct _GSocketAddressEnumerator GSocketAddressEnumerator;
typedef struct _GSocketConnectable GSocketConnectable;
@ -202,6 +221,46 @@ typedef void (*GSimpleAsyncThreadFunc) (GSimpleAsyncResult *res,
GObject *object,
GCancellable *cancellable);
/**
* GSocketSourceFunc:
* @user_data: data passed in by the user.
* @condition: the current condition at the source fired.
*
* This is the function type of the callback used for the #GSource
* returned by g_socket_create_source().
*
* Since: 2.22
*/
typedef gboolean (*GSocketSourceFunc) (gpointer user_data,
GIOCondition condition);
/**
* GInputVector:
*
* Structure used for scatter/gather data input.
*
* Since: 2.22
*/
typedef struct
{
gpointer buffer;
gsize size;
} GInputVector;
/**
* GOutputVector:
*
* Structure used for scatter/gather data output.
*
* Since: 2.22
*/
typedef struct
{
gconstpointer buffer;
gsize size;
} GOutputVector;
G_END_DECLS
#endif /* __GIO_TYPES_H__ */

2970
gio/gsocket.c Normal file

File diff suppressed because it is too large Load Diff

172
gio/gsocket.h Normal file
View File

@ -0,0 +1,172 @@
/*
* Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
* Copyright © 2009 Codethink Limited
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Christian Kellner <gicmo@gnome.org>
* Samuel Cormier-Iijima <sciyoshi@gmail.com>
* Ryan Lortie <desrt@desrt.ca>
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#ifndef __G_SOCKET_H__
#define __G_SOCKET_H__
#include <gio/giotypes.h>
G_BEGIN_DECLS
#define G_TYPE_SOCKET (g_socket_get_type ())
#define G_SOCKET(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
G_TYPE_SOCKET, GSocket))
#define G_SOCKET_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), \
G_TYPE_SOCKET, GSocketClass))
#define G_IS_SOCKET(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
G_TYPE_SOCKET))
#define G_IS_SOCKET_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), \
G_TYPE_SOCKET))
#define G_SOCKET_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), \
G_TYPE_SOCKET, GSocketClass))
typedef struct _GSocketPrivate GSocketPrivate;
typedef struct _GSocketClass GSocketClass;
struct _GSocketClass
{
GObjectClass parent_class;
/*< private >*/
/* Padding for future expansion */
void (*_g_reserved1) (void);
void (*_g_reserved2) (void);
void (*_g_reserved3) (void);
void (*_g_reserved4) (void);
void (*_g_reserved5) (void);
void (*_g_reserved6) (void);
void (*_g_reserved7) (void);
void (*_g_reserved8) (void);
void (*_g_reserved9) (void);
void (*_g_reserved10) (void);
};
struct _GSocket
{
GObject parent_instance;
GSocketPrivate *priv;
};
GType g_socket_get_type (void) G_GNUC_CONST;
GSocket * g_socket_new (GSocketFamily family,
GSocketType type,
const char *protocol,
GError **error);
GSocket * g_socket_new_from_fd (gint fd,
GError **error);
int g_socket_get_fd (GSocket *socket);
GSocketFamily g_socket_get_family (GSocket *socket);
GSocketType g_socket_get_socket_type (GSocket *socket);
const char * g_socket_get_protocol (GSocket *socket);
GSocketAddress * g_socket_get_local_address (GSocket *socket,
GError **error);
GSocketAddress * g_socket_get_remote_address (GSocket *socket,
GError **error);
void g_socket_set_blocking (GSocket *socket,
gboolean blocking);
gboolean g_socket_get_blocking (GSocket *socket);
void g_socket_set_reuse_address (GSocket *socket,
gboolean reuse);
gboolean g_socket_get_reuse_address (GSocket *socket);
void g_socket_set_keepalive (GSocket *socket,
gboolean keepalive);
gboolean g_socket_get_keepalive (GSocket *socket);
gint g_socket_get_listen_backlog (GSocket *socket);
void g_socket_set_listen_backlog (GSocket *socket,
gint backlog);
gboolean g_socket_is_connected (GSocket *socket);
gboolean g_socket_bind (GSocket *socket,
GSocketAddress *address,
gboolean allow_reuse,
GError **error);
gboolean g_socket_connect (GSocket *socket,
GSocketAddress *address,
GError **error);
gboolean g_socket_check_pending_error (GSocket *socket,
GError **error);
GIOCondition g_socket_condition_check (GSocket *socket,
GIOCondition condition);
gboolean g_socket_condition_wait (GSocket *socket,
GIOCondition condition,
GCancellable *cancellable,
GError **error);
GSocket * g_socket_accept (GSocket *socket,
GError **error);
gboolean g_socket_listen (GSocket *socket,
GError **error);
gssize g_socket_receive (GSocket *socket,
gchar *buffer,
gsize size,
GError **error);
gssize g_socket_receive_from (GSocket *socket,
GSocketAddress **address,
gchar *buffer,
gsize size,
GError **error);
gssize g_socket_send (GSocket *socket,
const gchar *buffer,
gsize size,
GError **error);
gssize g_socket_send_to (GSocket *socket,
GSocketAddress *address,
const gchar *buffer,
gsize size,
GError **error);
GSocketControlMessage *g_socket_receive_control_message (GSocket *socket,
GError **error);
gboolean g_socket_send_control_message (GSocket *socket,
GSocketControlMessage *message,
GError **error);
gssize g_socket_receive_message (GSocket *socket,
GSocketAddress **address,
GInputVector *vectors,
gint num_vectors,
GSocketControlMessage ***messages,
gint *num_messages,
gint *flags,
GError **error);
gssize g_socket_send_message (GSocket *socket,
GSocketAddress *address,
GOutputVector *vectors,
gint num_vectors,
GSocketControlMessage **messages,
gint num_messages,
gint flags,
GError **error);
gboolean g_socket_close (GSocket *socket,
GError **error);
gboolean g_socket_is_closed (GSocket *socket);
GSource * g_socket_create_source (GSocket *socket,
GIOCondition condition,
GCancellable *cancellable);
G_END_DECLS
#endif /* __G_SOCKET_H__ */

200
gio/gsocketcontrolmessage.c Normal file
View File

@ -0,0 +1,200 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright © 2009 Codethink Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* See the included COPYING file for more information.
*
* Authors: Ryan Lortie <desrt@desrt.ca>
*/
/**
* SECTION: gsocketcontrolmessage
* @title: GSocketControlMessage
* @short_description: a #GSocket control message
* @see_also: #GSocket.
*
* A #GSocketControlMessage is a special-purpose utility message that
* can be sent to or received from a #GSocket. These types of
* messages are often called "ancillary data".
*
* The message can represent some sort of special instruction to or
* information from the socket or can represent a special kind of
* transfer to the peer (for example, sending a file description over
* a UNIX socket).
*
* These messages are sent with g_socket_send_message() and received
* with g_socket_receive_message().
*
* To extend the set of control message that can be sent, subclass this
* class and override the get_size, get_level, get_type and serialize
* methods.
*
* To extend the set of control messages that can be received, subclass
* this class and implement the deserialize method. Also, make sure your
* class is registered with the GType typesystem before calling
* g_socket_receive_message() to read such a message.
*
* Since: 2.22
**/
#include "config.h"
#include "gsocketcontrolmessage.h"
#include "glibintl.h"
#ifndef G_OS_WIN32
#include "gunixfdmessage.h"
#include <sys/socket.h>
#else
# include <winsock2.h>
# include <mswsock.h>
#endif
#include "gioalias.h"
G_DEFINE_ABSTRACT_TYPE (GSocketControlMessage,
g_socket_control_message,
G_TYPE_OBJECT);
/**
* g_socket_control_message_get_size:
* @message: a #GSocketControlMessage
*
* Returns the space required for the control message, not including
* headers or alignment.
*
* Returns: The number of bytes required.
*
* Since: 2.22
**/
gsize
g_socket_control_message_get_size (GSocketControlMessage *message)
{
g_return_val_if_fail (G_IS_SOCKET_CONTROL_MESSAGE (message), 0);
return G_SOCKET_CONTROL_MESSAGE_GET_CLASS (message)->get_size (message);
}
/**
* g_socket_control_message_get_level:
* @message: a #GSocketControlMessage
*
* Returns the "level" (i.e. the originating protocol) of the control message.
* This is often SOL_SOCKET.
*
* Returns: and int describing the level
*
* Since: 2.22
**/
int
g_socket_control_message_get_level (GSocketControlMessage *message)
{
g_return_val_if_fail (G_IS_SOCKET_CONTROL_MESSAGE (message), 0);
return G_SOCKET_CONTROL_MESSAGE_GET_CLASS (message)->get_level (message);
}
/**
* g_socket_control_message_get_msg_type:
* @message: a #GSocketControlMessage
*
* Returns the protocol specify type of the control message.
* For instance, for unix fd passing this would be SCM_RIGHTS.
*
* Returns: and int describing the level
*
* Since: 2.22
**/
int
g_socket_control_message_get_msg_type (GSocketControlMessage *message)
{
g_return_val_if_fail (G_IS_SOCKET_CONTROL_MESSAGE (message), 0);
return G_SOCKET_CONTROL_MESSAGE_GET_CLASS (message)->get_type (message);
}
/**
* g_socket_control_message_serialize:
* @message: a #GSocketControlMessage
* @data: A buffer to write data to
*
* Converts the data in the message to bytes placed in the
* message.
*
* @data is guaranteed to have enough space to fit the size
* returned by g_socket_control_message_get_size() on this
* object.
*
* Since: 2.22
**/
void
g_socket_control_message_serialize (GSocketControlMessage *message,
gpointer data)
{
g_return_if_fail (G_IS_SOCKET_CONTROL_MESSAGE (message));
return G_SOCKET_CONTROL_MESSAGE_GET_CLASS (message)->serialize (message, data);
}
static void
g_socket_control_message_init (GSocketControlMessage *message)
{
}
static void
g_socket_control_message_class_init (GSocketControlMessageClass *class)
{
}
GSocketControlMessage *
g_socket_control_message_deserialize (int level,
int type,
gsize size,
gpointer data)
{
GSocketControlMessageClass *klass;
GSocketControlMessage *message;
GType *message_types;
guint n_message_types;
int i;
#ifndef G_OS_WIN32
volatile GType a_type;
#endif
/* Ensure we know about the built in types */
#ifndef G_OS_WIN32
a_type = g_unix_fd_message_get_type ();
#endif
message_types = g_type_children (G_TYPE_SOCKET_CONTROL_MESSAGE, &n_message_types);
message = NULL;
for (i = 0; i < n_message_types; i++)
{
klass = (GSocketControlMessageClass *)g_type_class_ref (type);
if (klass && klass->deserialize)
{
message = klass->deserialize (level, type, size, data);
g_type_class_unref ((GTypeClass *) klass);
}
if (message != NULL)
break;
}
g_free (message_types);
if (message == NULL)
g_warning ("unknown control message type %d:%d", level, type);
return message;
}
#define __G_SOCKET_CONTROL_MESSAGE_C__
#include "gioaliasdef.c"

View File

@ -0,0 +1,96 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright © 2009 Codethink Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Ryan Lortie <desrt@desrt.ca>
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#ifndef __G_SOCKET_CONTROL_MESSAGE_H__
#define __G_SOCKET_CONTROL_MESSAGE_H__
#include <gio/giotypes.h>
G_BEGIN_DECLS
#define G_TYPE_SOCKET_CONTROL_MESSAGE (g_socket_control_message_get_type ())
#define G_SOCKET_CONTROL_MESSAGE(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
G_TYPE_SOCKET_CONTROL_MESSAGE, \
GSocketControlMessage))
#define G_SOCKET_CONTROL_MESSAGE_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), \
G_TYPE_SOCKET_CONTROL_MESSAGE, \
GSocketControlMessageClass))
#define G_IS_SOCKET_CONTROL_MESSAGE(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
G_TYPE_SOCKET_CONTROL_MESSAGE))
#define G_IS_SOCKET_CONTROL_MESSAGE_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), \
G_TYPE_SOCKET_CONTROL_MESSAGE))
#define G_SOCKET_CONTROL_MESSAGE_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), \
G_TYPE_SOCKET_CONTROL_MESSAGE, \
GSocketControlMessageClass))
typedef struct _GSocketControlMessagePrivate GSocketControlMessagePrivate;
typedef struct _GSocketControlMessageClass GSocketControlMessageClass;
/**
* GSocketControlMessageClass:
* @get_size: gets the size of the message.
* @get_level: gets the protocol of the message.
* @get_type: gets the protocol specific type of the message.
* @serialize: Writes out the message data.
* @deserialize: Tries to deserialize a message.
**/
struct _GSocketControlMessageClass
{
GObjectClass parent_class;
gsize (* get_size) (GSocketControlMessage *message);
int (* get_level) (GSocketControlMessage *message);
int (* get_type) (GSocketControlMessage *message);
void (* serialize) (GSocketControlMessage *message,
gpointer data);
GSocketControlMessage *(* deserialize) (int level,
int type,
gsize size,
gpointer data);
};
struct _GSocketControlMessage
{
GObject parent_instance;
GSocketControlMessagePrivate *priv;
};
GType g_socket_control_message_get_type (void) G_GNUC_CONST;
gsize g_socket_control_message_get_size (GSocketControlMessage *message);
int g_socket_control_message_get_level (GSocketControlMessage *message);
int g_socket_control_message_get_msg_type (GSocketControlMessage *message);
void g_socket_control_message_serialize (GSocketControlMessage *message,
gpointer data);
GSocketControlMessage *g_socket_control_message_deserialize (int level,
int type,
gsize size,
gpointer data);
G_END_DECLS
#endif /* __G_SOCKET_CONTROL_MESSAGE_H__ */

259
gio/gunixfdmessage.c Normal file
View File

@ -0,0 +1,259 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright © 2009 Codethink Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* See the included COPYING file for more information.
*
* Authors: Ryan Lortie <desrt@desrt.ca>
*/
/**
* SECTION: gunixfdmessage
* @title: GUnixFDMessage
* @short_description: a #GSocketControlMessage containing a list of
* file descriptors
* @see_also: #GUnixConnection
*
* This #GSocketControlMessage contains a list of file descriptors.
* It may be sent using g_socket_send_message() and received using
* g_socket_receive_message() over UNIX sockets (ie: sockets in the
* %G_SOCKET_ADDRESS_UNIX family).
*
* For an easier way to send and receive file descriptors over
* stream-oriented UNIX sockets, see g_unix_connection_send_fd() and
* g_unix_connection_receive_fd().
**/
#include "config.h"
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "gunixfdmessage.h"
#include "gioerror.h"
#include "gioalias.h"
G_DEFINE_TYPE (GUnixFDMessage, g_unix_fd_message,
G_TYPE_SOCKET_CONTROL_MESSAGE);
struct _GUnixFDMessagePrivate
{
gint *fds;
gint nfd;
};
static gsize
g_unix_fd_message_get_size (GSocketControlMessage *message)
{
GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
return fd_message->priv->nfd * sizeof (gint);
}
static int
g_unix_fd_message_get_level (GSocketControlMessage *message)
{
return SOL_SOCKET;
}
static int
g_unix_fd_message_get_msg_type (GSocketControlMessage *message)
{
return SCM_RIGHTS;
}
static GSocketControlMessage *
g_unix_fd_message_deserialize (int level,
int type,
gsize size,
gpointer data)
{
GUnixFDMessage *message;
if (level != SOL_SOCKET ||
level != SCM_RIGHTS)
return NULL;
if (size % 4 > 0)
{
g_warning ("Kernel returned non-integral number of fds");
return NULL;
}
message = g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
message->priv->nfd = size / sizeof (gint);
message->priv->fds = g_new (gint, message->priv->nfd + 1);
memcpy (message->priv->fds, data, size);
message->priv->fds[message->priv->nfd] = -1;
return G_SOCKET_CONTROL_MESSAGE (message);
}
static void
g_unix_fd_message_serialize (GSocketControlMessage *message,
gpointer data)
{
GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
memcpy (data, fd_message->priv->fds,
sizeof (gint) * fd_message->priv->nfd);
}
static void
g_unix_fd_message_init (GUnixFDMessage *message)
{
message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
G_TYPE_UNIX_FD_MESSAGE,
GUnixFDMessagePrivate);
}
static void
g_unix_fd_message_finalize (GObject *object)
{
GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
gint i;
for (i = 0; i < message->priv->nfd; i++)
close (message->priv->fds[i]);
g_free (message->priv->fds);
G_OBJECT_CLASS (g_unix_fd_message_parent_class)
->finalize (object);
}
static void
g_unix_fd_message_class_init (GUnixFDMessageClass *class)
{
GSocketControlMessageClass *scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
GObjectClass *object_class = G_OBJECT_CLASS (class);
g_type_class_add_private (class, sizeof (GUnixFDMessagePrivate));
scm_class->get_size = g_unix_fd_message_get_size;
scm_class->get_level = g_unix_fd_message_get_level;
scm_class->get_type = g_unix_fd_message_get_msg_type;
scm_class->serialize = g_unix_fd_message_serialize;
scm_class->deserialize = g_unix_fd_message_deserialize;
object_class->finalize = g_unix_fd_message_finalize;
}
/**
* g_unix_fd_message_new:
* @returns: a new #GUnixFDMessage
*
* Creates a new #GUnixFDMessage containing no file descriptors.
**/
GSocketControlMessage *
g_unix_fd_message_new (void)
{
return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
}
/**
* g_unix_fd_message_steal_fds:
* @message: a #GUnixFDMessage
* @length: pointer to the length of the returned array, or %NULL
* @returns: an array of file descriptors
*
* Returns the array of file descriptors that is contained in this
* object.
*
* After this call, the descriptors are no longer contained in
* @message. Further calls will return an empty list (unless more
* descriptors have been added).
*
* The return result of this function must be freed with g_free().
* The caller is also responsible for closing all of the file
* descriptors.
*
* If @length is non-%NULL then it is set to the number of file
* descriptors in the returned array. The returned array is also
* terminated with -1.
*
* This function never returns NULL. In case there are no file
* descriptors contained in @message, an empty array is returned.
**/
gint *
g_unix_fd_message_steal_fds (GUnixFDMessage *message,
gint *length)
{
gint *result;
g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), NULL);
/* will be true for fresh object or if we were just called */
if (message->priv->fds == NULL)
{
message->priv->fds = g_new (gint, 1);
message->priv->fds[0] = -1;
message->priv->nfd = 0;
}
if (length)
*length = message->priv->nfd;
result = message->priv->fds;
message->priv->fds = NULL;
message->priv->nfd = 0;
return result;
}
/**
* g_unix_fd_message_append_fd:
* @message: a #GUnixFDMessage
* @fd: a valid open file descriptor
* @error: a #GError pointer
* @returns: %TRUE in case of success, else %FALSE (and @error is set)
*
* Adds a file descriptor to @message.
*
* The file descriptor is duplicated using dup(). You keep your copy
* of the descriptor and the copy contained in @message will be closed
* when @message is finalized.
*
* A possible cause of failure is exceeding the per-process or
* system-wide file descriptor limit.
**/
gboolean
g_unix_fd_message_append_fd (GUnixFDMessage *message,
gint fd,
GError **error)
{
gint new_fd;
g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), FALSE);
g_return_val_if_fail (fd >= 0, FALSE);
do
new_fd = dup (fd);
while (new_fd < 0 && (errno == EINTR));
if (fd < 0)
{
int saved_errno = errno;
g_set_error (error, G_IO_ERROR,
g_io_error_from_errno (saved_errno),
"dup: %s", g_strerror (saved_errno));
return FALSE;
}
message->priv->fds = g_realloc (message->priv->fds,
sizeof (gint) *
(message->priv->nfd + 2));
message->priv->fds[message->priv->nfd++] = new_fd;
message->priv->fds[message->priv->nfd] = -1;
return TRUE;
}
#define __G_UNIX_FD_MESSAGE_C__
#include "gioaliasdef.c"

67
gio/gunixfdmessage.h Normal file
View File

@ -0,0 +1,67 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright © 2009 Codethink Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Ryan Lortie <desrt@desrt.ca>
*/
#ifndef __G_UNIX_FD_MESSAGE_H__
#define __G_UNIX_FD_MESSAGE_H__
#include <gio/gsocketcontrolmessage.h>
G_BEGIN_DECLS
#define G_TYPE_UNIX_FD_MESSAGE (g_unix_fd_message_get_type ())
#define G_UNIX_FD_MESSAGE(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
G_TYPE_UNIX_FD_MESSAGE, GUnixFDMessage))
#define G_UNIX_FD_MESSAGE_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), \
G_TYPE_UNIX_FD_MESSAGE, GUnixFDMessageClass))
#define G_IS_UNIX_FD_MESSAGE(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
G_TYPE_UNIX_FD_MESSAGE))
#define G_IS_UNIX_FD_MESSAGE_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), \
G_TYPE_UNIX_FD_MESSAGE))
#define G_UNIX_FD_MESSAGE_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), \
G_TYPE_UNIX_FD_MESSAGE, GUnixFDMessageClass))
typedef struct _GUnixFDMessagePrivate GUnixFDMessagePrivate;
typedef struct _GUnixFDMessageClass GUnixFDMessageClass;
typedef struct _GUnixFDMessage GUnixFDMessage;
struct _GUnixFDMessageClass
{
GSocketControlMessageClass parent_class;
};
struct _GUnixFDMessage
{
GSocketControlMessage parent_instance;
GUnixFDMessagePrivate *priv;
};
GType g_unix_fd_message_get_type (void) G_GNUC_CONST;
GSocketControlMessage * g_unix_fd_message_new (void);
gint * g_unix_fd_message_steal_fds (GUnixFDMessage *message,
gint *length);
gboolean g_unix_fd_message_append_fd (GUnixFDMessage *message,
gint fd,
GError **error);
G_END_DECLS
#endif /* __G_UNIX_FD_MESSAGE_H__ */