Changeset c9402c3 in code


Ignore:
Timestamp:
Mar 5, 2014, 10:42:33 AM (11 years ago)
Author:
Mike Belopuhov <mike@…>
Branches:
master
Children:
fdbbc45
Parents:
efa8586
Message:

Rework bufferevent read code

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • icb.h

    refa8586 rc9402c3  
    7676        struct icb_group        *group;
    7777        size_t                   length;
     78        size_t                   rlen;
    7879        time_t                   login;
    7980        time_t                   last;
  • icbd.c

    refa8586 rc9402c3  
    312312        struct icb_session *is = (struct icb_session *)arg;
    313313
    314         if (is->length == 0) {
    315                 bzero(is->buffer, sizeof is->buffer);
    316                 /* read length */
    317                 (void)bufferevent_read(bev, is->buffer, 1);
    318                 /* we're about to read the whole packet */
    319                 is->length = (size_t)(unsigned char)is->buffer[0];
     314        while (EVBUFFER_LENGTH(EVBUFFER_INPUT(bev)) > 0) {
    320315                if (is->length == 0) {
    321                         icbd_drop(is, "invalid packet");
     316                        /* read length */
     317                        is->rlen = bufferevent_read(bev, is->buffer, 1);
     318                        is->length = (size_t)(unsigned char)is->buffer[0];
     319                        if (is->length == 0) {
     320                                icbd_drop(is, "invalid packet");
     321                                return;
     322                        }
     323                }
     324                /* read as much as we can */
     325                is->rlen += bufferevent_read(bev, &is->buffer[is->rlen],
     326                    is->length);
     327#ifdef DEBUG
     328                {
     329                        int i;
     330
     331                        printf("-> read %lu out of %lu from %s:%d:\n",
     332                            is->rlen, is->length, is->host, is->port);
     333                        for (i = 0; i < (int)is->rlen; i++)
     334                                printf(" %02x", (unsigned char)is->buffer[i]);
     335                        printf("\n");
     336                }
     337#endif
     338                /* see you next time around */
     339                if (is->rlen < is->length)
    322340                        return;
    323                 }
    324                 if (EVBUFFER_LENGTH(EVBUFFER_INPUT(bev)) < is->length) {
    325                         /* set watermark to the expected length */
    326                         bufferevent_setwatermark(bev, EV_READ, is->length, 0);
    327                         return;
    328                 }
    329         }
    330         (void)bufferevent_read(bev, &is->buffer[1], is->length);
    331 #ifdef DEBUG
    332         {
    333                 int i;
    334 
    335                 printf("-> read from %s:%d:\n", is->host, is->port);
    336                 for (i = 0; i < (int)is->length + 1; i++)
    337                         printf(" %02x", (unsigned char)is->buffer[i]);
    338                 printf("\n");
    339         }
    340 #endif
    341         icb_input(is);
    342         is->length = 0;
     341                /* process the message in full */
     342                icb_input(is);
     343                is->rlen = is->length = 0;
     344        }
    343345}
    344346
     
    352354                int i;
    353355
    354                 printf("-> wrote to %s:%d:\n", is->host, is->port);
     356                printf("-> wrote %lu to %s:%d:\n", size, is->host, is->port);
    355357                for (i = 0; i < size; i++)
    356358                        printf(" %02x", (unsigned char)buf[i]);
Note: See TracChangeset for help on using the changeset viewer.