Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
WebsocketFrame.h
1 /*
2  * WebsocketFrame.h
3  *
4  * Created on: 13 nov. 2016 г.
5  * Author: https://github.com/avr39-ripe - Alexander V, Ribchansky
6  *
7  * Some parts of code inspired by: https://github.com/Links2004/arduinoWebSockets and
8  * https://github.com/m8rge/cwebsocket
9  */
10 
15 #ifndef SMINGCORE_NETWORK_WEBSOCKETFRAME_H_
16 #define SMINGCORE_NETWORK_WEBSOCKETFRAME_H_
17 #include "../Wiring/WiringFrameworkIncludes.h"
18 
21 enum class WSFrameType : uint8_t
22 {
23  continuation = 0x00, // continuation frame, UNSUPPORTED BY NOW!
24  text = 0x01, // text frame
25  binary = 0x02, // binary frame
26  // %x3-7 are reserved for further non-control frames
27  close = 0x08, // connection close frame
28  ping = 0x09, // ping frame
29  pong = 0x0A, // pong frame
30  empty = 0xF0, // Empty frame, length == 0. Not from RFC6455! For internal class usage!
31  error = 0xF1, // Error while farsing frame. Not from RFC6455! For internal class usage!
32  incomplete = 0xF2 // Incomplete, inconsistent frame. Not from RFC6455! For internal class usage!
34 };
35 
36 namespace WSFlags
37 {
38  static const uint8_t payloadDeleteMemBit = 1u; //Delete memory reserved for payload in destructor
39  static const uint8_t headerDeleteMemBit = 2u; //Delete memory reserved for header in destructor
40 };
41 
42 // Declare classes where WebsocketFrameClass can be used they will have access to _payload and _header members
43 class HttpServer;
44 class Websocket;
45 class WebsocketClient;
46 
50 {
51  friend class HttpServer;
52  friend class Websocket;
53  friend class WebsocketClient;
54 public:
56  virtual ~WebsocketFrameClass();
57  uint8_t encodeFrame(WSFrameType frameType, uint8_t * payload, size_t length, uint8_t mask, uint8_t fin, uint8_t headerToPayload = true);
71  uint8_t decodeFrame(uint8_t * buffer, size_t length);
80 protected:
81  uint8_t* _payload = nullptr; // pointer to payload; in encode - will point to proper websocket frame payload, in decode - will point to websocket frame's decoded data
82  size_t _payloadLength = 0;
83  uint8_t* _header = nullptr; // pointer to header; in encode - will point to websocket frame header
84  size_t _headerLength = 0;
85  WSFrameType _frameType = WSFrameType::empty;
86  uint8_t _mask = 0;
87  uint8_t _getFrameSizes(uint8_t* buffer, size_t length); // used to get frame size from websocket buffer
88 private:
89  uint8_t _flags = 0; //Store flags for further freeing memory
90  size_t _nextReadOffset = 0; //Store offset in multiframe tcp buffer for next decodeFrame
91 };
93 #endif /* SMINGCORE_NETWORK_WEBSOCKETFRAME_H_ */
Definition: WebsocketFrame.h:36
xB-F are reserved for further control frame
WSFrameType
Websocket frame types enum.
Definition: WebsocketFrame.h:21
Websocket Frame.
Definition: WebsocketFrame.h:49
Definition: HttpServer.h:30
Delegate callback type for connection of websocket client.
Definition: WebsocketClient.h:53