我有以下读取回调函数readcb
,它是在读取事件时调用的。我的问题是,bufferevent_get_input
只能从底层缓冲区读取4096个字节。我怎样才能让它读取所有字节。我也使用了一个变体,下面给出readcb_variant
。它也只读取4096字节并且与n = 0
一起存在,意味着没有更多字节要读取。libevent:读取缓冲区中的所有字节
void readcb(struct bufferevent *bev, void *ctx){
size_t bytesread;
char buf[10000];
struct evbuffer *input, *output;
input = bufferevent_get_input(bev);
bytesread = evbuffer_get_length(input);
evbuffer_remove(input, buf, bytesread);
printf("%zu\n", bytesread); // prints 4096 for long (> 4096) byte stream
do_some_cool_stuff(buf);
}
void readcb_variant(struct bufferevent *bev, void *ctx){
char tmp[1000], buf[10000];
size_t n, bytesread = 0;
char *runner = buf;
while(1){
n = bufferevent_read(bev, tmp, sizeof(tmp));
if (n <= 0){
printf("%zu\n", n); // prints '0';
break;
}
memcpy(runner, tmp, n);
runner += n;
bytesread += n;
}
printf("%zu\n", bytesread); // prints 4096 for long (> 4096) byte stream
do_some_cool_stuff(buf);
}
更新 近距离观察到的libevent的源代码后,我发现,这行代码#define EVBUFFER_MAX_READ 4096
在source code。硬编码限制。
您可以尝试将'%zu'更改为'%lu'吗? http://en.cppreference.com/w/cpp/io/c/fprintf –