From fe4145239ef21a717e36da6d1859e6760395cdd2 Mon Sep 17 00:00:00 2001 From: gozfree Date: Mon, 20 Sep 2021 11:57:45 +0800 Subject: [PATCH] [libsock] refine api --- gear-lib/libsock/libsock_ext.c | 24 ++++++++++++------------ gear-lib/libsock/libsock_ext.h | 26 ++++++++++++++------------ gear-lib/libsock/test_libsock.c | 20 +++++++++++++------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/gear-lib/libsock/libsock_ext.c b/gear-lib/libsock/libsock_ext.c index a0b24de8..f66f32f1 100644 --- a/gear-lib/libsock/libsock_ext.c +++ b/gear-lib/libsock/libsock_ext.c @@ -40,11 +40,11 @@ static void on_recv(int fd, void *arg) struct sock_server *s = (struct sock_server *)arg; ret = sock_recv(fd, buf, 2048); if (ret > 0) { - s->on_buffer(fd, buf, ret); + s->on_buffer(s, buf, ret); } else if (ret == 0) { printf("delete connection fd:%d\n", fd); if (s->on_disconnect) { - s->on_disconnect(fd, NULL); + s->on_disconnect(s, NULL); } } else if (ret < 0) { printf("%s:%d recv failed!\n", __func__, __LINE__); @@ -59,11 +59,11 @@ static void on_client_recv(int fd, void *arg) struct sock_client *c = (struct sock_client *)arg; ret = sock_recv(fd, buf, 2048); if (ret > 0) { - c->on_buffer(fd, buf, ret); + c->on_buffer(c, buf, ret); } else if (ret == 0) { printf("delete connection fd:%d\n", fd); if (c->on_disconnect) { - c->on_disconnect(fd, NULL); + c->on_disconnect(c, NULL); } } else if (ret < 0) { printf("%s:%d recv failed!\n", __func__, __LINE__); @@ -114,7 +114,7 @@ static void on_tcp_connect(int fd, void *arg) sc.remote.ip = ip; sc.remote.port = port; sock_addr_ntop(sc.remote.ip_str, ip); - s->on_connect(fd, &sc); + s->on_connect(s, &sc); } e = gevent_create(afd, on_recv, NULL, on_error, s); if (-1 == gevent_add(s->evbase, &e)) { @@ -195,9 +195,9 @@ struct sock_server *sock_server_create(const char *host, uint16_t port, enum soc } int sock_server_set_callback(struct sock_server *s, - void (*on_connect)(int fd, struct sock_connection *conn), - void (*on_buffer)(int, void *buf, size_t len), - void (*on_disconnect)(int fd, struct sock_connection *conn)) + void (*on_connect)(struct sock_server *s, struct sock_connection *conn), + void (*on_buffer)(struct sock_server *s, void *buf, size_t len), + void (*on_disconnect)(struct sock_server *s, struct sock_connection *conn)) { struct gevent *e; if (!s) { @@ -274,9 +274,9 @@ struct sock_client *sock_client_create(const char *host, uint16_t port, enum soc } int sock_client_set_callback(struct sock_client *c, - void (*on_connect)(int fd, struct sock_connection *conn), - void (*on_buffer)(int, void *buf, size_t len), - void (*on_disconnect)(int fd, struct sock_connection *conn)) + void (*on_connect)(struct sock_client *c, struct sock_connection *conn), + void (*on_buffer)(struct sock_client *c, void *buf, size_t len), + void (*on_disconnect)(struct sock_client *c, struct sock_connection *conn)) { if (!c) { return -1; @@ -324,7 +324,7 @@ GEAR_API int sock_client_connect(struct sock_client *c) } if (c->conn) { if (c->on_connect) { - c->on_connect(c->conn->fd, c->conn); + c->on_connect(c, c->conn); } } c->thread = thread_create(sock_client_thread, c); diff --git a/gear-lib/libsock/libsock_ext.h b/gear-lib/libsock/libsock_ext.h index 234b9566..860bd88e 100644 --- a/gear-lib/libsock/libsock_ext.h +++ b/gear-lib/libsock/libsock_ext.h @@ -34,9 +34,10 @@ struct sock_server { struct sock_connection *conn; enum sock_type type; struct gevent_base *evbase; - void (*on_buffer)(int fd, void *buf, size_t len); - void (*on_connect)(int fd, struct sock_connection *conn); - void (*on_disconnect)(int fd, struct sock_connection *conn); + void (*on_buffer)(struct sock_server *s, void *buf, size_t len); + void (*on_connect)(struct sock_server *s, struct sock_connection *conn); + void (*on_disconnect)(struct sock_server *s, struct sock_connection *conn); + void *priv; }; /* @@ -44,9 +45,9 @@ struct sock_server { */ GEAR_API struct sock_server *sock_server_create(const char *host, uint16_t port, enum sock_type type); GEAR_API int sock_server_set_callback(struct sock_server *s, - void (*on_connect)(int fd, struct sock_connection *conn), - void (*on_buffer)(int, void *buf, size_t len), - void (*on_disconnect)(int fd, struct sock_connection *conn)); + void (*on_connect)(struct sock_server *s, struct sock_connection *conn), + void (*on_buffer)(struct sock_server *s, void *buf, size_t len), + void (*on_disconnect)(struct sock_server *s, struct sock_connection *conn)); GEAR_API int sock_server_dispatch(struct sock_server *s); GEAR_API void sock_server_destroy(struct sock_server *s); @@ -61,16 +62,17 @@ struct sock_client { enum sock_type type; struct gevent_base *evbase; struct thread *thread; - void (*on_buffer)(int fd, void *buf, size_t len); - void (*on_connect)(int fd, struct sock_connection *conn); - void (*on_disconnect)(int fd, struct sock_connection *conn); + void (*on_buffer)(struct sock_client *c, void *buf, size_t len); + void (*on_connect)(struct sock_client *c, struct sock_connection *conn); + void (*on_disconnect)(struct sock_client *c, struct sock_connection *conn); + void *priv; }; GEAR_API struct sock_client *sock_client_create(const char *host, uint16_t port, enum sock_type type); GEAR_API int sock_client_set_callback(struct sock_client *c, - void (*on_connect)(int fd, struct sock_connection *conn), - void (*on_buffer)(int, void *buf, size_t len), - void (*on_disconnect)(int fd, struct sock_connection *conn)); + void (*on_connect)(struct sock_client *c, struct sock_connection *conn), + void (*on_buffer)(struct sock_client *c, void *buf, size_t len), + void (*on_disconnect)(struct sock_client *c, struct sock_connection *conn)); GEAR_API int sock_client_connect(struct sock_client *c); GEAR_API int sock_client_disconnect(struct sock_client *c); GEAR_API void sock_client_destroy(struct sock_client *c); diff --git a/gear-lib/libsock/test_libsock.c b/gear-lib/libsock/test_libsock.c index ec0f4189..f0202591 100644 --- a/gear-lib/libsock/test_libsock.c +++ b/gear-lib/libsock/test_libsock.c @@ -30,17 +30,17 @@ #include #endif -static void on_connect_server(int fd, struct sock_connection *conn) +static void on_connect_server(struct sock_server *s, struct sock_connection *conn) { printf("on_connect_server: fd=%d local=%s:%d, remote=%s:%d\n", conn->fd, conn->local.ip_str, conn->local.port, conn->remote.ip_str, conn->remote.port); } -static void on_connect_client(int fd, struct sock_connection *conn) +static void on_connect_client(struct sock_client *c, struct sock_connection *conn) { int ret=0; - printf("on_connect_client: fd=%d local=%s:%d, remote=%s:%d\n", conn->fd, + printf("on_connect_client: fd=%d local=%s:%d, remote=%s:%d\n", c->conn->fd, conn->local.ip_str, conn->local.port, conn->remote.ip_str, conn->remote.port); #if defined (OS_LINUX) @@ -70,9 +70,15 @@ static void on_connect_client(int fd, struct sock_connection *conn) #endif } -static void on_recv_buf(int fd, void *buf, size_t len) +static void on_recv_buf(struct sock_server *s, void *buf, size_t len) { - printf("%s:%d fd = %d, recv buf = %s\n", __func__, __LINE__, fd, (char *)buf); + printf("%s:%d fd = %d, recv buf = %s\n", __func__, __LINE__, s->fd, (char *)buf); +} + + +static void on_recv_buf_cli(struct sock_client *c, void *buf, size_t len) +{ + printf("%s:%d fd = %d, recv buf = %s\n", __func__, __LINE__, c->fd, (char *)buf); } void usage() @@ -174,7 +180,7 @@ int main(int argc, char **argv) port = atoi(argv[3]); } sc = sock_client_create(ip, port, SOCK_TYPE_TCP); - sock_client_set_callback(sc, on_connect_client, on_recv_buf, NULL); + sock_client_set_callback(sc, on_connect_client, on_recv_buf_cli, NULL); sock_client_connect(sc); while (1) { memset(buf, 0, sizeof(buf)); @@ -197,7 +203,7 @@ int main(int argc, char **argv) } #ifdef ENABLE_PTCP sc = sock_client_create(ip, port, SOCK_TYPE_PTCP); - sock_client_set_callback(sc, on_connect_client, on_recv_buf, NULL); + sock_client_set_callback(sc, on_connect_client, on_recv_buf_cli, NULL); sock_client_connect(sc); while (1) { memset(buf, 0, sizeof(buf));