xbmc
WebSocket.h
1 /*
2  * Copyright (C) 2011-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <stdint.h>
12 #include <string>
13 #include <vector>
14 
15 enum WebSocketFrameOpcode
16 {
17  WebSocketContinuationFrame = 0x00,
18  WebSocketTextFrame = 0x01,
19  WebSocketBinaryFrame = 0x02,
20  //0x3 - 0x7 are reserved for non-control frames
21  WebSocketConnectionClose = 0x08,
22  WebSocketPing = 0x09,
23  WebSocketPong = 0x0A,
24  //0xB - 0xF are reserved for control frames
25  WebSocketUnknownFrame = 0x10
26 };
27 
28 enum WebSocketState
29 {
30  WebSocketStateNotConnected = 0,
31  WebSocketStateHandshaking = 1,
32  WebSocketStateConnected = 2,
33  WebSocketStateClosing = 3,
34  WebSocketStateClosed = 4
35 };
36 
37 enum WebSocketCloseReason
38 {
39  WebSocketCloseNormal = 1000,
40  WebSocketCloseLeaving = 1001,
41  WebSocketCloseProtocolError = 1002,
42  WebSocketCloseInvalidData = 1003,
43  WebSocketCloseFrameTooLarge = 1004,
44  // Reserved status code = 1005,
45  // Reserved status code = 1006,
46  WebSocketCloseInvalidUtf8 = 1007
47 };
48 
50 {
51 public:
52  CWebSocketFrame(const char* data, uint64_t length);
53  CWebSocketFrame(WebSocketFrameOpcode opcode, const char* data = NULL, uint32_t length = 0, bool final = true, bool masked = false, int32_t mask = 0, int8_t extension = 0);
54  virtual ~CWebSocketFrame();
55 
56  virtual bool IsValid() const { return m_valid; }
57  virtual uint64_t GetFrameLength() const { return m_lengthFrame; }
58  virtual bool IsFinal() const { return m_final; }
59  virtual int8_t GetExtension() const { return m_extension; }
60  virtual WebSocketFrameOpcode GetOpcode() const { return m_opcode; }
61  virtual bool IsControlFrame() const { return (m_valid && (m_opcode & 0x8) == 0x8); }
62  virtual bool IsMasked() const { return m_masked; }
63  virtual uint64_t GetLength() const { return m_length; }
64  virtual int32_t GetMask() const { return m_mask; }
65  virtual const char* GetFrameData() const { return m_data; }
66  virtual const char* GetApplicationData() const { return m_applicationData; }
67 
68 protected:
69  bool m_free;
70  const char *m_data;
71  uint64_t m_lengthFrame;
72  uint64_t m_length;
73  bool m_valid;
74  bool m_final;
75  int8_t m_extension;
76  WebSocketFrameOpcode m_opcode;
77  bool m_masked;
78  int32_t m_mask;
79  char *m_applicationData;
80 
81 private:
82  void reset();
83  CWebSocketFrame(const CWebSocketFrame&) = delete;
84  CWebSocketFrame& operator=(const CWebSocketFrame&) = delete;
85 };
86 
88 {
89 public:
91  virtual ~CWebSocketMessage();
92 
93  virtual bool IsFragmented() const { return m_fragmented; }
94  virtual bool IsComplete() const { return m_complete; }
95 
96  virtual bool AddFrame(const CWebSocketFrame* frame);
97  virtual const std::vector<const CWebSocketFrame *>& GetFrames() const { return m_frames; }
98 
99  virtual void Clear();
100 
101 protected:
102  std::vector<const CWebSocketFrame *> m_frames;
103  bool m_fragmented;
104  bool m_complete;
105 };
106 
108 {
109 public:
110  CWebSocket() { m_state = WebSocketStateNotConnected; m_message = NULL; }
111  virtual ~CWebSocket()
112  {
113  if (m_message)
114  delete m_message;
115  }
116 
117  int GetVersion() { return m_version; }
118  WebSocketState GetState() { return m_state; }
119 
120  virtual bool Handshake(const char* data, size_t length, std::string &response) = 0;
121  virtual const CWebSocketMessage* Handle(const char* &buffer, size_t &length, bool &send);
122  virtual const CWebSocketMessage* Send(WebSocketFrameOpcode opcode, const char* data = NULL, uint32_t length = 0);
123  virtual const CWebSocketFrame* Ping(const char* data = NULL) const = 0;
124  virtual const CWebSocketFrame* Pong(const char* data = NULL) const = 0;
125  virtual const CWebSocketFrame* Close(WebSocketCloseReason reason = WebSocketCloseNormal, const std::string &message = "") = 0;
126  virtual void Fail() = 0;
127 
128 protected:
129  int m_version;
130  WebSocketState m_state;
131  CWebSocketMessage *m_message;
132 
133  virtual CWebSocketFrame* GetFrame(const char* data, uint64_t length) = 0;
134  virtual CWebSocketFrame* GetFrame(WebSocketFrameOpcode opcode, const char* data = NULL, uint32_t length = 0, bool final = true, bool masked = false, int32_t mask = 0, int8_t extension = 0) = 0;
135  virtual CWebSocketMessage* GetMessage() = 0;
136 };
Definition: WebSocket.h:107
Definition: WebSocket.h:87
Definition: WebSocket.h:49