libevent 文档 (libevent Documentation)

介绍(Introduction

libevent is an event notification library for developing scalable network servers. The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Furthermore, libevent also support callbacks due to signals or regular timeouts.

 

libevent 是一个事件通知的库,这个是为开发可扩展网络服务而出现的。libevent的API提供一个机制用来在一个特定事件触发时或者一个超时的时刻执行一个回调函数。此外,libevent也支持由信号或规则的超时引起的事件。

 

libevent is meant to replace the event loop found in event driven network servers. An application just needs to call event_dispatch() and then add or remove events dynamically without having to change the event loop.

 

libevent意图是替换在网络服务中发现的循环事件。一个应用只需要调用event_dispatch()并通过动态的add 或者remove事件,而不用改变循环事件。

 

Currently, libevent supports /dev/poll, kqueue(2), select(2), poll(2) and epoll(4). It also has experimental support for real-time signals. The internal event mechanism is completely independent of the exposed event API, and a simple update of libevent can provide new functionality without having to redesign the applications. As a result, Libevent allows for portable application development and provides the most scalable event notification mechanism available on an operating system. Libevent can also be used for multi-threaded aplications; see Steven Grimm's explanation. Libevent should compile on Linux, *BSD, Mac OS X, Solaris and Windows.

 

 

Standard usage

Every program that uses libevent must include the <event.h > header, and pass the -levent flag to the linker. Before using any of the functions in the library, you must call event_init() or event_base_new() to perform one-time initialization of the libevent library.

Event notification

For each file descriptor that you wish to monitor, you must declare an event structure and call event_set() to initialize the members of the structure. To enable notification, you add the structure to the list of monitored events by calling event_add() . The event structure must remain allocated as long as it is active, so it should be allocated on the heap. Finally, you call event_dispatch() to loop and dispatch events.

I/O Buffers

libevent provides an abstraction on top of the regular event callbacks. This abstraction is called a buffered event. A buffered event provides input and output buffers that get filled and drained automatically. The user of a buffered event no longer deals directly with the I/O, but instead is reading from input and writing to output buffers.

Once initialized via bufferevent_new() , the bufferevent structure can be used repeatedly with bufferevent_enable() and bufferevent_disable() . Instead of reading and writing directly to a socket, you would call bufferevent_read() and bufferevent_write() .

When read enabled the bufferevent will try to read from the file descriptor and call the read callback. The write callback is executed whenever the output buffer is drained below the write low watermark, which is 0 by default.

Timers

libevent can also be used to create timers that invoke a callback after a certain amount of time has expired. The evtimer_set() function prepares an event struct to be used as a timer. To activate the timer, call evtimer_add() . Timers can be deactivated by calling evtimer_del() .

Timeouts

In addition to simple timers, libevent can assign timeout events to file descriptors that are triggered whenever a certain amount of time has passed with no activity on a file descriptor. The timeout_set() function initializes an event struct for use as a timeout. Once initialized, the event must be activated by using timeout_add() . To cancel the timeout, call timeout_del()

.

Asynchronous DNS resolution

libevent provides an asynchronous DNS resolver that should be used instead of the standard DNS resolver functions. These functions can be imported by including the <evdns.h > header in your program. Before using any of the resolver functions, you must call evdns_init() to initialize the library. To convert a hostname to an IP address, you call the evdns_resolve_ipv4() function. To perform a reverse lookup, you would call the evdns_resolve_reverse() function. All of these functions use callbacks to avoid blocking while the lookup is performed.

Event-driven HTTP servers

libevent provides a very simple event-driven HTTP server that can be embedded in your program and used to service HTTP requests.

To use this capability, you need to include the <evhttp.h > header in your program. You create the server by calling evhttp_new() . Add addresses and ports to listen on with evhttp_bind_socket() . You then register one or more callbacks to handle incoming requests. Each URI can be assigned a callback via the evhttp_set_cb() function. A generic callback function can also be registered via evhttp_set_gencb() ; this callback will be invoked if no other callbacks have been registered for a given URI.

A framework for RPC servers and clients

libevents provides a framework for creating RPC servers and clients. It takes care of marshaling and unmarshaling all data structures.

API Reference

To browse the complete documentation of the libevent API, click on any of the following links.

event.h The primary libevent header

evdns.h Asynchronous DNS resolution

evhttp.h An embedded libevent-based HTTP server

evrpc.h A framework for creating RPC servers and clients

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值