Skip to content

Commit f0fc28f

Browse files
schrodme-no-dev
authored andcommitted
Rework WiFiClient (#238)
* Rework WiFiClient Rework WiFiClient to correct error where making a copy of a WiFiClient object resulted in the socket being closed prematurely. Added loop and select to write to handle/prevent EAGAIN errors. * Rework WiFiClient to use shared_ptr Rework changes to utilize shared_ptr rather than manually maintaining reference count. Revert changes to write * Incorporate comments from review Move WiFiClientSocketHandle and fd() into WiFiClient.cpp
1 parent 770830a commit f0fc28f

File tree

2 files changed

+61
-31
lines changed

2 files changed

+61
-31
lines changed

Diff for: libraries/WiFi/src/WiFiClient.cpp

+51-22
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,33 @@
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

4061
WiFiClient::~WiFiClient()
@@ -45,27 +66,25 @@ WiFiClient::~WiFiClient()
4566
WiFiClient & 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

5374
void 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

6280
int 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

97116
int 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

117136
int 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)
126145
int 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

270289
bool 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+

Diff for: libraries/WiFi/src/WiFiClient.h

+10-9
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323

2424
#include "Arduino.h"
2525
#include "Client.h"
26+
#include <memory>
27+
28+
class WiFiClientSocketHandle;
2629

2730
class WiFiClient : public Client
2831
{
2932
protected:
30-
int sockfd;
33+
std::shared_ptr<WiFiClientSocketHandle> clientSocketHandle;
3134
bool _connected;
3235

3336
public:
@@ -69,21 +72,19 @@ class WiFiClient : public Client
6972
return !this->operator==(rhs);
7073
};
7174

72-
int fd()
73-
{
74-
return sockfd;
75-
}
76-
IPAddress remoteIP();
77-
uint16_t remotePort();
75+
int fd() const;
76+
7877
int setSocketOption(int option, char* value, size_t len);
7978
int setOption(int option, int *value);
8079
int getOption(int option, int *value);
8180
int setTimeout(uint32_t seconds);
8281
int setNoDelay(bool nodelay);
8382
bool getNoDelay();
8483

85-
IPAddress remoteIP(int fd);
86-
uint16_t remotePort(int fd);
84+
IPAddress remoteIP() const;
85+
IPAddress remoteIP(int fd) const;
86+
uint16_t remotePort() const;
87+
uint16_t remotePort(int fd) const;
8788

8889
//friend class WiFiServer;
8990
using Print::write;

0 commit comments

Comments
 (0)