glib/gio/gsocketcontrolmessage.c
Alexander Larsson 145cec3c93 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.
2009-05-14 15:44:36 +02:00

201 lines
5.2 KiB
C

/* 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"