-
Notifications
You must be signed in to change notification settings - Fork 44
/
libevent2-client.c
62 lines (50 loc) · 1.59 KB
/
libevent2-client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <evhttp.h>
#include <event2/event.h>
#include <event2/http.h>
#include <event2/buffer.h>
#include "config.h"
struct context {
struct event_base *base;
struct evhttp_connection **connections;
long nconnections;
long delay;
};
void http_request_done(struct evhttp_request *req, void *arg)
{
struct context *ctx = (struct context *)arg;
evbuffer_drain(req->input_buffer, 1024);
sleep(ctx->delay);
event_base_loopexit(ctx->base, NULL);
}
int main(int argc, char const *argv[])
{
if (argc != 5) {
fprintf(stderr, "Usage: %s <addr> <port> <connections> <delay>\n", argv[0]);
exit(EXIT_FAILURE);
}
const char *addr = argv[1];
int port = strtol(argv[2], NULL, 0);
struct context ctx;
ctx.nconnections = strtol(argv[3], NULL, 0);
ctx.connections = calloc(ctx.nconnections, sizeof(struct evhttp_connection *));
ctx.delay = strtol(argv[4], NULL, 0);
ctx.base = event_base_new();
struct evhttp_request *req;
for (size_t i = 0; i < ctx.nconnections; i++) {
ctx.connections[i] =
evhttp_connection_base_new(ctx.base, NULL, addr, port);
req = evhttp_request_new(http_request_done, &ctx);
evhttp_make_request(ctx.connections[i], req, EVHTTP_REQ_GET, "/");
evhttp_connection_set_timeout(req->evcon, 600);
}
event_base_dispatch(ctx.base);
for (size_t i = 0; i < ctx.nconnections; i++) {
evhttp_connection_free(ctx.connections[i]);
}
event_base_free(ctx.base);
free(ctx.connections);
return 0;
}