• 轻量级网络库libevent概况


      Libevent is a library for writing fast portable nonblocking IO.

      libevent是一个为编写快速可移植的非阻塞IO程序而设计的。

    libevent组件

      libevent包括了以下组件:

      1. evutil

      Generic functionality to abstract out the differences between different platforms' networking implementations.

      用于抽象不同平台网络实现差异的通用功能。

      2. event & event_base

      This is the heart of Libevent. It provides an abstract API to the various platform-specific, event-based nonblocking IO backends. It can let you know when sockets are ready to read or write, do basic timeout functionality, and detect OS signals.

      libevent的核心,为各种平台特定的、基于事件的非阻塞IO后端(backend,即epoll、select等)提供抽象API。它们能够通知应用套接字是否已经准备好读或者写,并具备处理超时的基本功能,且能够检测操作系统信号。

      3. bufferevent

      These functions provide a more convenient wrapper around Libevent’s event-based core. They let your application request buffered reads and writes, and rather than informing you when sockets are ready to do, they let you know when IO has actually occurred.

      (The bufferevent interface also has multiple backends, so that it can take advantage of systems that provide faster ways to do nonblocking IO, such as the Windows IOCP API.)

      bufferevent中的函数为libevent基于事件的核心提供了使用更方便的封装。它们使得我们的应用可以请求已经缓冲的读写操作。另外,它们让我们知道IO读写已经真正发生,而不是当套接字准备好读或写就通知我们。

      (bufferevent接口有多个后端,它可以采用系统提供的更快的非阻塞IO后端,例如Windows中的IOCP API。)

      4. evbuffer

      This module implements the buffers underlying bufferevents, and provides functions for efficient and/or convenient access.

      在bufferevent层之下实现了缓冲功能,并且提供了方便有效的访问函数。

      5. evhttp

      A simple HTTP client/server implementation.

      一个简单的HTTP客户端/服务器实现。

      6. evdns

      A simple DNS client/server implementation.

      一个简单的DNS客户端/服务器实现。

      7. evrpc

      A simple RPC implementation.

      一个简单的RPC实现。

    libevent头文件

      All current public Libevent headers are installed under the event2 directory. Headers fall into three broad classes:

      libevent公用头文件都安装在event2目录中。头文件可以大概分为三类:

      1. API headers

      An API header is one that defines current public interfaces to Libevent. These headers have no special suffix.

      定义libevent公用接口。这类头文件没有特定后缀。

      2. Compatibility headers

      A compatibility header includes definitions for deprecated functions. You shouldn’t include it unless you’re porting a program from an older version of Libevent.

      为已废弃的函数提供兼容。不应该使用这类头文件,除飞要移植使用较老版本libevent的程序。

      3. Structure headers

      These headers define structures with relatively volatile layouts. Some of these are exposed in case you need fast access to structure component; some are exposed for historical reasons. Relying on any of the structures in headers directly can break your program’s binary compatibility with other versions of Libevent, sometimes in hard-to-debug ways. These headers have the suffix "_struct.h" 

      这类头文件以相对不稳定的布局定义各种结构体。这些结构体中的一些是为了提供快速访问而暴露;一些是因为历史原因而暴露。直接依赖这类头文件中的任何结构体都会破坏程序对其他版本libevent的二进制兼容性。这些问题有时候是以非常难以调试的方式出现。这类头文件具有后缀“_struct.h”。

    源代码组织结构

      libevent的源代码虽然都在一层文件夹下面,但是其代码分类还是相当清晰的,主要可分为头文件、内部使用的头文件、辅助功能函数、日志、libevent框架、对系统I/O多路复用机制的封装、信号管理、定时事件管理、缓冲区管理、基本数据结构和基于libevent的两个实用库等几个部分,有些部分可能就是一个源文件。
      源代码中的test部分就不在我们关注的范畴了。
      1)头文件
      主要就是event.h:事件宏定义、接口函数声明,主要结构体event的声明;
      2)内部头文件
      xxx-internal.h:内部数据结构和函数,对外不可见,以达到信息隐藏的目的;
      3)libevent框架
      event.c:event整体框架的代码实现;
      4)对系统I/O多路复用机制的封装
      epoll.c:对epoll的封装;
      select.c:对select的封装;
      devpoll.c:对dev/poll的封装;
      kqueue.c:对kqueue的封装;
      5)定时事件管理
      min-heap.h:其实就是一个以时间作为key的小根堆结构;
      6)信号管理
      signal.c:对信号事件的处理;
      7)辅助功能函数
      evutil.h 和evutil.c:一些辅助功能函数,包括创建socket pair和一些时间操作函数:加、减和比较等。
      8)日志
      log.h和log.c:log日志函数
      9)缓冲区管理
      evbuffer.c和buffer.c:libevent对缓冲区的封装;
      10)基本数据结构
      compat/sys下的两个源文件:queue.h是libevent基本数据结构的实现,包括链表,双向链表,队列等;_libevent_time.h:一些用于时间操作的结构体定义、函数和宏定义;
      11)实用网络库
      http和evdns:是基于libevent实现的http服务器和异步dns查询库;

    libevent编译后的库

      1. event_core

      All core event and buffer functionality. This library contains all the event_base, evbuffer, bufferevent, and utility functions.

      该库包含了所有核心的事件和缓冲功能,包含了所有的event_base、evbuffer、bufferevent和辅助(utility)函数。

      2. event_extra

      This library defines protocol-specific functionality that you may or may not want for your application, including HTTP, DNS, and RPC.

      该库定义了程序可能需要,也可能不需要的协议特定功能,包括HTTP、DNS和RPC。

      3. event

      This library exists for historical reasons; it contains the contents of both libevent_core and libevent_extra. You shouldn’t use it; it may go away in a future version of Libevent.

      这个库因为历史原因而存在,它包含了libevent_core和libevent_extra的内容。我们不应该使用这个库。未来版本的libevent可能去掉这个库。

      某些平台上可能安装下列库:

      4. event_pthreads

      This library adds threading and locking implementations based on the pthreads portable threading library. It is separated from libevent_core so that you don’t need to link against pthreads to use Libevent unless you are actually using Libevent in a multithreaded way.

      该库添加了基于pthread可移植线程库的线程和锁实现。它独立于libevent_core,这样程序使用libevent时就不需要链接到pthread,除非是以多线程方式使用libevent。

      5. event_openssl

      This library provides support for encrypted communications using bufferevents and the OpenSSL library. It is separated from libevent_core so that you don’t need to link against OpenSSL to use Libevent unless you are actually using encrypted connections.

      该库为使用bufferevent和OpenSSL的加密通信提供支持。它独立于libevent_core,这样程序使用libevent时就不需要链接到OpenSSL,除非是进行加密通信。

    新老版本比较

      libevent 2.0以更合理的、不易出错的方式修正了API。如果可能,编写新程序时应该使用libevent 2.0。但是有时候可能需要使用较老的API,例如在升级已存的应用时,或者支持因为某些原因不能安装2.0或者更新版本libevent的环境时。较老版本的libevent头文件较少,也不安装在event2目录中。

      

      在2.0以及以后版本的libevent中,老的头文件仍然会作为新头文件的封装而存在。
      其他关于使用较老版本的提示:

    • V1.4版之前只有一个库libevent,它包含现在分散到libevent_core和libevent_extra中的所有功能。
    • V2.0版之前不支持锁:只有确定不同时在多个线程中使用同一个结构体时,libevent才是线程安全的。

    参考资料

      The Libevent Reference Manual: Preliminaries

      Libevent参考手册:前言

      libevent源码深度剖析四

  • 相关阅读:
    详解Java API之正则表达式
    ios UIWindow 错误使用导致无法接收motionEnded(摇一摇)函数
    ios NSTimer的强引用问题
    ionic 接触的第一个Hybrid项目
    iOS 小经验:UIAnimation空对象导致crash
    iOS GCD 必读推荐,有关于单例使用问题
    ios NSString 转 float的注意
    iOS UIViewController 和 nib 相关的3个方法
    ios delegate 使用注意 assign,weak
    ios 使用autolayout 后button 的frame 无法设置问题!
  • 原文地址:https://www.cnblogs.com/xiehongfeng100/p/4692819.html
Copyright © 2020-2023  润新知