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_UPGRADE)
29 DECLARE_FSTR(WSSTR_WEBSOCKET)
30 DECLARE_FSTR(WSSTR_SECRET)
31 
33 
35 
38 using WebsocketBinaryDelegate = Delegate<void(WebsocketConnection&, uint8_t* data, size_t size)>;
39 
47 };
48 
49 struct WsFrameInfo {
50  ws_frame_type_t type = WS_FRAME_TEXT;
51  char* payload = nullptr;
52  size_t payloadLength = 0;
53 };
54 
56 {
57 public:
63  WebsocketConnection(HttpConnection* connection = nullptr, bool isClientConnection = true);
64 
66  {
67  close();
68  }
69 
76  bool bind(HttpRequest& request, HttpResponse& response);
77 
84  bool send(const char* message, size_t length, ws_frame_type_t type = WS_FRAME_TEXT);
85 
92  bool send(const String& message, ws_frame_type_t type = WS_FRAME_TEXT)
93  {
94  return send(message.c_str(), message.length(), type);
95  }
96 
106  bool send(IDataSourceStream* source, ws_frame_type_t type = WS_FRAME_TEXT, bool useMask = false, bool isFin = true);
107 
114  static void broadcast(const char* message, size_t length, ws_frame_type_t type = WS_FRAME_TEXT);
115 
121  static void broadcast(const String& message, ws_frame_type_t type = WS_FRAME_TEXT)
122  {
123  broadcast(message.c_str(), message.length(), type);
124  }
125 
130  bool sendString(const String& message)
131  {
132  return send(message, WS_FRAME_TEXT);
133  }
134 
140  bool sendBinary(const uint8_t* data, size_t length)
141  {
142  return send(reinterpret_cast<const char*>(data), length, WS_FRAME_BINARY);
143  }
144 
148  void close();
149 
153  void reset();
154 
159  void setUserData(void* userData)
160  {
161  this->userData = userData;
162  }
163 
168  void* getUserData()
169  {
170  return userData;
171  }
172 
177  bool operator==(const WebsocketConnection& rhs) const
178  {
179  return (this == &rhs);
180  }
181 
188  {
189  return websocketList;
190  }
191 
197  {
198  wsConnect = handler;
199  }
200 
206  {
207  wsMessage = handler;
208  }
209 
215  {
216  wsBinary = handler;
217  }
223  {
224  wsPong = handler;
225  }
231  {
232  wsDisconnect = handler;
233  }
234 
239  void activate();
240 
245  bool onConnected();
246 
252  {
253  return connection;
254  }
255 
261  void setConnection(HttpConnection* connection, bool isClientConnection = true);
262 
267  {
268  return state;
269  }
270 
271 protected:
272  // Static handlers for ws_parser
273  static int staticOnDataBegin(void* userData, ws_frame_type_t type);
274  static int staticOnDataPayload(void* userData, const char* at, size_t length);
275  static int staticOnDataEnd(void* userData);
276  static int staticOnControlBegin(void* userData, ws_frame_type_t type);
277  static int staticOnControlPayload(void* userData, const char*, size_t length);
278  static int staticOnControlEnd(void* userData);
279 
286  bool processFrame(TcpClient& client, char* at, int size);
287 
288 protected:
294 
295  void* userData = nullptr;
296 
298 
299 private:
300  ws_frame_type_t frameType = WS_FRAME_TEXT;
301  WsFrameInfo controlFrame;
302 
303  ws_parser_t parser;
304  static const ws_parser_callbacks_t parserSettings;
305 
306  static WebsocketList websocketList;
307 
308  HttpConnection* connection = nullptr;
309  bool isClientConnection;
310  bool activated = false;
311 };
312 
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:133
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:609
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:238
Definition: TcpClient.h:46
Vector class template.
Definition: WVector.h:32
Definition: WebsocketConnection.h:56
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:92
void * getUserData()
Retrieves user data attached.
Definition: WebsocketConnection.h:168
WsConnectionState getState()
Gets the state of the websocket connection.
Definition: WebsocketConnection.h:266
WebsocketDelegate wsPong
Definition: WebsocketConnection.h:292
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:291
static const WebsocketList & getActiveWebsockets()
Obtain the list of active websockets.
Definition: WebsocketConnection.h:187
WebsocketMessageDelegate wsMessage
Definition: WebsocketConnection.h:290
void setBinaryHandler(WebsocketBinaryDelegate handler)
Sets the callback handler to be called after a binary websocket message is received.
Definition: WebsocketConnection.h:214
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:121
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:289
void setPongHandler(WebsocketDelegate handler)
Sets the callback handler to be called when pong reply received.
Definition: WebsocketConnection.h:222
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:140
WsConnectionState state
Definition: WebsocketConnection.h:297
void setUserData(void *userData)
Attaches a user data to a websocket connection.
Definition: WebsocketConnection.h:159
void * userData
Definition: WebsocketConnection.h:295
void setDisconnectionHandler(WebsocketDelegate handler)
Sets the callback handler to be called before closing a websocket connection.
Definition: WebsocketConnection.h:230
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:205
virtual ~WebsocketConnection()
Definition: WebsocketConnection.h:65
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:293
bool operator==(const WebsocketConnection &rhs) const
Test if another connection refers to the same object.
Definition: WebsocketConnection.h:177
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:196
bool sendString(const String &message)
Sends a string websocket message.
Definition: WebsocketConnection.h:130
static int staticOnControlEnd(void *userData)
HttpConnection * getConnection()
Gets the underlying HTTP connection.
Definition: WebsocketConnection.h:251
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:43
@ eWSCS_Closed
Definition: WebsocketConnection.h:46
@ eWSCS_Open
Definition: WebsocketConnection.h:45
@ eWSCS_Ready
Definition: WebsocketConnection.h:44
Definition: WebsocketConnection.h:49
char * payload
Definition: WebsocketConnection.h:51
ws_frame_type_t type
Definition: WebsocketConnection.h:50
size_t payloadLength
Definition: WebsocketConnection.h:52