Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0ed8ea7

Browse files
committed
SocketWrapper - copyable networking clients
1 parent7b95100 commit0ed8ea7

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
#defineethernetclient_h
2222

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

2626
namespacearduino {
2727

28-
classEthernetClient :publicMbedClient {
28+
classEthernetClient :publicAClient {
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
#defineETHERNETSSLCLIENT_H
2222

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

2626
externconstchar CA_CERTIFICATES[];
2727

2828
namespacearduino {
2929

30-
classEthernetSSLClient :publicarduino::MbedSSLClient {
30+
classEthernetSSLClient :publicarduino::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
#definegsmclient_h
2222

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

2626
namespacearduino {
2727

28-
classGSMClient :publicMbedClient {
28+
classGSMClient :publicAClient {
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
#defineGSMSSLCLIENT_H
2222

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

2626
externconstchar CA_CERTIFICATES[];
2727

2828
namespacearduino {
2929

30-
classGSMSSLClient :publicarduino::MbedSSLClient {
30+
classGSMSSLClient :publicarduino::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(unsignedlong timeout) {
6+
setSocketTimeout(timeout);
7+
}
8+
9+
voidarduino::AClient::newMbedClient() {
10+
client.reset(newMbedClient());
11+
client->setNetwork(getNetwork());
12+
}
13+
14+
arduino::AClient::operatorbool() {
15+
return client && *client;
16+
}
17+
18+
voidarduino::AClient::setSocket(Socket *sock) {
19+
if (!client) {
20+
newMbedClient();
21+
}
22+
client->setSocket(sock);
23+
}
24+
25+
voidarduino::AClient::setSocketTimeout(unsignedlong timeout) {
26+
if (!client) {
27+
newMbedClient();
28+
}
29+
client->setTimeout(timeout);
30+
}
31+
32+
intarduino::AClient::connect(IPAddress ip,uint16_t port) {
33+
if (!client) {
34+
newMbedClient();
35+
}
36+
return client->connect(ip, port);
37+
}
38+
39+
intarduino::AClient::connect(constchar *host,uint16_t port) {
40+
if (!client) {
41+
newMbedClient();
42+
}
43+
return client->connect(host, port);
44+
}
45+
46+
intarduino::AClient::connectSSL(IPAddress ip,uint16_t port) {
47+
if (!client) {
48+
newMbedClient();
49+
}
50+
return client->connectSSL(ip, port);
51+
}
52+
53+
intarduino::AClient::connectSSL(constchar *host,uint16_t port,bool disableSNI) {
54+
if (!client) {
55+
newMbedClient();
56+
}
57+
return client->connectSSL(host, port, disableSNI);
58+
}
59+
60+
voidarduino::AClient::stop() {
61+
if (!client)
62+
return;
63+
client->stop();
64+
}
65+
66+
uint8_tarduino::AClient::connected() {
67+
if (!client)
68+
returnfalse;
69+
return client->connected();
70+
}
71+
72+
IPAddressarduino::AClient::remoteIP() {
73+
if (!client)
74+
return INADDR_NONE;
75+
return client->remoteIP();
76+
}
77+
78+
uint16_tarduino::AClient::remotePort() {
79+
if (!client)
80+
return0;
81+
return client->remotePort();
82+
}
83+
84+
size_tarduino::AClient::write(uint8_t b) {
85+
if (!client)
86+
return0;
87+
return client->write(b);
88+
}
89+
90+
size_tarduino::AClient::write(constuint8_t *buf,size_t size) {
91+
if (!client)
92+
return0;
93+
return client->write(buf, size);
94+
}
95+
96+
voidarduino::AClient::flush() {
97+
if (!client)
98+
return;
99+
client->flush();
100+
}
101+
102+
intarduino::AClient::available() {
103+
if (!client)
104+
return0;
105+
return client->available();
106+
}
107+
108+
intarduino::AClient::read() {
109+
if (!client)
110+
return -1;
111+
return client->read();
112+
}
113+
114+
intarduino::AClient::read(uint8_t *buf,size_t size) {
115+
if (!client)
116+
return0;
117+
return client->read(buf, size);
118+
}
119+
120+
intarduino::AClient::peek() {
121+
if (!client)
122+
return -1;
123+
return client->peek();
124+
}
125+
126+
voidarduino::ASslClient::newMbedClient() {
127+
client.reset(newMbedSSLClient());
128+
client->setNetwork(getNetwork());
129+
}
130+
131+
voidarduino::ASslClient::disableSNI(bool statusSNI) {
132+
if (!client) {
133+
newMbedClient();
134+
}
135+
static_cast<MbedSSLClient*>(client.get())->disableSNI(statusSNI);
136+
}
137+
138+
voidarduino::ASslClient::appendCustomCACert(constchar* ca_cert) {
139+
if (!client) {
140+
newMbedClient();
141+
}
142+
static_cast<MbedSSLClient*>(client.get())->appendCustomCACert(ca_cert);
143+
}
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+
#defineMBEDACLIENT_H
21+
22+
#include<Arduino.h>
23+
#include"MbedClient.h"
24+
25+
namespacearduino {
26+
27+
classAClient :publicClient {
28+
public:
29+
30+
AClient() {}
31+
AClient(unsignedlong timeout);
32+
33+
virtualintconnect(IPAddress ip,uint16_t port);
34+
virtualintconnect(constchar *host,uint16_t port);
35+
intconnectSSL(IPAddress ip,uint16_t port);
36+
intconnectSSL(constchar* host,uint16_t port,bool disableSNI =false);
37+
virtualvoidstop();
38+
39+
virtualexplicitoperatorbool();
40+
virtualuint8_tconnected();
41+
uint8_tstatus();
42+
43+
IPAddressremoteIP();
44+
uint16_tremotePort();
45+
46+
virtualsize_twrite(uint8_t);
47+
virtualsize_twrite(constuint8_t *buf,size_t size);
48+
virtualvoidflush();
49+
50+
virtualintavailable();
51+
virtualintread();
52+
virtualintread(uint8_t *buf,size_t size);
53+
virtualintpeek();
54+
55+
voidsetSocketTimeout(unsignedlong timeout);
56+
57+
protected:
58+
friendclassEthernetServer;
59+
friendclassWiFiServer;
60+
61+
std::shared_ptr<MbedClient> client;
62+
virtual NetworkInterface*getNetwork() = 0;
63+
virtualvoidnewMbedClient();
64+
voidsetSocket(Socket* sock);
65+
66+
};
67+
68+
classASslClient :publicAClient {
69+
public:
70+
71+
ASslClient() {}
72+
ASslClient(unsignedlong timeout) : AClient(timeout) {}
73+
74+
voiddisableSNI(bool statusSNI);
75+
76+
voidappendCustomCACert(constchar* ca_cert);
77+
78+
protected:
79+
virtualvoidnewMbedClient();
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)

[8]ページ先頭

©2009-2025 Movatter.jp