-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
114 lines (100 loc) · 2.92 KB
/
client.c
File metadata and controls
114 lines (100 loc) · 2.92 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include "mtime.h"
#include "clients.h"
#include "client.h"
inline void client_update(client_t *client)
{
char buffer[CLIENT_RCVBUF];
ssize_t size;
struct linger l;
int e, n;
switch(client->state)
{
case CLIENT_READY:
client->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (client->fd == -1)
err(1, "socket");
e = fcntl(client->fd, F_SETFL, O_NONBLOCK);
if (e == -1)
err(1, "fcntl");
n = 1;
e = setsockopt(client->fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &n, sizeof n);
if (e == -1)
err(1, "setsockopt TCP_QUICKACK");
n = 1;
e = setsockopt(client->fd, IPPROTO_TCP, TCP_NODELAY, (char *) &n, sizeof n);
if (e == -1)
err(1, "setsockopt NODELAY");
n = CLIENT_RCVBUF;
e = setsockopt(client->fd, SOL_SOCKET, SO_RCVBUF, (char *) &n, sizeof n);
if (e == -1)
err(1, "setsockopt SO_RCVBUF");
l.l_onoff = 1;
l.l_linger = 0;
e = setsockopt(client->fd, SOL_SOCKET, SO_LINGER, &l, sizeof l);
if (e == -1)
err(1, "setsockopt SO_LINGER");
client->state = CLIENT_CONNECTING;
e = connect(client->fd, (struct sockaddr *) &client->clients->server_addr, sizeof client->clients->server_addr);
if (e == -1 && errno != EINPROGRESS)
err(1, "connect");
if (errno == EINPROGRESS)
{
client->ev.events = EPOLLOUT | EPOLLET;
client->ev.data.ptr = client;
e = epoll_ctl(client->clients->epollfd, EPOLL_CTL_ADD, client->fd, &client->ev);
if (e == -1)
err(1, "epoll_ctl add");
break;
}
/* fall through */
case CLIENT_CONNECTING:
n = client->clients->request_buffer_length;
size = send(client->fd, client->clients->request_buffer, n, 0);
if (size != n)
err(1, "send");
client->ev.events = EPOLLIN | EPOLLET;
e = epoll_ctl(client->clients->epollfd, EPOLL_CTL_MOD, client->fd, &client->ev);
if (e == -1)
err(1, "epoll_ctl mod");
client->state = CLIENT_REQUESTING;
break;
case CLIENT_REQUESTING:
size = recv(client->fd, buffer, sizeof buffer, 0);
if (size == -1)
err(1, "recv");
if (size > 0)
{
client->ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
e = epoll_ctl(client->clients->epollfd, EPOLL_CTL_MOD, client->fd, &client->ev);
if (e == -1)
err(1, "epoll_ctl mod");
client->clients->received_total += size;
}
else
{
e = epoll_ctl(client->clients->epollfd, EPOLL_CTL_DEL, client->fd, NULL);
if (e == -1)
err(1, "epoll_ctl del");
e = close(client->fd);
if (e == -1)
err(1, "close");
client->state = CLIENT_READY;
client->clients->nrequests_done ++;
}
break;
}
}