@@ -27,6 +27,18 @@ Connector::Connector(EventLoop *loop, InetAddress &&addr, bool retry)
27
27
{
28
28
}
29
29
30
+ Connector::~Connector ()
31
+ {
32
+ if (socketHanded_ == false && fd_ != -1 )
33
+ {
34
+ #ifndef _WIN32
35
+ ::close (fd_);
36
+ #else
37
+ closesocket (fd_);
38
+ #endif
39
+ }
40
+ }
41
+
30
42
void Connector::start ()
31
43
{
32
44
connect_ = true ;
@@ -65,9 +77,10 @@ void Connector::startInLoop()
65
77
}
66
78
void Connector::connect ()
67
79
{
68
- int sockfd = Socket::createNonblockingSocketOrDie (serverAddr_.family ());
80
+ socketHanded_ = false ;
81
+ fd_ = Socket::createNonblockingSocketOrDie (serverAddr_.family ());
69
82
errno = 0 ;
70
- int ret = Socket::connect (sockfd , serverAddr_);
83
+ int ret = Socket::connect (fd_ , serverAddr_);
71
84
int savedErrno = (ret == 0 ) ? 0 : errno;
72
85
switch (savedErrno)
73
86
{
@@ -76,7 +89,7 @@ void Connector::connect()
76
89
case EINTR:
77
90
case EISCONN:
78
91
LOG_TRACE << " connecting" ;
79
- connecting (sockfd );
92
+ connecting (fd_ );
80
93
break ;
81
94
82
95
case EAGAIN:
@@ -86,7 +99,7 @@ void Connector::connect()
86
99
case ENETUNREACH:
87
100
if (retry_)
88
101
{
89
- retry (sockfd );
102
+ retry (fd_ );
90
103
}
91
104
break ;
92
105
@@ -99,10 +112,11 @@ void Connector::connect()
99
112
case ENOTSOCK:
100
113
LOG_SYSERR << " connect error in Connector::startInLoop "
101
114
<< savedErrno;
115
+ socketHanded_ = true ;
102
116
#ifndef _WIN32
103
- ::close (sockfd );
117
+ ::close (fd_ );
104
118
#else
105
- closesocket (sockfd );
119
+ closesocket (fd_ );
106
120
#endif
107
121
if (errorCallback_)
108
122
errorCallback_ ();
@@ -111,10 +125,11 @@ void Connector::connect()
111
125
default :
112
126
LOG_SYSERR << " Unexpected error in Connector::startInLoop "
113
127
<< savedErrno;
128
+ socketHanded_ = true ;
114
129
#ifndef _WIN32
115
- ::close (sockfd );
130
+ ::close (fd_ );
116
131
#else
117
- closesocket (sockfd );
132
+ closesocket (fd_ );
118
133
#endif
119
134
if (errorCallback_)
120
135
errorCallback_ ();
@@ -154,6 +169,7 @@ int Connector::removeAndResetChannel()
154
169
155
170
void Connector::handleWrite ()
156
171
{
172
+ socketHanded_ = true ;
157
173
if (status_ == Status::Connecting)
158
174
{
159
175
int sockfd = removeAndResetChannel ();
@@ -168,6 +184,7 @@ void Connector::handleWrite()
168
184
}
169
185
else
170
186
{
187
+ socketHanded_ = true ;
171
188
#ifndef _WIN32
172
189
::close (sockfd);
173
190
#else
@@ -188,6 +205,7 @@ void Connector::handleWrite()
188
205
}
189
206
else
190
207
{
208
+ socketHanded_ = true ;
191
209
#ifndef _WIN32
192
210
::close (sockfd);
193
211
#else
@@ -208,6 +226,7 @@ void Connector::handleWrite()
208
226
}
209
227
else
210
228
{
229
+ socketHanded_ = true ;
211
230
#ifndef _WIN32
212
231
::close (sockfd);
213
232
#else
@@ -225,6 +244,7 @@ void Connector::handleWrite()
225
244
226
245
void Connector::handleError ()
227
246
{
247
+ socketHanded_ = true ;
228
248
if (status_ == Status::Connecting)
229
249
{
230
250
status_ = Status::Disconnected;
0 commit comments