unsigned char *bipbuf_poll(bipbuf_t* me, const unsigned int size) { void *end; if (bipbuf_is_empty(me)) return NULL; /* make sure we can actually poll this data */ if (me->size < me->a_start + size) return NULL; end = me->data + me->a_start; me->a_start += size; /* we seem to be empty.. */ if (me->a_start == me->a_end) { /* replace a with region b */ if (1 == me->b_inuse) { me->a_start = 0; me->a_end = me->b_end; me->b_end = me->b_inuse = 0; } else { /* safely move the pointer back to the start because we are empty */ me->a_start = me->a_end = 0; } } return end; }
unsigned char *bipbuf_peek_all(const bipbuf_t* me, unsigned int *size) { if (bipbuf_is_empty(me)) return NULL; *size = me->a_end - me->a_start; return (unsigned char*)me->data + me->a_start; }
unsigned char *bipbuf_peek(const bipbuf_t* me, const unsigned int size) { /* make sure we can actually peek at this data */ if (me->size < me->a_start + size) return NULL; if (bipbuf_is_empty(me)) return NULL; return me->data + me->a_start; }
/** * poll info peer has information * */ int network_poll(void* caller, void **udata, const int msec_timeout, int (*func_process) (void *caller, void* nethandle, const char* buf, unsigned int len), void (*func_process_connection) (void *, void* nethandle, char *ip, int port) ) { client_t* me = *udata; hashmap_iterator_t iter; /* loop throught each connection for this peer */ for ( hashmap_iterator(me->connections, &iter); hashmap_iterator_has_next(me->connections, &iter); ) { client_connection_t* cn; cn = hashmap_iterator_next_value(me->connections, &iter); /* we need to process a connection request created by the peer */ if (0 == cn->connect_status) { char ip[32]; sprintf(ip, "%p", cn->nethandle); #if 0 /* debugging */ printf("processing connection me:%d them:%d\n", me->nethandle, cn->nethandle); #endif func_process_connection(me->bt, cn->nethandle, ip, 4000); cn->connect_status = CS_CONNECTED; } else if (!bipbuf_is_empty(cn->inbox)) { int len = bipbuf_get_spaceused(cn->inbox); if (0 < len) func_process(me->bt, (char*)cn->nethandle, (char*)bipbuf_poll(cn->inbox, len), len); } } return 1; }