-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconnector.cpp
More file actions
255 lines (219 loc) · 5.19 KB
/
Copy pathconnector.cpp
File metadata and controls
255 lines (219 loc) · 5.19 KB
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
#include "connector.h"
#include "socket_ops.h"
#include "channel.h"
#include "epoller.h"
#include <boost/bind.hpp>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "logging.h"
using namespace ydx;
const int Connector::kMaxRetryDelayMs;
Connector::Connector(EPollPoller* ep, const InetAddress& serverAddr)
: state_(kDisconnected),
serverAddr_(serverAddr),
connect_(false),
epoller_(ep)
//retryDelayMs_(kInitRetryDelayMs)
{
//LOG_DEBUG << "ctor[" << this << "]";
}
Connector::~Connector()
{
//LOG_DEBUG << "dtor[" << this << "]";
//assert(!channel_);
}
void Connector::start()
{
connect_ = true;
connect();// FIXME: unsafe
//loopConnect();
}
void Connector::connect()
{
int sockfd = sockets::create_nonblock_socket();
int ret = sockets::connect(sockfd, serverAddr_.getSockAddrInet());
int savedErrno = (ret == 0) ? 0 : errno;
switch (savedErrno)
{
case 0:
case EINPROGRESS:
case EINTR:
case EISCONN:
connecting(sockfd);
break;
case EAGAIN:
case EADDRINUSE:
case EADDRNOTAVAIL:
case ECONNREFUSED:
case ENETUNREACH:
retry(sockfd);
break;
case EACCES:
case EPERM:
case EAFNOSUPPORT:
case EALREADY:
case EBADF:
case EFAULT:
case ENOTSOCK:
//LOG_SYSERR << "connect error in Connector::startInLoop " << savedErrno;
printf("connect error in Connector::connect\n");
sockets::close(sockfd);
break;
default:
//LOG_SYSERR << "Unexpected error in Connector::startInLoop " << savedErrno;
printf("Unexpected error in Connector::connect\n");
sockets::close(sockfd);
// connectErrorCallback_();
break;
}
}
void Connector::connecting(int sockfd)
{
setState(kConnecting);
//assert(!channel_);
channel_.reset(new Channel(epoller_, sockfd));
channel_->setWriteCallback(
boost::bind(&Connector::handleWrite, this)); // FIXME: unsafe
channel_->setErrorCallback(
boost::bind(&Connector::handleError, this)); // FIXME: unsafe
//channel_->setErrorCallback(
// boost::bind(&Connector::loopHandleError, this)); // FIXME: unsafe
// channel_->tie(shared_from_this()); is not working,
// as channel_ is not managed by shared_ptr
channel_->enableWriting();
}
void Connector::stop()
{
connect_ = false;
if (state_ == kConnecting)
{
setState(kDisconnected);
int sockfd = removeAndResetChannel();
retry(sockfd);
}
}
void Connector::restart()
{
setState(kDisconnected);
connect_ = true;
connect();
//loopConnect();
}
void Connector::retry(int sockfd)
{
sockets::close(sockfd);
setState(kDisconnected);
if (connect_)
{
//LOG_INFO << "Connector::retry - Retry connecting to " << serverAddr_.toIpPort()
// << " in " << retryDelayMs_ << " milliseconds. ";
//loop_->runAfter(retryDelayMs_/1000.0,
// boost::bind(&Connector::startInLoop, shared_from_this()));
//retryDelayMs_ = std::min(retryDelayMs_ * 2, kMaxRetryDelayMs);
sleep(kMaxRetryDelayMs);
start();
}
else
{
//LOG_DEBUG << "do not connect";
}
}
void Connector::resetChannel()
{
channel_.reset();
}
int Connector::removeAndResetChannel()
{
channel_->disableAll();
channel_->remove();
int sockfd = channel_->fd();
// Can't reset channel_ here, because we are inside Channel::handleEvent
resetChannel(); // FIXME: unsafe
return sockfd;
}
void Connector::handleWrite()
{
//LOG_TRACE << "Connector::handleWrite " << state_;
if (state_ == kConnecting)
{
int sockfd = removeAndResetChannel();
int err = sockets::get_socket_error(sockfd);
if (err)
{
//LOG_WARN << "Connector::handleWrite - SO_ERROR = "
// << err << " " << strerror_tl(err);
retry(sockfd);
}
else if (sockets::isSelfConnect(sockfd))
{
//LOG_WARN << "Connector::handleWrite - Self connect";
retry(sockfd);
}
else
{
setState(kConnected);
if (connect_)
{
newConnectionCallback_(sockfd);
}
else
{
sockets::close(sockfd);
}
}
}
else
{
// what happened?
//assert(state_ == kDisconnected);
}
}
void Connector::handleError()
{
//LOG_ERROR << "Connector::handleError state=" << state_;
printf("Connector::handleError state= %d\n", state_);
if (state_ == kConnecting)
{
int sockfd = removeAndResetChannel();
int err = sockets::get_socket_error(sockfd);
//LOG_TRACE << "SO_ERROR = " << err << " " << strerror_tl(err);
printf("error no : %d\n", err);
retry(sockfd);
}
}
void Connector::loopHandleError()
{
printf("Connector::handleError state= %d\n", state_);
if (state_ == kConnecting)
{
int sockfd = removeAndResetChannel();
int err = sockets::get_socket_error(sockfd);
//LOG_TRACE << "SO_ERROR = " << err << " " << strerror_tl(err);
printf("error no : %d, %s\n", err, strerror_tl(err));
retry(sockfd);
}
}
void Connector::loopConnect()
{
// bool connected = false;
int sockfd;
for(;;)
{
sockfd = sockets::create_nonblock_socket(); //if failed prog die
int ret = sockets::connect(sockfd, serverAddr_.getSockAddrInet());
if(ret == INVALID_SOCKET)
{
printf("Connector::loopConnect connect failed..\n");
sockets::close(sockfd);
sleep(5);
}
else
{
// connected = true;
break;
}
}
connecting(sockfd);
}