-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SocketWrapper - copyable networking clients
- Loading branch information
Showing
14 changed files
with
284 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
|
||
#include "AClient.h" | ||
#include "MbedSSLClient.h" | ||
|
||
AClient::AClient(unsigned long timeout) { | ||
setSocketTimeout(timeout); | ||
} | ||
|
||
void arduino::AClient::newMbedClient() { | ||
client.reset(new MbedClient()); | ||
client->setNetwork(getNetwork()); | ||
} | ||
|
||
arduino::AClient::operator bool() { | ||
return client && *client; | ||
} | ||
|
||
void arduino::AClient::setSocket(Socket *sock) { | ||
if (!client) { | ||
newMbedClient(); | ||
} | ||
client->setSocket(sock); | ||
} | ||
|
||
void arduino::AClient::setSocketTimeout(unsigned long timeout) { | ||
if (!client) { | ||
newMbedClient(); | ||
} | ||
client->setTimeout(timeout); | ||
} | ||
|
||
int arduino::AClient::connect(IPAddress ip, uint16_t port) { | ||
if (!client) { | ||
newMbedClient(); | ||
} | ||
return client->connect(ip, port); | ||
} | ||
|
||
int arduino::AClient::connect(const char *host, uint16_t port) { | ||
if (!client) { | ||
newMbedClient(); | ||
} | ||
return client->connect(host, port); | ||
} | ||
|
||
int arduino::AClient::connectSSL(IPAddress ip, uint16_t port) { | ||
if (!client) { | ||
newMbedClient(); | ||
} | ||
return client->connectSSL(ip, port); | ||
} | ||
|
||
int arduino::AClient::connectSSL(const char *host, uint16_t port, bool disableSNI) { | ||
if (!client) { | ||
newMbedClient(); | ||
} | ||
return client->connectSSL(host, port, disableSNI); | ||
} | ||
|
||
void arduino::AClient::stop() { | ||
if (!client) | ||
return; | ||
client->stop(); | ||
} | ||
|
||
uint8_t arduino::AClient::connected() { | ||
if (!client) | ||
return false; | ||
return client->connected(); | ||
} | ||
|
||
IPAddress arduino::AClient::remoteIP() { | ||
if (!client) | ||
return INADDR_NONE; | ||
return client->remoteIP(); | ||
} | ||
|
||
uint16_t arduino::AClient::remotePort() { | ||
if (!client) | ||
return 0; | ||
return client->remotePort(); | ||
} | ||
|
||
size_t arduino::AClient::write(uint8_t b) { | ||
if (!client) | ||
return 0; | ||
return client->write(b); | ||
} | ||
|
||
size_t arduino::AClient::write(const uint8_t *buf, size_t size) { | ||
if (!client) | ||
return 0; | ||
return client->write(buf, size); | ||
} | ||
|
||
void arduino::AClient::flush() { | ||
if (!client) | ||
return; | ||
client->flush(); | ||
} | ||
|
||
int arduino::AClient::available() { | ||
if (!client) | ||
return 0; | ||
return client->available(); | ||
} | ||
|
||
int arduino::AClient::read() { | ||
if (!client) | ||
return -1; | ||
return client->read(); | ||
} | ||
|
||
int arduino::AClient::read(uint8_t *buf, size_t size) { | ||
if (!client) | ||
return 0; | ||
return client->read(buf, size); | ||
} | ||
|
||
int arduino::AClient::peek() { | ||
if (!client) | ||
return -1; | ||
return client->peek(); | ||
} | ||
|
||
void arduino::ASslClient::newMbedClient() { | ||
client.reset(new MbedSSLClient()); | ||
client->setNetwork(getNetwork()); | ||
} | ||
|
||
void arduino::ASslClient::disableSNI(bool statusSNI) { | ||
if (!client) { | ||
newMbedClient(); | ||
} | ||
static_cast<MbedSSLClient*>(client.get())->disableSNI(statusSNI); | ||
} | ||
|
||
void arduino::ASslClient::appendCustomCACert(const char* ca_cert) { | ||
if (!client) { | ||
newMbedClient(); | ||
} | ||
static_cast<MbedSSLClient*>(client.get())->appendCustomCACert(ca_cert); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
AClient.h - Copyable Client implementation for Mbed Core | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef MBEDACLIENT_H | ||
#define MBEDACLIENT_H | ||
|
||
#include <Arduino.h> | ||
#include "MbedClient.h" | ||
|
||
namespace arduino { | ||
|
||
class AClient : public Client { | ||
public: | ||
|
||
AClient() {} | ||
AClient(unsigned long timeout); | ||
|
||
virtual int connect(IPAddress ip, uint16_t port); | ||
virtual int connect(const char *host, uint16_t port); | ||
int connectSSL(IPAddress ip, uint16_t port); | ||
int connectSSL(const char* host, uint16_t port, bool disableSNI = false); | ||
virtual void stop(); | ||
|
||
virtual explicit operator bool(); | ||
virtual uint8_t connected(); | ||
uint8_t status(); | ||
|
||
IPAddress remoteIP(); | ||
uint16_t remotePort(); | ||
|
||
virtual size_t write(uint8_t); | ||
virtual size_t write(const uint8_t *buf, size_t size); | ||
virtual void flush(); | ||
|
||
virtual int available(); | ||
virtual int read(); | ||
virtual int read(uint8_t *buf, size_t size); | ||
virtual int peek(); | ||
|
||
using Print::write; | ||
|
||
void setSocketTimeout(unsigned long timeout); | ||
|
||
protected: | ||
friend class EthernetServer; | ||
friend class WiFiServer; | ||
|
||
std::shared_ptr<MbedClient> client; | ||
virtual NetworkInterface* getNetwork() = 0; | ||
virtual void newMbedClient(); | ||
void setSocket(Socket* sock); | ||
|
||
}; | ||
|
||
class ASslClient : public AClient { | ||
public: | ||
|
||
ASslClient() {} | ||
ASslClient(unsigned long timeout) : AClient(timeout) {} | ||
|
||
void disableSNI(bool statusSNI); | ||
|
||
void appendCustomCACert(const char* ca_cert); | ||
|
||
protected: | ||
virtual void newMbedClient(); | ||
}; | ||
|
||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.