Skip to content

Commit fe41452

Browse files
committed
[libsock] refine api
1 parent eab8188 commit fe41452

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

gear-lib/libsock/libsock_ext.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ static void on_recv(int fd, void *arg)
4040
struct sock_server *s = (struct sock_server *)arg;
4141
ret = sock_recv(fd, buf, 2048);
4242
if (ret > 0) {
43-
s->on_buffer(fd, buf, ret);
43+
s->on_buffer(s, buf, ret);
4444
} else if (ret == 0) {
4545
printf("delete connection fd:%d\n", fd);
4646
if (s->on_disconnect) {
47-
s->on_disconnect(fd, NULL);
47+
s->on_disconnect(s, NULL);
4848
}
4949
} else if (ret < 0) {
5050
printf("%s:%d recv failed!\n", __func__, __LINE__);
@@ -59,11 +59,11 @@ static void on_client_recv(int fd, void *arg)
5959
struct sock_client *c = (struct sock_client *)arg;
6060
ret = sock_recv(fd, buf, 2048);
6161
if (ret > 0) {
62-
c->on_buffer(fd, buf, ret);
62+
c->on_buffer(c, buf, ret);
6363
} else if (ret == 0) {
6464
printf("delete connection fd:%d\n", fd);
6565
if (c->on_disconnect) {
66-
c->on_disconnect(fd, NULL);
66+
c->on_disconnect(c, NULL);
6767
}
6868
} else if (ret < 0) {
6969
printf("%s:%d recv failed!\n", __func__, __LINE__);
@@ -114,7 +114,7 @@ static void on_tcp_connect(int fd, void *arg)
114114
sc.remote.ip = ip;
115115
sc.remote.port = port;
116116
sock_addr_ntop(sc.remote.ip_str, ip);
117-
s->on_connect(fd, &sc);
117+
s->on_connect(s, &sc);
118118
}
119119
e = gevent_create(afd, on_recv, NULL, on_error, s);
120120
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
195195
}
196196

197197
int sock_server_set_callback(struct sock_server *s,
198-
void (*on_connect)(int fd, struct sock_connection *conn),
199-
void (*on_buffer)(int, void *buf, size_t len),
200-
void (*on_disconnect)(int fd, struct sock_connection *conn))
198+
void (*on_connect)(struct sock_server *s, struct sock_connection *conn),
199+
void (*on_buffer)(struct sock_server *s, void *buf, size_t len),
200+
void (*on_disconnect)(struct sock_server *s, struct sock_connection *conn))
201201
{
202202
struct gevent *e;
203203
if (!s) {
@@ -274,9 +274,9 @@ struct sock_client *sock_client_create(const char *host, uint16_t port, enum soc
274274
}
275275

276276
int sock_client_set_callback(struct sock_client *c,
277-
void (*on_connect)(int fd, struct sock_connection *conn),
278-
void (*on_buffer)(int, void *buf, size_t len),
279-
void (*on_disconnect)(int fd, struct sock_connection *conn))
277+
void (*on_connect)(struct sock_client *c, struct sock_connection *conn),
278+
void (*on_buffer)(struct sock_client *c, void *buf, size_t len),
279+
void (*on_disconnect)(struct sock_client *c, struct sock_connection *conn))
280280
{
281281
if (!c) {
282282
return -1;
@@ -324,7 +324,7 @@ GEAR_API int sock_client_connect(struct sock_client *c)
324324
}
325325
if (c->conn) {
326326
if (c->on_connect) {
327-
c->on_connect(c->conn->fd, c->conn);
327+
c->on_connect(c, c->conn);
328328
}
329329
}
330330
c->thread = thread_create(sock_client_thread, c);

gear-lib/libsock/libsock_ext.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,20 @@ struct sock_server {
3434
struct sock_connection *conn;
3535
enum sock_type type;
3636
struct gevent_base *evbase;
37-
void (*on_buffer)(int fd, void *buf, size_t len);
38-
void (*on_connect)(int fd, struct sock_connection *conn);
39-
void (*on_disconnect)(int fd, struct sock_connection *conn);
37+
void (*on_buffer)(struct sock_server *s, void *buf, size_t len);
38+
void (*on_connect)(struct sock_server *s, struct sock_connection *conn);
39+
void (*on_disconnect)(struct sock_server *s, struct sock_connection *conn);
40+
void *priv;
4041
};
4142

4243
/*
4344
* socket server high-level API
4445
*/
4546
GEAR_API struct sock_server *sock_server_create(const char *host, uint16_t port, enum sock_type type);
4647
GEAR_API int sock_server_set_callback(struct sock_server *s,
47-
void (*on_connect)(int fd, struct sock_connection *conn),
48-
void (*on_buffer)(int, void *buf, size_t len),
49-
void (*on_disconnect)(int fd, struct sock_connection *conn));
48+
void (*on_connect)(struct sock_server *s, struct sock_connection *conn),
49+
void (*on_buffer)(struct sock_server *s, void *buf, size_t len),
50+
void (*on_disconnect)(struct sock_server *s, struct sock_connection *conn));
5051
GEAR_API int sock_server_dispatch(struct sock_server *s);
5152
GEAR_API void sock_server_destroy(struct sock_server *s);
5253

@@ -61,16 +62,17 @@ struct sock_client {
6162
enum sock_type type;
6263
struct gevent_base *evbase;
6364
struct thread *thread;
64-
void (*on_buffer)(int fd, void *buf, size_t len);
65-
void (*on_connect)(int fd, struct sock_connection *conn);
66-
void (*on_disconnect)(int fd, struct sock_connection *conn);
65+
void (*on_buffer)(struct sock_client *c, void *buf, size_t len);
66+
void (*on_connect)(struct sock_client *c, struct sock_connection *conn);
67+
void (*on_disconnect)(struct sock_client *c, struct sock_connection *conn);
68+
void *priv;
6769
};
6870

6971
GEAR_API struct sock_client *sock_client_create(const char *host, uint16_t port, enum sock_type type);
7072
GEAR_API int sock_client_set_callback(struct sock_client *c,
71-
void (*on_connect)(int fd, struct sock_connection *conn),
72-
void (*on_buffer)(int, void *buf, size_t len),
73-
void (*on_disconnect)(int fd, struct sock_connection *conn));
73+
void (*on_connect)(struct sock_client *c, struct sock_connection *conn),
74+
void (*on_buffer)(struct sock_client *c, void *buf, size_t len),
75+
void (*on_disconnect)(struct sock_client *c, struct sock_connection *conn));
7476
GEAR_API int sock_client_connect(struct sock_client *c);
7577
GEAR_API int sock_client_disconnect(struct sock_client *c);
7678
GEAR_API void sock_client_destroy(struct sock_client *c);

