基于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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值