Skip to content

Commit 9c9dab8

Browse files
committed
EthernetClient: using std::shared_ptr<SocketImpl>
1 parent e91fb71 commit 9c9dab8

File tree

3 files changed

+11
-19
lines changed

3 files changed

+11
-19
lines changed

ArduinoCore-Linux/cores/arduino/Ethernet.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <arpa/inet.h> // for inet_pton
2323
#include <netdb.h> // for gethostbyname, struct hostent
2424
#include <unistd.h> // for close
25+
#include <memory> // This is the include you need
2526

2627
#include "ArduinoLogger.h"
2728
#include "RingBufferExt.h"
@@ -75,13 +76,13 @@ class EthernetClient : public Client {
7576
public:
7677
EthernetClient() {
7778
setTimeout(2000);
78-
p_sock = new SocketImpl();
79+
p_sock = std::make_shared<SocketImpl>();
7980
readBuffer = RingBufferExt(bufferSize);
8081
writeBuffer = RingBufferExt(bufferSize);
8182
registerCleanup();
8283
active_clients().push_back(this);
8384
}
84-
EthernetClient(SocketImpl* sock, int bufferSize = 256, long timeout = 2000) {
85+
EthernetClient(std::shared_ptr<SocketImpl> sock, int bufferSize = 256, long timeout = 2000) {
8586
if (sock) {
8687
setTimeout(timeout);
8788
this->bufferSize = bufferSize;
@@ -97,22 +98,8 @@ class EthernetClient : public Client {
9798
setTimeout(2000);
9899
readBuffer = RingBufferExt(bufferSize);
99100
writeBuffer = RingBufferExt(bufferSize);
100-
p_sock = new SocketImpl(socket);
101+
p_sock = std::make_shared<SocketImpl>(socket);
101102
is_connected = p_sock->connected();
102-
registerCleanup();
103-
active_clients().push_back(this);
104-
}
105-
106-
virtual ~EthernetClient() {
107-
auto& clients = active_clients();
108-
auto it = std::find(clients.begin(), clients.end(), this);
109-
if (it != clients.end()) {
110-
clients.erase(it);
111-
}
112-
if (p_sock) {
113-
delete p_sock;
114-
p_sock = nullptr;
115-
}
116103
}
117104

118105
// checks if we are connected - using a timeout
@@ -264,7 +251,7 @@ class EthernetClient : public Client {
264251

265252
protected:
266253
const char* WIFICLIENT = "EthernetClient";
267-
SocketImpl* p_sock = nullptr;
254+
std::shared_ptr<SocketImpl> p_sock = nullptr;
268255
int bufferSize = 256;
269256
RingBufferExt readBuffer;
270257
RingBufferExt writeBuffer;

ArduinoCore-Linux/cores/arduino/EthernetServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class EthernetServer : public Server {
184184
Logger.error("accept failed");
185185
return result;
186186
}
187-
SocketImpl* sock_impl = new SocketImpl(client_fd, (struct sockaddr_in*)&client_addr);
187+
std::shared_ptr<SocketImpl> sock_impl = std::make_shared<SocketImpl>(client_fd, (struct sockaddr_in*)&client_addr);
188188
EthernetClient result{sock_impl};
189189
return result;
190190
}

ArduinoCore-Linux/cores/arduino/SocketImpl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class SocketImpl {
3636
is_connected = true;
3737
serv_addr = *address;
3838
};
39+
virtual ~SocketImpl() {
40+
if (sock != -1) {
41+
close();
42+
}
43+
}
3944
// checks if we are connected
4045
virtual uint8_t connected();
4146
// opens a conection

0 commit comments

Comments
 (0)