Changeset 1563
- Timestamp:
- Mar 12, 2007, 5:42:39 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gtk/ipc.c
r1504 r1563 44 44 #include "util.h" 45 45 46 #define PROTOVERS 1 /* IPC protocol version */ 46 /* IPC protocol version */ 47 #define PROTO_VERS_MIN ( 1 ) 48 #define PROTO_VERS_MAX ( 1 ) 47 49 48 50 /* int, IPC protocol version */ … … 79 81 GSource *source; 80 82 int fd; 83 int vers; 81 84 const struct handlerdef *funcs; 82 85 enum contype type; … … 99 102 send_msg(struct constate *con, const char *name, benc_val_t *val); 100 103 static int 101 send_msg_int(struct constate *con, const char *name, int num); 104 send_msg_vers_new(struct constate *con); 105 static int 106 send_msg_vers_old(struct constate *con); 102 107 static unsigned int 103 108 all_io_received(GSource *source, char *data, unsigned int len, void *vdata); … … 107 112 all_io_closed(GSource *source, void *vdata); 108 113 static void 114 srv_vers(struct constate *con, const char *name, benc_val_t *val); 115 static void 109 116 srv_addfile(struct constate *con, const char *name, benc_val_t *val); 110 117 static void … … 114 121 static void 115 122 afc_io_sent(GSource *source, unsigned int id, void *vdata); 123 static int 124 ipc_checkversion( benc_val_t * vers ); 125 static int 126 getvers( benc_val_t * dict, const char * key ); 116 127 117 128 static const struct handlerdef gl_funcs_serv[] = { 129 {MSG_VERSION, srv_vers}, 118 130 {MSG_ADDFILES, srv_addfile}, 119 131 {MSG_QUIT, srv_quit}, … … 138 150 con->source = NULL; 139 151 con->fd = -1; 152 con->vers = -1; 140 153 con->funcs = gl_funcs_serv; 141 154 con->type = CON_SERV; … … 159 172 con->source = NULL; 160 173 con->fd = -1; 174 con->vers = -1; 161 175 con->funcs = gl_funcs_client; 162 176 con->type = CON_CLIENT; … … 263 277 all_io_closed, con); 264 278 279 if( NULL != con->source ) 280 { 281 send_msg_vers_new( con ); 282 } 283 265 284 return TRUE; 266 285 } … … 277 296 newcon->source = io_new(fd, NULL, all_io_received, all_io_closed, newcon); 278 297 298 /* XXX need to check for incoming version from client */ 299 279 300 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 ); 281 303 else { 282 304 g_free(newcon); … … 324 346 325 347 static 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); 348 send_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 372 static int 373 send_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 ); 334 382 } 335 383 … … 396 444 397 445 static void 446 srv_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 460 static void 398 461 srv_addfile(struct constate *con, const char *name SHUTUP, benc_val_t *val) { 399 462 struct constate_serv *srv = &con->u.serv; … … 431 494 benc_val_t list, *str; 432 495 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; 437 509 } 438 510 … … 475 547 } 476 548 } 549 550 int 551 ipc_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 586 int 587 getvers( 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.