-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclients.c
More file actions
87 lines (70 loc) · 2.06 KB
/
Copy pathclients.c
File metadata and controls
87 lines (70 loc) · 2.06 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#include "mtime.h"
#include "clients.h"
#include "client.h"
int clients_init(clients_t *clients, int nrequests, int nclients, u_int32_t ip, u_int16_t port, char *path)
{
int n, i;
bzero(clients, sizeof *clients);
clients->nrequests = nrequests;
clients->nrequests_started = 0;
clients->nrequests_done = 0;
clients->nclients = nclients;
clients->received_total = 0;
n = snprintf(clients->request_buffer, sizeof clients->request_buffer,
"GET %s HTTP/1.0\r\n"
"Connection: close\r\n"
"\r\n",
path);
if (n == -1 || n >= sizeof clients->request_buffer)
return -1;
clients->request_buffer_length = n;
bzero(&clients->server_addr, sizeof clients->server_addr);
clients->server_addr.sin_family = AF_INET;
clients->server_addr.sin_addr.s_addr = ip;
clients->server_addr.sin_port = port;
clients->client = calloc(clients->nclients, sizeof *clients->client);
if (!clients->client)
return -1;
for (i = 0; i < clients->nclients; i++)
{
clients->client[i].state = CLIENT_READY;
clients->client[i].clients = clients;
}
clients->epollfd = epoll_create(1024);
if (clients->epollfd == -1)
return -1;
clients->time_done = 0;
clients->time_started = mtime();
clients_update(clients);
return 0;
}
int clients_loop(clients_t *clients)
{
struct epoll_event events[CLIENTS_MAX_EVENTS];
int n = -1, i;
while (clients->nrequests_done < clients->nrequests)
{
n = epoll_wait(clients->epollfd, events, CLIENTS_MAX_EVENTS, -1);
if (n <= 0)
break;
for (i = 0; i < n; i ++)
client_update(events[i].data.ptr);
clients_update(clients);
}
clients->time_done = mtime();
return n;
}
void clients_update(clients_t *clients)
{
int i;
for (i = 0; i < clients->nclients && clients->nrequests_started < clients->nrequests; i++)
if (clients->client[i].state == CLIENT_READY)
{
client_update(&clients->client[i]);
clients->nrequests_started ++;
}
}