Skip to content

Commit 31cf0e2

Browse files
committed
1 parent 810e079 commit 31cf0e2

File tree

2 files changed

+115
-67
lines changed

2 files changed

+115
-67
lines changed

src/WebSocket.hpp

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -146,37 +146,10 @@ enum class WebSocketReadyState : uint8_t {
146146
struct WebSocketHeader_t;
147147
/** @endcond */
148148

149-
/** @class WebSocket */
150-
template <class NetClient> class WebSocket {
151-
template <class, class, uint8_t> friend class WebSocketServer;
152-
153-
public:
154-
/**
155-
* @param ws Closing endpoint.
156-
* @param code Close event code.
157-
* @param reason Contains a message for a close event, c-string, non
158-
* NULL-terminated. Might be empty.
159-
* @param length The number of characters in the reason c-string.
160-
*/
161-
using onCloseCallback = void (*)(
162-
WebSocket &ws, WebSocketCloseCode code, const char *reason,
163-
uint16_t length);
164-
165-
/**
166-
* @param ws Source of a message.
167-
* @param dataType Type of a message.
168-
* @param message Non NULL-terminated.
169-
* @param length Number of data bytes.
170-
*/
171-
using onMessageCallback = void (*)(
172-
WebSocket &ws, WebSocketDataType dataType, const char *message,
173-
uint16_t length);
174-
149+
/** @class IWebSocket */
150+
class IWebSocket {
175151
public:
176-
WebSocket(const WebSocket &) = delete;
177-
virtual ~WebSocket();
178-
179-
WebSocket &operator=(const WebSocket &) = delete;
152+
virtual ~IWebSocket() = default;
180153

181154
/**
182155
* @brief Sends a close event.
@@ -185,36 +158,48 @@ template <class NetClient> class WebSocket {
185158
* NULL-terminated. Max length = 123 characters.
186159
* @param length The number of characters in the reason c-string.
187160
*/
188-
void close(
189-
WebSocketCloseCode, bool instant, const char *reason = nullptr,
190-
uint16_t length = 0);
161+
virtual void close(
162+
WebSocketCloseCode, bool instant, const char *reason,
163+
uint16_t length) = 0;
191164
/** @brief Immediately closes the connection. */
192-
void terminate();
165+
virtual void terminate() = 0;
193166

194167
/** @return Endpoint connection status. */
195-
WebSocketReadyState getReadyState() const;
168+
virtual WebSocketReadyState getReadyState() const = 0;
196169
/** @brief Verifies endpoint connection. */
197-
bool isAlive() const;
170+
virtual bool isAlive() const = 0;
198171

199172
/**
200173
* @return Endpoint IP address.
201174
* @remark For some microcontrollers it might be empty.
202175
*/
203-
IPAddress getRemoteIP() const;
204-
const char *getProtocol() const;
176+
virtual IPAddress getRemoteIP() const = 0;
177+
virtual const char *getProtocol() const = 0;
205178

206179
/**
207180
* @brief Sends a message frame.
208181
* @param message Doesn't have to be NULL-terminated.
209182
*/
210-
void send(WebSocketDataType, const char *message, uint16_t length);
183+
virtual void
184+
send(WebSocketDataType, const char *message, uint16_t length) = 0;
211185
/**
212186
* @brief Sends a ping message.
213187
* @param payload An additional message, doesn't have to be NULL-terminated.
214188
* Max length = 125.
215189
* @param length The number of characters in payload.
216190
*/
217-
void ping(const char *payload = nullptr, uint16_t length = 0);
191+
virtual void ping(const char *payload, uint16_t length) = 0;
192+
193+
/**
194+
* @param ws Closing endpoint.
195+
* @param code Close event code.
196+
* @param reason Contains a message for a close event, c-string, non
197+
* NULL-terminated. Might be empty.
198+
* @param length The number of characters in the reason c-string.
199+
*/
200+
using onCloseCallback = void (*)(
201+
IWebSocket &ws, WebSocketCloseCode code, const char *reason,
202+
uint16_t length);
218203

219204
/**
220205
* @brief Sets the close event handler.
@@ -225,7 +210,18 @@ template <class NetClient> class WebSocket {
225210
* });
226211
* @endcode
227212
*/
228-
void onClose(const onCloseCallback &);
213+
virtual void onClose(const onCloseCallback &) = 0;
214+
215+
/**
216+
* @param ws Source of a message.
217+
* @param dataType Type of a message.
218+
* @param message Non NULL-terminated.
219+
* @param length Number of data bytes.
220+
*/
221+
using onMessageCallback = void (*)(
222+
IWebSocket &ws, WebSocketDataType dataType, const char *message,
223+
uint16_t length);
224+
229225
/**
230226
* @brief Sets the message handler function.
231227
* @code{.cpp}
@@ -235,6 +231,36 @@ template <class NetClient> class WebSocket {
235231
* });
236232
* @endcode
237233
*/
234+
virtual void onMessage(const onMessageCallback &) = 0;
235+
};
236+
237+
/** @class WebSocket */
238+
template <class NetClient> class WebSocket : public IWebSocket {
239+
template <class, class, uint8_t> friend class WebSocketServer;
240+
241+
public:
242+
public:
243+
WebSocket(const WebSocket &) = delete;
244+
~WebSocket() override;
245+
246+
WebSocket &operator=(const WebSocket &) = delete;
247+
248+
void close(
249+
WebSocketCloseCode, bool instant, const char *reason = nullptr,
250+
uint16_t length = 0);
251+
void terminate();
252+
253+
WebSocketReadyState getReadyState() const;
254+
bool isAlive() const;
255+
256+
IPAddress getRemoteIP() const;
257+
const char *getProtocol() const;
258+
259+
void send(WebSocketDataType, const char *message, uint16_t length);
260+
261+
void ping(const char *payload = nullptr, uint16_t length = 0);
262+
263+
void onClose(const onCloseCallback &);
238264
void onMessage(const onMessageCallback &);
239265

240266
protected:
@@ -293,9 +319,9 @@ void encodeSecKey(const char *key, char output[]);
293319
void generateMask(char output[]);
294320

295321
template <typename NetClient>
296-
inline IPAddress fetchRemoteIp(NetClient &&client) {
322+
inline IPAddress fetchRemoteIp(const NetClient &client) {
297323
if constexpr (has_remoteIP<NetClient>::value)
298-
return client.remoteIP();
324+
return const_cast<NetClient &>(client).remoteIP();
299325
else
300326
return {};
301327
}

src/WebSocketServer.hpp

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,20 @@
66

77
namespace net {
88

9-
/** @class WebSocketServer */
10-
template <class NetServer, class NetClient, uint8_t _MaxConnections = 4>
11-
class WebSocketServer {
12-
static_assert(_MaxConnections <= 8, "I don't think so ...");
13-
9+
/** @class IWebSocketServer */
10+
class IWebSocketServer {
1411
public:
12+
virtual ~IWebSocketServer() = default;
13+
1514
/**
1615
* @param header c-string, NULL-terminated.
1716
* @param value c-string, NULL-terminated.
1817
*/
1918
using verifyClientCallback =
2019
bool (*)(const IPAddress &, const char *header, const char *value);
2120
/** @param ws Accepted client. */
22-
using onConnectionCallback = void (*)(WebSocket<NetClient> &ws);
2321
using protocolHandlerCallback = const char *(*)(const char *);
2422

25-
public:
26-
/**
27-
* @brief Initializes server on given port.
28-
* @note Don't forget to call begin()
29-
*/
30-
explicit WebSocketServer(uint16_t port = 3000);
31-
WebSocketServer(const WebSocketServer &) = delete;
32-
~WebSocketServer();
33-
34-
WebSocketServer &operator=(const WebSocketServer &) = delete;
35-
3623
/**
3724
* @brief Startup server.
3825
* @code{.cpp}
@@ -48,20 +35,24 @@ class WebSocketServer {
4835
* @param callback Function called for every header during hadshake (except
4936
* for those required by protocol, like **Connection**, **Upgrade** etc.)
5037
*/
51-
void begin(
38+
virtual void begin(
5239
const verifyClientCallback &verifyClient = nullptr,
53-
const protocolHandlerCallback &protocolHandler = nullptr);
40+
const protocolHandlerCallback &protocolHandler = nullptr) = 0;
41+
5442
/** @brief Disconnects all clients. */
55-
void shutdown();
43+
virtual void shutdown() = 0;
5644

5745
/** @brief Sends message to all connected clients. */
58-
void broadcast(WebSocketDataType, const char *message, uint16_t length);
46+
virtual void
47+
broadcast(WebSocketDataType, const char *message, uint16_t length) = 0;
5948

6049
/** @note Call this in main loop. */
61-
void listen();
50+
virtual void listen() = 0;
6251

6352
/** @return Amount of connected clients. */
64-
uint8_t countClients() const;
53+
virtual uint8_t countClients() const = 0;
54+
55+
using onConnectionCallback = void (*)(IWebSocket &ws);
6556

6657
/**
6758
* @brief
@@ -82,7 +73,38 @@ class WebSocketServer {
8273
* @param callback Function that will be called for every successfully
8374
* connected client.
8475
*/
85-
void onConnection(const onConnectionCallback &callback);
76+
virtual void onConnection(const onConnectionCallback &callback) = 0;
77+
};
78+
79+
/** @class WebSocketServer */
80+
template <class NetServer, class NetClient, uint8_t _MaxConnections = 4>
81+
class WebSocketServer : public IWebSocketServer {
82+
static_assert(_MaxConnections <= 8, "I don't think so ...");
83+
84+
public:
85+
/**
86+
* @brief Initializes server on given port.
87+
* @note Don't forget to call begin()
88+
*/
89+
explicit WebSocketServer(uint16_t port = 3000);
90+
WebSocketServer(const WebSocketServer &) = delete;
91+
~WebSocketServer() override;
92+
93+
WebSocketServer &operator=(const WebSocketServer &) = delete;
94+
95+
void begin(
96+
const verifyClientCallback &verifyClient = nullptr,
97+
const protocolHandlerCallback &protocolHandler = nullptr) override;
98+
void shutdown() override;
99+
100+
void
101+
broadcast(WebSocketDataType, const char *message, uint16_t length) override;
102+
103+
void listen() override;
104+
105+
uint8_t countClients() const override;
106+
107+
void onConnection(const onConnectionCallback &callback) override;
86108

87109
private:
88110
/** @cond */

0 commit comments

Comments
 (0)