libuv介绍和实现的基本流程


前言

libuv是一个高性能的,事件驱动的I/O库,并且提供了跨平台(如windows, linux)的API。

一、libuv核心是什么?

libuv核心工作是提供一个event-loop,还有基于I/O和其它事件通知的回调函数。libuv还提供了一些核心工具,例如定时器,非阻塞的网络支持,异步文件系统访问,子进程等。

二、libuv简单介绍

1.简单实现

1、libuv的实现流程为:
(1) 声明事件句柄(handle)
2) 初始化句柄,在loop中注册事件
(3)在handle上注册回调函数
(4) 启动事件循环
(5) 退出事件循环,释放资源。

代码如下:

uv_idle_t idler;  (1)
uv_idle_init(uv_default_loop(), &idler); (2)  使用默认loop
uv_idle_start(&idler, wait_for_a_while); (3)
uv_run(uv_default_loop(), UV_RUN_DEFAULT);(4)
uv_loop_close(uv_default_loop());    (5)

2.监视器

libuv 通过监视器(Watcher)来对特定事件进行监控, 监视器通常是类似 uv_TYPE_t 结构体的封装, TYPE 代表该监视器的用途, libuv 所有的监视器类型
如下:
其中句柄类型(handle)代表持久对象。请求类型(request)是短暂对象(即通常只维持一个回调函数)。
Requests用于初始函数和回调函数之间传递上下文。比如uv_udp_t代表udp事件,当每次有udp事件时,会先向回调函数传递一个uv_udp_send_t请求

常用的监视器:

/* 句柄类型*/
typedef struct uv_loop_s uv_loop_t;
typedef struct uv_handle_s uv_handle_t;
typedef struct uv_stream_s uv_stream_t;
typedef struct uv_tcp_s uv_tcp_t;
typedef struct uv_udp_s uv_udp_t;
typedef struct uv_pipe_s uv_pipe_t;
typedef struct uv_tty_s uv_tty_t;
typedef struct uv_poll_s uv_poll_t;
typedef struct uv_timer_s uv_timer_t;
typedef struct uv_prepare_s uv_prepare_t;   //准备句柄
typedef struct uv_check_s uv_check_t;      //检查句柄
typedef struct uv_idle_s uv_idle_t;         //空闲句柄
typedef struct uv_async_s uv_async_t;     //异步句柄
typedef struct uv_process_s uv_process_t;  //进程
typedef struct uv_fs_event_s uv_fs_event_t;
typedef struct uv_fs_poll_s uv_fs_poll_t;
typedef struct uv_signal_s uv_signal_t;
/* 请求类型*/
typedef struct uv_req_s uv_req_t;
typedef struct uv_getaddrinfo_s uv_getaddrinfo_t;
typedef struct uv_getnameinfo_s uv_getnameinfo_t;
typedef struct uv_shutdown_s uv_shutdown_t;
typedef struct uv_write_s uv_write_t;
typedef struct uv_connect_s uv_connect_t;
typedef struct uv_udp_send_s uv_udp_send_t;
typedef struct uv_fs_s uv_fs_t;
typedef struct uv_work_s uv_work_t;

/* 其它类型. */
typedef struct uv_cpu_info_s uv_cpu_info_t;
typedef struct uv_interface_address_s
 uv_interface_address_t;
typedef struct uv_dirent_s uv_dirent_t;

监视器的初始化和注册:

监视器初始化并注册到loop中;
uv_idle_init(uv_default_loop(), &idler); //第一个参数是event_loop,第二个是事件句柄。
监视器设置回调函数并监听事件
uv_idle_start(&idler, wait_for_a_while); //第一个参数是事件句柄,第二个是回调函数名。
停止监听事件函数:uv_idle_stop(&idler);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);//运行loop,开始循环活动的监视器;第一个参数是loop,第二个是启动默认参数
uv_loop_close(uv_default_loop()); //关闭loop,退出程序。

3.handle的通用API

int uv_is_active(const uv_handle_t* handle);
int uv_is_closing(const uv_handle_t* handle);
void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
void uv_ref(uv_handle_t*);
void uv_unref(uv_handle_t*);
int uv_has_ref(const uv_handle_t*);
size_t uv_handle_size(uv_handle_type type);
uv_handle_type uv_handle_get_type(const uv_handle_t* handle);
const char* uv_handle_type_name(uv_handle_type type);
uv_loop_t* uv_handle_get_loop(const uv_handle_t* handle);
void* uv_handle_get_data(const uv_handle_t* handle);
void uv_handle_set_data(uv_handle_t* handle, void* data);
int uv_send_buffer_size(uv_handle_t* handle, int* value);
int uv_recv_buffer_size(uv_handle_t* handle, int* value);
int uv_fileno(const uv_handle_t* handle, uv_os_fd_t* fd);

uv_is_active 请求是否处于活动状态,活动状态返回非0.判读是否是活动状态标准如下
uv_async_t 启动后就处于active状态,uv_close 后,处于非active状态
uv_pipe_t, uv_tcp_t, uv_udp_t, etc. handle等I/O handle当处于读,写,状态时,处于active状态
异步handle执行uv_*start后,就处于active状态,uv_*stop后,处于非active状态
其他的hangle,比如 uv_foo_t,执行 vu_foo_start()函数的时候激活,执行uv_foo_stop()处于非激活状态
uv_is_closing: 是否关闭,如果正在关闭或已经关闭返回非0
uv_close:从endgame队列中删除,关闭指定句柄,回调可以为空(同步返回),比如关闭定时器、idle、prepare等,一般和各种init成对出现。
uv_handle_get_type: 返回句柄的类型
uv_handle_type_name: 类型名称, e.g. “pipe” (as in uv_pipe_t) for UV_NAMED_PIPE.
uv_handle_get_loop:返回loop.
uv_handle_get_data:返回句柄携带的数据
uv_handle_set_data:给句柄设置数据.
uv_send_buffer_size:设置,发送缓存大小,注意:linux会设置二倍大小
uv_recv_buffer_size : 获取,发送缓存大小,注意:linux会设置二倍大小
uv_fileno : 提取句柄。比如:handle->io_watcher.fd;


总结

libuv的核心搞清楚了,后面的学习就比较轻松了。
参考:libuv官文文档

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值