-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.c
160 lines (139 loc) · 3.9 KB
/
task.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "task.h"
#include "common.h"
void fill_read_task(read_task_struct *read_task, int fd, void *buf, size_t count) {
read_task->fd = fd;
read_task->buf = buf;
read_task->count = count;
}
/* return:
-1 -- failed task->ret = errno
0 -- read all task->ret = 0
1 -- read not all, try again task->ret not seted
*/
int continue_read_task(read_task_struct *task) {
assert(task->count > 0);
ssize_t bytes = read_wrapper(task->fd, task->buf, task->count);
if (bytes < 0) {
task->ret = bytes;
return -1;
} else if (bytes == 0) {
return 1;
} else {
assert(bytes <= (ssize_t)(task->count));
task->buf += bytes;
task->count -= bytes;
if (task->count > 0) {
return 1;
} else {
task->ret = 0;
return 0;
}
}
}
int get_read_result(read_task_struct *read_task) {
return read_task->ret;
}
void fill_write_task(write_task_struct *write_task, int fd, void *buf, size_t count) {
write_task->fd = fd;
write_task->buf = buf;
write_task->count = count;
}
/* return:
-1 -- failed task->ret = errno
0 -- write all task->ret = 0
1 -- write not all, try again task->ret not seted
*/
int continue_write_task(write_task_struct *task) {
assert(task->count > 0);
ssize_t bytes = write_wrapper(task->fd, task->buf, task->count);
if (bytes < 0) {
task->ret = bytes;
return -1;
} else if (bytes == 0) {
return 1;
} else {
assert(bytes <= (ssize_t)(task->count));
task->buf += bytes;
task->count -= bytes;
if (task->count > 0) {
return 1;
} else {
task->ret = 0;
return 0;
}
}
}
int get_write_result(write_task_struct *write_task) {
return write_task->ret;
}
void fill_getaddrinfo_task(getaddrinfo_task_struct *getaddrinfo_task,
const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {
getaddrinfo_task->node = node;
getaddrinfo_task->service = service;
getaddrinfo_task->hints = hints;
getaddrinfo_task->res = res;
}
int get_getaddrinfo_result(getaddrinfo_task_struct *getaddrinfo_task) {
return getaddrinfo_task->ret;
}
void fill_connect_task(connect_task_struct *connect_task, int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
connect_task->sockfd = sockfd;
connect_task->addr = addr;
connect_task->addrlen = addrlen;
}
static int handle_connect_err(connect_task_struct *connect_task, int err) {
switch (err) {
case 0:
connect_task->ret = 0;
return 0;
case EINPROGRESS:
return 1;
case ECONNREFUSED:
case ETIMEDOUT:
case ENETUNREACH:
connect_task->ret = err;
return -1;
default:
// not 'perror' because 'errno' may be not equal to 'err'
printf_err("connect: %s", strerror(err));
connect_task->ret = err;
return -1;
}
}
/* return:
-1 -- failed connect_task->ret = errno
0 -- succeeded connect_task->ret = 0
1 -- not completed yet, try again connect_task->ret not seted
*/
int first_try_connect_task(connect_task_struct *connect_task) {
int err = connect(connect_task->sockfd, connect_task->addr, connect_task->addrlen);
if (err) err = errno;
return handle_connect_err(connect_task, err);
}
/* return:
-1 -- failed connect_task->ret = errno
0 -- succeeded connect_task->ret = 0
1 -- not completed yet, try again connect_task->ret not seted
*/
int continue_connect_task(connect_task_struct *connect_task) {
int err;
socklen_t err_len = sizeof(err);
int ret = getsockopt(connect_task->sockfd, SOL_SOCKET, SO_ERROR, &err, &err_len);
if (ret) {
perror("getsockopt");
connect_task->ret = errno;
return -1;
}
assert(err_len == sizeof(err));
return handle_connect_err(connect_task, err);
}
int get_connect_result(connect_task_struct *connect_task) {
return connect_task->ret;
}