Changeset 1563


Ignore:
Timestamp:
Mar 12, 2007, 5:42:39 AM (15 years ago)
Author:
joshe
Message:

Use a new, more flexible version message in ipc.
Don't actually have the server send it yet, old clients can't handle it.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/ipc.c

    r1504 r1563  
    4444#include "util.h"
    4545
    46 #define PROTOVERS               1       /* IPC protocol version */
     46/* IPC protocol version */
     47#define PROTO_VERS_MIN          ( 1 )
     48#define PROTO_VERS_MAX          ( 1 )
    4749
    4850/* int, IPC protocol version */
     
    7981  GSource *source;
    8082  int fd;
     83  int vers;
    8184  const struct handlerdef *funcs;
    8285  enum contype type;
     
    99102send_msg(struct constate *con, const char *name, benc_val_t *val);
    100103static int
    101 send_msg_int(struct constate *con, const char *name, int num);
     104send_msg_vers_new(struct constate *con);
     105static int
     106send_msg_vers_old(struct constate *con);
    102107static unsigned int
    103108all_io_received(GSource *source, char *data, unsigned int len, void *vdata);
     
    107112all_io_closed(GSource *source, void *vdata);
    108113static void
     114srv_vers(struct constate *con, const char *name, benc_val_t *val);
     115static void
    109116srv_addfile(struct constate *con, const char *name, benc_val_t *val);
    110117static void
     
    114121static void
    115122afc_io_sent(GSource *source, unsigned int id, void *vdata);
     123static int
     124ipc_checkversion( benc_val_t * vers );
     125static int
     126getvers( benc_val_t * dict, const char * key );
    116127
    117128static const struct handlerdef gl_funcs_serv[] = {
     129  {MSG_VERSION,  srv_vers},
    118130  {MSG_ADDFILES, srv_addfile},
    119131  {MSG_QUIT,     srv_quit},
     
    138150  con->source = NULL;
    139151  con->fd = -1;
     152  con->vers = -1;
    140153  con->funcs = gl_funcs_serv;
    141154  con->type = CON_SERV;
     
    159172  con->source = NULL;
    160173  con->fd = -1;
     174  con->vers = -1;
    161175  con->funcs = gl_funcs_client;
    162176  con->type = CON_CLIENT;
     
    263277                       all_io_closed, con);
    264278
     279  if( NULL != con->source )
     280  {
     281      send_msg_vers_new( con );
     282  }
     283
    265284  return TRUE;
    266285}
     
    277296  newcon->source = io_new(fd, NULL, all_io_received, all_io_closed, newcon);
    278297
     298  /* XXX need to check for incoming version from client */
     299
    279300  if(NULL != newcon->source)
    280     send_msg_int(newcon, MSG_VERSION, PROTOVERS);
     301    /* XXX need to switch to new version scheme after the next release */
     302    send_msg_vers_old( newcon );
    281303  else {
    282304    g_free(newcon);
     
    324346
    325347static int
    326 send_msg_int(struct constate *con, const char *name, int num) {
    327   benc_val_t val;
    328 
    329   bzero(&val, sizeof(val));
    330   val.type = TYPE_INT;
    331   val.val.i = num;
    332 
    333   return send_msg(con, name, &val);
     348send_msg_vers_new( struct constate * con )
     349{
     350    benc_val_t dict;
     351
     352    /* XXX ugh, I need to merge the pex branch and use it's benc funcs */
     353    bzero( &dict, sizeof dict );
     354    dict.type                  = TYPE_DICT;
     355    dict.val.l.alloc           = 4;
     356    dict.val.l.count           = 4;
     357    dict.val.l.vals            = g_new0( benc_val_t, 4 );
     358    dict.val.l.vals[0].type    = TYPE_STR;
     359    dict.val.l.vals[0].val.s.i = 3;
     360    dict.val.l.vals[0].val.s.s = "min";
     361    dict.val.l.vals[1].type    = TYPE_INT;
     362    dict.val.l.vals[1].val.i   = PROTO_VERS_MIN;
     363    dict.val.l.vals[2].type    = TYPE_STR;
     364    dict.val.l.vals[2].val.s.i = 3;
     365    dict.val.l.vals[2].val.s.s = "max";
     366    dict.val.l.vals[3].type    = TYPE_INT;
     367    dict.val.l.vals[3].val.i   = PROTO_VERS_MAX;
     368
     369    return send_msg( con, MSG_VERSION, &dict );
     370}
     371
     372static int
     373send_msg_vers_old( struct constate * con )
     374{
     375    benc_val_t val;
     376
     377    bzero( &val, sizeof val );
     378    val.type  = TYPE_INT;
     379    val.val.i = PROTO_VERS_MIN;
     380
     381    return send_msg( con, MSG_VERSION, &val );
    334382}
    335383
     
    396444
    397445static void
     446srv_vers( struct constate * con, const char * name SHUTUP, benc_val_t * val )
     447{
     448    if( 0 > con->vers )
     449    {
     450        con->vers = ipc_checkversion( val );
     451        if( 0 > con->vers )
     452        {
     453            fprintf( stderr, _("bad IPC protocol version\n") );
     454            destroycon( con );
     455            return;
     456        }
     457    }
     458}
     459
     460static void
    398461srv_addfile(struct constate *con, const char *name SHUTUP, benc_val_t *val) {
    399462  struct constate_serv *srv = &con->u.serv;
     
    431494  benc_val_t list, *str;
    432495
    433   if(TYPE_INT != val->type || PROTOVERS != val->val.i) {
    434     fprintf(stderr, _("bad IPC protocol version\n"));
    435     destroycon(con);
    436     return;
     496  if( 0 > con->vers )
     497  {
     498      con->vers = ipc_checkversion( val );
     499      if( 0 > con->vers )
     500      {
     501          fprintf( stderr, _("bad IPC protocol version\n") );
     502          destroycon( con );
     503          return;
     504      }
     505  }
     506  else
     507  {
     508      return;
    437509  }
    438510
     
    475547  }
    476548}
     549
     550int
     551ipc_checkversion( benc_val_t * vers )
     552{
     553    int min, max;
     554
     555    if( TYPE_INT == vers->type )
     556    {
     557        if( 0 > vers->val.i )
     558        {
     559            return -1;
     560        }
     561        min = max = vers->val.i;
     562    }
     563    else if( TYPE_DICT == vers->type )
     564    {
     565        min = getvers( vers, "min" );
     566        max = getvers( vers, "max" );
     567        if( 0 > min || 0 > max )
     568        {
     569            return -1;
     570        }
     571    }
     572    else
     573    {
     574        return -1;
     575    }
     576
     577    g_assert( PROTO_VERS_MIN <= PROTO_VERS_MAX );
     578    if( min > max || PROTO_VERS_MAX < min || PROTO_VERS_MIN > max )
     579    {
     580        return -1;
     581    }
     582
     583    return MIN( PROTO_VERS_MAX, max );
     584}
     585
     586int
     587getvers( benc_val_t * dict, const char * key )
     588{
     589    benc_val_t * val;
     590
     591    val = tr_bencDictFind( dict, key );
     592    if( NULL == val || TYPE_INT != val->type || 0 > val->val.i )
     593    {
     594        return -1;
     595    }
     596
     597    return val->val.i;
     598}
Note: See TracChangeset for help on using the changeset viewer.