me
me copied to clipboard
学习 C/C++ (Part 24: libuv)
setup
跟着 libuv/build-instructions 指引通过cmake编译和安装:
~ mkdir -p build
~ cd build
~ cmake ..
~ cmake --build build
~ sudo make install
hello world
guide#hello-world uv.c
// clang -arch arm64 -luv uv.c
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
int main() {
uv_loop_t *loop = malloc(sizeof(uv_loop_t));
uv_loop_init(loop);
printf("Now quitting.\n");
uv_run(loop, UV_RUN_DEFAULT);
uv_loop_close(loop);
free(loop);
return 0;
}
tcp echo
guide中的代码在src/docs/code下, GitHub: tcp-echo-server/main.c,根据这个操练一遍。
uv.c
// clang -arch arm64 -luv uv.c
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
#define DEFAULT_PORT 3000
#define DEFAULT_BACKLOG 128
uv_loop_t *loop;
struct sockaddr_in addr;
void on_new_connection(uv_stream_t *server, int status);
void on_close(uv_handle_t *handle);
int main() {
loop = uv_default_loop();
uv_tcp_t server;
uv_tcp_init(loop, &server);
uv_ip4_addr("0.0.0.0", DEFAULT_PORT, &addr);
uv_tcp_bind(&server, (const struct sockaddr *) &addr, 0);
int r = uv_listen((uv_stream_t *) &server, DEFAULT_BACKLOG, on_new_connection);
if (r) {
fprintf(stderr, "Listen error %s\n", uv_strerror(r));
return 1;
}
return uv_run(loop, UV_RUN_DEFAULT);
}
void on_new_connection(uv_stream_t *server, int status) {
if (status < 0) {
fprintf(stderr, "New connection error %s\n", uv_strerror(status));
// error!
return;
}
uv_tcp_t *client = (uv_tcp_t *) malloc(sizeof(uv_tcp_t));
uv_tcp_init(loop, client);
if (uv_accept(server, (uv_stream_t *) client) == 0) {
printf("new connection.\n");
// uv_read_start((uv_stream_t *) client, alloc_buffer, echo_read);
} else {
uv_close((uv_handle_t *) client, on_close);
}
}
void on_close(uv_handle_t *handle) {
free(handle);
}
通过nc localhost 3000
可以看到new connection输出,这里必须在on_new_connection中调用uv_accept否则不能响应第二次接入请求,你可以理解uv_accept重新投递listen请求,这个和IOCP的概念是一致的。
完整的代码直接看github就好,很容易理解。
小结:
- libuv通过异步方式抽象和封装了file, socket,建立了一个模型,nodejs证明了这个模式非常ok, 绝对值得学习