-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.c
229 lines (202 loc) · 6.09 KB
/
test.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <getopt.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <sys/times.h>
#include <signal.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/ioctl.h>
#include "map.h"
#define MAX_FD (1024 * 1024 * 10)
#define MAX_EVENTS 1024
#define BUF_SIZE 1600
typedef struct _conn_info
{
int status;
char buf[BUF_SIZE];
int send_size;
int skt;
} conn_info_t;
static int do_connect(conn_info_t *info, const char *ip, int port, const char *path, int efd, map_t *map)
{
int unblock = 1;
struct sockaddr_in sockaddr;
in_addr_t serverip;
struct epoll_event event;
int ret;
memset(info, 0, sizeof(*info));
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(port);
serverip = inet_addr(ip);
sockaddr.sin_addr = *(struct in_addr *)&serverip;
info->skt = socket(AF_INET, SOCK_STREAM, 0);
if (info->skt == -1)
{
fprintf(stderr, "socket FAILED! %s\n", strerror(errno));
return -1;
}
ioctl(info->skt, FIONBIO, (int)&unblock);
if (connect(info->skt, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0)
{
fprintf(stderr, "connect FAILED! %s\n", strerror(errno));
return -1;
}
snprintf(info->buf, BUF_SIZE, "GET %s HTTP/1.1\r\n\r\n", path);
memset(&event, 0, sizeof(event));
event.data.fd = info->skt;
event.events = EPOLLOUT;
ret = epoll_ctl(efd, EPOLL_CTL_ADD, info->skt, &event);
if (ret == -1)
{
fprintf(stderr, "epoll_ctl add error: %s\n", strerror(errno));
return -1;
}
if (map_add(map, info->skt, info) != 0)
{
fprintf(stderr, "add %d to map FAILED!\n", info->skt);
return -1;
}
return 0;
}
static int do_disconnect(conn_info_t *info, int efd, map_t *map)
{
struct epoll_event event;
memset(&event, 0, sizeof(event));
event.data.fd = info->skt;
epoll_ctl(efd, EPOLL_CTL_DEL, info->skt, &event);
close(info->skt);
map_remove(map, info->skt);
memset(info, 0, sizeof(*info));
return 0;
}
int main(int argc, char **argv)
{
char ip[256];
int port;
char path[1024];
int count;
conn_info_t *conns;
conn_info_t *info;
struct epoll_event event;
map_t *map;
int ret;
int i;
int efd;
struct epoll_event *events;
snprintf(ip, 256, "%s", argv[1]);
port = atoi(argv[2]);
snprintf(path, 1024, "%s", argv[3]);
fprintf(stderr, "ip:%s, port:%d, path:%s\n", ip, port, path);
count = atoi(argv[4]);
fprintf(stderr, "info count:%d\n", count);
map = map_create(MAX_FD);
if (map == NULL)
{
fprintf(stderr, "map_create FAILED!\n");
return -1;
}
efd = epoll_create1(0);
if (efd == -1)
{
fprintf(stderr, "epoll_create1 error:%s\n", strerror(errno));
return -1;
}
events = (struct epoll_event *)malloc(MAX_EVENTS * sizeof(struct epoll_event));
if (events == NULL)
{
fprintf(stderr, "malloc events FAILED!\n");
return -1;
}
conns = (conn_info_t *)malloc(sizeof(conn_info_t) * count);
if (conns == NULL)
{
fprintf(stderr, "malloc conns FAILED!\n");
return -1;
}
for (i = 0; i < count; i++)
{
ret = do_connect(&conns[i], ip, port, path, efd, map);
if (ret != 0)
{
fprintf(stderr, "connect %d FAILED!\n", i);
exit(-1);
}
}
for (; ;)
{
count = epoll_wait(efd, events, MAX_EVENTS, 100);
for (i = 0; i < count; i++)
{
if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP))
{
info = map_get(map, events[i].data.fd);
if (info != NULL)
do_disconnect(info, efd, map);
}
else
{
info = map_get(map, events[i].data.fd);
if (info == NULL)
{
fprintf(stderr, "info is NULL, should not go here!\n");
continue;
}
if (info->status == 0)
{
ret = send(info->skt, info->buf + info->send_size, strlen(info->buf) - info->send_size, 0);
if (ret == -1)
{
if (errno != EAGAIN)
{
fprintf(stderr, "%d| send got error:%s\n", info->skt, strerror(errno));
do_disconnect(info, efd, map);
}
}
else if (ret == 0)
{
do_disconnect(info, efd, map);
}
else
{
info->send_size += ret;
if (info->send_size >= strlen(info->buf))
{
//fprintf(stderr, "send header ok %d\n", info->skt);
memset(&event, 0, sizeof(event));
event.data.fd = info->skt;
event.events = EPOLLIN;
ret = epoll_ctl(efd, EPOLL_CTL_MOD, info->skt, &event);
if (ret == -1)
{
fprintf(stderr, "%d| epoll_ctl EPOLL_CTL_MOD EPOLLOUT FAILED!\n", info->skt);
continue;
}
info->status = 1;
}
}
}
else
{
ret = read(info->skt, info->buf, BUF_SIZE);
if (ret == -1)
{
if (errno != EAGAIN)
{
printf("read got error:%s\n", strerror(errno));
continue;
}
}
}
}
}
}
return 0;
}