The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / liboi / test / ping_pong.c @ 40c0f755
History | View | Annotate | Download (3.47 KB)
1 |
#include "test/common.c" |
---|---|
2 |
|
3 |
#define PING "PING" |
4 |
#define PONG "PONG" |
5 |
#define EXCHANGES 100 |
6 |
|
7 |
int successful_ping_count;
|
8 |
|
9 |
static void |
10 |
on_peer_read(oi_socket *socket, const void *base, size_t len) |
11 |
{ |
12 |
if(len == 0) |
13 |
return;
|
14 |
|
15 |
char buf[2000]; |
16 |
strncpy(buf, base, len); |
17 |
buf[len] = 0;
|
18 |
//printf("server got message: %s\n", buf);
|
19 |
|
20 |
oi_socket_write_simple(socket, PONG, sizeof PONG);
|
21 |
} |
22 |
|
23 |
static void |
24 |
on_peer_error(oi_socket *socket, struct oi_error e)
|
25 |
{ |
26 |
assert(0);
|
27 |
} |
28 |
|
29 |
static void |
30 |
on_client_close(oi_socket *socket) |
31 |
{ |
32 |
//printf("client connection closed\n");
|
33 |
ev_unloop(socket->loop, EVUNLOOP_ALL); |
34 |
} |
35 |
|
36 |
static oi_socket*
|
37 |
on_server_connection(oi_server *server, struct sockaddr *addr, socklen_t len)
|
38 |
{ |
39 |
oi_socket *socket = malloc(sizeof(oi_socket));
|
40 |
oi_socket_init(socket, 5.0); |
41 |
socket->on_read = on_peer_read; |
42 |
socket->on_error = on_peer_error; |
43 |
socket->on_close = on_peer_close; |
44 |
socket->on_timeout = on_peer_timeout; |
45 |
|
46 |
nconnections++; |
47 |
|
48 |
#if HAVE_GNUTLS
|
49 |
# if SECURE
|
50 |
anon_tls_server(socket); |
51 |
# endif
|
52 |
#endif
|
53 |
|
54 |
//printf("on server connection\n");
|
55 |
|
56 |
return socket;
|
57 |
} |
58 |
|
59 |
static void |
60 |
on_client_connect(oi_socket *socket) |
61 |
{ |
62 |
//printf("client connected. sending ping\n");
|
63 |
oi_socket_write_simple(socket, PING, sizeof PING);
|
64 |
} |
65 |
|
66 |
static void |
67 |
on_client_read(oi_socket *socket, const void *base, size_t len) |
68 |
{ |
69 |
char buf[200000]; |
70 |
strncpy(buf, base, len); |
71 |
buf[len] = 0;
|
72 |
//printf("client got message: %s\n", buf);
|
73 |
|
74 |
if(strcmp(buf, PONG) == 0) { |
75 |
|
76 |
if(++successful_ping_count > EXCHANGES) {
|
77 |
oi_socket_close(socket); |
78 |
return;
|
79 |
} |
80 |
oi_socket_write_simple(socket, PING, sizeof PING);
|
81 |
} else {
|
82 |
assert(0);
|
83 |
} |
84 |
} |
85 |
|
86 |
int
|
87 |
main(int argc, const char *argv[]) |
88 |
{ |
89 |
int r;
|
90 |
struct ev_loop *loop = ev_default_loop(0); |
91 |
oi_server server; |
92 |
oi_socket client; |
93 |
|
94 |
//printf("sizeof(oi_server): %d\n", sizeof(oi_server));
|
95 |
//printf("sizeof(oi_socket): %d\n", sizeof(oi_socket));
|
96 |
|
97 |
oi_server_init(&server, 10);
|
98 |
server.on_connection = on_server_connection; |
99 |
|
100 |
#if HAVE_GNUTLS
|
101 |
# if SECURE
|
102 |
anon_tls_init(); |
103 |
# endif
|
104 |
#endif
|
105 |
|
106 |
struct addrinfo *servinfo;
|
107 |
struct addrinfo hints;
|
108 |
memset(&hints, 0, sizeof hints); |
109 |
#if TCP
|
110 |
hints.ai_family = AF_UNSPEC; |
111 |
hints.ai_socktype = SOCK_STREAM; |
112 |
hints.ai_flags = AI_PASSIVE; |
113 |
r = getaddrinfo(NULL, PORT, &hints, &servinfo);
|
114 |
assert(r == 0);
|
115 |
#else
|
116 |
struct stat tstat;
|
117 |
if (lstat(SOCKFILE, &tstat) == 0) { |
118 |
if (S_ISSOCK(tstat.st_mode))
|
119 |
unlink(SOCKFILE); |
120 |
} |
121 |
|
122 |
servinfo = malloc(sizeof(struct addrinfo)); |
123 |
servinfo->ai_family = AF_UNIX; |
124 |
servinfo->ai_socktype = SOCK_STREAM; |
125 |
servinfo->ai_protocol = 0;
|
126 |
|
127 |
struct sockaddr_un *sockaddr = calloc(sizeof(struct sockaddr_un), 1); |
128 |
sockaddr->sun_family = AF_UNIX; |
129 |
strcpy(sockaddr->sun_path, SOCKFILE); |
130 |
|
131 |
servinfo->ai_addr = (struct sockaddr*)sockaddr;
|
132 |
servinfo->ai_addrlen = sizeof(struct sockaddr_un); |
133 |
#endif
|
134 |
r = oi_server_listen(&server, servinfo); |
135 |
assert(r == 0);
|
136 |
oi_server_attach(&server, loop); |
137 |
|
138 |
oi_socket_init(&client, 5.0); |
139 |
client.on_read = on_client_read; |
140 |
client.on_error = on_client_error; |
141 |
client.on_connect = on_client_connect; |
142 |
client.on_close = on_client_close; |
143 |
client.on_timeout = on_client_timeout; |
144 |
|
145 |
#if HAVE_GNUTLS
|
146 |
# if SECURE
|
147 |
anon_tls_client(&client); |
148 |
# endif
|
149 |
#endif
|
150 |
|
151 |
r = oi_socket_connect(&client, servinfo); |
152 |
assert(r == 0 && "problem connecting"); |
153 |
oi_socket_attach(&client, loop); |
154 |
|
155 |
ev_loop(loop, 0);
|
156 |
|
157 |
assert(successful_ping_count == EXCHANGES + 1);
|
158 |
assert(nconnections == 1);
|
159 |
|
160 |
#if TCP
|
161 |
freeaddrinfo(servinfo); |
162 |
#endif
|
163 |
|
164 |
return 0; |
165 |
} |