-
Notifications
You must be signed in to change notification settings - Fork 11
/
epoll_client.cpp
360 lines (322 loc) · 10.8 KB
/
epoll_client.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#ifndef _DEFINE_EPOLLCLIENT_H_
#define _DEFINE_EPOLLCLIENT_H_
#define _MAX_SOCKFD_COUNT 65535
#include<iostream>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <string>
using namespace std;
/**
* @brief
*/
typedef enum _EPOLL_USER_STATUS_EM
{
FREE = 0,
CONNECT_OK = 1,//
SEND_OK = 2,//
RECV_OK = 3,//
}EPOLL_USER_STATUS_EM;
/*@brief
*@CEpollClient class
*/
struct UserStatus
{
EPOLL_USER_STATUS_EM iUserStatus;//
int iSockFd;//socketfd
char cSendbuff[1024];//
int iBuffLen;//
unsigned int uEpollEvents;//Epoll events
};
class CEpollClient
{
public:
/**
* @brief
* :CEpollClient
* :
* @param [in] iUserCount
* @param [in] pIP IP
* @param [in] iPort
* @return
*/
CEpollClient(int iUserCount, const char* pIP, int iPort);
/**
* @brief
* :CEpollClient
* :
* @return
*/
~CEpollClient();
/**
* @brief
* :RunFun
* :epoll
* @return
*/
int RunFun();
private:
/**
* @brief
* :ConnectToServer
* :
* @param [in] iUserId ID
* @param [in] pServerIp IP
* @param [in] uServerPort
* @return socketfd,socketfd-1
*/
int ConnectToServer(int iUserId,const char *pServerIp,unsigned short uServerPort);
/**
* @brief
* :SendToServerData
* :(iUserId)
* @param [in] iUserId ID
* @return
*/
int SendToServerData(int iUserId);
/**
* @brief
* :RecvFromServer
* :
* @param [in] iUserId ID
* @param [in] pRecvBuff
* @param [in] iBuffLen
* @return -1
*/
int RecvFromServer(int iUserid,char *pRecvBuff,int iBuffLen);
/**
* @brief
* :CloseUser
* :
* @param [in] iUserId ID
* @return true
*/
bool CloseUser(int iUserId);
/**
* @brief
* :DelEpoll
* :epoll
* @param [in] iSockFd socket FD
* @return true
*/
bool DelEpoll(int iSockFd);
private:
int m_iUserCount;//
struct UserStatus *m_pAllUserStatus;//
int m_iEpollFd;//epollfd
int m_iSockFd_UserId[_MAX_SOCKFD_COUNT];//IDsocketid
int m_iPort;//
char m_ip[100];//IP
};
#endif
CEpollClient::CEpollClient(int iUserCount, const char* pIP, int iPort)
{
strcpy(m_ip, pIP);
m_iPort = iPort;
m_iUserCount = iUserCount;
m_iEpollFd = epoll_create(_MAX_SOCKFD_COUNT);
m_pAllUserStatus = (struct UserStatus*)malloc(iUserCount*sizeof(struct UserStatus));
for(int iuserid=0; iuserid<iUserCount ; iuserid++)
{
m_pAllUserStatus[iuserid].iUserStatus = FREE;
sprintf(m_pAllUserStatus[iuserid].cSendbuff, "Client: %d send message \"Hello Server!\"\r\n", iuserid);
m_pAllUserStatus[iuserid].iBuffLen = strlen(m_pAllUserStatus[iuserid].cSendbuff) + 1;
m_pAllUserStatus[iuserid].iSockFd = -1;
}
memset(m_iSockFd_UserId, 0xFF, sizeof(m_iSockFd_UserId));
}
CEpollClient::~CEpollClient()
{
free(m_pAllUserStatus);
}
int CEpollClient::ConnectToServer(int iUserId,const char *pServerIp,unsigned short uServerPort)
{
if( (m_pAllUserStatus[iUserId].iSockFd = socket(AF_INET,SOCK_STREAM,0) ) < 0 )
{
cout <<"[CEpollClient error]: init socket fail, reason is:"<<strerror(errno)<<",errno is:"<<errno<<endl;
m_pAllUserStatus[iUserId].iSockFd = -1;
return m_pAllUserStatus[iUserId].iSockFd;
}
struct sockaddr_in addr;
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(uServerPort);
addr.sin_addr.s_addr = inet_addr(pServerIp);
int ireuseadd_on = 1;//
setsockopt(m_pAllUserStatus[iUserId].iSockFd, SOL_SOCKET, SO_REUSEADDR, &ireuseadd_on, sizeof(ireuseadd_on));
unsigned long ul = 1;
ioctl(m_pAllUserStatus[iUserId].iSockFd, FIONBIO, &ul); //
connect(m_pAllUserStatus[iUserId].iSockFd, (const sockaddr*)&addr, sizeof(addr));
m_pAllUserStatus[iUserId].iUserStatus = CONNECT_OK;
m_pAllUserStatus[iUserId].iSockFd = m_pAllUserStatus[iUserId].iSockFd;
return m_pAllUserStatus[iUserId].iSockFd;
}
int CEpollClient::SendToServerData(int iUserId)
{
//sleep(1);//
int isendsize = -1;
if( CONNECT_OK == m_pAllUserStatus[iUserId].iUserStatus || RECV_OK == m_pAllUserStatus[iUserId].iUserStatus)
{
isendsize = send(m_pAllUserStatus[iUserId].iSockFd, m_pAllUserStatus[iUserId].cSendbuff, m_pAllUserStatus[iUserId
].iBuffLen, MSG_NOSIGNAL);
if(isendsize < 0)
{
cout <<"[CEpollClient error]: SendToServerData, send fail, reason is:"<<strerror(errno)<<",errno is:"<<errno<<endl;
}
else
{
printf("[CEpollClient info]: iUserId: %d Send Msg Content:%s\n", iUserId, m_pAllUserStatus[iUserId].cSendbuff
);
m_pAllUserStatus[iUserId].iUserStatus = SEND_OK;
}
}
return isendsize;
}
int CEpollClient::RecvFromServer(int iUserId,char *pRecvBuff,int iBuffLen)
{
int irecvsize = -1;
if(SEND_OK == m_pAllUserStatus[iUserId].iUserStatus)
{
irecvsize = recv(m_pAllUserStatus[iUserId].iSockFd, pRecvBuff, iBuffLen, 0);
if(0 > irecvsize)
{
cout <<"[CEpollClient error]: iUserId: " << iUserId << "RecvFromServer, recv fail, reason is:"<<strerror(errno)<<",errno is:"<<errno<<endl;
}
else if(0 == irecvsize)
{
cout <<"[warning:] iUserId: "<< iUserId << "RecvFromServer, STB0,irecvsize:"<<irecvsize<<",iSockFd:"<< m_pAllUserStatus[iUserId].iSockFd << endl;
}
else
{
printf("Recv Server Msg Content:%s\n", pRecvBuff);
m_pAllUserStatus[iUserId].iUserStatus = RECV_OK;
}
}
return irecvsize;
}
bool CEpollClient::CloseUser(int iUserId)
{
close(m_pAllUserStatus[iUserId].iSockFd);
m_pAllUserStatus[iUserId].iUserStatus = FREE;
m_pAllUserStatus[iUserId].iSockFd = -1;
return true;
}
int CEpollClient::RunFun()
{
int isocketfd = -1;
for(int iuserid=0; iuserid<m_iUserCount; iuserid++)
{
struct epoll_event event;
isocketfd = ConnectToServer(iuserid, m_ip, m_iPort);
if(isocketfd < 0)
cout <<"[CEpollClient error]: RunFun, connect fail" <<endl;
m_iSockFd_UserId[isocketfd] = iuserid;//IDsocketid
event.data.fd = isocketfd;
event.events = EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP;
m_pAllUserStatus[iuserid].uEpollEvents = event.events;
epoll_ctl(m_iEpollFd, EPOLL_CTL_ADD, event.data.fd, &event);
}
while(1)
{
struct epoll_event events[_MAX_SOCKFD_COUNT];
char buffer[1024];
memset(buffer,0,1024);
int nfds = epoll_wait(m_iEpollFd, events, _MAX_SOCKFD_COUNT, 100 );//epoll
for (int ifd=0; ifd<nfds; ifd++)//
{
struct epoll_event event_nfds;
int iclientsockfd = events[ifd].data.fd;
cout << "events[ifd].data.fd: " << events[ifd].data.fd << endl;
int iuserid = m_iSockFd_UserId[iclientsockfd];//socketfdID
if( events[ifd].events & EPOLLOUT )
{
int iret = SendToServerData(iuserid);
if( 0 < iret )
{
event_nfds.events = EPOLLIN|EPOLLERR|EPOLLHUP;
event_nfds.data.fd = iclientsockfd;
epoll_ctl(m_iEpollFd, EPOLL_CTL_MOD, event_nfds.data.fd, &event_nfds);
}
else
{
cout <<"[CEpollClient error:] EpollWait, SendToServerData fail, send iret:"<<iret<<",iuserid:"<<iuserid<<",fd:"<<events[ifd].data.fd<<endl;
DelEpoll(events[ifd].data.fd);
CloseUser(iuserid);
}
}
else if( events[ifd].events & EPOLLIN )//
{
int ilen = RecvFromServer(iuserid, buffer, 1024);
if(0 > ilen)
{
cout <<"[CEpollClient error]: RunFun, recv fail, reason is:"<<strerror(errno)<<",errno is:"<<errno<<endl;
DelEpoll(events[ifd].data.fd);
CloseUser(iuserid);
}
else if(0 == ilen)
{
cout <<"[CEpollClient warning:] server disconnect,ilen:"<<ilen<<",iuserid:"<<iuserid<<",fd:"<<events[ifd].data.fd<<endl;
DelEpoll(events[ifd].data.fd);
CloseUser(iuserid);
}
else
{
m_iSockFd_UserId[iclientsockfd] = iuserid;//socketfdID
event_nfds.data.fd = iclientsockfd;
event_nfds.events = EPOLLOUT|EPOLLERR|EPOLLHUP;
epoll_ctl(m_iEpollFd, EPOLL_CTL_MOD, event_nfds.data.fd, &event_nfds);
}
}
else
{
cout <<"[CEpollClient error:] other epoll error"<<endl;
DelEpoll(events[ifd].data.fd);
CloseUser(iuserid);
}
}
}
}
bool CEpollClient::DelEpoll(int iSockFd)
{
bool bret = false;
struct epoll_event event_del;
if(0 < iSockFd)
{
event_del.data.fd = iSockFd;
event_del.events = 0;
if( 0 == epoll_ctl(m_iEpollFd, EPOLL_CTL_DEL, event_del.data.fd, &event_del) )
{
bret = true;
}
else
{
cout <<"[SimulateStb error:] DelEpoll,epoll_ctl error,iSockFd:"<<iSockFd<<endl;
}
m_iSockFd_UserId[iSockFd] = -1;
}
else
{
bret = true;
}
return bret;
}
int main(int argc, char *argv[])
{
CEpollClient *pCEpollClient = new CEpollClient(2, "127.0.0.1", 4444);
if(NULL == pCEpollClient)
{
cout<<"[epollclient error]:main init"<<"Init CEpollClient fail"<<endl;
}
pCEpollClient->RunFun();
if(NULL != pCEpollClient)
{
delete pCEpollClient;
pCEpollClient = NULL;
}
return 0;
}