source: protocols/events_libevent.c @ 782d988

Last change on this file since 782d988 was 782d988, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-05-13T19:01:14Z

libevent code works better with epoll() now in some (pretty common) situations.

  • Property mode set to 100644
File size: 5.4 KB
Line 
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
41/* Since libevent doesn't handle two event handlers for one fd-condition
42   very well (which happens sometimes when BitlBee changes event handlers
43   for a combination), let's buid some indexes so we can delete them here
44   already, just in time. */
45static GHashTable *read_hash;
46static GHashTable *write_hash;
47
48struct b_event_data
49{
50        guint id;
51        struct event evinfo;
52        b_event_handler function;
53        void *data;
54};
55
56void b_main_init()
57{
58        event_init();
59       
60        id_next = 1;
61        id_hash = g_hash_table_new( g_int_hash, g_int_equal );
62        read_hash = g_hash_table_new( g_int_hash, g_int_equal );
63        write_hash = g_hash_table_new( g_int_hash, g_int_equal );
64}
65
66void b_main_run()
67{
68        event_dispatch();
69}
70
71void b_main_quit()
72{
73        struct timeval tv;
74       
75        memset( &tv, 0, sizeof( struct timeval ) );
76        event_loopexit( &tv );
77}
78
79static void b_event_passthrough( int fd, short event, void *data )
80{
81        struct b_event_data *b_ev = data;
82        b_input_condition cond = 0;
83        int id;
84       
85        if( fd >= 0 )
86        {
87                if( event & EV_READ )
88                        cond |= GAIM_INPUT_READ;
89                if( event & EV_WRITE )
90                        cond |= GAIM_INPUT_WRITE;
91        }
92       
93        event_debug( "b_event_passthrough( %d, %d, 0x%x ) (%d)\n", fd, event, (int) data, b_ev->id );
94       
95        /* Since the called function might cancel this handler already
96           (which free()s b_ev, we have to remember the ID here. */
97        id = b_ev->id;
98       
99        if( !b_ev->function( b_ev->data, fd, cond ) )
100        {
101                event_debug( "Handler returned FALSE: " );
102                b_event_remove( id );
103        }
104}
105
106gint b_input_add( gint fd, b_input_condition condition, b_event_handler function, gpointer data )
107{
108        struct b_event_data *b_ev;
109       
110        event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) ", fd, condition, function, data );
111       
112        if( ( condition & GAIM_INPUT_READ  && ( b_ev = g_hash_table_lookup( read_hash,  &fd ) ) ) ||
113            ( condition & GAIM_INPUT_WRITE && ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) )
114        {
115                /* We'll stick with this libevent entry, but give it a new BitlBee id. */
116                g_hash_table_remove( id_hash, &b_ev->id );
117               
118                event_debug( "(replacing old handler (id = %d)) = %d\n", b_ev->id, id_next );
119               
120                b_ev->id = id_next++;
121                b_ev->function = function;
122                b_ev->data = data;
123        }
124        else
125        {
126                GIOCondition out_cond;
127               
128                event_debug( "(new) = %d\n", id_next );
129               
130                b_ev = g_new0( struct b_event_data, 1 );
131                b_ev->id = id_next++;
132                b_ev->function = function;
133                b_ev->data = data;
134               
135                out_cond = EV_PERSIST;
136                if( condition & GAIM_INPUT_READ )
137                        out_cond |= EV_READ;
138                if( condition & GAIM_INPUT_WRITE )
139                        out_cond |= EV_WRITE;
140               
141                event_set( &b_ev->evinfo, fd, out_cond, b_event_passthrough, b_ev );
142                event_add( &b_ev->evinfo, NULL );
143               
144                if( out_cond & EV_READ )
145                        g_hash_table_insert( read_hash, &b_ev->evinfo.ev_fd, b_ev );
146                if( out_cond & EV_WRITE )
147                        g_hash_table_insert( write_hash, &b_ev->evinfo.ev_fd, b_ev );
148        }
149       
150        g_hash_table_insert( id_hash, &b_ev->id, b_ev );
151        return b_ev->id;
152}
153
154/* TODO: Persistence for timers! */
155gint b_timeout_add( gint timeout, b_event_handler function, gpointer data )
156{
157        struct b_event_data *b_ev = g_new0( struct b_event_data, 1 );
158        struct timeval tv;
159       
160        b_ev->id = id_next++;
161        b_ev->function = function;
162        b_ev->data = data;
163       
164        tv.tv_sec = timeout / 1000;
165        tv.tv_usec = ( timeout % 1000 ) * 1000;
166       
167        evtimer_set( &b_ev->evinfo, b_event_passthrough, b_ev );
168        evtimer_add( &b_ev->evinfo, &tv );
169       
170        event_debug( "b_timeout_add( %d, 0x%x, 0x%x ) = %d\n", timeout, function, data, b_ev->id );
171       
172        g_hash_table_insert( id_hash, &b_ev->id, b_ev );
173       
174        return b_ev->id;
175}
176
177void b_event_remove( gint id )
178{
179        struct b_event_data *b_ev = g_hash_table_lookup( id_hash, &id );
180       
181        event_debug( "b_event_remove( %d )\n", id );
182        if( b_ev )
183        {
184                g_hash_table_remove( id_hash, &b_ev->id );
185                if( b_ev->evinfo.ev_fd >= 0 )
186                {
187                        if( b_ev->evinfo.ev_events & EV_READ )
188                                g_hash_table_remove( read_hash, &b_ev->evinfo.ev_fd );
189                        if( b_ev->evinfo.ev_events & EV_WRITE )
190                                g_hash_table_remove( write_hash, &b_ev->evinfo.ev_fd );
191                }
192               
193                event_del( &b_ev->evinfo );
194                g_free( b_ev );
195        }
196        else
197        {
198                event_debug( "Already removed?\n" );
199        }
200}
201
202gboolean b_event_remove_by_data( gpointer data )
203{
204        /* FIXME! */
205        event_debug( "FALSE!\n" );
206        return FALSE;
207}
Note: See TracBrowser for help on using the repository browser.