Socket Dispatcher


Detailed Description

Copyright (C) 2007-2008 Ola Lundqvist <ola@inguza.com>

All rights reserved.

Socket Dispatcher is a generic dispatcher library for sockets.

To make use of the functionality the dispatcher need to be initialized using initSocketDispatcher. It is then possible to add sockets to the dispatcher using addSocket and remove using delSocket. To execute the dispatcher function the socketDispatcher function is used. See below for more information on exact usage.

The supported event types are:

 Example:
 void callback(int fd, int type) {
     ...
 }
 int main(int argc, char** argv) {
     struct socketDispatcher_t d;
     initSocketDispatcher(&d, SELECT, callBackFunction, 1000000);
     addSocket(&d, <socket>, DISPATCHER_READ);
     while(1) {
         socketDispatcher(&d);
     }
 }
 

The Socket Dispatcher Library supports a number of different socket event mechanisms available in different operating systems:

The dispatcher is thread safe, which means that the internal data structures is protected so that different threads can not manipulate data at the same time.

This means that during the dispatcher call, no elements can be removed and for select dispatcher not added either. The dispatcher can not be called twice as well. It also means that data can not be added and removed at the same time including two additions and two removes.

Before initSocketDispatcher and after closeSocketDispatcher is called the library have undefined function and is not thread safe. The library also have undefined functionality for unsupported dispatcher types, including those disabled with DISABLE_<type>.

NOTE! This means that deadlocks is possible, especially for infinit timeouts in dispatcher. Avoid infinit timeouts in dispatcher if you have a multithreaded program that rely on the possibility to add and remove sockets to the dispatcher.

When building the library there are a number of ways to define what methods to include depending on the supported operating system or the desired size of the library. The below list show how big the library will be on disk with the following defined.

Measurements on Linux 2.6 and rounded to closest KB:

 SELECT POLL EPOLL KQUEUE  Dynamic lib  Static lib
     E    E     E      D       18 KB        15 KB
     E    E     D      D       17 KB        13 KB
     E    D     E      D       17 KB        14 KB
     E    D     D      D       15 KB        12 KB
     D    E     E      D       16 KB        13 KB
     D    E     D      D       15 KB        11 KB
     D    D     E      D       15 KB        11 KB
     D    D     D      D       13 KB         9 KB
 

Measurements on FreeBSD 6.2 and rounded to closest KB:

 SELECT POLL EPOLL KQUEUE  Dynamic lib  Static lib
     E    E     D      E       18 KB        14 KB
     E    E     D      D       15 KB        12 KB
     E    D     D      E       16 KB        13 KB
     E    D     D      D       14 KB        11 KB
     D    E     D      E       15 KB        12 KB
     D    E     D      D       13 KB        10 KB
     D    D     D      E       14 KB        11 KB
     D    D     D      D       12 KB         8 KB
 


Data Structures

struct  socketDispatcher_t

Defines

#define DISPATCHER_READ   1
#define DISPATCHER_WRITE   2
#define DISPATCHER_OTHER   4

Typedefs

typedef void(*) callbackFunction (int, int)
typedef int(*) dispatcherFunction (struct socketDispatcher_t *data)

Enumerations

enum  dispatcherType {
  NONE = 0, SELECT = 1, POLL = 2, EPOLL = 3,
  KQUEUE = 4
}

Functions

int socketDispatcher (struct socketDispatcher_t *data)
int initSocketDispatcher (struct socketDispatcher_t *data, enum dispatcherType type, callbackFunction callback, int timeout)
void setSocketDispatcherTimeout (struct socketDispatcher_t *data, int timeout)
void closeSocketDispatcher (struct socketDispatcher_t *data)
int addSocket (struct socketDispatcher_t *data, int socket, int type)
void delSocket (struct socketDispatcher_t *data, int socket)
int getNumberOfSockets (struct socketDispatcher_t *data)


Define Documentation

#define DISPATCHER_OTHER   4

Definition for other events.

OBS! The DISPATCHER_OTHER is not fully tested, and is known to work differently on different type of dispatchers

#define DISPATCHER_READ   1

Definition for read events.

#define DISPATCHER_WRITE   2

Definition for write events.


Typedef Documentation

typedef void(*) callbackFunction(int, int)

Callback function definition.

Parameters:
socket The socket that is triggered.
type The type of event caused the trigger.

typedef int(*) dispatcherFunction(struct socketDispatcher_t *data)

Dispatcher function definition.

Parameters:
data Data structure for the dispatcher.


Enumeration Type Documentation

enum dispatcherType

Type definition for the dispatcher.

Enumerator:
NONE 
SELECT 
POLL 
EPOLL 
KQUEUE 


Function Documentation

int addSocket ( struct socketDispatcher_t data,
int  socket,
int  type 
)

Add a socket to the socket dispatcher

See also:
delSocket

DISPATCHER_READ

DISPATCHER_WRITE

DISPATCHER_OTHER

Parameters:
data Data structure for the dispatcher.
socket The socket to add
type Type of events to listen to.
Returns:
0 on success

void closeSocketDispatcher ( struct socketDispatcher_t data  ) 

Close dispatcher data strucutres. OBS! This do not close the sockets inside the dispatcher.

Parameters:
data Data structure for the dispatcher.

void delSocket ( struct socketDispatcher_t data,
int  socket 
)

Remove a socket to the socket dispatcher

See also:
delSocket
Parameters:
data Data structure for the dispatcher.
socket The socket to remove.

int getNumberOfSockets ( struct socketDispatcher_t data  ) 

Get the number of sockets registered in the dispatcher.

Parameters:
data Data structure for the dispatcher.
Returns:
Number of sockets registered in the dispatcher.

int initSocketDispatcher ( struct socketDispatcher_t data,
enum dispatcherType  type,
callbackFunction  callback,
int  timeout 
)

Initializer for the dispatcher data structure

See also:
setSocketDispatcherTimeout

closeSocketDispatcher

Parameters:
data Data structure for the dispatcher.
type Type of dispatcher
callback A callback function for the dispatcher to call.
timeout Maximum timeout in micro seconds (< 0 gives an infinite timeout).
Returns:
  • 0 Initialisation ok.
  • -1 Error: errno variable set appropriately. ENOSYS for use of unsupported dispatcher type.

void setSocketDispatcherTimeout ( struct socketDispatcher_t data,
int  timeout 
)

Set timeout for the dispatcher

Parameters:
data Data structure for the dispatcher.
timeout Maximum timeout in micro seconds (< 0 gives an infinite timeout).

int socketDispatcher ( struct socketDispatcher_t data  ) 

The dispatcher function.

Parameters:
data Data structure for the dispatcher.
Returns:
  • >= 0 The number of calls to the callback function.
  • -1 Error: errno variable set appropriately.


Generated on Mon Apr 7 22:13:48 2008 for SocketDispatcher by  doxygen 1.5.1