-
Notifications
You must be signed in to change notification settings - Fork 35
/
process_debt.c
60 lines (56 loc) · 1.7 KB
/
process_debt.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
#include "main.h"
/* TODO: too many common code with process_read */
void process_debt(int fd) {
/* After a short write, we finally can deliver the "debt" */
ssize_t ret;
dpf("Selecting %d for writing because of debt %d\n", fd, fdinfo[fd].debt);
send_was_interrupted2:
ret = send(fd, fdinfo[fd].buff, fdinfo[fd].debt, 0);
dpf(" sent %d bytes\n", ret);
if (ret == 0) {
fprintf(stderr, "send returned 0? Submit to codinghorror?\n");
close_fd(fd);
return;
} else
if (ret < 0) {
if(errno==EINTR) goto send_was_interrupted2;
if(errno==EAGAIN) {
fdinfo[fd].writeready=0;
fdinfo[fd].we_should_epoll_for_writes=1;
epoll_update(fd);
} else {
fprintf(stderr, "Why epoll hasn't told us that there would be error at sending?\n");
perror("send");
close_fd(fd);
return;
}
} else {
if (ret < fdinfo[fd].debt) {
dpf(" short write at %d\n", fd);
/* _Again_ short write. Need to remember the rest of the buffer somewhere. */
int l = fdinfo[fd].debt - ret;
char* b = (char*)malloc(l);
if (b == NULL) {
perror("malloc");
close_fd(fd);
return;
}
memcpy(b, fdinfo[fd].buff + ret, l);
free(fdinfo[fd].buff);
fdinfo[fd].buff = b;
fdinfo[fd].debt = l;
fdinfo[fd].we_should_epoll_for_writes=1;
epoll_update(fd);
dpf(" re-stored debt %d for %d\n", l, fd);
} else {
/* Now the debt is fulfilled */
free(fdinfo[fd].buff);
fdinfo[fd].buff = NULL;
fdinfo[fd].debt = 0;
dpf(" debt is fulfilled for %d\n", fd);
/* and we can start a new cycle */
fdinfo[fdinfo[fd].peerfd].we_should_epoll_for_reads = 1;
epoll_update(fdinfo[fd].peerfd);
}
}
}