一些常用结构体:
struct uv_handle_t
{
//UV_HANDLE_FIELDS 展开
/* public */
void* data;
/* read-only */
uv_loop_t* loop;
uv_handle_type type;
/* private */
uv_close_cb close_cb;
void* handle_queue[2];
void* reserved[4];
//UV_HANDLE_PRIVATE_FIELDS 展开
uv_handle_t* endgame_next;
unsigned int flags;// 赋值为 UV_HANDLE_ZERO_READ UV_HANDLE_READING 之类的
};
struct uv_stream_t
{
//UV_HANDLE_FIELDS 宏 展开 跟 uv_handle_t 定义 从 uv_handle_t 继承
//UV_STREAM_FIELDS 宏
size_t write_queue_size;
uv_alloc_cb alloc_cb;
uv_read_cb read_cb;
//UV_STREAM_PRIVATE_FIELDS 展开
unsigned int reqs_pending;
int activecnt;
uv_read_t read_req;
};
struct uv_tcp_t
{
//UV_HANDLE_FIELDS 展开 跟 uv_handle_t 一样成员
//UV_STREAM_FIELDS 展开 从 uv_stream_t 继承
//UV_TCP_PRIVATE_FIELDS 展开 如下
SOCKET socket;
int delayed_error;
};
struct uv_timer_t
{
//UV_HANDLE_FIELDS 展开 从 uv_handle_t继承
// UV_TIMER_PRIVATE_FIELDS 私有成员
RB_ENTRY(uv_timer_s) tree_entry;
uint64_t due;
uint64_t repeat;
uint64_t start_id;
uv_timer_cb timer_cb;
};
struct uv_loop_t
{
/* User data - use this for whatever. */
void* data;
/* Loop reference counting. */
unsigned int active_handles;
void* handle_queue[2];
void* active_reqs[2];
/* Internal flag to signal loop stop. */
unsigned int stop_flag;
//UV_LOOP_PRIVATE_FIELDS 私有数据成员 展开
/* The loop's I/O completion port */
HANDLE iocp;
/* The current time according to the event loop. in msecs. */
uint64_t time;
/* Tail of a single-linked circular queue of pending reqs. If the queue */ \
/* is empty, tail_ is NULL. If there is only one item, */ \
/* tail_->next_req == tail_ */ \
uv_req_t* pending_reqs_tail; \
/* Head of a single-linked list of closed handles */ \
uv_handle_t* endgame_handles; \
/* The head of the timers tree */ \
struct uv_timer_tree_s timers; \
/* Lists of active loop (prepare / check / idle) watchers */ \
uv_prepare_t* prepare_handles; \
uv_check_t* check_handles; \
uv_idle_t* idle_handles; \
/* This pointer will refer to the prepare/check/idle handle whose */ \
/* callback is scheduled to be called next. This is needed to allow */ \
/* safe removal from one of the lists above while that list being */ \
/* iterated over. */ \
uv_prepare_t* next_prepare_handle; \
uv_check_t* next_check_handle; \
uv_idle_t* next_idle_handle; \
/* This handle holds the peer sockets for the fast variant of uv_poll_t */ \
SOCKET poll_peer_sockets[UV_MSAFD_PROVIDER_COUNT]; \
/* Counter to keep track of active tcp streams */ \
unsigned int active_tcp_streams; \
/* Counter to keep track of active udp streams */ \
unsigned int active_udp_streams; \
/* Counter to started timer */ \
uint64_t timer_counter; \
/* Threadpool */ \
void* wq[2]; \
uv_mutex_t wq_mutex;
uv_async_t wq_async;
};
--loop循环 UV_RUN_ONCE 等待一次io, UV_RUN_DEFAULT 等待IO直到没有 UV_RUN_NOWAIT
int uv_run(uv_loop_t *loop, uv_run_mode mode)
--》static void uv_poll(uv_loop_t* loop, DWORD timeout) 或者uv_poll_ex()--》GetQueuedCompletionStatus()--> uv_insert_pending_req(loop, req);
--一个IO tcp 数据读
int uv_run(uv_loop_t *loop, uv_run_mode mode)
-->uv_process_reqs()--> case UV_READ: call uv_process_tcp_read_req() 调用对应的WSARecv() 先alloc_cb 回调