FYI: http://www.wangafu.net/~nickm/libevent-book/Ref5_evutil.html

Helper functions and types for Libevent

Basic types

 #ifdef WIN32
#define evutil_socket_t intptr_t
#else
#define evutil_socket_t int
#endif

Miscellaneous compatibility types

The ev_ssize_t type is defined to ssize_t (signed size_t) on platforms that have one, and to a reasonable default on platforms that don’t. The largest possible value of ev_ssize_t is EV_SSIZE_MAX; the smallest is EV_SSIZE_MIN. (The largest possible value for size_t is EV_SIZE_MAX, in case your platform doesn’t define a SIZE_MAX for you.)

The ev_off_t type is used to represent offset into a file or a chunk of memory. It is defined to off_t on platforms with a reasonable off_t definition, and to ev_int64_t on Windows.

Some implementations of the sockets API provide a length type, socklen_t, and some do not. The ev_socklen_t is defined to this type where it exists, and a reasonable default otherwise.

The ev_intptr_t type is a signed integer that is large enough to hold a pointer without loss of bits. The ev_uintptr_t type is an unsigned integer large enough to hold a pointer without loss of bits.

Timer portability functions

 #define evutil_timeradd(tvp, uvp, vvp) /* .+. */
#define evutil_timersub(tvp, uvp, vvp) /* .-. */
#define evutil_timerclear(tvp) /* .clear. */
#define evutil_timerisset(tvp) /* .clear. */
#define evutil_timercmp(tvp, uvp, cmp) /* <=, <, ==, >, >=, !=*/
int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
Note It wasn’t safe to use ⇐ or >= with timercmp before Libevent 1.4.4.

Socket API compatibility

This section exists because, for historical reasons, Windows has never really implemented the Berkeley sockets API in a nice compatible (and nicely compatible) way. Here are some functions you can use in order to pretend that it has.

Interface
 int evutil_closesocket(evutil_socket_t s);
#define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
#define EVUTIL_SOCKET_ERROR()
#define EVUTIL_SET_SOCKET_ERROR(errcode)
#define evutil_socket_geterror(sock)
#define evutil_socket_error_to_string(errcode)

These macros access and manipulate socket error codes. EVUTIL_SOCKET_ERROR() returns the global error code for the last socket operation from this thread, and evutil_socket_geterror() does so for a particular socket. (Both are errno on Unix-like systems.) EVUTIL_SET_SOCKET_ERROR() changes the current socket error code (like setting errno on Unix), and evutil_socket_error_to_string() returns a string representation of a given socket error code (like strerror() on Unix).

(We need these functions because Windows doesn’t use errno for errors from socket functions, but instead uses WSAGetLastError().)

Note that the Windows socket errors are not the same as the standard-C errors you would see in errno; watch out.

Interface
 int evutil_make_socket_nonblocking(evutil_socket_t sock);

Even the call you need to do nonblocking IO on a socket is not portable to Windows. The evutil_make_socket_nonblocking() function takes a new socket (from socket() or accept()) and turns it into a nonblocking socket. (It sets O_NONBLOCK on Unix and FIONBIO on Windows.)

Interface
int evutil_make_listen_socket_reuseable(evutil_socket_t sock);

This function makes sure that the address used by a listener socket will be available to another socket immediately after the socket is closed. (It sets SO_REUSEADDR on Unix and does nothing on Windows. You don’t want to use SO_REUSEADDR on Windows; it means something different there.)

Interface
int evutil_make_socket_closeonexec(evutil_socket_t sock);

This call tells the operating system that this socket should be closed if we ever call exec(). It sets the FD_CLOEXEC flag on Unix, and does nothing on Windows.

Interface
int evutil_socketpair(int family, int type, int protocol, evutil_socket_t sv[]);

This function behaves as the Unix socketpair() call: it makes two sockets that are connected with each other and can be used with ordinary socket IO calls. It stores the two sockets in sv[0] and sv[1], and returns 0 for success and -1 for failure.

