source: protocols/events_libevent.c @ fc2ee0f

Last change on this file since fc2ee0f was fc2ee0f, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-05-13T10:29:53Z

It works, it works! \o/

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[13cc96c]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2006 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/*
8 * Event handling (using libevent)
9 */
10
11/*
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  GNU General Public License for more details.
21
22  You should have received a copy of the GNU General Public License with
23  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
24  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
25  Suite 330, Boston, MA  02111-1307  USA
26*/
27
28#define BITLBEE_CORE
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/types.h>
33#include "proxy.h"
34
35#include <sys/time.h>
36#include <event.h>
37
38static guint id_next;
39static GHashTable *id_hash;
40
41struct b_event_data
42{
43        guint id;
44        struct event evinfo;
45        b_event_handler function;
46        void *data;
47};
48
49void b_main_init()
50{
51        event_init();
52       
53        id_next = 1;
54        id_hash = g_hash_table_new( g_int_hash, g_int_equal );
55}
56
57void b_main_run()
58{
59        event_dispatch();
60}
61
62void b_main_quit()
63{
64        struct timeval tv;
65       
66        memset( &tv, 0, sizeof( struct timeval ) );
67        event_loopexit( &tv );
68}
69
70static void b_event_passthrough( int fd, short event, void *data )
71{
72        struct b_event_data *b_ev = data;
73        b_input_condition cond = 0;
[fc2ee0f]74        int id;
[09f8cd1]75       
76        if( fd >= 0 )
77        {
78                if( event & EV_READ )
79                        cond |= GAIM_INPUT_READ;
80                if( event & EV_WRITE )
81                        cond |= GAIM_INPUT_WRITE;
82        }
83       
[b642f381]84        event_debug( "b_event_passthrough( %d, %d, 0x%x ) (%d)\n", fd, event, (int) data, b_ev->id );
85       
[fc2ee0f]86        /* Since the called function might cancel this handler already
87           (which free()s b_ev, we have to remember the ID here. */
88        id = b_ev->id;
89       
[13cc96c]90        if( !b_ev->function( b_ev->data, fd, cond ) )
[b642f381]91        {
92                event_debug( "Handler returned FALSE: " );
[fc2ee0f]93                b_event_remove( id );
[b642f381]94        }
[13cc96c]95}
96
[b642f381]97gint b_input_add( gint fd, b_input_condition condition, b_event_handler function, gpointer data )
[13cc96c]98{
99        struct b_event_data *b_ev = g_new0( struct b_event_data, 1 );
[b642f381]100        GIOCondition out_cond;
[13cc96c]101       
[09f8cd1]102        b_ev->id = id_next++;
[13cc96c]103        b_ev->function = function;
104        b_ev->data = data;
105       
[b642f381]106        out_cond = EV_PERSIST;
[13cc96c]107        if( condition & GAIM_INPUT_READ )
[b642f381]108                out_cond |= EV_READ;
[13cc96c]109        if( condition & GAIM_INPUT_WRITE )
[b642f381]110                out_cond |= EV_WRITE;
[13cc96c]111       
[b642f381]112        event_set( &b_ev->evinfo, fd, out_cond, b_event_passthrough, b_ev );
[13cc96c]113        event_add( &b_ev->evinfo, NULL );
114       
[b642f381]115        event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) = %d\n", fd, condition, function, data, b_ev->id );
116       
[13cc96c]117        g_hash_table_insert( id_hash, &b_ev->id, b_ev );
118       
119        return b_ev->id;
120}
121
[09f8cd1]122/* TODO: Persistence for timers! */
[13cc96c]123gint b_timeout_add( gint timeout, b_event_handler function, gpointer data )
124{
125        struct b_event_data *b_ev = g_new0( struct b_event_data, 1 );
126        struct timeval tv;
127       
[09f8cd1]128        b_ev->id = id_next++;
[13cc96c]129        b_ev->function = function;
130        b_ev->data = data;
131       
132        tv.tv_sec = timeout / 1000;
133        tv.tv_usec = ( timeout % 1000 ) * 1000;
134       
135        evtimer_set( &b_ev->evinfo, b_event_passthrough, b_ev );
[09f8cd1]136        evtimer_add( &b_ev->evinfo, &tv );
[13cc96c]137       
[b642f381]138        event_debug( "b_timeout_add( %d, %d, 0x%x ) = %d\n", timeout, function, data, b_ev->id );
139       
[13cc96c]140        g_hash_table_insert( id_hash, &b_ev->id, b_ev );
141       
142        return b_ev->id;
143}
144
[b642f381]145void b_event_remove( gint id )
[13cc96c]146{
[b642f381]147        struct b_event_data *b_ev = g_hash_table_lookup( id_hash, &id );
[13cc96c]148       
[b642f381]149        event_debug( "b_event_remove( %d )\n", id );
[13cc96c]150        if( b_ev )
151        {
152                event_del( &b_ev->evinfo );
[09f8cd1]153                g_hash_table_remove( id_hash, &b_ev->id );
[13cc96c]154                g_free( b_ev );
155        }
[b642f381]156        else
157        {
[fc2ee0f]158                event_debug( "Double remove?\n" );
[b642f381]159        }
[13cc96c]160}
161
162gboolean b_event_remove_by_data( gpointer data )
163{
164        /* FIXME! */
[b642f381]165        event_debug( "FALSE!\n" );
[13cc96c]166        return FALSE;
167}
Note: See TracBrowser for help on using the repository browser.