libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
WebSocket.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef WEBSOCKET_H_
32 #define WEBSOCKET_H_
33 
34 #include "../Exception.h"
35 
36 #include <memory>
37 #include <vector>
38 
39 namespace BaseLib
40 {
42 {
43 public:
44  WebSocketException(std::string message) : BaseLib::Exception(message) {}
45 };
46 
47 class WebSocket
48 {
49 public:
50  struct Header
51  {
52  struct Opcode
53  {
54  enum Enum { continuation, text, binary, reserved1, reserved2, reserved3, reserved4, reserved5, close, ping, pong, reserved6, reserved7, reserved8, reserved9, reserved10 };
55  };
56  bool close = false;
57  bool parsed = false;
58  uint64_t length = 0;
59  bool fin = false;
60  bool rsv1 = false;
61  bool rsv2 = false;
62  bool rsv3 = false;
63  Opcode::Enum opcode = Opcode::close;
64  bool hasMask = false;
65  std::vector<char> maskingKey;
66  };
67 
68  WebSocket();
69  virtual ~WebSocket() {}
70 
71  bool isFinished() { return _finished; }
72 
79  void setFinished();
80 
81  std::vector<char>& getContent() { return _content; }
82  uint32_t getContentSize() { return _content.size(); }
83  Header& getHeader() { return _header; }
84  void reset();
85 
94  uint32_t process(char* buffer, int32_t bufferLength);
95 
96  bool dataProcessingStarted() { return _dataProcessingStarted; }
97 
105  static void encode(const std::vector<char>& data, Header::Opcode::Enum messageType, std::vector<char>& output);
106 
112  static void encodeClose(std::vector<char>& output);
113 private:
114  Header _header;
115  std::vector<char> _content;
116  uint32_t _oldContentSize = 0;
117  bool _finished = false;
118  bool _dataProcessingStarted = false;
119  std::vector<char> _rawHeader;
120 
121  uint32_t processHeader(char** buffer, int32_t& bufferLength);
122  uint32_t processContent(char* buffer, int32_t bufferLength);
123  void applyMask();
124 
125 };
126 }
127 #endif
bool isFinished()
Definition: WebSocket.h:71
Definition: BaseLib.cpp:34
virtual ~WebSocket()
Definition: WebSocket.h:69
WebSocketException(std::string message)
Definition: WebSocket.h:44
std::vector< char > maskingKey
Definition: WebSocket.h:65
std::vector< char > & getContent()
Definition: WebSocket.h:81
Header & getHeader()
Definition: WebSocket.h:83
Definition: WebSocket.h:52
Definition: WebSocket.h:41
Definition: WebSocket.h:50
Definition: WebSocket.h:47
bool dataProcessingStarted()
Definition: WebSocket.h:96
uint32_t getContentSize()
Definition: WebSocket.h:82
Enum
Definition: WebSocket.h:54
Base class for all exceptions defined in Homegear.
Definition: Exception.h:41