libevent源码分析bufferevent_read,evbuffer_remove,evbuffer_copyout_from
read,读取数据
/** Read data from a bufferevent buffer. The bufferevent_read() function is used to read data from the input buffer.从输入缓存中读取数据 @param bufev the bufferevent to be read from 将要读取的bufferevent @param data pointer to a buffer that will store the data 存储数据的缓存区 @param size the size of the data buffer, in bytes 数据缓存区的大小,单位字节 @return the amount of data read, in bytes. 读取的数据大小,单位字节 */
size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size) { return (evbuffer_remove(bufev->input, data, size)); }
/** Read data from an evbuffer and drain the bytes read. 读取缓存区的数据,并清空读取的读取的字节 If more bytes are requested than are available in the evbuffer, we only extract as many bytes as were available. 如果请求的字节数超过了缓存区的可用字节数,只会提取尽可能多的字节 @param buf the evbuffer to be read from 将要读取数据的缓存区 @param data the destination buffer to store the result 用于存储结果的目标缓存区 @param datlen the maximum size of the destination buffer 目标缓存区的最大值 @return the number of bytes read, or -1 if we can't drain the buffer. 返回读取的字节数,如果无法清空缓存,返回-1 */
int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
/* Reads data from an event buffer and drains the bytes read */ int evbuffer_remove(struct evbuffer *buf, void *data_out, size_t datlen) { ev_ssize_t n; EVBUFFER_LOCK(buf); // 添加了锁 n = evbuffer_copyout_from(buf, NULL, data_out, datlen); if (n > 0) { if (evbuffer_drain(buf, n)<0) n = -1; } EVBUFFER_UNLOCK(buf); return (int)n; }
/** Read data from the middle of an evbuffer, and leave the buffer unchanged. 从缓存区中读取数据,同时不改变缓存区 If more bytes are requested than are available in the evbuffer, we only extract as many bytes as were available. 如果请求的字节数超过了缓存区的可用字节数,只提取尽可能多的字节 @param buf the evbuffer to be read from 将要读取的缓存区 @param pos the position to start reading from 开始读取的位置 @param data_out the destination buffer to store the result 存储结果的目标缓存区 @param datlen the maximum size of the destination buffer 目标缓存区的最大值 @return the number of bytes read, or -1 if we can't drain the buffer. 读取的字节数,如果无法清空缓存返回-1 */
ev_ssize_t evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos, void *data_out, size_t datlen);
ev_ssize_t evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos, void *data_out, size_t datlen) { /*XXX fails badly on sendfile case. */ struct evbuffer_chain *chain; char *data = data_out; size_t nread; ev_ssize_t result = 0; size_t pos_in_chain; EVBUFFER_LOCK(buf); if (pos) { if (datlen > (size_t)(EV_SSIZE_MAX - pos->pos)) { result = -1; goto done; } chain = pos->internal_.chain; pos_in_chain = pos->internal_.pos_in_chain; if (datlen + pos->pos > buf->total_len) datlen = buf->total_len - pos->pos; } else { chain = buf->first; pos_in_chain = 0; if (datlen > buf->total_len) datlen = buf->total_len; } if (datlen == 0) goto done; if (buf->freeze_start) { result = -1; goto done; } nread = datlen; while (datlen && datlen >= chain->off - pos_in_chain) { size_t copylen = chain->off - pos_in_chain; memcpy(data, chain->buffer + chain->misalign + pos_in_chain, copylen); data += copylen; datlen -= copylen; chain = chain->next; pos_in_chain = 0; EVUTIL_ASSERT(chain || datlen==0); } if (datlen) { EVUTIL_ASSERT(chain); EVUTIL_ASSERT(datlen+pos_in_chain <= chain->off); memcpy(data, chain->buffer + chain->misalign + pos_in_chain, datlen); } result = nread; done: EVBUFFER_UNLOCK(buf); return result; }

浙公网安备 33010602011771号