> > It seems that this can be done with a buffered event which keeps > changing > > the read low watermark (twice per message).
Your sample code did not include the watermark part. May I assume that at the end of the code snippet you would set the low watermark ? If I set the low watermark to X, am I guaranteed that the next time I reach the callback at least X bytes are available ? Is that also true if I leave some bytes in the buffer at the end of my processing ? > > 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); Is there a good reason to do the pullup before checking the length, or was that just placed here while you quickly came up with a sample code ? > > 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); > } > *********************************************************************** To unsubscribe, send an e-mail to majord...@freehaven.net with unsubscribe libevent-users in the body.