forked from Tencent/libco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_echocli.cpp
218 lines (197 loc) · 4.27 KB
/
example_echocli.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* 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 <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
using namespace std;
struct stEndPoint
{
char *ip;
unsigned short int 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 int iSuccCnt = 0;
static int iFailCnt = 0;
static int iTime = 0;
void AddSuccCnt()
{
int now = time(NULL);
if (now >iTime)
{
printf("time %d Succ Cnt %d Fail Cnt %d\n", iTime, iSuccCnt, iFailCnt);
iTime = now;
iSuccCnt = 0;
iFailCnt = 0;
}
else
{
iSuccCnt++;
}
}
void AddFailCnt()
{
int now = time(NULL);
if (now >iTime)
{
printf("time %d Succ Cnt %d Fail Cnt %d\n", iTime, iSuccCnt, iFailCnt);
iTime = now;
iSuccCnt = 0;
iFailCnt = 0;
}
else
{
iFailCnt++;
}
}
static void *readwrite_routine( void *arg )
{
co_enable_hook_sys();
stEndPoint *endpoint = (stEndPoint *)arg;
char str[8]="sarlmol";
char buf[ 1024 * 16 ];
int fd = -1;
int ret = 0;
for(;;)
{
if ( fd < 0 )
{
fd = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
SetAddr(endpoint->ip, endpoint->port, addr);
ret = connect(fd,(struct sockaddr*)&addr,sizeof(addr));
if ( errno == EALREADY || errno == EINPROGRESS )
{
struct pollfd pf = { 0 };
pf.fd = fd;
pf.events = (POLLOUT|POLLERR|POLLHUP);
co_poll( co_get_epoll_ct(),&pf,1,200);
//check connect
int error = 0;
uint32_t socklen = sizeof(error);
errno = 0;
ret = getsockopt(fd, SOL_SOCKET, SO_ERROR,(void *)&error, &socklen);
if ( ret == -1 )
{
//printf("getsockopt ERROR ret %d %d:%s\n", ret, errno, strerror(errno));
close(fd);
fd = -1;
AddFailCnt();
continue;
}
if ( error )
{
errno = error;
//printf("connect ERROR ret %d %d:%s\n", error, errno, strerror(errno));
close(fd);
fd = -1;
AddFailCnt();
continue;
}
}
}
ret = write( fd,str, 8);
if ( ret > 0 )
{
ret = read( fd,buf, sizeof(buf) );
if ( ret <= 0 )
{
//printf("co %p read ret %d errno %d (%s)\n",
// co_self(), ret,errno,strerror(errno));
close(fd);
fd = -1;
AddFailCnt();
}
else
{
//printf("echo %s fd %d\n", buf,fd);
AddSuccCnt();
}
}
else
{
//printf("co %p write ret %d errno %d (%s)\n",
// co_self(), ret,errno,strerror(errno));
close(fd);
fd = -1;
AddFailCnt();
}
}
return 0;
}
int main(int argc,char *argv[])
{
stEndPoint endpoint;
endpoint.ip = argv[1];
endpoint.port = atoi(argv[2]);
int cnt = atoi( argv[3] );
int proccnt = atoi( argv[4] );
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigaction( SIGPIPE, &sa, NULL );
for(int k=0;k<proccnt;k++)
{
pid_t pid = fork();
if( pid > 0 )
{
continue;
}
else if( pid < 0 )
{
break;
}
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 );
exit(0);
}
return 0;
}
/*./example_echosvr 127.0.0.1 10000 100 50*/