My Project
 All Classes Functions Variables Pages
WebSocket.h
1 /*
2  * WebSocket.h
3  *
4  * Created on: Sep 2, 2017
5  * Author: kolban
6  */
7 
8 #ifndef COMPONENTS_WEBSOCKET_H_
9 #define COMPONENTS_WEBSOCKET_H_
10 #include <string>
11 #include "Socket.h"
12 
13 #undef close
14 #undef send
15 class WebSocketReader;
16 class WebSocket;
17 
18 // +-------------------------------+
19 // | WebSocketInputStreambuf |
20 // +-------------------------------+
21 class WebSocketInputStreambuf : public std::streambuf {
22 public:
24  Socket socket,
25  size_t dataLength,
26  uint8_t* pMask = nullptr,
27  size_t bufferSize = 2048);
29  int_type underflow();
30  void discard();
31  size_t getRecordSize();
32 
33 private:
34  char* m_buffer;
35  Socket m_socket;
36  size_t m_dataLength;
37  size_t m_bufferSize;
38  size_t m_sizeRead;
39  uint8_t* m_pMask;
40 
41 };
42 
43 
44 // +------------------+
45 // | WebSocketHandler |
46 // +------------------+
48 public:
49  virtual ~WebSocketHandler();
50  virtual void onClose();
51  virtual void onMessage(WebSocketInputStreambuf* pWebSocketInputStreambuf, WebSocket* pWebSocket);
52  virtual void onError(std::string error);
53 
54 };
55 
56 
57 // +-----------+
58 // | WebSocket |
59 // +-----------+
60 class WebSocket {
61 public:
62  static const uint16_t CLOSE_NORMAL_CLOSURE = 1000;
63  static const uint16_t CLOSE_GOING_AWAY = 1001;
64  static const uint16_t CLOSE_PROTOCOL_ERROR = 1002;
65  static const uint16_t CLOSE_CANNOT_ACCEPT = 1003;
66  static const uint16_t CLOSE_NO_STATUS_CODE = 1005;
67  static const uint16_t CLOSE_CLOSED_ABNORMALLY = 1006;
68  static const uint16_t CLOSE_NOT_CONSISTENT = 1007;
69  static const uint16_t CLOSE_VIOLATED_POLICY = 1008;
70  static const uint16_t CLOSE_TOO_BIG = 1009;
71  static const uint16_t CLOSE_NO_EXTENSION = 1010;
72  static const uint16_t CLOSE_UNEXPECTED_CONDITION = 1011;
73  static const uint16_t CLOSE_SERVICE_RESTART = 1012;
74  static const uint16_t CLOSE_TRY_AGAIN_LATER = 1013;
75  static const uint16_t CLOSE_TLS_HANDSHAKE_FAILURE = 1015;
76 
77  static const uint8_t SEND_TYPE_BINARY = 0x01;
78  static const uint8_t SEND_TYPE_TEXT = 0x02;
79 
80  WebSocket(Socket socket);
81  virtual ~WebSocket();
82 
83  void close(uint16_t status = CLOSE_NORMAL_CLOSURE, std::string message = "");
85  Socket getSocket();
86  void send(std::string data, uint8_t sendType = SEND_TYPE_BINARY);
87  void send(uint8_t* data, uint16_t length, uint8_t sendType = SEND_TYPE_BINARY);
88  void setHandler(WebSocketHandler *handler);
89 
90 private:
91  friend class WebSocketReader;
92  friend class HttpServerTask;
93  void startReader();
94  bool m_receivedClose; // True when we have received a close request.
95  bool m_sentClose; // True when we have sent a close request.
96  Socket m_socket; // Partner socket.
97  WebSocketHandler* m_pWebSocketHandler;
98  WebSocketReader* m_pWebSockerReader;
99 
100 }; // WebSocket
101 
102 #endif /* COMPONENTS_WEBSOCKET_H_ */
Encapsulate a socket.
Definition: Socket.h:62
~WebSocketInputStreambuf()
Destructor.
Definition: WebSocket.cpp:423
int_type underflow()
Handle the request to read data from the stream but we need more data from the source.
Definition: WebSocket.cpp:465
A task that will watch web socket inputs.
Definition: WebSocket.cpp:96
virtual void onError(std::string error)
The default onError handler. If no over-riding handler is provided for the "error" event...
Definition: WebSocket.cpp:230
Socket getSocket()
Get the underlying socket for the websocket.
Definition: WebSocket.cpp:312
virtual void onMessage(WebSocketInputStreambuf *pWebSocketInputStreambuf, WebSocket *pWebSocket)
The default onData handler. If no over-riding handler is provided for the "message" event...
Definition: WebSocket.cpp:220
Definition: WebSocket.h:21
WebSocketInputStreambuf(Socket socket, size_t dataLength, uint8_t *pMask=nullptr, size_t bufferSize=2048)
Create a Web Socket input record streambuf.
Definition: WebSocket.cpp:404
WebSocket(Socket socket)
Construct a WebSocket instance.
Definition: WebSocket.cpp:239
virtual void onClose()
The default onClose handler. If no over-riding handler is provided for the "close" event...
Definition: WebSocket.cpp:204
Definition: WebSocket.h:60
virtual ~WebSocketHandler()
Destructor.
Definition: WebSocket.cpp:511
size_t getRecordSize()
Get the size of the expected record.
Definition: WebSocket.cpp:456
Definition: WebSocket.h:47
Be an HTTP server task. Here we define a Task that will be run when the HTTP server starts...
Definition: HttpServer.cpp:53
void setHandler(WebSocketHandler *handler)
Set the Web socket handler associated with this Websocket.
Definition: WebSocket.cpp:382
void discard()
Discard data for the record that has not yet been read.
Definition: WebSocket.cpp:441
virtual ~WebSocket()
Destructor.
Definition: WebSocket.cpp:251
void close(uint16_t status=CLOSE_NORMAL_CLOSURE, std::string message="")
Close the Web socket.
Definition: WebSocket.cpp:262
WebSocketHandler * getHandler()
Get the current WebSocketHandler A web socket handler is a user registered class instance that is cal...
Definition: WebSocket.cpp:303
void send(std::string data, uint8_t sendType=SEND_TYPE_BINARY)
Send data down the web socket See the WebSocket spec (RFC6455) section "6.1 Sending Data"...
Definition: WebSocket.cpp:324