Skip to content

Commit

Permalink
[libsock] refine api
Browse files Browse the repository at this point in the history
  • Loading branch information
gozfree committed Sep 20, 2021
1 parent eab8188 commit fe41452
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
24 changes: 12 additions & 12 deletions gear-lib/libsock/libsock_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand All @@ -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__);
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 14 additions & 12 deletions gear-lib/libsock/libsock_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@ 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;
};

/*
* socket server high-level API
*/
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);

Expand All @@ -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);
Expand Down
20 changes: 13 additions & 7 deletions gear-lib/libsock/test_libsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
#include <signal.h>
#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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand Down

0 comments on commit fe41452

Please sign in to comment.