2929#undef write
3030#undef read
3131
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 )
3353{
3454}
3555
36- WiFiClient::WiFiClient (int fd):sockfd(fd), _connected(true ),next(NULL )
56+ WiFiClient::WiFiClient (int fd):_connected(true ),next(NULL )
3757{
58+ clientSocketHandle.reset (new WiFiClientSocketHandle (fd));
3859}
3960
4061WiFiClient::~WiFiClient ()
@@ -45,27 +66,25 @@ WiFiClient::~WiFiClient()
4566WiFiClient & WiFiClient::operator =(const WiFiClient &other)
4667{
4768 stop ();
48- sockfd = other.sockfd ;
69+ clientSocketHandle = other.clientSocketHandle ;
4970 _connected = other._connected ;
5071 return *this ;
5172}
5273
5374void WiFiClient::stop ()
5475{
55- if (_connected && sockfd >= 0 ) {
56- close (sockfd);
57- sockfd = -1 ;
58- _connected = false ;
59- }
76+ clientSocketHandle = NULL ;
77+ _connected = false ;
6078}
6179
6280int WiFiClient::connect (IPAddress ip, uint16_t port)
6381{
64- sockfd = socket (AF_INET, SOCK_STREAM, 0 );
82+ int sockfd = socket (AF_INET, SOCK_STREAM, 0 );
6583 if (sockfd < 0 ) {
6684 log_e (" socket: %d" , errno);
6785 return 0 ;
6886 }
87+
6988 uint32_t ip_addr = ip;
7089 struct sockaddr_in serveraddr;
7190 bzero ((char *) &serveraddr, sizeof (serveraddr));
@@ -76,9 +95,9 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
7695 if (res < 0 ) {
7796 log_e (" lwip_connect_r: %d" , errno);
7897 close (sockfd);
79- sockfd = -1 ;
8098 return 0 ;
8199 }
100+ clientSocketHandle.reset (new WiFiClientSocketHandle (sockfd));
82101 _connected = true ;
83102 return 1 ;
84103}
@@ -96,7 +115,7 @@ int WiFiClient::connect(const char *host, uint16_t port)
96115
97116int WiFiClient::setSocketOption (int option, char * value, size_t len)
98117{
99- int res = setsockopt (sockfd , SOL_SOCKET, option, value, len);
118+ int res = setsockopt (fd () , SOL_SOCKET, option, value, len);
100119 if (res < 0 ) {
101120 log_e (" %d" , errno);
102121 }
@@ -116,7 +135,7 @@ int WiFiClient::setTimeout(uint32_t seconds)
116135
117136int WiFiClient::setOption (int option, int *value)
118137{
119- int res = setsockopt (sockfd , IPPROTO_TCP, option, (char *)value, sizeof (int ));
138+ int res = setsockopt (fd () , IPPROTO_TCP, option, (char *) value, sizeof (int ));
120139 if (res < 0 ) {
121140 log_e (" %d" , errno);
122141 }
@@ -126,7 +145,7 @@ int WiFiClient::setOption(int option, int *value)
126145int WiFiClient::getOption (int option, int *value)
127146{
128147 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);
130149 if (res < 0 ) {
131150 log_e (" %d" , errno);
132151 }
@@ -209,7 +228,7 @@ int WiFiClient::read(uint8_t *buf, size_t size)
209228 if (!available ()) {
210229 return -1 ;
211230 }
212- int res = recv (sockfd , buf, size, MSG_DONTWAIT);
231+ int res = recv (fd () , buf, size, MSG_DONTWAIT);
213232 if (res < 0 && errno != EWOULDBLOCK) {
214233 log_e (" %d" , errno);
215234 stop ();
@@ -223,7 +242,7 @@ int WiFiClient::available()
223242 return 0 ;
224243 }
225244 int count;
226- int res = ioctl (sockfd , FIONREAD, &count);
245+ int res = ioctl (fd () , FIONREAD, &count);
227246 if (res < 0 ) {
228247 log_e (" %d" , errno);
229248 stop ();
@@ -239,7 +258,7 @@ uint8_t WiFiClient::connected()
239258 return _connected;
240259}
241260
242- IPAddress WiFiClient::remoteIP (int fd)
261+ IPAddress WiFiClient::remoteIP (int fd) const
243262{
244263 struct sockaddr_storage addr;
245264 socklen_t len = sizeof addr;
@@ -248,7 +267,7 @@ IPAddress WiFiClient::remoteIP(int fd)
248267 return IPAddress ((uint32_t )(s->sin_addr .s_addr ));
249268}
250269
251- uint16_t WiFiClient::remotePort (int fd)
270+ uint16_t WiFiClient::remotePort (int fd) const
252271{
253272 struct sockaddr_storage addr;
254273 socklen_t len = sizeof addr;
@@ -257,17 +276,27 @@ uint16_t WiFiClient::remotePort(int fd)
257276 return ntohs (s->sin_port );
258277}
259278
260- IPAddress WiFiClient::remoteIP ()
279+ IPAddress WiFiClient::remoteIP () const
261280{
262- return remoteIP (sockfd );
281+ return remoteIP (fd () );
263282}
264283
265- uint16_t WiFiClient::remotePort ()
284+ uint16_t WiFiClient::remotePort () const
266285{
267- return remotePort (sockfd );
286+ return remotePort (fd () );
268287}
269288
270289bool WiFiClient::operator ==(const WiFiClient& rhs)
271290{
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+ }
273301}
302+
0 commit comments