User Guide - Event Handling

# The Event Manager

The W3C Reference Library can be used in either a single-threaded or the multi-threaded programming style. In this section we will havea look at how to enable this functionality and what the API is for applications to use it. We will not describe the underlying design model as thsi is described in detail in the Library Architecture documentation.

If you are working on a MSWindows platform then you have the possibility of using asynchronous socket management (proactive mode) instead of typical Unix select based I/O (reactive mode). Please read the Windows documentation for more details.

## Event Handlers

The appplication registers a set of event handlers to be used on a specified set of sockets. An event handler is a function of type

    typedef int HTEventCallback (SOCKET, HTRequest *, SockOps);

### Register a TTY Event Handler

Register the tty (console) as having events. If the TTY is select()-able (as is true under Unix), then we treat it as just another socket. Otherwise, take steps depending on the platform. This is the function to use to register user events!

    extern int HTEvent_RegisterTTY	(SOCKET, HTRequest *, SockOps, HTEventCallback *, HTPriority);

### Unregister a TTY Event Handler

Unregisters TTY I/O channel. If the TTY is select()-able (as is true under Unix), then we treat it as just another socket.

    extern int HTEvent_UnRegisterTTY (SOCKET, SockOps);

### Register an Event Handler

For a given socket, reqister a request structure, a set of operations, a HTEventCallback function, and a priority. For this implementation, we allow only a single HTEventCallback function for all operations. and the priority field is ignored.

    extern int HTEvent_Register	(SOCKET, HTRequest *, SockOps, HTEventCallback *, HTPriority);

### Unregister an Event Handler

Remove the registered information for the specified socket for the actions specified in ops. if no actions remain after the unregister, the registered info is deleted, and, if the socket has been registered for notification, the HTEventCallback will be invoked.

    extern int HTEvent_UnRegister	(SOCKET, SockOps);

### Unregister ALL Event Handlers

Unregister all sockets. N.B. we just remove them for our internal data structures: it is up to the application to actually close the socket.

    extern int HTEvent_UnregisterAll (void);

## Handler for Timeout on Sockets

This function sets the timeout for sockets in the `select()` call and registers a timeout function that is called if select times out. This does only works on NON windows platforms as we need to poll for the console on windows If `tv = NULL` then timeout is disabled. Default is no timeout. If always=YES then the callback is called at all times, if NO then only when Library sockets are active. Returns YES if OK else NO.

    typedef int HTEventTimeout (HTRequest *);

    extern BOOL HTEvent_registerTimeout (struct timeval *tp, HTRequest * request,
    				     HTEventTimeout *tcbf, BOOL always);

## Start the Event Loop

That is, we wait for activity from one of our registered channels, and dispatch on that. Under Windows/NT, we must treat the console and sockets as distinct. That means we can't avoid a busy wait, but we do our best.

    extern int HTEvent_Loop (HTRequest * request);

## Stop the Event Loop

Stops the (select based) event loop. The function does not guarantee that all requests have terminated. This is for the app to do

    extern void HTEvent_stopLoop (void);