WebSocket.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <enet/Http.hpp>
9 #include <ememory/memory.hpp>
10 #include <vector>
11 #include <map>
12 
13 namespace enet {
14  class WebSocket {
15  protected:
16  std::vector<uint8_t> m_sendBuffer;
17  bool m_connectionValidate;
19  std::vector<uint8_t> m_buffer;
20  std::string m_checkKey;
21  std::chrono::steady_clock::time_point m_lastReceive;
22  std::chrono::steady_clock::time_point m_lastSend;
23  public:
24  const std::chrono::steady_clock::time_point& getLastTimeReceive() {
25  return m_lastReceive;
26  }
27  const std::chrono::steady_clock::time_point& getLastTimeSend() {
28  return m_lastSend;
29  }
30  public:
31  WebSocket();
32  WebSocket(enet::Tcp _connection, bool _isServer=false);
33  void setInterface(enet::Tcp _connection, bool _isServer=false);
34  virtual ~WebSocket();
35  void start(const std::string& _uri="", const std::vector<std::string>& _listProtocols=std::vector<std::string>());
36  void stop(bool _inThread=false);
37  bool isAlive() const {
38  if (m_interface == nullptr) {
39  return false;
40  }
41  return m_interface->isAlive();
42  }
43  void onReceiveData(enet::Tcp& _data);
44  void onReceiveRequest(const enet::HttpRequest& _data);
45  void onReceiveAnswer(const enet::HttpAnswer& _data);
46  protected:
47  std::string m_protocol;
48  public:
49  void setProtocol(const std::string& _protocol) {
50  m_protocol = _protocol;
51  }
52  public:
53  using Observer = std::function<void(std::vector<uint8_t>&, bool)>;
54  protected:
55  Observer m_observer;
56  public:
63  template<class CLASS_TYPE>
64  void connect(CLASS_TYPE* _class, void (CLASS_TYPE::*_func)(std::vector<uint8_t>&, bool)) {
65  m_observer = [=](std::vector<uint8_t>& _value, bool _isString){
66  (*_class.*_func)(_value, _isString);
67  };
68  }
69  void connect(Observer _func) {
70  m_observer = _func;
71  }
72  // Only server:
73  public:
74  using ObserverUriCheck = std::function<bool(const std::string&, const std::vector<std::string>&)>;
75  protected:
76  ObserverUriCheck m_observerUriCheck;
77  public:
84  template<class CLASS_TYPE>
85  void connectUri(CLASS_TYPE* _class, bool (CLASS_TYPE::*_func)(const std::string&, const std::vector<std::string>&)) {
86  m_observerUriCheck = [=](const std::string& _value, const std::vector<std::string>& _protocols){
87  return (*_class.*_func)(_value, _protocols);
88  };
89  }
90  void connectUri(ObserverUriCheck _func) {
91  m_observerUriCheck = _func;
92  }
93  private:
94  bool m_isString;
95  bool m_haveMask;
96  uint8_t m_dataMask[4];
97  public:
98  bool configHeader(bool _isString=false, bool _mask= false);
99  int32_t writeData(uint8_t* _data, int32_t _len);
100  int32_t send();
108  //TODO : ...
109  int32_t write(const void* _data, int32_t _len, bool _isString=false, bool _mask= false);
117  int32_t write(const std::string& _data, bool _writeBackSlashZero = true) {
118  if (_data.size() == 0) {
119  return 0;
120  }
121  if (_writeBackSlashZero == true) {
122  return write(_data.c_str(), _data.size()+1, true);
123  }
124  return write(_data.c_str(), _data.size(), true);
125  }
133  template <class T>
134  int32_t write(const std::vector<T>& _data) {
135  if (_data.size() == 0) {
136  return 0;
137  }
138  size_t ret = write(&_data[0], _data.size()*sizeof(T));
139  if (ret <=0) {
140  return ret;
141  }
142  return ret/sizeof(T);
143  }
144  public:
145  void controlPing();
146  void controlPong();
147  void controlClose();
148  };
149 }
Definition: Http.hpp:137
int32_t write(const std::string &_data, bool _writeBackSlashZero=true)
Write a chunk of data on the socket.
Definition: WebSocket.hpp:117
std::function< bool(const std::string &, const std::vector< std::string > &)> ObserverUriCheck
Define an Observer: function pointer.
Definition: WebSocket.hpp:74
std::function< void(std::vector< uint8_t > &, bool)> Observer
Define an Observer: function pointer.
Definition: WebSocket.hpp:53
void connectUri(CLASS_TYPE *_class, bool(CLASS_TYPE::*_func)(const std::string &, const std::vector< std::string > &))
Connect an function member on the signal with the shared_ptr object.
Definition: WebSocket.hpp:85
int32_t write(const std::vector< T > &_data)
Write a chunk of data on the socket.
Definition: WebSocket.hpp:134
Definition: WebSocket.hpp:14
void connect(CLASS_TYPE *_class, void(CLASS_TYPE::*_func)(std::vector< uint8_t > &, bool))
Connect an function member on the signal with the shared_ptr object.
Definition: WebSocket.hpp:64
int32_t write(const void *_data, int32_t _len, bool _isString=false, bool _mask=false)
Write a chunk of data on the socket.
Definition: Tcp.hpp:16
Definition: Http.hpp:166
Main esvg namespace.
Definition: enet.hpp:17