gear-lib/libsock/test_libsock.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030
#include <signal.h>
3131
#endif
3232

33-
static void on_connect_server(int fd, struct sock_connection *conn)
33+
static void on_connect_server(struct sock_server *s, struct sock_connection *conn)
3434
{
3535
printf("on_connect_server: fd=%d local=%s:%d, remote=%s:%d\n", conn->fd,
3636
conn->local.ip_str, conn->local.port,
3737
conn->remote.ip_str, conn->remote.port);
3838
}
3939

40-
static void on_connect_client(int fd, struct sock_connection *conn)
40+
static void on_connect_client(struct sock_client *c, struct sock_connection *conn)
4141
{
4242
int ret=0;
43-
printf("on_connect_client: fd=%d local=%s:%d, remote=%s:%d\n", conn->fd,
43+
printf("on_connect_client: fd=%d local=%s:%d, remote=%s:%d\n", c->conn->fd,
4444
conn->local.ip_str, conn->local.port,
4545
conn->remote.ip_str, conn->remote.port);
4646
#if defined (OS_LINUX)
@@ -70,9 +70,15 @@ static void on_connect_client(int fd, struct sock_connection *conn)
7070
#endif
7171
}
7272

73-
static void on_recv_buf(int fd, void *buf, size_t len)
73+
static void on_recv_buf(struct sock_server *s, void *buf, size_t len)
7474
{
75-
printf("%s:%d fd = %d, recv buf = %s\n", __func__, __LINE__, fd, (char *)buf);
75+
printf("%s:%d fd = %d, recv buf = %s\n", __func__, __LINE__, s->fd, (char *)buf);
76+
}
77+
78+
79+
static void on_recv_buf_cli(struct sock_client *c, void *buf, size_t len)
80+
{
81+
printf("%s:%d fd = %d, recv buf = %s\n", __func__, __LINE__, c->fd, (char *)buf);
7682
}
7783

7884
void usage()
@@ -174,7 +180,7 @@ int main(int argc, char **argv)
174180
port = atoi(argv[3]);
175181
}
176182
sc = sock_client_create(ip, port, SOCK_TYPE_TCP);
177-
sock_client_set_callback(sc, on_connect_client, on_recv_buf, NULL);
183+
sock_client_set_callback(sc, on_connect_client, on_recv_buf_cli, NULL);
178184
sock_client_connect(sc);
179185
while (1) {
180186
memset(buf, 0, sizeof(buf));
@@ -197,7 +203,7 @@ int main(int argc, char **argv)
197203
}
198204
#ifdef ENABLE_PTCP
199205
sc = sock_client_create(ip, port, SOCK_TYPE_PTCP);
200-
sock_client_set_callback(sc, on_connect_client, on_recv_buf, NULL);
206+
sock_client_set_callback(sc, on_connect_client, on_recv_buf_cli, NULL);
201207
sock_client_connect(sc);
202208
while (1) {
203209
memset(buf, 0, sizeof(buf));

0 commit comments

Comments
 (0)