您的位置:首页 > Web前端

libeventReferenceManual阅读笔记

2016-11-08 10:53 218 查看
一、01_intro.html

Example:A low-level ROT13 server with Libevent

这是一个利用event实现的server实例

Example:A simpler ROT13 server with Libevent

这是一个利用bufferevent实现的server实例

二、Ref0_meta.html

1.libevent设计目标:

Portability(可移植)、Speed(速度)、Scalability(可扩展性)、Convenience(便捷)

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

三、Ref1_libsetup.html

1.Log message in Libevent

2.Handling fatal errors

3.Memory management

You can have Libevent use another memory manager by providing your own replacements for malloc, realloc, and free.

4.Locks and threading

To get locking in Libevent, you must tell Libevent which locking functions to use. You need to do this before you call any Libevent function that allocates a structure that needs to be shared between threads.If you are using the pthreads library, or the native Windows threading code, you’re in luck. There are pre-defined functions that will set Libevent up to use the right pthreads or Windows functions for you.

四、Ref2_eventbase.html

1.Creating an event_base

If an event_base is set up to use locking, it is safe to access it between multiple threads. Its loop can only be run in a single thread, however. If you want to have multiple threads polling for IO, you need to have an event_base for each thread.

2.Setting up a default event_base

Interface:

struct event_base *event_base_new(void);

3.Setting up a complicated event_base

Interface:

struct event_config *event_config_new(void);
struct event_base *event_base_new_with_config(const struct event_config *cfg);
void event_config_free(struct event_config *cfg);

int event_config_avoid_method(struct event_config *cfg, const char *method);

enum event_method_feature {
EV_FEATURE_ET = 0x01,
EV_FEATURE_O1 = 0x02,
EV_FEATURE_FDS = 0x04,
};
int event_config_require_features(struct event_config *cfg,
enum event_method_feature feature);

enum event_base_config_flag {
EVENT_BASE_FLAG_NOLOCK = 0x01,
EVENT_BASE_FLAG_IGNORE_ENV = 0x02,
EVENT_BASE_FLAG_STARTUP_IOCP = 0x04,
EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08,
EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10,
EVENT_BASE_FLAG_PRECISE_TIMER = 0x20
};
int event_config_set_flag(struct event_config *cfg,
enum event_base_config_flag flag);

int event_config_set_num_cpus_hint(struct event_config *cfg, int cpus)

4.Examining an event_base's backend method

Sometimes you want to see which features are actually available in an event_base, or which method it’s using.

Interface:

const char **event_get_supported_methods(void);

const char *event_base_get_method(const struct event_base *base);
enum event_method_feature event_base_get_features(const struct event_base *base);

5.Deallocating an event_base

Interface:

void event_base_free(struct event_base *base);

五、Ref3_eventloop.html

1.Running the loop

Interface:

#define EVLOOP_ONCE             0x01
#define EVLOOP_NONBLOCK         0x02
#define EVLOOP_NO_EXIT_ON_EMPTY 0x04

int event_base_loop(struct event_base *base, int flags);

int event_base_dispatch(struct event_base *base);

int event_base_loopexit(struct event_base *base, const struct timeval *tv);
int event_base_loopbreak(struct event_base *base);

2.Checking the internal time cache

Interface:

int event_base_gettimeofday_cached(struct event_base *base,
struct timeval *tv_out);

int event_base_update_cache_time(struct event_base *base);

3.Running a function over every event in an event_base

Interface:

typedef int (*event_base_foreach_event_cb)(const struct event_base *,
const struct event *, void *);

int event_base_foreach_event(struct event_base *base,
event_base_foreach_event_cb fn,
void *arg);

六、Ref4_event.html

Libevent’s basic unit of operation is the event. Every event represents a set of conditions, including:

A file descriptor being ready to read from or write to.

A file descriptor becoming ready to read from or write to (Edge-triggered IO only).

A timeout expiring.

A signal occurring.

A user-triggered event.

1.Constructing event objects

Interface:

#define EV_TIMEOUT      0x01
#define EV_READ         0x02
#define EV_WRITE        0x04
#define EV_SIGNAL       0x08
#define EV_PERSIST      0x10
#define EV_ET           0x20

typedef void (*event_callback_fn)(evutil_socket_t, short, void *);

struct event *event_new(struct event_base *base, evutil_socket_t fd,
short what, event_callback_fn cb,
void *arg);

void event_free(struct event *event);

2.Creating an event as its own callback argument

Interface:

void *event_self_cbarg();

The event_self_cbarg() function returns a "magic" pointer which, when passed as an event callback argument, tells event_new() to create an event receiving itself as its callback argument.

3.Making events pending and non-pending

Interface:

int event_add(struct event *ev, const struct timeval *tv);

int event_del(struct event *ev);

4.Configuring one-off events

If you don’t need to add an event more than once, or delete it once it has been added, and it doesn’t have to be persistent, you can use event_base_once()

Interface:

int event_base_once(struct event_base *, evutil_socket_t, short,
void (*)(evutil_socket_t, short, void *), void *, const struct timeval *);

5.Manually activating an event

Rarely, you may want to make an event active even though its conditions have not triggered.

Interface:

void event_active(struct event *ev, int what, short ncalls);

七、Ref5_evutil.html

1.Basic types

evutil_socket_t

2.Timer portability functions

3.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: