InfIo

InfIo — Event loop abstraction

Stability Level

Unstable, unless otherwise indicated

Synopsis


#include <libinfinity/common/inf-io.h>


                    InfIo;
                    InfIoIface;
typedef             InfNativeSocket;
enum                InfIoEvent;
void                (*InfIoFunc)                        (InfNativeSocket *socket,
                                                         InfIoEvent event,
                                                         gpointer user_data);
void                (*InfIoTimeoutFunc)                 (gpointer user_data);
void                inf_io_watch                        (InfIo *io,
                                                         InfNativeSocket *socket,
                                                         InfIoEvent events,
                                                         InfIoFunc func,
                                                         gpointer user_data,
                                                         GDestroyNotify notify);
gpointer            inf_io_add_timeout                  (InfIo *io,
                                                         guint msecs,
                                                         InfIoTimeoutFunc func,
                                                         gpointer user_data,
                                                         GDestroyNotify notify);
void                inf_io_remove_timeout               (InfIo *io,
                                                         gpointer timeout);

Object Hierarchy


  GInterface
   +----InfIo

Prerequisites

InfIo requires GObject.

Known Implementations

InfIo is implemented by InfStandaloneIo.

Description

The InfIo interface is used to schedule timeouts and to watch sockets for events to occur. An actual implementation usually integrates this into the application main loop, such as GMainLoop. There is also a standalone implementation, InfStandaloneIo, that can directly be used as the application's main loop.

Every object in Libinfinity that needs to schedule timeouts or watches sockets uses a InfIo to do so. This allows to use libinfinity with different main event loops, not only Glib's one.

Details

InfIo

typedef struct _InfIo InfIo;

InfIo is an opaque data type. You should only access it via the public API functions.


InfIoIface

typedef struct {
  void (*watch)(InfIo* io,
                InfNativeSocket* socket,
                InfIoEvent events,
                InfIoFunc func,
                gpointer user_data,
                GDestroyNotify notify);

  gpointer (*add_timeout)(InfIo* io,
                          guint msecs,
                          InfIoTimeoutFunc func,
                          gpointer user_data,
                          GDestroyNotify notify);

  void (*remove_timeout)(InfIo* io,
                         gpointer timeout);
} InfIoIface;

The virtual methods of InfIo. These allow to set up socket watches and timeouts.

watch ()

Changes the events the given socket is watched for. If events is 0, removes the watch for socket.

add_timeout ()

Schedules func to be called at least msecs milliseconds in the future.

remove_timeout ()

Removes a scheduled timeout again. The timeout is removed automatically when it has elapsed, so there is no need to call this function in that case.

InfNativeSocket

Native socket type on the target platform. This typedef is a simple int on Unix and a SOCKET on Windows.


enum InfIoEvent

typedef enum _InfIoEvent {
  INF_IO_INCOMING = 1 << 0,
  INF_IO_OUTGOING = 1 << 1,
  INF_IO_ERROR    = 1 << 2
} InfIoEvent;

This enumeration specifies events that can be watched.

INF_IO_INCOMING

Data can be read from the socket without blocking, or the connection has been closed (which is the case when recv() returns 0).

INF_IO_OUTGOING

Data can be sent without blocking.

INF_IO_ERROR

An error with the socket occured, or the connection has been closed. Use getsockopt() to read do SO_ERROR option to find out what the problem is.

InfIoFunc ()

void                (*InfIoFunc)                        (InfNativeSocket *socket,
                                                         InfIoEvent event,
                                                         gpointer user_data);

Callback function that is called when an event occurs on a watched socket.

socket :

The socket on which an event occured.

event :

A bitmask of the events that occured.

user_data :

User-defined data specified in inf_io_watch().

InfIoTimeoutFunc ()

void                (*InfIoTimeoutFunc)                 (gpointer user_data);

Callback function that is called when a timeout has elapsed.

user_data :

User-defined data specified in inf_io_add_timeout().

inf_io_watch ()

void                inf_io_watch                        (InfIo *io,
                                                         InfNativeSocket *socket,
                                                         InfIoEvent events,
                                                         InfIoFunc func,
                                                         gpointer user_data,
                                                         GDestroyNotify notify);

Monitors the given socket for activity and calls func if one of the events specified in events occurs.

io :

A InfIo.

socket :

The socket to watch.

events :

Events to watch for.

func :

Function to be called when one of the events occurs.

user_data :

Extra data to pass to func.

notify :

A GDestroyNotify that is called when user_data is no longer needed, or NULL.

inf_io_add_timeout ()

gpointer            inf_io_add_timeout                  (InfIo *io,
                                                         guint msecs,
                                                         InfIoTimeoutFunc func,
                                                         gpointer user_data,
                                                         GDestroyNotify notify);

Calls func after at least msecs milliseconds have elapsed. The timeout is removed after it has elapsed.

io :

A InfIo.

msecs :

Number of milliseconds after which the timeout should be elapsed.

func :

Function to be called when the timeout elapsed.

user_data :

Extra data to pass to func.

notify :

A GDestroyNotify that is called when user_data is no longer needed, or NULL.

Returns :

A timeout handle that can be used to remove the timeout.

inf_io_remove_timeout ()

void                inf_io_remove_timeout               (InfIo *io,
                                                         gpointer timeout);

Removes the given timeout.

io :

A InfIo.

timeout :

A timeout handle obtained from inf_io_add_timeout().

See Also

InfStandaloneIo