Skip to content

Commit 0ed8ea7

Browse files
committed
SocketWrapper - copyable networking clients
1 parent 7b95100 commit 0ed8ea7

File tree

12 files changed

+254
-47
lines changed

12 files changed

+254
-47
lines changed

libraries/Ethernet/src/EthernetClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
#define ethernetclient_h
2222

2323
#include "Ethernet.h"
24-
#include "MbedClient.h"
24+
#include "AClient.h"
2525

2626
namespace arduino {
2727

28-
class EthernetClient : public MbedClient {
28+
class EthernetClient : public AClient {
2929
NetworkInterface *getNetwork() {
3030
return Ethernet.getNetwork();
3131
}

libraries/Ethernet/src/EthernetSSLClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
#define ETHERNETSSLCLIENT_H
2222

2323
#include "EthernetClient.h"
24-
#include "MbedSSLClient.h"
24+
#include "AClient.h"
2525

2626
extern const char CA_CERTIFICATES[];
2727

2828
namespace arduino {
2929

30-
class EthernetSSLClient : public arduino::MbedSSLClient {
30+
class EthernetSSLClient : public arduino::ASslClient {
3131
NetworkInterface *getNetwork() {
3232
return Ethernet.getNetwork();
3333
}

libraries/GSM/src/GSMClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
#include "GSMClient.h"
2121

22-
arduino::GSMClient::GSMClient(): MbedClient(100) {
22+
arduino::GSMClient::GSMClient(): AClient(100) {
2323

2424
}

libraries/GSM/src/GSMClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
#define gsmclient_h
2222

2323
#include "GSM.h"
24-
#include "MbedClient.h"
24+
#include "AClient.h"
2525

2626
namespace arduino {
2727

28-
class GSMClient : public MbedClient {
28+
class GSMClient : public AClient {
2929
public:
3030
GSMClient();
3131

libraries/GSM/src/GSMSSLClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
#include "GSMSSLClient.h"
2121

22-
arduino::GSMSSLClient::GSMSSLClient(): MbedSSLClient(100) {
22+
arduino::GSMSSLClient::GSMSSLClient(): ASslClient(100) {
2323

2424
}

libraries/GSM/src/GSMSSLClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
#define GSMSSLCLIENT_H
2222

2323
#include "GSM.h"
24-
#include "MbedSSLClient.h"
24+
#include "AClient.h"
2525

2626
extern const char CA_CERTIFICATES[];
2727

2828
namespace arduino {
2929

30-
class GSMSSLClient : public arduino::MbedSSLClient {
30+
class GSMSSLClient : public arduino::ASslClient {
3131
public:
3232
GSMSSLClient();
3333

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
2+
#include "AClient.h"
3+
#include "MbedSSLClient.h"
4+
5+
AClient::AClient(unsigned long timeout) {
6+
setSocketTimeout(timeout);
7+
}
8+
9+
void arduino::AClient::newMbedClient() {
10+
client.reset(new MbedClient());
11+
client->setNetwork(getNetwork());
12+
}
13+
14+
arduino::AClient::operator bool() {
15+
return client && *client;
16+
}
17+
18+
void arduino::AClient::setSocket(Socket *sock) {
19+
if (!client) {
20+
newMbedClient();
21+
}
22+
client->setSocket(sock);
23+
}
24+
25+
void arduino::AClient::setSocketTimeout(unsigned long timeout) {
26+
if (!client) {
27+
newMbedClient();
28+
}
29+
client->setTimeout(timeout);
30+
}
31+
32+
int arduino::AClient::connect(IPAddress ip, uint16_t port) {
33+
if (!client) {
34+
newMbedClient();
35+
}
36+
return client->connect(ip, port);
37+
}
38+
39+
int arduino::AClient::connect(const char *host, uint16_t port) {
40+
if (!client) {
41+
newMbedClient();
42+
}
43+
return client->connect(host, port);
44+
}
45+
46+
int arduino::AClient::connectSSL(IPAddress ip, uint16_t port) {
47+
if (!client) {
48+
newMbedClient();
49+
}
50+
return client->connectSSL(ip, port);
51+
}
52+
53+
int arduino::AClient::connectSSL(const char *host, uint16_t port, bool disableSNI) {
54+
if (!client) {
55+
newMbedClient();
56+
}
57+
return client->connectSSL(host, port, disableSNI);
58+
}
59+
60+
void arduino::AClient::stop() {
61+
if (!client)
62+
return;
63+
client->stop();
64+
}
65+
66+
uint8_t arduino::AClient::connected() {
67+
if (!client)
68+
return false;
69+
return client->connected();
70+
}
71+
72+
IPAddress arduino::AClient::remoteIP() {
73+
if (!client)
74+
return INADDR_NONE;
75+
return client->remoteIP();
76+
}
77+
78+
uint16_t arduino::AClient::remotePort() {
79+
if (!client)
80+
return 0;
81+
return client->remotePort();
82+
}
83+
84+
size_t arduino::AClient::write(uint8_t b) {
85+
if (!client)
86+
return 0;
87+
return client->write(b);
88+
}
89+
90+
size_t arduino::AClient::write(const uint8_t *buf, size_t size) {
91+
if (!client)
92+
return 0;
93+
return client->write(buf, size);
94+
}
95+
96+
void arduino::AClient::flush() {
97+
if (!client)
98+
return;
99+
client->flush();
100+
}
101+
102+
int arduino::AClient::available() {
103+
if (!client)
104+
return 0;
105+
return client->available();
106+
}
107+
108+
int arduino::AClient::read() {
109+
if (!client)
110+
return -1;
111+
return client->read();
112+
}
113+
114+
int arduino::AClient::read(uint8_t *buf, size_t size) {
115+
if (!client)
116+
return 0;
117+
return client->read(buf, size);
118+
}
119+
120+
int arduino::AClient::peek() {
121+
if (!client)
122+
return -1;
123+
return client->peek();
124+
}
125+
126+
void arduino::ASslClient::newMbedClient() {
127+
client.reset(new MbedSSLClient());
128+
client->setNetwork(getNetwork());
129+
}
130+
131+
void arduino::ASslClient::disableSNI(bool statusSNI) {
132+
if (!client) {
133+
newMbedClient();
134+
}
135+
static_cast<MbedSSLClient*>(client.get())->disableSNI(statusSNI);
136+
}
137+
138+
void arduino::ASslClient::appendCustomCACert(const char* ca_cert) {
139+
if (!client) {
140+
newMbedClient();
141+
}
142+
static_cast<MbedSSLClient*>(client.get())->appendCustomCACert(ca_cert);
143+
}

libraries/SocketWrapper/src/AClient.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
AClient.h - Copyable Client implementation for Mbed Core
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef MBEDACLIENT_H
20+
#define MBEDACLIENT_H
21+
22+
#include <Arduino.h>
23+
#include "MbedClient.h"
24+
25+
namespace arduino {
26+
27+
class AClient : public Client {
28+
public:
29+
30+
AClient() {}
31+
AClient(unsigned long timeout);
32+
33+
virtual int connect(IPAddress ip, uint16_t port);
34+
virtual int connect(const char *host, uint16_t port);
35+
int connectSSL(IPAddress ip, uint16_t port);
36+
int connectSSL(const char* host, uint16_t port, bool disableSNI = false);
37+
virtual void stop();
38+
39+
virtual explicit operator bool();
40+
virtual uint8_t connected();
41+
uint8_t status();
42+
43+
IPAddress remoteIP();
44+
uint16_t remotePort();
45+
46+
virtual size_t write(uint8_t);
47+
virtual size_t write(const uint8_t *buf, size_t size);
48+
virtual void flush();
49+
50+
virtual int available();
51+
virtual int read();
52+
virtual int read(uint8_t *buf, size_t size);
53+
virtual int peek();
54+
55+
void setSocketTimeout(unsigned long timeout);
56+
57+
protected:
58+
friend class EthernetServer;
59+
friend class WiFiServer;
60+
61+
std::shared_ptr<MbedClient> client;
62+
virtual NetworkInterface* getNetwork() = 0;
63+
virtual void newMbedClient();
64+
void setSocket(Socket* sock);
65+
66+
};
67+
68+
class ASslClient : public AClient {
69+
public:
70+
71+
ASslClient() {}
72+
ASslClient(unsigned long timeout) : AClient(timeout) {}
73+
74+
void disableSNI(bool statusSNI);
75+
76+
void appendCustomCACert(const char* ca_cert);
77+
78+
protected:
79+
virtual void newMbedClient();
80+
};
81+
82+
}
83+
#endif

libraries/SocketWrapper/src/MbedClient.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void arduino::MbedClient::readSocket() {
2424
continue;
2525
}
2626
mutex->lock();
27-
if (sock == nullptr || (closing && borrowed_socket)) {
27+
if (sock == nullptr) {
2828
goto cleanup;
2929
}
3030
ret = sock->recv(data, rxBuffer.availableForStore());
@@ -270,15 +270,14 @@ void arduino::MbedClient::stop() {
270270
if (mutex != nullptr) {
271271
mutex->lock();
272272
}
273-
if (sock != nullptr && borrowed_socket == false) {
273+
if (sock != nullptr) {
274274
if (_own_socket) {
275275
delete sock;
276276
} else {
277277
sock->close();
278278
}
279279
sock = nullptr;
280280
}
281-
closing = true;
282281
if (mutex != nullptr) {
283282
mutex->unlock();
284283
}

0 commit comments

Comments
 (0)