IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> 基于libevent实现http服务 -> 正文阅读

[网络协议]基于libevent实现http服务

0.引用

官网中BASE相关接口

官网中HTTP相关接口

1.重要数据类型的定义

1.1 【struct event】

Structure to represent a single event.

An event can have some underlying condition it represents: a socket becoming readable or 
writeable (or both), or a signal becoming raised. (An event that represents no underlying 
condition is still useful: you can use one to implement a timer, or to communicate between 
threads.)

Generally, you can create events with event_new(), then make them pending with event_add(). 
As your event_base runs, it will run the callbacks of an events whose conditions are 
triggered. When you no longer want the event, free it with event_free().

?More Detail...

1.2??【struct event_base】

Structure to hold information and state for a Libevent dispatch loop.

The event_base lies at the center of Libevent; every application will have one. 
It keeps track of all pending and active events, and notifies your application 
of the active ones.

This is an opaque structure; you can allocate one using event_base_new() or event_base_new_with_config().

See also event_base_new(), event_base_free(), event_base_loop(), event_base_new_with_config()

1.3 【struct event_config】

Configuration for an event_base.

There are many options that can be used to alter the behavior and implementation of an event_base. To avoid having to pass them all in a complex many-argument constructor, we provide an abstract data type where you set up configuration information before passing it to event_base_new_with_config().

See also
event_config_new(), event_config_free(), event_base_new_with_config(), event_config_avoid_method(), event_config_require_features(), event_config_set_flag(), event_config_set_num_cpus_hint()

?

2.开发流程中的相关重要接口

2.1 创建【struct event_base】类型的指针

EVENT2_EXPORT_SYMBOL struct event_base* event_base_new(void)	
Create and return a new event_base to use with the rest of Libevent.

ReturnValue
Returns a new event_base on success, or NULL on failure.
See also event_base_free(), event_base_new_with_config()

2.2 创建【struct evhttp】类型的指针


EVENT2_EXPORT_SYMBOL struct evhttp* evhttp_new(struct event_base * 	base)	
Create a new HTTP server.

Parameters 
base	(optional) the event base to receive the HTTP events

ReturnValue
Returns a pointer to a newly initialized evhttp server structure or NULL on error
See also evhttp_free()

2.3 用evhttp_bind_socket函数来绑定端口

EVENT2_EXPORT_SYMBOL int evhttp_bind_socket	
(	
struct evhttp * 	http,
const char * 	address,
ev_uint16_t 	port 
)		
Binds an HTTP server on the specified address and port.

Can be called multiple times to bind the same http server to multiple different ports.

Parameters
http	a pointer to an evhttp object
address	a string containing the IP address to listen(2) on
port	the port number to listen on

ReturnValue
Returns 0 on success, -1 on failure.
See also evhttp_accept_socket()

2.4 用evhttp_set_gencb设置除用evhttp_set_cb设置的特殊处理函数以外请求的处理函数

EVENT2_EXPORT_SYMBOL void evhttp_set_gencb	
(	
    struct evhttp * http,
    void(*)(struct evhttp_request *, void *) cb,
    void * 	arg 
)		
Set a callback for all requests that are not caught by specific callbacks.
Invokes the specified callback for all requests that do not match any of the previously 
specified request paths. 
This is catchall for requests not specifically configured with evhttp_set_cb().

Parameters
http	the evhttp server object for which to set the callback
cb	    the callback to invoke for any unmatched requests
arg	    an context argument for the callback

2.5 用event_base_dispatch循环处理事件

EVENT2_EXPORT_SYMBOL int event_base_dispatch(struct event_base * base)	
Event dispatching loop.

This loop will run the event base until either there are no more pending or active, or 
until something calls event_base_loopbreak() or event_base_loopexit().

Parameters
base	the event_base structure returned by event_base_new() or 
event_base_new_with_config()

ReturnValue
Returns 0 if successful, -1 if an error occurred, or 1 if we exited because no events 
were pending or active.
See also event_base_loop()

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-04-06 16:25:55  更:2022-04-06 16:27:03 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 4:47:39-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码