forked from wayland-chen/libco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_get_url.cpp
197 lines (166 loc) · 4.17 KB
/
example_get_url.cpp
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
/*
* Tencent is pleased to support the open source community by making Libco available.
* Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "co_routine.h"
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <stack>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include <iostream>
using namespace std;
struct SEndPoint
{
string sIp;
uint16_t port;
};
static void SetAddr(const char *pszIP,const unsigned short shPort,struct sockaddr_in &addr)
{
bzero(&addr,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(shPort);
int nIP = 0;
if( !pszIP || '\0' == *pszIP
|| 0 == strcmp(pszIP,"0") || 0 == strcmp(pszIP,"0.0.0.0")
|| 0 == strcmp(pszIP,"*")
)
{
nIP = htonl(INADDR_ANY);
}
else
{
nIP = inet_addr(pszIP);
}
addr.sin_addr.s_addr = nIP;
}
static void *readwrite_routine( void *arg )
{
co_enable_hook_sys();
SEndPoint *endpoint = (SEndPoint *)arg;
int fd = 0;
int ret = 0;
fd = socket(AF_INET , SOCK_STREAM, 0);
struct timeval timeout={1,0};
ret = setsockopt(fd,SOL_SOCKET,SO_RCVTIMEO,(const char*)&timeout,sizeof(timeout));
if(ret < 0)
{
printf("setsockopt fail fd:%d\n",fd);
close(fd);
return 0;
}
struct sockaddr_in addr;
SetAddr(endpoint->sIp.c_str(), endpoint->port, addr);
ret = connect(fd,(struct sockaddr*)&addr,sizeof(addr));
printf("connect fd:%d\n",fd);
if ( errno == EALREADY || errno == EINPROGRESS )
{
printf("fd:%d,connecting\n",fd);
struct pollfd pf = { 0 };
pf.fd = fd;
pf.events = (POLLOUT|POLLERR|POLLHUP);
ret = co_poll( co_get_epoll_ct(),&pf,1,2000);
if(ret <= 0)
{
printf("connect timeout fd:%d\n", fd);
close(fd);
return 0;
}
int error = 0;
uint32_t socklen = sizeof(error);
ret = getsockopt(fd, SOL_SOCKET, SO_ERROR,(void *)&error, &socklen);
if ( ret < 0 )
{
printf("getsockopt ERROR ret %d %d:%s\n", ret, errno, strerror(errno));
close(fd);
return 0;
}
if ( error )
{
errno = error;
printf("connect ERROR ret %d %d:%s\n", error, errno, strerror(errno));
close(fd);
return 0;
}
}
else
{
printf("connect ERROR ret %d:%s\n", errno, strerror(errno));
close(fd);
return 0;
}
string sRequest;
sRequest = "GET http://www.baidu.com/ HTTP/1.1\r\n";
sRequest+="Accept: text/html, application/xhtml+xml, */*\r\n";
sRequest+="Accept-Language: zh-CN\r\n";
sRequest+="Host: www.baidu.com\r\n";
sRequest+="Connection: close\r\n\r\n";
ret = write( fd,sRequest.data(), sRequest.size());
if(ret < 0)
{
printf("co %p write ret %d errno %d (%s)\n",co_self(), ret,errno,strerror(errno));
close(fd);
return 0;
}
char szRecvBuf[8193];
string sResp;
while(ret> 0)
{
memset(szRecvBuf,0,sizeof(szRecvBuf));
ret = read(fd,szRecvBuf,sizeof(szRecvBuf)-1);
printf("recv data len:%d\n",ret);
if(ret > 0)
{
sResp.append(szRecvBuf);
}
}
printf("get data from fd:%d:\n%s\n",fd, sResp.c_str());
close(fd);
return 0;
}
int main(int argc,char *argv[])
{
if(argc < 4)
{
cout<<"usage:"<<argv[0]<<" ip port cnt"<<endl;
return 0;
}
SEndPoint endpoint;
endpoint.sIp = argv[1];
endpoint.port = atoi(argv[2]);
int cnt = atoi( argv[3] );
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigaction( SIGPIPE, &sa, NULL );
for(int i=0;i<cnt;i++)
{
stCoRoutine_t *co = 0;
co_create( &co,NULL,readwrite_routine, &endpoint);
co_resume( co );
}
co_eventloop( co_get_epoll_ct(),0,0 );
return 0;
}
/*./example_echosvr 127.0.0.1 10000 100 50*/