29
29
#undef write
30
30
#undef read
31
31
32
- WiFiClient::WiFiClient ():sockfd(-1 ),_connected(false ),next(NULL )
32
+ class WiFiClientSocketHandle {
33
+ private:
34
+ int sockfd;
35
+
36
+ public:
37
+ WiFiClientSocketHandle (int fd):sockfd(fd)
38
+ {
39
+ }
40
+
41
+ ~WiFiClientSocketHandle ()
42
+ {
43
+ close (sockfd);
44
+ }
45
+
46
+ int fd ()
47
+ {
48
+ return sockfd;
49
+ }
50
+ };
51
+
52
+ WiFiClient::WiFiClient ():_connected(false ),next(NULL )
33
53
{
34
54
}
35
55
36
- WiFiClient::WiFiClient (int fd):sockfd(fd), _connected(true ),next(NULL )
56
+ WiFiClient::WiFiClient (int fd):_connected(true ),next(NULL )
37
57
{
58
+ clientSocketHandle.reset (new WiFiClientSocketHandle (fd));
38
59
}
39
60
40
61
WiFiClient::~WiFiClient ()
@@ -45,27 +66,25 @@ WiFiClient::~WiFiClient()
45
66
WiFiClient & WiFiClient::operator =(const WiFiClient &other)
46
67
{
47
68
stop ();
48
- sockfd = other.sockfd ;
69
+ clientSocketHandle = other.clientSocketHandle ;
49
70
_connected = other._connected ;
50
71
return *this ;
51
72
}
52
73
53
74
void WiFiClient::stop ()
54
75
{
55
- if (_connected && sockfd >= 0 ) {
56
- close (sockfd);
57
- sockfd = -1 ;
58
- _connected = false ;
59
- }
76
+ clientSocketHandle = NULL ;
77
+ _connected = false ;
60
78
}
61
79
62
80
int WiFiClient::connect (IPAddress ip, uint16_t port)
63
81
{
64
- sockfd = socket (AF_INET, SOCK_STREAM, 0 );
82
+ int sockfd = socket (AF_INET, SOCK_STREAM, 0 );
65
83
if (sockfd < 0 ) {
66
84
log_e (" socket: %d" , errno);
67
85
return 0 ;
68
86
}
87
+
69
88
uint32_t ip_addr = ip;
70
89
struct sockaddr_in serveraddr;
71
90
bzero ((char *) &serveraddr, sizeof (serveraddr));
@@ -76,9 +95,9 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
76
95
if (res < 0 ) {
77
96
log_e (" lwip_connect_r: %d" , errno);
78
97
close (sockfd);
79
- sockfd = -1 ;
80
98
return 0 ;
81
99
}
100
+ clientSocketHandle.reset (new WiFiClientSocketHandle (sockfd));
82
101
_connected = true ;
83
102
return 1 ;
84
103
}
@@ -96,7 +115,7 @@ int WiFiClient::connect(const char *host, uint16_t port)
96
115
97
116
int WiFiClient::setSocketOption (int option, char * value, size_t len)
98
117
{
99
- int res = setsockopt (sockfd , SOL_SOCKET, option, value, len);
118
+ int res = setsockopt (fd () , SOL_SOCKET, option, value, len);
100
119
if (res < 0 ) {
101
120
log_e (" %d" , errno);
102
121
}
@@ -116,7 +135,7 @@ int WiFiClient::setTimeout(uint32_t seconds)
116
135
117
136
int WiFiClient::setOption (int option, int *value)
118
137
{
119
- int res = setsockopt (sockfd , IPPROTO_TCP, option, (char *)value, sizeof (int ));
138
+ int res = setsockopt (fd () , IPPROTO_TCP, option, (char *) value, sizeof (int ));
120
139
if (res < 0 ) {
121
140
log_e (" %d" , errno);
122
141
}
@@ -126,7 +145,7 @@ int WiFiClient::setOption(int option, int *value)
126
145
int WiFiClient::getOption (int option, int *value)
127
146
{
128
147
size_t size = sizeof (int );
129
- int res = getsockopt (sockfd , IPPROTO_TCP, option, (char *)value, &size);
148
+ int res = getsockopt (fd () , IPPROTO_TCP, option, (char *)value, &size);
130
149
if (res < 0 ) {
131
150
log_e (" %d" , errno);
132
151
}
@@ -209,7 +228,7 @@ int WiFiClient::read(uint8_t *buf, size_t size)
209
228
if (!available ()) {
210
229
return -1 ;
211
230
}
212
- int res = recv (sockfd , buf, size, MSG_DONTWAIT);
231
+ int res = recv (fd () , buf, size, MSG_DONTWAIT);
213
232
if (res < 0 && errno != EWOULDBLOCK) {
214
233
log_e (" %d" , errno);
215
234
stop ();
@@ -223,7 +242,7 @@ int WiFiClient::available()
223
242
return 0 ;
224
243
}
225
244
int count;
226
- int res = ioctl (sockfd , FIONREAD, &count);
245
+ int res = ioctl (fd () , FIONREAD, &count);
227
246
if (res < 0 ) {
228
247
log_e (" %d" , errno);
229
248
stop ();
@@ -239,7 +258,7 @@ uint8_t WiFiClient::connected()
239
258
return _connected;
240
259
}
241
260
242
- IPAddress WiFiClient::remoteIP (int fd)
261
+ IPAddress WiFiClient::remoteIP (int fd) const
243
262
{
244
263
struct sockaddr_storage addr;
245
264
socklen_t len = sizeof addr;
@@ -248,7 +267,7 @@ IPAddress WiFiClient::remoteIP(int fd)
248
267
return IPAddress ((uint32_t )(s->sin_addr .s_addr ));
249
268
}
250
269
251
- uint16_t WiFiClient::remotePort (int fd)
270
+ uint16_t WiFiClient::remotePort (int fd) const
252
271
{
253
272
struct sockaddr_storage addr;
254
273
socklen_t len = sizeof addr;
@@ -257,17 +276,27 @@ uint16_t WiFiClient::remotePort(int fd)
257
276
return ntohs (s->sin_port );
258
277
}
259
278
260
- IPAddress WiFiClient::remoteIP ()
279
+ IPAddress WiFiClient::remoteIP () const
261
280
{
262
- return remoteIP (sockfd );
281
+ return remoteIP (fd () );
263
282
}
264
283
265
- uint16_t WiFiClient::remotePort ()
284
+ uint16_t WiFiClient::remotePort () const
266
285
{
267
- return remotePort (sockfd );
286
+ return remotePort (fd () );
268
287
}
269
288
270
289
bool WiFiClient::operator ==(const WiFiClient& rhs)
271
290
{
272
- return sockfd == rhs.sockfd && remotePort (sockfd) == remotePort (rhs.sockfd ) && remoteIP (sockfd) == remoteIP (rhs.sockfd );
291
+ return clientSocketHandle == rhs.clientSocketHandle && remotePort () == rhs.remotePort () && remoteIP () == rhs.remoteIP ();
292
+ }
293
+
294
+ int WiFiClient::fd () const
295
+ {
296
+ if (clientSocketHandle == NULL ) {
297
+ return -1 ;
298
+ } else {
299
+ return clientSocketHandle->fd ();
300
+ }
273
301
}
302
+
0 commit comments