On Windows, this only supports family AF_INET, type SOCK_STREAM, and protocol 0. Note that this can fail on some Windows hosts where firewall software has cleverly firewalled 127.0.0.1 to keep the host from talking to itself.

Portable string manipulation functions

 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
int evutil_snprintf(char *buf, size_t buflen, const char *format, ...);
int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap);

Locale-independent string manipulation functions

Sometimes, when implementing ASCII-based protocols, you want to manipulate strings according to ASCII’s notion of character type, regardless of your current locale. Libevent provides a few functions to help with this:

Interface
 int evutil_ascii_strcasecmp(const char *str1, const char *str2);
int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);

IPv6 helper and portability functions

 const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
int evutil_inet_pton(int af, const char *src, void *dst);

These functions behave as the standard inet_ntop() and inet_pton() functions for parsing and formatting IPv4 and IPv6 addresses, as specified in RFC3493.

evutil_inet_ntop():

an IPv4 address,  af set to AF_INET, src pointing to a struct in_addr, and dst pointing to a character buffer of size len.

an IPv6 address, af is AF_INET6 and src is a struct in6_addr.

To parse an IPv4 address, call evutil_inet_pton() with af set to AF_INET or AF_INET6, the string to parse in src, and dst pointing to an in_addr or an in_addr6 as appropriate.

The return value from evutil_inet_ntop() is NULL on failure and otherwise points to dst. The return value from evutil_inet_pton() is 0 on success and -1 on failure.

Interface
 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out,
int *outlen);

This function parses an address from str and writes the result to out. The outlen argument must point to an integer holding the number of bytes available in out; it is altered to hold the number of bytes actually used. This function returns 0 on success and -1 on failure. It recognizes the following address formats:

  • [ipv6]:port (as in "[ffff::]:80")

  • ipv6 (as in "ffff::")

  • [ipv6] (as in "[ffff::]")

  • ipv4:port (as in "1.2.3.4:80")

  • ipv4 (as in "1.2.3.4")

If no port is given, the port in the resulting sockaddr is set to 0.

Interface
int evutil_sockaddr_cmp(const struct sockaddr *sa1,
const struct sockaddr *sa2, int include_port);

The evutil_sockaddr_cmp() function compares two addresses, and returns negative if sa1 precedes sa2, 0 if they are equal, and positive if sa2 precedes sa1. It works for AF_INET and AF_INET6 addresses, and returns undefined output for other addresses. It’s guaranteed to give a total order for these addresses, but the ordering may change between Libevent versions.

If the include_port argument is false, then two sockaddrs are treated as equal if they differ only in their port. Otherwise, sockaddrs with different ports are treated as unequal.

Structure macro portability functions

Interface
#define evutil_offsetof(type, field) /* ... */

As the standard offsetof macro, this macro yields the number of bytes from the start of type at which field occurs.

This macro was introduced in Libevent 2.0.1-alpha. It was buggy in every version before Libevent 2.0.3-alpha.

Secure random number generator

Many applications (including evdns) need a source of hard-to-predict random numbers for their security.

Interface
void evutil_secure_rng_get_bytes(void *buf, size_t n);

This function fills n-byte buffer at buf with n bytes of random data.

If your platform provides the arc4random() function, Libevent uses that. Otherwise, it uses its own implementation of arc4random(), seeded by your operating system’s entropy pool (CryptGenRandom on Windows, /dev/urandom everywhere else).

Interface
int evutil_secure_rng_init(void);
void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);

You do not need to manually initialize the secure random number generator, but if you want to make sure it is successfully initialized, you can do so by calling evutil_secure_rng_init(). It seeds the RNG (if it was not already seeded) and returns 0 on success. If it returns -1, Libevent wasn’t able to find a good source of entropy on your OS, and you can’t use the RNG safely without initializing it yourself.

 
 
 

