lievent源码分析:evbuffer
Posted 冷冰若水
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lievent源码分析:evbuffer相关的知识,希望对你有一定的参考价值。
struct evbuffer定义在evbuffer-internal.h文件中。
evbuffer结构内部保存一个以evbuffer-chain结构为节点的链表,evbuffer内部有两个分别指向首尾节点的指针。
1 struct evbuffer { 2 /** The first chain in this buffer‘s linked list of chains. */ 3 struct evbuffer_chain *first; 4 /** The last chain in this buffer‘s linked list of chains. */ 5 struct evbuffer_chain *last; 6 7 /** Pointer to the next pointer pointing at the ‘last_with_data‘ chain. 8 * 9 * To unpack: 10 * 11 * The last_with_data chain is the last chain that has any data in it. 12 * If all chains in the buffer are empty, it is the first chain. 13 * If the buffer has no chains, it is NULL. 14 * 15 * The last_with_datap pointer points at _whatever ‘next‘ pointer_ 16 * points at the last_with_datap chain. If the last_with_data chain 17 * is the first chain, or it is NULL, then the last_with_datap pointer 18 * is &buf->first. 19 */ 20 struct evbuffer_chain **last_with_datap; 21 22 /** Total amount of bytes stored in all chains.*/ 23 size_t total_len; 24 25 /** Number of bytes we have added to the buffer since we last tried to 26 * invoke callbacks. */ 27 size_t n_add_for_cb; 28 /** Number of bytes we have removed from the buffer since we last 29 * tried to invoke callbacks. */ 30 size_t n_del_for_cb; 31 32 #ifndef EVENT__DISABLE_THREAD_SUPPORT 33 /** A lock used to mediate access to this buffer. */ 34 void *lock; 35 #endif 36 /** True iff we should free the lock field when we free this 37 * evbuffer. */ 38 unsigned own_lock : 1; 39 /** True iff we should not allow changes to the front of the buffer 40 * (drains or prepends). */ 41 unsigned freeze_start : 1; 42 /** True iff we should not allow changes to the end of the buffer 43 * (appends) */ 44 unsigned freeze_end : 1; 45 /** True iff this evbuffer‘s callbacks are not invoked immediately 46 * upon a change in the buffer, but instead are deferred to be invoked 47 * from the event_base‘s loop. Useful for preventing enormous stack 48 * overflows when we have mutually recursive callbacks, and for 49 * serializing callbacks in a single thread. */ 50 unsigned deferred_cbs : 1; 51 #ifdef _WIN32 52 /** True iff this buffer is set up for overlapped IO. */ 53 unsigned is_overlapped : 1; 54 #endif 55 /** Zero or more EVBUFFER_FLAG_* bits */ 56 ev_uint32_t flags; 57 58 /** Used to implement deferred callbacks. */ 59 struct event_base *cb_queue; 60 61 /** A reference count on this evbuffer. When the reference count 62 * reaches 0, the buffer is destroyed. Manipulated with 63 * evbuffer_incref and evbuffer_decref_and_unlock and 64 * evbuffer_free. */ 65 int refcnt; 66 67 /** A struct event_callback handle to make all of this buffer‘s callbacks 68 * invoked from the event loop. */ 69 struct event_callback deferred; 70 71 /** A doubly-linked-list of callback functions */ 72 LIST_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks; 73 74 /** The parent bufferevent object this evbuffer belongs to. 75 * NULL if the evbuffer stands alone. */ 76 struct bufferevent *parent; 77 };
struct evbuffer_chain:
evbuffer-chain结构内部保存一个表示buffer内容长度的变量以及一个char*的指针指向buffer内容所在的位置。
1 /** A single item in an evbuffer. */ 2 struct evbuffer_chain { 3 /** points to next buffer in the chain */ 4 struct evbuffer_chain *next; 5 6 /** total allocation available in the buffer field. */ 7 size_t buffer_len; 8 9 /** unused space at the beginning of buffer or an offset into a 10 * file for sendfile buffers. */ 11 ev_misalign_t misalign; 12 13 /** Offset into buffer + misalign at which to start writing. 14 * In other words, the total number of bytes actually stored 15 * in buffer. */ 16 size_t off; 17 18 /** Set if special handling is required for this chain */ 19 unsigned flags; 20 #define EVBUFFER_FILESEGMENT 0x0001 /**< A chain used for a file segment */ 21 #define EVBUFFER_SENDFILE 0x0002 /**< a chain used with sendfile */ 22 #define EVBUFFER_REFERENCE 0x0004 /**< a chain with a mem reference */ 23 #define EVBUFFER_IMMUTABLE 0x0008 /**< read-only chain */ 24 /** a chain that mustn‘t be reallocated or freed, or have its contents 25 * memmoved, until the chain is un-pinned. */ 26 #define EVBUFFER_MEM_PINNED_R 0x0010 27 #define EVBUFFER_MEM_PINNED_W 0x0020 28 #define EVBUFFER_MEM_PINNED_ANY (EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W) 29 /** a chain that should be freed, but can‘t be freed until it is 30 * un-pinned. */ 31 #define EVBUFFER_DANGLING 0x0040 32 /** a chain that is a referenced copy of another chain */ 33 #define EVBUFFER_MULTICAST 0x0080 34 35 /** number of references to this chain */ 36 int refcnt; 37 38 /** Usually points to the read-write memory belonging to this 39 * buffer allocated as part of the evbuffer_chain allocation. 40 * For mmap, this can be a read-only buffer and 41 * EVBUFFER_IMMUTABLE will be set in flags. For sendfile, it 42 * may point to NULL. 43 */ 44 unsigned char *buffer; 45 }; 46 47 /** callback for a reference chain; lets us know what to do with it when 48 * we‘re done with it. Lives at the end of an evbuffer_chain with the 49 * EVBUFFER_REFERENCE flag set */ 50 struct evbuffer_chain_reference { 51 evbuffer_ref_cleanup_cb cleanupfn; 52 void *extra; 53 };
以上是关于lievent源码分析:evbuffer的主要内容,如果未能解决你的问题,请参考以下文章
Android 插件化VirtualApp 源码分析 ( 目前的 API 现状 | 安装应用源码分析 | 安装按钮执行的操作 | 返回到 HomeActivity 执行的操作 )(代码片段
Android 逆向整体加固脱壳 ( DEX 优化流程分析 | DexPrepare.cpp 中 dvmOptimizeDexFile() 方法分析 | /bin/dexopt 源码分析 )(代码片段
Android 事件分发事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )(代码片段