WebsocketConnection.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * WebsocketConnection.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "Network/TcpServer.h"
14 #include "../HttpConnection.h"
15 
16 extern "C" {
17 #include "ws_parser/ws_parser.h"
18 }
19 
26 #define WEBSOCKET_VERSION 13 // 1.3
27 
28 DECLARE_FSTR(WSSTR_CONNECTION)
29 DECLARE_FSTR(WSSTR_UPGRADE)
30 DECLARE_FSTR(WSSTR_WEBSOCKET)
31 DECLARE_FSTR(WSSTR_HOST)
32 DECLARE_FSTR(WSSTR_ORIGIN)
33 DECLARE_FSTR(WSSTR_KEY)
34 DECLARE_FSTR(WSSTR_PROTOCOL)
35 DECLARE_FSTR(WSSTR_VERSION)
36 DECLARE_FSTR(WSSTR_SECRET)
37 
39 
41 
44 using WebsocketBinaryDelegate = Delegate<void(WebsocketConnection&, uint8_t* data, size_t size)>;
45 
53 };
54 
55 struct WsFrameInfo {
56  ws_frame_type_t type = WS_FRAME_TEXT;
57  char* payload = nullptr;
58  size_t payloadLength = 0;
59 
60  WsFrameInfo() = default;
61 
62  WsFrameInfo(ws_frame_type_t type, char* payload, size_t payloadLength)
64  {
65  }
66 };
67 
69 {
70 public:
76  WebsocketConnection(HttpConnection* connection, bool isClientConnection = true);
77 
79  {
80  close();
81  }
82 
89  bool bind(HttpRequest& request, HttpResponse& response);
90 
97  bool send(const char* message, size_t length, ws_frame_type_t type = WS_FRAME_TEXT);
98 
105  bool send(const String& message, ws_frame_type_t type = WS_FRAME_TEXT)
106  {
107  return send(message.c_str(), message.length(), type);
108  }
109 
119  bool send(IDataSourceStream* stream, ws_frame_type_t type = WS_FRAME_TEXT, bool useMask = false, bool isFin = true);
120 
127  static void broadcast(const char* message, size_t length, ws_frame_type_t type = WS_FRAME_TEXT);
128 
134  static void broadcast(const String& message, ws_frame_type_t type = WS_FRAME_TEXT)
135  {
136  broadcast(message.c_str(), message.length(), type);
137  }
138 
143  bool sendString(const String& message)
144  {
145  return send(message, WS_FRAME_TEXT);
146  }
147 
153  bool sendBinary(const uint8_t* data, size_t length)
154  {
155  return send(reinterpret_cast<const char*>(data), length, WS_FRAME_BINARY);
156  }
157 
161  void close();
162 
166  void reset();
167 
172  void setUserData(void* userData)
173  {
174  this->userData = userData;
175  }
176 
181  void* getUserData()
182  {
183  return userData;
184  }
185 
190  bool operator==(const WebsocketConnection& rhs) const
191  {
192  return (this == &rhs);
193  }
194 
201  {
202  return websocketList;
203  }
204 
210  {
211  wsConnect = handler;
212  }
213 
219  {
220  wsMessage = handler;
221  }
222 
228  {
229  wsBinary = handler;
230  }
236  {
237  wsPong = handler;
238  }
244  {
245  wsDisconnect = handler;
246  }
247 
252  void activate();
253 
258  bool onConnected();
259 
265  {
266  return connection;
267  }
268 
274  void setConnection(HttpConnection* connection, bool isClientConnection = true)
275  {
276  this->connection = connection;
277  this->isClientConnection = isClientConnection;
278  }
279 
284  {
285  return state;
286  }
287 
288 protected:
289  // Static handlers for ws_parser
290  static int staticOnDataBegin(void* userData, ws_frame_type_t type);
291  static int staticOnDataPayload(void* userData, const char* at, size_t length);
292  static int staticOnDataEnd(void* userData);
293  static int staticOnControlBegin(void* userData, ws_frame_type_t type);
294  static int staticOnControlPayload(void* userData, const char*, size_t length);
295  static int staticOnControlEnd(void* userData);
296 
303  bool processFrame(TcpClient& client, char* at, int size);
304 
305 protected:
311 
312  void* userData = nullptr;
313 
315 
316 private:
317  ws_frame_type_t frameType = WS_FRAME_TEXT;
318  WsFrameInfo controlFrame;
319 
320  ws_parser_t parser;
321  static const ws_parser_callbacks_t parserSettings;
322 
323  static WebsocketList websocketList;
324 
325  bool isClientConnection = true;
326 
327  HttpConnection* connection = nullptr;
328  bool activated = false;
329 };
330 
void * userData
Definition: WebsocketConnection.h:312
Base class for read-only stream.
Definition: DataSourceStream.h:45
WebsocketBinaryDelegate wsBinary
Definition: WebsocketConnection.h:308
WsFrameInfo(ws_frame_type_t type, char *payload, size_t payloadLength)
Definition: WebsocketConnection.h:62
virtual ~WebsocketConnection()
Definition: WebsocketConnection.h:78
Provides http base used for client and server connections.
Definition: HttpConnection.h:27
bool sendBinary(const uint8_t *data, size_t length)
Sends a binary websocket message.
Definition: WebsocketConnection.h:153
Definition: WebsocketConnection.h:55
WebsocketMessageDelegate wsMessage
Definition: WebsocketConnection.h:307
void setConnectionHandler(WebsocketDelegate handler)
Sets the callback handler to be called after successful websocket connection.
Definition: WebsocketConnection.h:209
static const WebsocketList & getActiveWebsockets()
Obtain the list of active websockets.
Definition: WebsocketConnection.h:200
bool onConnected()
Call this method when the websocket connection was (re)activated.
HttpConnection * getConnection()
Gets the underlying HTTP connection.
Definition: WebsocketConnection.h:264
@ eWSCS_Open
Definition: WebsocketConnection.h:51
The String class.
Definition: WString.h:136
Represents either an incoming or outgoing response to a HTTP request.
Definition: HttpResponse.h:25
static int staticOnControlPayload(void *userData, const char *, size_t length)
void activate()
Should be called after a websocket connection is established to activate the websocket parser and all...
WsConnectionState
Current state of Websocket connection.
Definition: WebsocketConnection.h:49
static int staticOnDataPayload(void *userData, const char *at, size_t length)
bool operator==(const WebsocketConnection &rhs) const
Test if another connection refers to the same object.
Definition: WebsocketConnection.h:190
void setUserData(void *userData)
Attaches a user data to a websocket connection.
Definition: WebsocketConnection.h:172
static void broadcast(const char *message, size_t length, ws_frame_type_t type=WS_FRAME_TEXT)
Broadcasts a message to all active websocket connections.
Definition: TcpClient.h:45
void reset()
Resets a websocket connection.
WebsocketDelegate wsDisconnect
Definition: WebsocketConnection.h:310
WsConnectionState state
Definition: WebsocketConnection.h:314
static int staticOnDataEnd(void *userData)
char * payload
Definition: WebsocketConnection.h:57
static int staticOnControlBegin(void *userData, ws_frame_type_t type)
@ eWSCS_Closed
Definition: WebsocketConnection.h:52
bool bind(HttpRequest &request, HttpResponse &response)
Binds websocket connection to an http server connection.
WsConnectionState getState()
Gets the state of the websocket connection.
Definition: WebsocketConnection.h:283
void setConnection(HttpConnection *connection, bool isClientConnection=true)
Sets the underlying (transport ) HTTP connection.
Definition: WebsocketConnection.h:274
WsFrameInfo()=default
Definition: WebsocketConnection.h:68
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
bool send(const char *message, size_t length, ws_frame_type_t type=WS_FRAME_TEXT)
Sends a websocket message from a buffer.
void * getUserData()
Retrieves user data attached.
Definition: WebsocketConnection.h:181
WebsocketConnection(HttpConnection *connection, bool isClientConnection=true)
Constructs a websocket connection on top of http client or server connection.
void setMessageHandler(WebsocketMessageDelegate handler)
Sets the callback handler to be called after a websocket message is received.
Definition: WebsocketConnection.h:218
bool sendString(const String &message)
Sends a string websocket message.
Definition: WebsocketConnection.h:143
WebsocketDelegate wsConnect
Definition: WebsocketConnection.h:306
void setDisconnectionHandler(WebsocketDelegate handler)
Sets the callback handler to be called before closing a websocket connection.
Definition: WebsocketConnection.h:243
void setBinaryHandler(WebsocketBinaryDelegate handler)
Sets the callback handler to be called after a binary websocket message is received.
Definition: WebsocketConnection.h:227
WebsocketDelegate wsPong
Definition: WebsocketConnection.h:309
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:36
@ eWSCS_Ready
Definition: WebsocketConnection.h:50
#define DECLARE_FSTR(name)
Declare a global FSTR::String& reference.
Definition: String.hpp:63
void setPongHandler(WebsocketDelegate handler)
Sets the callback handler to be called when pong reply received.
Definition: WebsocketConnection.h:235
static int staticOnDataBegin(void *userData, ws_frame_type_t type)
bool processFrame(TcpClient &client, char *at, int size)
Callback handler to process a received TCP data frame.
ws_frame_type_t type
Definition: WebsocketConnection.h:56
static int staticOnControlEnd(void *userData)
Vector class template.
Definition: WVector.h:31
static void broadcast(const String &message, ws_frame_type_t type=WS_FRAME_TEXT)
Broadcasts a message to all active websocket connections.
Definition: WebsocketConnection.h:134
void close()
Closes a websocket connection (without closing the underlying http connection)
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
size_t payloadLength
Definition: WebsocketConnection.h:58
bool send(const String &message, ws_frame_type_t type=WS_FRAME_TEXT)
Sends websocket message from a String.
Definition: WebsocketConnection.h:105