forked from liu-jianhao/springsnail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgr.cpp
313 lines (300 loc) · 8.57 KB
/
mgr.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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <exception>
#include "log.h"
#include "mgr.h"
using std::pair;
int mgr::m_epollfd = -1;
int mgr::conn2srv( const sockaddr_in& address )
{
int sockfd = socket( PF_INET, SOCK_STREAM, 0 );
if( sockfd < 0 )
{
return -1;
}
if ( connect( sockfd, ( struct sockaddr* )&address, sizeof( address ) ) != 0 )
{
log(LOG_INFO, __FILE__, __LINE__, "connect error");
close( sockfd );
return -1;
}
return sockfd;
}
mgr::mgr( int epollfd, const host& srv ) : m_logic_srv( srv )
{
m_epollfd = epollfd;
int ret = 0;
struct sockaddr_in address;
bzero( &address, sizeof( address ) );
address.sin_family = AF_INET;
inet_pton( AF_INET, srv.m_hostname, &address.sin_addr );
address.sin_port = htons( srv.m_port );
log( LOG_INFO, __FILE__, __LINE__, "logcial srv host info: (%s, %d)", srv.m_hostname, srv.m_port );
for( int i = 0; i < srv.m_conncnt; ++i )
{
sleep( 1 );
int sockfd = conn2srv( address );
if( sockfd < 0 )
{
log( LOG_ERR, __FILE__, __LINE__, "build connection %d failed", i );
}
else
{
log( LOG_INFO, __FILE__, __LINE__, "build connection %d to server success", i );
conn* tmp = NULL;
try
{
tmp = new conn;
}
catch( ... )
{
close( sockfd );
continue;
}
tmp->init_srv( sockfd, address );
m_conns.insert( pair< int, conn* >( sockfd, tmp ) );
}
}
}
mgr::~mgr()
{
}
int mgr::get_used_conn_cnt()
{
return m_used.size();
}
conn* mgr::pick_conn( int cltfd )
{
if( m_conns.empty() )
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "not enough srv connections to server" );
return NULL;
}
map< int, conn* >::iterator iter = m_conns.begin();
int srvfd = iter->first;
conn* tmp = iter->second;
if( !tmp )
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "empty server connection object" );
return NULL;
}
m_conns.erase( iter );
m_used.insert( pair< int, conn* >( cltfd, tmp ) );
m_used.insert( pair< int, conn* >( srvfd, tmp ) );
add_read_fd( m_epollfd, cltfd );
add_read_fd( m_epollfd, srvfd );
log( LOG_INFO, __FILE__, __LINE__, "bind client sock %d with server sock %d", cltfd, srvfd );
return tmp;
}
void mgr::free_conn( conn* connection )
{
int cltfd = connection->m_cltfd;
int srvfd = connection->m_srvfd;
closefd( m_epollfd, cltfd );
closefd( m_epollfd, srvfd );
m_used.erase( cltfd );
m_used.erase( srvfd );
connection->reset();
m_freed.insert( pair< int, conn* >( srvfd, connection ) );
}
void mgr::recycle_conns()
{
if( m_freed.empty() )
{
return;
}
for( map< int, conn* >::iterator iter = m_freed.begin(); iter != m_freed.end(); iter++ )
{
sleep( 1 );
int srvfd = iter->first;
conn* tmp = iter->second;
srvfd = conn2srv( tmp->m_srv_address );
if( srvfd < 0 )
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "fix connection failed");
}
else
{
log( LOG_INFO, __FILE__, __LINE__, "%s", "fix connection success" );
tmp->init_srv( srvfd, tmp->m_srv_address );
m_conns.insert( pair< int, conn* >( srvfd, tmp ) );
}
}
m_freed.clear();
}
RET_CODE mgr::process( int fd, OP_TYPE type )
{
conn* connection = m_used[ fd ];
if( !connection )
{
return NOTHING;
}
if( connection->m_cltfd == fd )
{
int srvfd = connection->m_srvfd;
switch( type )
{
case READ:
{
RET_CODE res = connection->read_clt();
switch( res )
{
case OK:
{
log( LOG_DEBUG, __FILE__, __LINE__, "content read from client: %s", connection->m_clt_buf );
}
case BUFFER_FULL:
{
modfd( m_epollfd, srvfd, EPOLLOUT );
break;
}
case IOERR:
case CLOSED:
{
free_conn( connection );
return CLOSED;
}
default:
break;
}
if( connection->m_srv_closed )
{
free_conn( connection );
return CLOSED;
}
break;
}
case WRITE:
{
RET_CODE res = connection->write_clt();
switch( res )
{
case TRY_AGAIN:
{
modfd( m_epollfd, fd, EPOLLOUT );
break;
}
case BUFFER_EMPTY:
{
modfd( m_epollfd, srvfd, EPOLLIN );
modfd( m_epollfd, fd, EPOLLIN );
break;
}
case IOERR:
case CLOSED:
{
free_conn( connection );
return CLOSED;
}
default:
break;
}
if( connection->m_srv_closed )
{
free_conn( connection );
return CLOSED;
}
break;
}
default:
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "other operation not support yet" );
break;
}
}
}
else if( connection->m_srvfd == fd )
{
int cltfd = connection->m_cltfd;
switch( type )
{
case READ:
{
RET_CODE res = connection->read_srv();
switch( res )
{
case OK:
{
log( LOG_DEBUG, __FILE__, __LINE__, "content read from server: %s", connection->m_srv_buf );
}
case BUFFER_FULL:
{
modfd( m_epollfd, cltfd, EPOLLOUT );
break;
}
case IOERR:
case CLOSED:
{
modfd( m_epollfd, cltfd, EPOLLOUT );
connection->m_srv_closed = true;
break;
}
default:
break;
}
break;
}
case WRITE:
{
RET_CODE res = connection->write_srv();
switch( res )
{
case TRY_AGAIN:
{
modfd( m_epollfd, fd, EPOLLOUT );
break;
}
case BUFFER_EMPTY:
{
modfd( m_epollfd, cltfd, EPOLLIN );
modfd( m_epollfd, fd, EPOLLIN );
break;
}
case IOERR:
case CLOSED:
{
/*
if( connection->m_srv_write_idx == connection->m_srvread_idx )
{
free_conn( connection );
}
else
{
modfd( m_epollfd, cltfd, EPOLLOUT );
}
*/
modfd( m_epollfd, cltfd, EPOLLOUT );
connection->m_srv_closed = true;
break;
}
default:
break;
}
break;
}
default:
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "other operation not support yet" );
break;
}
}
}
else
{
return NOTHING;
}
return OK;
}