基于libuv的tcp端口转发(keepalive和超时设置)
前言
网上关于libuv的代码示例较少,官方文档和代码中的示例都只是给了简单的例子,实际使用的时候可能会遇到很多内存泄漏和指针异常的问题
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uv.h>
#define CRTDBG_MAP_ALLOC
#include "crtdbg.h"
#define DEFAULT_PORT 7000
#define DEFAULT_BACKLOG 128
struct sockaddr_in toaddr;
uv_loop_t *loop;
struct sockaddr_in addr;
#define CONTAINER_OF(ptr, type, member) \
((type *) ((char *) (ptr) - offsetof(type, member)))
struct mysocket{
uv_tcp_t stream;
uv_timer_t streamtime;
uv_tcp_t tostream;
};
typedef struct {
uv_write_t req;
uv_buf_t buf;
} write_req_t;
void free_write_req(uv_write_t *req) {
write_req_t *wr = (write_req_t*) req;
free(wr->buf.base);
free(wr);
}
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
buf->base = (char*) malloc(suggested_size);
buf->len = suggested_size;
}
void on_close(uv_handle_t* handle) {
printf("on_close\n");
struct mysocket *myysocket;
myysocket = CONTAINER_OF(handle,struct mysocket,stream);
free(myysocket);
}
void on_closeto(uv_handle_t* handle) {
printf("on_close\n");
struct mysocket *myysocket;
myysocket = CONTAINER_OF(handle,struct mysocket,tostream);
free(myysocket);
}
void echo_write(uv_write_t *req, int status) {
if (status) {
fprintf(stderr, "Write error %s\n", uv_strerror(status));
}
printf("echo_write\n");
free_write_req(req);
}
void socket_timer_expire_cb(uv_timer_t *handle)
{
struct mysocket *myysocket;
printf("socket_timer_expire_cb\n");
myysocket = CONTAINER_OF(handle,struct mysocket,streamtime);
uv_timer_stop(&myysocket->streamtime);
uv_close((uv_handle_t*)&myysocket->stream,NULL);
uv_close((uv_handle_t*)&myysocket->tostream,NULL);
uv_close((uv_handle_t*)handle,NULL);
free(myysocket);
}
void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
printf("echo_read\n");
uv_tcp_t *streamto = client->data;
struct mysocket *myysocket;
myysocket = CONTAINER_OF(client,struct mysocket,stream);
if (nread > 0) {
write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
req->buf = uv_buf_init(buf->base, nread);
uv_write((uv_write_t*) req, streamto, &req->buf, 1, echo_write);
return;
}
if (nread < 0) {
if (nread != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
uv_close((uv_handle_t*) client, on_close);
}
free(buf->base);
}
void echo_readto(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
printf("echo_readto\n");
uv_tcp_t *streamto = client->data;
struct mysocket *myysocket;
myysocket = CONTAINER_OF(streamto,struct mysocket,tostream);
uv_timer_t* streamtime = &myysocket->streamtime;
if (nread > 0) {
write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
req->buf = uv_buf_init(buf->base, nread);
uv_write((uv_write_t*) req, streamto, &req->buf, 1, echo_write);
uv_timer_start(streamtime, socket_timer_expire_cb, 20000, 0);
printf("%p\n",&myysocket->streamtime);
return;
}
if (nread < 0) {
if (nread != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
uv_close((uv_handle_t*) streamto, on_closeto);
}
free(buf->base);
}
void on_connect(uv_connect_t *req, int status) {
if (status < 0) {
fprintf(stderr, "connect failed error %s\n", uv_err_name(status));
free(req);
return;
}
struct mysocket *myysocket;
myysocket = CONTAINER_OF(req->handle,struct mysocket,tostream);
uv_stream_t *tostream = &myysocket->tostream;
uv_tcp_keepalive(tostream,1,10);
uv_tcp_keepalive(tostream->data,1,10);
uv_read_start((uv_stream_t*) tostream, alloc_buffer, echo_read);
uv_read_start((uv_stream_t*) tostream->data, alloc_buffer, echo_readto);
free(req);
}
void on_new_connection(uv_stream_t *server, int status) {
if (status < 0) {
fprintf(stderr, "New connection error %s\n", uv_strerror(status));
return;
}
struct mysocket *myysocket = (struct mysocket*) malloc(sizeof (struct mysocket));
server->loop->data = myysocket;
uv_tcp_t *client = &myysocket->stream;
uv_tcp_t *streamto = &myysocket->tostream;
uv_timer_t* streamtime = &myysocket->streamtime;
uv_tcp_init(loop, client);
uv_timer_init(loop,streamtime);
if (uv_accept(server, (uv_stream_t*) client) != 0) {
uv_close((uv_handle_t*) client, on_close);
}
uv_ip4_addr("192.168.164.128", 80, &toaddr);
uv_connect_t *connect_req = (uv_connect_t*) malloc(sizeof(uv_connect_t));
uv_tcp_init(loop, streamto);
streamto->data=(uv_stream_t*)client;
client->data=(uv_stream_t*)streamto;
uv_tcp_connect(connect_req, streamto, (const struct sockaddr*) &toaddr, on_connect);
}
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;
}
uv_run(loop, UV_RUN_DEFAULT);
_CrtDumpMemoryLeaks();
uv_loop_close(loop);
return 0;
}
参考文章
https://libuv-docs-chinese.readthedocs.io/zh/latest/timer.html https://blog.csdn.net/bqw2008/article/details/42154939
|