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 
62 {
63 public:
69  WebsocketConnection(HttpConnection* connection = nullptr, bool isClientConnection = true);
70 
72  {
73  close();
74  }
75 
82  bool bind(HttpRequest& request, HttpResponse& response);
83 
90  bool send(const char* message, size_t length, ws_frame_type_t type = WS_FRAME_TEXT);
91 
98  bool send(const String& message, ws_frame_type_t type = WS_FRAME_TEXT)
99  {
100  return send(message.c_str(), message.length(), type);
101  }
102 
112  bool send(IDataSourceStream* source, ws_frame_type_t type = WS_FRAME_TEXT, bool useMask = false, bool isFin = true);
113 
120  static void broadcast(const char* message, size_t length, ws_frame_type_t type = WS_FRAME_TEXT);
121 
127  static void broadcast(const String& message, ws_frame_type_t type = WS_FRAME_TEXT)
128  {
129  broadcast(message.c_str(), message.length(), type);
130  }
131 
136  bool sendString(const String& message)
137  {
138  return send(message, WS_FRAME_TEXT);
139  }
140 
146  bool sendBinary(const uint8_t* data, size_t length)
147  {
148  return send(reinterpret_cast<const char*>(data), length, WS_FRAME_BINARY);
149  }
150 
154  void close();
155 
159  void reset();
160 
165  void setUserData(void* userData)
166  {
167  this->userData = userData;
168  }
169 
174  void* getUserData()
175  {
176  return userData;
177  }
178 
183  bool operator==(const WebsocketConnection& rhs) const
184  {
185  return (this == &rhs);
186  }
187 
194  {
195  return websocketList;
196  }
197 
203  {
204  wsConnect = handler;
205  }
206 
212  {
213  wsMessage = handler;
214  }
215 
221  {
222  wsBinary = handler;
223  }
229  {
230  wsPong = handler;
231  }
237  {
238  wsDisconnect = handler;
239  }
240 
245  void activate();
246 
251  bool onConnected();
252 
258  {
259  return connection;
260  }
261 
267  void setConnection(HttpConnection* connection, bool isClientConnection = true);
268 
273  {
274  return state;
275  }
276 
277 protected:
278  // Static handlers for ws_parser
279  static int staticOnDataBegin(void* userData, ws_frame_type_t type);
280  static int staticOnDataPayload(void* userData, const char* at, size_t length);
281  static int staticOnDataEnd(void* userData);
282  static int staticOnControlBegin(void* userData, ws_frame_type_t type);
283  static int staticOnControlPayload(void* userData, const char*, size_t length);
284  static int staticOnControlEnd(void* userData);
285 
292  bool processFrame(TcpClient& client, char* at, int size);
293 
294 protected:
300 
301  void* userData = nullptr;
302 
304 
305 private:
306  ws_frame_type_t frameType = WS_FRAME_TEXT;
307  WsFrameInfo controlFrame;
308 
309  ws_parser_t parser;
310  static const ws_parser_callbacks_t parserSettings;
311 
312  static WebsocketList websocketList;
313 
314  HttpConnection* connection = nullptr;
315  bool isClientConnection;
316  bool activated = false;
317 };
318 
Provides http base used for client and server connections.
Definition: HttpConnection.h:28
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:37
Represents either an incoming or outgoing response to a HTTP request.
Definition: HttpResponse.h:26
Base class for read-only stream.
Definition: DataSourceStream.h:46
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:617
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:244
Definition: TcpClient.h:46
Vector class template.
Definition: WVector.h:32
Definition: WebsocketConnection.h:62
bool onConnected()
Call this method when the websocket connection was (re)activated.
bool send(const char *message, size_t length, ws_frame_type_t type=WS_FRAME_TEXT)
Sends a websocket message from a buffer.
static int staticOnDataBegin(void *userData, ws_frame_type_t type)
bool send(const String &message, ws_frame_type_t type=WS_FRAME_TEXT)
Sends websocket message from a String.
Definition: WebsocketConnection.h:98
void * getUserData()
Retrieves user data attached.
Definition: WebsocketConnection.h:174
WsConnectionState getState()
Gets the state of the websocket connection.
Definition: WebsocketConnection.h:272
WebsocketDelegate wsPong
Definition: WebsocketConnection.h:298
void setConnection(HttpConnection *connection, bool isClientConnection=true)
Sets the underlying (transport ) HTTP connection.
void reset()
Resets a websocket connection.
void close()
Closes a websocket connection (without closing the underlying http connection)
WebsocketBinaryDelegate wsBinary
Definition: WebsocketConnection.h:297
static const WebsocketList & getActiveWebsockets()
Obtain the list of active websockets.
Definition: WebsocketConnection.h:193
WebsocketMessageDelegate wsMessage
Definition: WebsocketConnection.h:296
void setBinaryHandler(WebsocketBinaryDelegate handler)
Sets the callback handler to be called after a binary websocket message is received.
Definition: WebsocketConnection.h:220
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:127
static int staticOnControlBegin(void *userData, ws_frame_type_t type)
bool processFrame(TcpClient &client, char *at, int size)
Callback handler to process a received TCP data frame.
WebsocketDelegate wsConnect
Definition: WebsocketConnection.h:295
void setPongHandler(WebsocketDelegate handler)
Sets the callback handler to be called when pong reply received.
Definition: WebsocketConnection.h:228
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.
static int staticOnDataEnd(void *userData)
bool send(IDataSourceStream *source, ws_frame_type_t type=WS_FRAME_TEXT, bool useMask=false, bool isFin=true)
Sends websocket message from a stream.
bool sendBinary(const uint8_t *data, size_t length)
Sends a binary websocket message.
Definition: WebsocketConnection.h:146
WsConnectionState state
Definition: WebsocketConnection.h:303
void setUserData(void *userData)
Attaches a user data to a websocket connection.
Definition: WebsocketConnection.h:165
void * userData
Definition: WebsocketConnection.h:301
void setDisconnectionHandler(WebsocketDelegate handler)
Sets the callback handler to be called before closing a websocket connection.
Definition: WebsocketConnection.h:236
static int staticOnControlPayload(void *userData, const char *, size_t length)
void setMessageHandler(WebsocketMessageDelegate handler)
Sets the callback handler to be called after a websocket message is received.
Definition: WebsocketConnection.h:211
virtual ~WebsocketConnection()
Definition: WebsocketConnection.h:71
static int staticOnDataPayload(void *userData, const char *at, size_t length)
void activate()
Should be called after a websocket connection is established to activate the websocket parser and all...
WebsocketDelegate wsDisconnect
Definition: WebsocketConnection.h:299
bool operator==(const WebsocketConnection &rhs) const
Test if another connection refers to the same object.
Definition: WebsocketConnection.h:183
bool bind(HttpRequest &request, HttpResponse &response)
Binds websocket connection to an http server connection.
void setConnectionHandler(WebsocketDelegate handler)
Sets the callback handler to be called after successful websocket connection.
Definition: WebsocketConnection.h:202
bool sendString(const String &message)
Sends a string websocket message.
Definition: WebsocketConnection.h:136
static int staticOnControlEnd(void *userData)
HttpConnection * getConnection()
Gets the underlying HTTP connection.
Definition: WebsocketConnection.h:257
WebsocketConnection(HttpConnection *connection=nullptr, bool isClientConnection=true)
Constructs a websocket connection on top of http client or server connection.
#define DECLARE_FSTR(name)
Declare a global FSTR::String& reference.
Definition: String.hpp:63
WsConnectionState
Current state of Websocket connection.
Definition: WebsocketConnection.h:49
@ eWSCS_Closed
Definition: WebsocketConnection.h:52
@ eWSCS_Open
Definition: WebsocketConnection.h:51
@ eWSCS_Ready
Definition: WebsocketConnection.h:50
Definition: WebsocketConnection.h:55
char * payload
Definition: WebsocketConnection.h:57
ws_frame_type_t type
Definition: WebsocketConnection.h:56
size_t payloadLength
Definition: WebsocketConnection.h:58