libevent reference Mannual IV --Helper functions and types的更多相关文章

  1. libevent学习五(Helper functions and types for Libevent)

    基础类型   #ifdef WIN32 #define evutil_socket_t intptr_t #else #define evutil_socket_t int #endif ev_ssi ...

  2. libevent reference Mannual II--library

    FYI: http://www.wangafu.net/~nickm/libevent-book/TOC.html The Libevent Reference Manual: Preliminari ...

  3. libevent reference Mannual V -- Bufferevents

    FYI: http://www.wangafu.net/~nickm/libevent-book/Ref6_bufferevent.html Bufferevents: concepts and ba ...

  4. libevent reference Mannual III--working with events

    FYI: http://www.wangafu.net/~nickm/libevent-book/TOC.html Working with events Libevent’s basic unit ...

  5. libevent reference Mannual I

    FYI:http://www.wangafu.net/~nickm/libevent-book/ This lib is a integral of asynchronous IO. we shoul ...

  6. Creating Your Own PHP Helper Functions In Laravel

    By Hamza Ali LAST UPDATED AUG 26, 2018  12,669 104 Laravel provides us with many built-in helper fun ...

  7. asp.net MVC 帮助助手和函数( @helper @functions)

    asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...

  8. 【ARM-Linux开发】DRM学习(一)

    http://www.landley.NET/kdocs/htmldocs/drm.html 非常好的一个链接,直接把DRM说的很透.很多API的功能都写全了. Table of Contents 1 ...

  9. Go Pentester - HTTP CLIENTS(2)

    Building an HTTP Client That Interacts with Shodan Shadon(URL:https://www.shodan.io/)  is the world' ...

随机推荐

  1. linux用于文件解压缩的命令

    1 gzip gzip -<压缩率> 压缩率用数字(1-9)来表示,越大,则压缩率越大. 2 bz2 解压bz2 bzip2 -d filename.bz2

  2. JButton的setRollover出现的奇怪问题

    设置了setRollover,可以正常出现状态但是却不会回到默认状态. 研究了一下才发现,repaint的时候不会清除背板而是覆盖上去的, 所以如果原图是透明图就会出现状态不变的情况

  3. 【Dairy】2016.10.30 BirthdayParty

    今天又有人生日耶,鹏哥和骥哥两兄弟,Happy Birthday 开始听到这件事,我傻逼的想了一下,咦,这两人这么有缘,同一天生日...脑抽了... 小胖犇极缓音调来了首烟花易冷,劲啊! 发张什么图呢 ...

  4. python-----复制文件夹

    python复制文件夹有两种方法: 1.使用shutil模块:(要保存的路径必须事先不存在) #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time ...

  5. MySQL-day1数据库的安装与介绍

    一.mysql的安装步骤 以5.7.20版本为例: 第1步: 第2步: 第3步: 第4步: 第5步: 第6步: 第7步: 第8步: 第9步: 第10步: 第11步: 第12步: 第13步: 第14步: ...

  6. MyBatis高级查询 存储过程

    1.第一个存储过程  根据用户id查询用户其他信息 #第一个存储过程 #根据用户id查询用户其他信息 DROP PROCEDURE IF EXISTS `select_user_by_id`; DEL ...

  7. 人脸识别(初学篇)-VS2015+opencv3.2的配置

    初学人脸识别,感觉安装也是一个很大的麻烦. 写在这里记录一下吧 一:先安装好我们需要的软件 首先安装Vs2015,在官网或者csdn搜一下应该找的到. 安装步骤没有太多讲究. 点击exe文件,我选择的 ...

  8. vbnet 进程监控,监控Acad开启的数量,并且添加到开机启动

    1# 自定义函数,添加到注册表 Public Sub StartRunRegHKLM() REM HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node \ Micro ...

  9. macbookpro安装Ubuntu16.04.1 LTS爬坑之旅。亲测有效(集众家之长)。安装时间为2017-11-19。

    1.格式化U盘 要求:(1)切换分区格式为Mac OS扩展 (日志型):(2)方案(scheme)设置为:GUID Partition Map:如图(使用mac自带磁盘工具) 2.给Ubuntu划分磁 ...

  10. [转]c++中的string常用函数用法总结

    标准c++中string类函数介绍 注意不是CString之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为 ...