Changeset 607

Show
Ignore:
Timestamp:
07/23/05 15:30:35 (3 years ago)
Author:
jajcus
Message:

- Use GSource and GPollFD instad of higher level GIOChannel as the socket is handled (eg. closed or replaced) by libgadu

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/sessions.c

    r605 r607  
    4848static int session_destroy(Session *s); 
    4949static void resource_remove(Resource *r,int kill_session); 
     50static int session_io_handler(Session *s); 
    5051 
    5152static void session_stream_destroyed(gpointer key,gpointer value,gpointer user_data){ 
     
    5960 
    6061        g_hash_table_foreach(sessions_jid,session_stream_destroyed,stream); 
     62} 
     63 
     64typedef struct { 
     65        GSource g_src; 
     66        Session *session; 
     67}Source; 
     68 
     69static gboolean session_g_source_prepare(GSource *source, gint* timeout_){ 
     70 
     71        *timeout_=-1; 
     72 
     73        return FALSE; 
     74} 
     75static gboolean session_g_source_check(GSource *source){ 
     76Source *src=(Source *)(gpointer)source; 
     77 
     78        return src->session->g_pollfd.revents != 0; 
     79} 
     80static gboolean session_g_source_dispatch(GSource *source,GSourceFunc callback,gpointer user_data){ 
     81Source *src=(Source *)(gpointer)source; 
     82 
     83        session_io_handler(src->session); 
     84        return TRUE; 
     85} 
     86 
     87static GSourceFuncs source_funcs = { 
     88        session_g_source_prepare, 
     89        session_g_source_check, 
     90        session_g_source_dispatch, 
     91        NULL 
     92}; 
     93 
     94static void session_setup_g_source(Session *s) { 
     95 
     96        if (!s->ggs) return; 
     97         
     98        if (s->g_source && s->g_pollfd.fd != s->ggs->fd) { 
     99                g_source_remove_poll(s->g_source, &s->g_pollfd); 
     100        } 
     101 
     102        if (!s->g_source) { 
     103                s->g_source = g_source_new(&source_funcs, sizeof(Source)); 
     104                ((Source *)(gpointer)s->g_source)->session=s; 
     105                g_source_attach(s->g_source,g_main_loop_get_context(main_loop)); 
     106        } 
     107         
     108        s->g_pollfd.events=G_IO_ERR|G_IO_HUP|G_IO_NVAL; 
     109        if (s->ggs->check&GG_CHECK_READ) s->g_pollfd.events|=G_IO_IN; 
     110        if (s->ggs->check&GG_CHECK_WRITE) s->g_pollfd.events|=G_IO_OUT; 
     111 
     112        if (s->g_pollfd.fd != s->ggs->fd) { 
     113                s->g_pollfd.fd=s->ggs->fd; 
     114                if (s->g_pollfd.fd!=-1) g_source_add_poll(s->g_source, &s->g_pollfd); 
     115        } 
     116} 
     117 
     118static void session_remove_g_source(Session *s) { 
     119 
     120        if (!s->g_source) return; 
     121 
     122        if (s->g_pollfd.fd!=-1) g_source_remove_poll(s->g_source, &s->g_pollfd); 
     123        g_source_destroy(s->g_source); 
     124        s->g_source=NULL; 
    61125} 
    62126 
     
    349413        } 
    350414        s->connected=0; 
    351         s->io_watch=0; 
    352415        session_schedule_reconnect(s); 
    353416        session_remove(s); 
     
    355418 
    356419 
    357 int session_io_handler(GIOChannel *source,GIOCondition condition,gpointer data){ 
    358 Session *s; 
     420int session_io_handler(Session *s){ 
    359421struct gg_event *event; 
    360422char *jid,*str; 
    361423int chat; 
    362 GIOCondition cond
     424GIOCondition condition=s->g_pollfd.revents
    363425time_t timestamp; 
    364426 
    365         s=(Session *)data; 
    366427        user_load_locale(s->user); 
    367428        debug(L_("Checking error conditions...")); 
     
    401462                                jabber_iq_send_error(s->s,s->jid,NULL,s->req_id,401,_("Unauthorized")); 
    402463                        else presence_send(s->s,NULL,s->user->jid,0,NULL,"Login failed",0); 
    403                         s->io_watch=0; 
    404464                        if (!s->req_id) 
    405465                                session_schedule_reconnect(s); 
     
    546606        } 
    547607 
    548         cond=G_IO_ERR|G_IO_HUP|G_IO_NVAL; 
    549         if (s->ggs->check&GG_CHECK_READ) cond|=G_IO_IN; 
    550         if (s->ggs->check&GG_CHECK_WRITE) cond|=G_IO_OUT; 
    551         s->io_watch=g_io_add_watch(s->ioch,cond,session_io_handler,s); 
     608        session_setup_g_source(s); 
    552609 
    553610        gg_event_free(event); 
     
    562619 
    563620        g_message(L_("Deleting session for '%s'"),s->jid); 
    564         if (s->io_watch) g_source_remove(s->io_watch); 
    565621        if (s->ping_timeout_func) g_source_remove(s->ping_timeout_func); 
    566622        if (s->timeout_func) g_source_remove(s->timeout_func); 
     
    579635                } 
    580636        } 
    581         if (s->ioch) g_io_channel_close(s->ioch); 
     637        session_remove_g_source(s); 
    582638        if (s->ggs){ 
    583639                if (s->connected){ 
     
    673729static int session_try_login(Session *s){ 
    674730struct gg_login_params login_params; 
    675 GIOCondition cond; 
    676731GgServer *serv; 
    677732int r; 
     
    681736 
    682737        if (s->timeout_func) g_source_remove(s->timeout_func); 
    683         if (s->io_watch) g_source_remove(s->io_watch); 
    684         if (s->ioch) { 
    685                 g_io_channel_close(s->ioch)
    686                s->ioch=NULL; 
    687         } 
     738        if (s->ggs) { 
     739               gg_free_session(s->ggs); 
     740                s->ggs=NULL
     741        } 
     742        session_remove_g_source(s); 
    688743 
    689744        memset(&login_params,0,sizeof(login_params)); 
     
    711766#endif 
    712767 
    713         if (s->ggs) gg_free_session(s->ggs); 
    714768        s->ggs=gg_login(&login_params); 
    715769        if (!s->ggs){ 
     
    717771                return 1; 
    718772        } 
    719  
    720         s->ioch=g_io_channel_unix_new(s->ggs->fd); 
    721         cond=G_IO_ERR|G_IO_HUP|G_IO_NVAL; 
    722         if (s->ggs->check&GG_CHECK_READ) cond|=G_IO_IN; 
    723         if (s->ggs->check&GG_CHECK_WRITE) cond|=G_IO_OUT; 
    724         s->io_watch=g_io_add_watch(s->ioch,cond,session_io_handler,s); 
     773        session_setup_g_source(s); 
    725774 
    726775        s->timeout_func=g_timeout_add(conn_timeout*1000,session_timeout,s); 
    727  
    728  
    729776        return FALSE; 
    730777} 
     
    750797        s->query=xmlnode_dup(query); 
    751798        s->current_server=g_list_first(gg_servers); 
     799        s->g_pollfd.fd=-1; 
    752800 
    753801        if (!delay_login && session_try_login(s)) return NULL; 
     
    10371085        } 
    10381086        g_message(L_("%sGG session: %p"),space,s->ggs); 
    1039         g_message(L_("%sIO Channel: %p"),space,s->ioch); 
     1087        g_message(L_("%sGSource: %p"),space,s->g_source); 
    10401088        g_message(L_("%sStream: %p"),space,s->s); 
    10411089        g_message(L_("%sConnected: %i"),space,s->connected); 
  • trunk/src/sessions.h

    r595 r607  
    4040        char *gg_status_descr; 
    4141 
    42         GIOChannel *ioch; /* GG IO Channel */ 
    43         guint io_watch
     42        GSource *g_source; 
     43        GPollFD g_pollfd
    4444 
    4545        int connected;