三年前libuv还是0.10版本时,公司项目的服务端网络层我用了libuv。今天准备将libuv作为当前新项目的网络底层库,发现曾经的api已大改。好在官方给了一份变动文档。
libuv 0.10 -> 1.0.0 migration guide
http://docs.libuv.org/en/v1.x/migration_010_100.html
Some APIs changed quite a bit throughout the 1.0.0 development process. Here is a migration guide for the most significant changes that happened after 0.10 was released.
Loop initialization and closing
In libuv 0.10 (and previous versions), loops were created with uv_loop_new, which allocated memory for a new loop and initialized it; and destroyed withuv_loop_delete, which destroyed the loop and freed the memory. Starting with 1.0, those are deprecated and the user is responsible for allocating the memory and then initializing the loop.
libuv 0.10
uv_loop_t* loop = uv_loop_new();
...
uv_loop_delete(loop);
libuv 1.0
uv_loop_t* loop = malloc(sizeof *loop);
uv_loop_init(loop);
...
uv_loop_close(loop);
free(loop);
Note
Error handling was omitted for brevity. Check the documentation for uv_loop_init()
and uv_loop_close()
.
Error handling
Error handling had a major overhaul in libuv 1.0. In general, functions and status parameters would get 0 for success and -1 for failure on libuv 0.10, and the user had to use uv_last_error to fetch the error code, which was a positive number.
In 1.0, functions and status parameters contain the actual error code, which is 0 for success, or a negative number in case of error.
libuv 0.10
... assume 'server' is a TCP server which is already listening
r = uv_listen((uv_stream_t*) server, 511, NULL);
if (r == -1) {
uv_err_t err = uv_last_error(uv_default_loop());
/* err.code contains UV_EADDRINUSE */
}
libuv 1.0
... assume 'server' is a TCP server which is already listening
r = uv_listen((uv_stream_t*) server, 511, NULL);
if (r < 0) {
/* r contains UV_EADDRINUSE */
}
Threadpool changes
In libuv 0.10 Unix used a threadpool which defaulted to 4 threads, while Windows used the QueueUserWorkItem API, which uses a Windows internal threadpool, which defaults to 512 threads per process.
In 1.0, we unified both implementations, so Windows now uses the same implementation Unix does. The threadpool size can be set by exporting theUV_THREADPOOL_SIZE
environment variable. See Thread pool work scheduling.
Allocation callback API change
In libuv 0.10 the callback had to return a filled uv_buf_t
by value:
uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
return uv_buf_init(malloc(size), size);
}
In libuv 1.0 a pointer to a buffer is passed to the callback, which the user needs to fill:
void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
buf->base = malloc(size);
buf->len = size;
}
Unification of IPv4 / IPv6 APIs
libuv 1.0 unified the IPv4 and IPv6 APIS. There is no longer a uv_tcp_bind and uv_tcp_bind6 duality, there is only uv_tcp_bind()
now.
IPv4 functions took struct sockaddr_in
structures by value, and IPv6 functions took struct sockaddr_in6
. Now functions take a struct sockaddr*
(note it’s a pointer). It can be stack allocated.
libuv 0.10
struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", 1234);
...
uv_tcp_bind(&server, addr)
libuv 1.0
struct sockaddr_in addr;
uv_ip4_addr("0.0.0.0", 1234, &addr)
...
uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
The IPv4 and IPv6 struct creating functions (uv_ip4_addr()
and uv_ip6_addr()
) have also changed, make sure you check the documentation.
Streams / UDP data receive callback API change
The streams and UDP data receive callbacks now get a pointer to a uv_buf_t
buffer, not a structure by value.
libuv 0.10
void on_read(uv_stream_t* handle,
ssize_t nread,
uv_buf_t buf) {
...
}
void recv_cb(uv_udp_t* handle,
ssize_t nread,
uv_buf_t buf,
struct sockaddr* addr,
unsigned flags) {
...
}
libuv 1.0
void on_read(uv_stream_t* handle,
ssize_t nread,
const uv_buf_t* buf) {
...
}
void recv_cb(uv_udp_t* handle,
ssize_t nread,
const uv_buf_t* buf,
const struct sockaddr* addr,
unsigned flags) {
...
}
Receiving handles over pipes API change
In libuv 0.10 (and earlier versions) the uv_read2_start function was used to start reading data on a pipe, which could also result in the reception of handles over it. The callback for such function looked like this:
void on_read(uv_pipe_t* pipe,
ssize_t nread,
uv_buf_t buf,
uv_handle_type pending) {
...
}
In libuv 1.0, uv_read2_start was removed, and the user needs to check if there are pending handles using uv_pipe_pending_count()
and uv_pipe_pending_type()
while in the read callback:
void on_read(uv_stream_t* handle,
ssize_t nread,
const uv_buf_t* buf) {
...
while (uv_pipe_pending_count((uv_pipe_t*) handle) != 0) {
pending = uv_pipe_pending_type((uv_pipe_t*) handle);
...
}
...
}
Extracting the file descriptor out of a handle
While it wasn’t supported by the API, users often accessed the libuv internals in order to get access to the file descriptor of a TCP handle, for example.
fd = handle->io_watcher.fd;
This is now properly exposed through the uv_fileno()
function.
uv_fs_readdir rename and API change
uv_fs_readdir returned a list of strings in the req->ptr field upon completion in libuv 0.10. In 1.0, this function got renamed to uv_fs_scandir()
, since it’s actually implemented using scandir(3)
.
In addition, instead of allocating a full list strings, the user is able to get one result at a time by using the uv_fs_scandir_next()
function. This function does not need to make a roundtrip to the threadpool, because libuv will keep the list of dents returned by scandir(3)
around.
相关推荐
libuv api 说明及用例 英文文档 但是很好理解
libuv 第一个版本源代码,可以马上了解作者当初开发该软件的基本原理,有需要的朋友请下载。
本资源在 debian 9 交叉编译器版本arm-linuxgnueabihf-gcc -v gcc version 6.3.0 20170516 (Debian 6.3.0-18) 下成功编译生成arm-linux下能运行的库,暂时编译成功还没进行测试,
libuv源代码移植到CLion工程
运行node ./release.js --version xxx --dir path --remote name ,其中xxx是您正在创建的libuv的版本, path是计算机上libuv核心存储库的位置, name是libuv核心git远程。 这将执行一些任务,例如在必要时更新...
“This ‘book’ is a small set of tutorials about using libuv [https://github.com/libuv/libuv] as a high performance evented I/O library which offers the same API on Windows and Unix. It is meant to ...
libuv 的文档提供了详细的设计概述、安装指南、API 文档和示例代码等。 libuv 的特点可以总结为: * 跨平台支持,支持多种操作系统和架构。 * 全功能的事件循环,支持多种 I/O polling 机制。 * 异步 TCP 和 UDP ...
libuv book 中文文档 An introduction to libuv libuv book 中文文档 An introduction to libuv
libuv源码 libuv-1.x
libuv中文入门学习书籍
libuv_tcp, 使用libuv的tcp服务器和客户端包 欢迎来到libuv_tcp维基 !简介:libuv_tcp是一个包含tcp服务器和客户端的C 类,它使用libuv包。用法:请参阅 [ test_tcpserver.cpp ] 1 和 [ test_tcpclient.cpp
libuv 在xp系统下的 vc6.0项目 兼容win xp
封装libuv库,可以方便的继承实现自己的功能。便于以后的扩展。
libuv在centos下的编译安装 主要章节: 一、 工具安装 二、 下载libuv源代码 三、 编译安装libuv 四、 测试验证
libuv下载
iOS版本libuv.a静态库,支持i386 armv7 armv7s x86_64 arm64
基于libuv库的c++实现的tcp通讯,包括客户端和服务器
编译好的libuv ,包含dll和引入库lib和头文件,头文件精简后只需要包含uv.h即可
libvu官方教程的中文翻译版,详细讲述了libuv各个功能模块的使用方法,适用于刚入门学习libuv的人们
libuv is a multi-platform support library with a focus on asynchronous I/O. It was primarily developed for use by [Node.js][], but it's also used by [Luvit](http://luvit.io/), [Julia]...