[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [Libevent-users] Dynamic changes of read water mark
On Sun, Aug 29, 2010 at 08:08:12AM -0700, Gilad Benjamini wrote:
> My eventloop needs to go through this cycle
> - Read 16 bytes off the network
> - The 16 bytes contain a length parameter
> - Read additional "length" bytes from the network.
>
> It seems that this can be done with a buffered event which keeps changing
> the read low watermark (twice per message).
> Is this a "proper" way to use buffered events ?
>
Yes, bufferevents are fine for this, this abstracts some of the hard
work. I do the same thing in one of my applications. Something along
the lines of:
struct frame {
uint16_t something;
uint16_t length;
};
size_t frame_length = 4;
buf = bufferevent_get_input(bev);
while (evbuffer_get_length(buf) > 0) {
unsigned char *data;
unsigned char *payload;
struct frame frame;
data = evbuffer_pullup(buf, frame_length);
if (evbuffer_get_length(data) < frame_length) {
/* we don't have enough data yet to fill in our frame, return and wait for more */
break;
}
memcpy((char *)&frame.something, data, sizeof(uint16_t));
memcpy((char *)&frame.length, &data[sizeof(uint16_t)], sizeof(uint16_t))/
if ((evbuffer_get_length(buf) - frame_length) < frame.length) {
/* whole payload hasn't been seen */
break;
}
/* drain the header portion from the evbuffer */
evbuffer_drain(buf, frame_length);
if (!(payload = malloc(frame.length))) {
perror("malloc");
return;
}
evbuffer_remove_buffer(buf, payload, frame.length);
}
(not tested, just real quick example I could come up with)
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users in the body.