Skip to content

Commit 88bedfb

Browse files
committed
MERGED: recent changes from TinyWebsockets. WIP - no proper support for esp8266 and esp32
1 parent be4c138 commit 88bedfb

19 files changed

+265
-140
lines changed

src/crypto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace websockets { namespace crypto {
4949
WSString result;
5050
result.reserve(len);
5151

52-
for(size_t i = 0; i < len; i++) {
52+
for(size_t i = onlyOnce; i < len; i++) {
5353
result += "0123456789abcdefABCDEFGHIJKLMNOPQRSTUVEXYZ"[rand() % 42];
5454
}
5555
return result;

src/linux_tcp.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace websockets { namespace network {
5252
return close( _socket );
5353
}
5454

55-
bool linuxTcpSend(int _socket, uint8_t* data, size_t len) {
55+
bool linuxTcpSend(const int _socket, const uint8_t* data, const size_t len) {
5656
auto res = send(_socket, data, len, MSG_NOSIGNAL );
5757
if(res < 0) {
5858
return false;
@@ -65,11 +65,11 @@ namespace websockets { namespace network {
6565
return read(_socket, buffer, len) > 0;
6666
}
6767

68-
LinuxTcpClient::LinuxTcpClient(int socket) : _socket(socket) {
68+
LinuxTcpClient::LinuxTcpClient(const int socket) : _socket(socket) {
6969
// Empty
7070
}
7171

72-
bool LinuxTcpClient::connect(WSString host, int port) {
72+
bool LinuxTcpClient::connect(const WSString& host, const int port) {
7373
this->_socket = linuxTcpConnect(host, port);
7474
return available();
7575
}
@@ -82,13 +82,20 @@ namespace websockets { namespace network {
8282
bool LinuxTcpClient::available() {
8383
return this->_socket != INVALID_SOCKET;
8484
}
85-
void LinuxTcpClient::send(WSString data) {
85+
void LinuxTcpClient::send(const WSString& data) {
8686
return this->send(
87-
reinterpret_cast<uint8_t*>(const_cast<char*>(data.c_str())),
87+
reinterpret_cast<const uint8_t*>(data.c_str()),
8888
data.size()
8989
);
9090
}
91-
void LinuxTcpClient::send(uint8_t* data, uint32_t len) {
91+
void LinuxTcpClient::send(const WSString&& data) {
92+
return this->send(
93+
reinterpret_cast<const uint8_t*>(data.c_str()),
94+
data.size()
95+
);
96+
}
97+
98+
void LinuxTcpClient::send(const uint8_t* data, const uint32_t len) {
9299
if(!available()) return;// false;
93100

94101
auto success = linuxTcpSend(
@@ -100,7 +107,7 @@ namespace websockets { namespace network {
100107
if(!success) close();
101108
// return success;
102109
}
103-
110+
104111
WSString LinuxTcpClient::readLine() {
105112
uint8_t byte = '0';
106113
WSString line;
@@ -114,7 +121,7 @@ namespace websockets { namespace network {
114121
return line;
115122
}
116123

117-
void LinuxTcpClient::read(uint8_t* buffer, uint32_t len) {
124+
void LinuxTcpClient::read(uint8_t* buffer, const uint32_t len) {
118125
auto success = linuxTcpRead(this->_socket, buffer, len);
119126
if(!success) close();
120127
}
@@ -133,8 +140,7 @@ namespace websockets { namespace network {
133140
namespace websockets { namespace network {
134141

135142
int linuxTcpServerInit(const size_t backlog, int port) {
136-
socklen_t clilen;
137-
struct sockaddr_in serv_addr, cli_addr;
143+
struct sockaddr_in serv_addr;
138144

139145
// socket init
140146
auto sockfd = socket(AF_INET, SOCK_STREAM, 0);
@@ -157,7 +163,7 @@ namespace websockets { namespace network {
157163
return sockfd;
158164
}
159165

160-
bool LinuxTcpServer::listen(uint16_t port) {
166+
bool LinuxTcpServer::listen(const uint16_t port) {
161167
this->_socket = linuxTcpServerInit(this->_num_backlog, port);
162168
return this->available();
163169
}

src/tiny_websockets/client.hpp

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <tiny_websockets/internals/data_frame.hpp>
66
#include <tiny_websockets/internals/websockets_endpoint.hpp>
77
#include <tiny_websockets/message.hpp>
8+
#include <memory>
89
#include <functional>
910

1011
namespace websockets {
@@ -21,54 +22,58 @@ namespace websockets {
2122
typedef std::function<void(WebsocketsClient&, WebsocketsEvent, WSInterfaceString)> EventCallback;
2223
typedef std::function<void(WebsocketsEvent, WSInterfaceString)> PartialEventCallback;
2324

24-
class WebsocketsClient : private internals::WebsocketsEndpoint {
25+
class WebsocketsClient {
2526
public:
26-
WebsocketsClient(network::TcpClient* client = new WSDefaultTcpClient);
27+
WebsocketsClient();
28+
WebsocketsClient(std::shared_ptr<network::TcpClient> client);
2729

2830
WebsocketsClient(const WebsocketsClient& other);
2931
WebsocketsClient(const WebsocketsClient&& other);
3032

3133
WebsocketsClient& operator=(const WebsocketsClient& other);
3234
WebsocketsClient& operator=(const WebsocketsClient&& other);
3335

34-
bool connect(WSInterfaceString url);
35-
bool connect(WSInterfaceString host, int port, WSInterfaceString path);
36+
bool connect(const WSInterfaceString url);
37+
bool connect(const WSInterfaceString host, const int port, const WSInterfaceString path);
3638

37-
void onMessage(MessageCallback callback);
38-
void onMessage(PartialMessageCallback callback);
39+
void onMessage(const MessageCallback callback);
40+
void onMessage(const PartialMessageCallback callback);
3941

40-
void onEvent(EventCallback callback);
41-
void onEvent(PartialEventCallback callback);
42+
void onEvent(const EventCallback callback);
43+
void onEvent(const PartialEventCallback callback);
4244

4345
bool poll();
44-
bool available(bool activeTest = false);
46+
bool available(const bool activeTest = false);
4547

46-
bool send(WSInterfaceString data);
47-
bool send(const char* data, size_t len);
48+
bool send(const WSInterfaceString&& data);
49+
bool send(const WSInterfaceString& data);
50+
bool send(const char* data);
51+
bool send(const char* data, const size_t len);
4852

49-
bool sendBinary(WSInterfaceString data);
50-
bool sendBinary(const char* data, size_t len);
53+
bool sendBinary(const WSInterfaceString data);
54+
bool sendBinary(const char* data, const size_t len);
5155

5256
// stream messages
53-
bool stream(WSInterfaceString data = "");
54-
bool streamBinary(WSInterfaceString data = "");
55-
bool end(WSInterfaceString data = "");
57+
bool stream(const WSInterfaceString data = "");
58+
bool streamBinary(const WSInterfaceString data = "");
59+
bool end(const WSInterfaceString data = "");
5660

57-
void setFragmentsPolicy(FragmentsPolicy newPolicy);
58-
FragmentsPolicy getFragmentsPolicy();
61+
void setFragmentsPolicy(const FragmentsPolicy newPolicy);
62+
FragmentsPolicy getFragmentsPolicy() const;
5963

6064
WebsocketsMessage readBlocking();
6165

62-
bool ping(WSInterfaceString data = "");
63-
bool pong(WSInterfaceString data = "");
66+
bool ping(const WSInterfaceString data = "");
67+
bool pong(const WSInterfaceString data = "");
6468

65-
void close(CloseReason reason = CloseReason_NormalClosure);
66-
CloseReason getCloseReason();
69+
void close(const CloseReason reason = CloseReason_NormalClosure);
70+
CloseReason getCloseReason() const;
6771

6872
virtual ~WebsocketsClient();
6973

7074
private:
71-
network::TcpClient* _client;
75+
std::shared_ptr<network::TcpClient> _client;
76+
internals::WebsocketsEndpoint _endpoint;
7277
bool _connectionOpen;
7378
MessageCallback _messagesCallback;
7479
EventCallback _eventsCallback;
@@ -80,5 +85,7 @@ namespace websockets {
8085
void _handlePing(WebsocketsMessage);
8186
void _handlePong(WebsocketsMessage);
8287
void _handleClose(WebsocketsMessage);
88+
89+
void upgradeToSecuredConnection();
8390
};
8491
}

src/tiny_websockets/internals/websockets_endpoint.hpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <tiny_websockets/network/tcp_client.hpp>
55
#include <tiny_websockets/internals/data_frame.hpp>
66
#include <tiny_websockets/message.hpp>
7+
#include <memory>
78

89
namespace websockets {
910
enum FragmentsPolicy {
@@ -31,7 +32,7 @@ namespace websockets {
3132

3233
class WebsocketsEndpoint {
3334
public:
34-
WebsocketsEndpoint(network::TcpClient* socket, FragmentsPolicy fragmentsPolicy = FragmentsPolicy_Aggregate);
35+
WebsocketsEndpoint(std::shared_ptr<network::TcpClient> socket, FragmentsPolicy fragmentsPolicy = FragmentsPolicy_Aggregate);
3536

3637
WebsocketsEndpoint(const WebsocketsEndpoint& other);
3738
WebsocketsEndpoint(const WebsocketsEndpoint&& other);
@@ -41,21 +42,24 @@ namespace websockets {
4142

4243
bool poll();
4344
WebsocketsMessage recv();
44-
bool send(const char* data, size_t len, uint8_t opcode, bool fin = true, bool mask = false, uint8_t maskingKey[4] = nullptr);
45-
bool send(WSString data, uint8_t opcode, bool fin = true, bool mask = false, uint8_t maskingKey[4] = nullptr);
45+
bool send(const char* data, const size_t len, const uint8_t opcode, const bool fin = true, const bool mask = false, const uint8_t maskingKey[4] = nullptr);
46+
bool send(const WSString& data, const uint8_t opcode, const bool fin = true, const bool mask = false, const uint8_t maskingKey[4] = nullptr);
4647

47-
bool ping(WSString msg = "");
48-
bool pong(WSString msg = "");
48+
bool ping(const WSString& msg);
49+
bool ping(const WSString&& msg);
4950

50-
void close(CloseReason reason = CloseReason_NormalClosure);
51-
CloseReason getCloseReason();
51+
bool pong(const WSString& msg);
52+
bool pong(const WSString&& msg);
5253

53-
void setFragmentsPolicy(FragmentsPolicy newPolicy);
54-
FragmentsPolicy getFragmentsPolicy();
54+
void close(const CloseReason reason = CloseReason_NormalClosure);
55+
CloseReason getCloseReason() const;
56+
57+
void setFragmentsPolicy(const FragmentsPolicy newPolicy);
58+
FragmentsPolicy getFragmentsPolicy() const;
5559

5660
virtual ~WebsocketsEndpoint();
5761
private:
58-
network::TcpClient* _client;
62+
std::shared_ptr<network::TcpClient> _client;
5963
FragmentsPolicy _fragmentsPolicy;
6064
enum RecvMode {
6165
RecvMode_Normal,

src/tiny_websockets/internals/ws_common.hpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
#include <string>
55
#include <Arduino.h>
66

7+
// Versioning
8+
#define TINY_WS_VERSION_STRING "0.1.0"
9+
#define TINY_WS_VERSION_MAJOR 0
10+
#define TINY_WS_VERSION_MINOR 1
11+
#define TINY_WS_VERSION_PATCH 0
12+
713
namespace websockets {
814
typedef std::string WSString;
915
typedef String WSInterfaceString;
1016

1117
namespace internals {
12-
WSString fromInterfaceString(WSInterfaceString& str);
13-
WSString fromInterfaceString(WSInterfaceString&& str);
14-
WSInterfaceString fromInternalString(WSString& str);
15-
WSInterfaceString fromInternalString(WSString&& str);
18+
WSString fromInterfaceString(const WSInterfaceString& str);
19+
WSString fromInterfaceString(const WSInterfaceString&& str);
20+
WSInterfaceString fromInternalString(const WSString& str);
21+
WSInterfaceString fromInternalString(const WSString&& str);
1622
}
1723
}
1824

@@ -21,11 +27,25 @@ namespace websockets {
2127
#include <tiny_websockets/network/windows/win_tcp_server.hpp>
2228
#define WSDefaultTcpClient websockets::network::WinTcpClient
2329
#define WSDefaultTcpServer websockets::network::WinTcpServer
30+
31+
#ifndef _WS_CONFIG_NO_SSL
32+
// OpenSSL Dependent
33+
#include <tiny_websockets/network/openssl_secure_tcp_client.hpp>
34+
#define WSDefaultSecuredTcpClient websockets::network::OpenSSLSecureTcpClient<WSDefaultTcpClient>
35+
#endif //_WS_CONFIG_NO_SSL
36+
2437
#elif defined(__linux__)
2538
#include <tiny_websockets/network/linux/linux_tcp_client.hpp>
2639
#include <tiny_websockets/network/linux/linux_tcp_server.hpp>
2740
#define WSDefaultTcpClient websockets::network::LinuxTcpClient
2841
#define WSDefaultTcpServer websockets::network::LinuxTcpServer
42+
43+
#ifndef _WS_CONFIG_NO_SSL
44+
// OpenSSL Dependent
45+
#include <tiny_websockets/network/openssl_secure_tcp_client.hpp>
46+
#define WSDefaultSecuredTcpClient websockets::network::OpenSSLSecureTcpClient<WSDefaultTcpClient>
47+
#endif //_WS_CONFIG_NO_SSL
48+
2949
#elif defined(ESP8266)
3050

3151
#define PLATFORM_DOES_NOT_SUPPORT_BLOCKING_READ

src/tiny_websockets/message.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ namespace websockets {
167167
private:
168168
bool _dummyMode;
169169
bool _empty;
170-
bool _isComplete;
170+
bool _isComplete = false;
171171
WSString _content;
172172
MessageType _type;
173173
bool _didErrored;

src/tiny_websockets/network/linux/linux_tcp_client.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,23 @@ namespace websockets { namespace network {
1212
class LinuxTcpClient : public TcpClient {
1313
public:
1414
LinuxTcpClient(int socket = INVALID_SOCKET);
15-
bool connect(WSString host, int port) override;
15+
bool connect(const WSString& host, int port) override;
1616
bool poll() override;
1717
bool available() override;
18-
void send(WSString data) override;
19-
void send(uint8_t* data, uint32_t len) override;
18+
void send(const WSString& data) override;
19+
void send(const WSString&& data) override;
20+
void send(const uint8_t* data, const uint32_t len) override;
2021
WSString readLine() override;
21-
void read(uint8_t* buffer, uint32_t len) override;
22+
void read(uint8_t* buffer, const uint32_t len) override;
2223
void close() override;
2324
virtual ~LinuxTcpClient();
2425

26+
protected:
27+
virtual int getSocket() const override { return _socket; }
28+
2529
private:
2630
int _socket;
2731
};
2832
}} // websockets::network
2933

30-
#endif // #ifdef __linux__
34+
#endif // #ifdef __linux__

src/tiny_websockets/network/linux/linux_tcp_server.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ namespace websockets { namespace network {
1212
class LinuxTcpServer : public TcpServer {
1313
public:
1414
LinuxTcpServer(size_t backlog = DEFAULT_BACKLOG_SIZE) : _num_backlog(backlog) {}
15-
bool listen(uint16_t port) override;
15+
bool listen(const uint16_t port) override;
1616
bool poll() override;
17-
TcpClient* accept() override;
17+
TcpClient* accept() override;
1818
bool available() override;
1919
void close() override;
2020
virtual ~LinuxTcpServer();
21+
22+
protected:
23+
virtual int getSocket() const override { return _socket; }
24+
2125
private:
2226
int _socket;
2327
size_t _num_backlog;
2428
};
2529
}} // websockets::network
2630

27-
#endif // #ifdef __linux__
31+
#endif // #ifdef __linux__

src/tiny_websockets/network/tcp_client.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
namespace websockets { namespace network {
77
struct TcpClient : public TcpSocket {
88
virtual bool poll() = 0;
9-
virtual void send(WSString data) = 0;
10-
virtual void send(uint8_t* data, uint32_t len) = 0;
11-
virtual WSString readLine() = 0;
12-
virtual void read(uint8_t* buffer, uint32_t len) = 0;
13-
virtual bool connect(WSString host, int port) = 0;
9+
virtual void send(const WSString& data) = 0;
10+
virtual void send(const WSString&& data) = 0;
11+
virtual void send(const uint8_t* data, const uint32_t len) = 0;
12+
virtual WSString readLine() = 0;
13+
virtual void read(uint8_t* buffer, const uint32_t len) = 0;
14+
virtual bool connect(const WSString& host, int port) = 0;
1415
virtual ~TcpClient() {}
1516
};
1617
}} // websockets::network

src/tiny_websockets/network/tcp_server.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace websockets { namespace network {
88
struct TcpServer : public TcpSocket {
99
virtual bool poll() = 0;
10-
virtual bool listen(uint16_t port) = 0;
10+
virtual bool listen(const uint16_t port) = 0;
1111
virtual TcpClient* accept() = 0;
1212
virtual ~TcpServer() {}
1313
};

0 commit comments

Comments
 (0)