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 / connection_interruption.c @ 90fc8d36
History | View | Annotate | Download (3.38 KB)
1 |
#include "test/common.c" |
---|---|
2 |
#define NCONN 100 |
3 |
#define TIMEOUT 1000.0 |
4 |
|
5 |
static oi_server server;
|
6 |
|
7 |
static void |
8 |
on_peer_read(oi_socket *socket, const void *base, size_t len) |
9 |
{ |
10 |
assert(len == 0);
|
11 |
oi_socket_write_simple(socket, "BYE", 3); |
12 |
//printf("server wrote bye\n");
|
13 |
} |
14 |
|
15 |
static void |
16 |
on_peer_drain(oi_socket *socket) |
17 |
{ |
18 |
oi_socket_close(socket); |
19 |
} |
20 |
|
21 |
static void |
22 |
on_peer_error2(oi_socket *socket, struct oi_error e)
|
23 |
{ |
24 |
if(e.domain == OI_ERROR_GNUTLS) return; |
25 |
assert(0);
|
26 |
} |
27 |
|
28 |
static oi_socket*
|
29 |
on_server_connection(oi_server *server, struct sockaddr *addr, socklen_t len)
|
30 |
{ |
31 |
oi_socket *socket = malloc(sizeof(oi_socket));
|
32 |
oi_socket_init(socket, TIMEOUT); |
33 |
socket->on_read = on_peer_read; |
34 |
socket->on_drain = on_peer_drain; |
35 |
socket->on_error = on_peer_error2; |
36 |
socket->on_close = on_peer_close; |
37 |
socket->on_timeout = on_peer_timeout; |
38 |
|
39 |
#if HAVE_GNUTLS
|
40 |
# if SECURE
|
41 |
anon_tls_server(socket); |
42 |
# endif
|
43 |
#endif
|
44 |
|
45 |
//printf("on server connection\n");
|
46 |
|
47 |
return socket;
|
48 |
} |
49 |
|
50 |
static void |
51 |
on_client_connect(oi_socket *socket) |
52 |
{ |
53 |
//printf("on client connection\n");
|
54 |
oi_socket_write_eof(socket); |
55 |
} |
56 |
|
57 |
static void |
58 |
on_client_close(oi_socket *socket) |
59 |
{ |
60 |
oi_socket_close(socket); // already closed, but it shouldn't crash if we try to do it again
|
61 |
|
62 |
//printf("client connection closed\n");
|
63 |
if(++nconnections == NCONN) {
|
64 |
oi_server_detach(&server); |
65 |
//printf("detaching server\n");
|
66 |
} |
67 |
} |
68 |
|
69 |
static void |
70 |
on_client_read(oi_socket *socket, const void *base, size_t len) |
71 |
{ |
72 |
char buf[200000]; |
73 |
strncpy(buf, base, len); |
74 |
buf[len] = 0;
|
75 |
|
76 |
//printf("client got message: %s\n", buf);
|
77 |
|
78 |
if(strcmp(buf, "BYE") == 0) { |
79 |
oi_socket_close(socket); |
80 |
} else {
|
81 |
assert(0);
|
82 |
} |
83 |
} |
84 |
|
85 |
int
|
86 |
main(int argc, const char *argv[]) |
87 |
{ |
88 |
int r;
|
89 |
|
90 |
oi_server_init(&server, 1000);
|
91 |
server.on_connection = on_server_connection; |
92 |
#if HAVE_GNUTLS
|
93 |
# if SECURE
|
94 |
anon_tls_init(); |
95 |
# endif
|
96 |
#endif
|
97 |
|
98 |
struct addrinfo *servinfo;
|
99 |
struct addrinfo hints;
|
100 |
memset(&hints, 0, sizeof hints); |
101 |
#if TCP
|
102 |
hints.ai_family = AF_UNSPEC; |
103 |
hints.ai_socktype = SOCK_STREAM; |
104 |
hints.ai_flags = AI_PASSIVE; |
105 |
r = getaddrinfo(NULL, PORT, &hints, &servinfo);
|
106 |
assert(r == 0);
|
107 |
#else
|
108 |
struct stat tstat;
|
109 |
if (lstat(SOCKFILE, &tstat) == 0) { |
110 |
if (S_ISSOCK(tstat.st_mode))
|
111 |
unlink(SOCKFILE); |
112 |
} |
113 |
|
114 |
servinfo = malloc(sizeof(struct addrinfo)); |
115 |
servinfo->ai_family = AF_UNIX; |
116 |
servinfo->ai_socktype = SOCK_STREAM; |
117 |
servinfo->ai_protocol = 0;
|
118 |
|
119 |
struct sockaddr_un *sockaddr = calloc(sizeof(struct sockaddr_un), 1); |
120 |
sockaddr->sun_family = AF_UNIX; |
121 |
strcpy(sockaddr->sun_path, SOCKFILE); |
122 |
|
123 |
servinfo->ai_addr = (struct sockaddr*)sockaddr;
|
124 |
servinfo->ai_addrlen = sizeof(struct sockaddr_un); |
125 |
#endif
|
126 |
|
127 |
oi_server_listen(&server, servinfo); |
128 |
oi_server_attach(EV_DEFAULT_ &server); |
129 |
|
130 |
int i;
|
131 |
for(i = 0; i < NCONN; i++) { |
132 |
oi_socket *client = malloc(sizeof(oi_socket));
|
133 |
oi_socket_init(client, TIMEOUT); |
134 |
client->on_read = on_client_read; |
135 |
client->on_error = on_client_error; |
136 |
client->on_connect = on_client_connect; |
137 |
client->on_close = on_client_close; |
138 |
client->on_timeout = on_client_timeout; |
139 |
#if HAVE_GNUTLS
|
140 |
#if SECURE
|
141 |
anon_tls_client(client); |
142 |
#endif
|
143 |
#endif
|
144 |
r = oi_socket_connect(client, servinfo); |
145 |
assert(r == 0 && "problem connecting"); |
146 |
oi_socket_attach(EV_DEFAULT_ client); |
147 |
} |
148 |
|
149 |
ev_loop(EV_DEFAULT_ 0);
|
150 |
|
151 |
assert(nconnections == NCONN); |
152 |
|
153 |
#if TCP
|
154 |
freeaddrinfo(servinfo); |
155 |
#endif
|
156 |
|
157 |
return 0; |
158 |
} |