WebServer.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 #include <zeus/Buffer.hpp>
8 #include <zeus/BufferAnswer.hpp>
9 #include <enet/WebSocket.hpp>
10 #include <thread>
11 #include <ememory/memory.hpp>
13 #include <zeus/FutureBase.hpp>
14 
15 namespace zeus {
21  ememory::SharedPtr<zeus::BufferCall> createBaseCall(uint64_t _transactionId, const std::string& _functionName, const uint32_t& _serviceId=0);
27  void createParam(int32_t _paramId,
29 
35  template<class ZEUS_TYPE, class... _ARGS>
36  void createParam(int32_t _paramId,
38  const ZEUS_TYPE& _param,
39  _ARGS&&... _args) {
40  _obj->addParameter<ZEUS_TYPE>(_param);
41  _paramId++;
42  createParam(_paramId, _obj, std::forward<_ARGS>(_args)...);
43  }
49  // convert const char in std::string ...
50  template<class... _ARGS>
51  void createParam(int32_t _paramId,
53  const char* _param,
54  _ARGS&&... _args) {
55  createParam(_paramId, _obj, std::string(_param), std::forward<_ARGS>(_args)...);
56  }
57 
63  template<class... _ARGS>
64  ememory::SharedPtr<zeus::BufferCall> createCall(uint64_t _transactionId, const std::string& _functionName, _ARGS&&... _args) {
65  ememory::SharedPtr<zeus::BufferCall> callElem = createBaseCall(_transactionId, _functionName);
66  if (callElem == nullptr) {
67  return nullptr;
68  }
69  createParam(0, callElem, std::forward<_ARGS>(_args)...);
70  return callElem;
71  }
77  template<class... _ARGS>
78  ememory::SharedPtr<zeus::BufferCall> createCallService(uint64_t _transactionId, const uint32_t& _serviceName, const std::string& _functionName, _ARGS&&... _args) {
79  ememory::SharedPtr<zeus::BufferCall> callElem = createBaseCall(_transactionId, _functionName, _serviceName);
80  if (callElem == nullptr) {
81  return nullptr;
82  }
83  createParam(0, callElem, std::forward<_ARGS>(_args)...);
84  return callElem;
85  }
86 
90  class WebServer {
91  private:
92  enet::WebSocket m_connection;
93  uint16_t m_transmissionId;
94  uint16_t getId() {
95  return m_transmissionId++;
96  }
97  std::mutex m_pendingCallMutex;
98  std::vector<std::pair<uint64_t, zeus::FutureBase>> m_pendingCall;
99  public:
100  using Observer = std::function<void(ememory::SharedPtr<zeus::Buffer>)>;
101  Observer m_observerElement;
107  template<class CLASS_TYPE>
108  void connect(CLASS_TYPE* _class, void (CLASS_TYPE::*_func)(ememory::SharedPtr<zeus::Buffer>)) {
109  m_observerElement = [=](ememory::SharedPtr<zeus::Buffer> _value){
110  (*_class.*_func)(_value);
111  };
112  }
113  public:
119  WebServer();
125  WebServer(enet::Tcp _connection, bool _isServer);
131  virtual ~WebServer();
137  void setInterface(enet::Tcp _connection, bool _isServer);
143  void connect(bool _async = false);
149  void disconnect(bool _inThreadStop = false);
155  bool isActive() const;
161  void setInterfaceName(const std::string& _name);
173  void ping();
179  bool onReceiveUri(const std::string& _uri, const std::vector<std::string>& _protocols);
185  void onReceiveData(std::vector<uint8_t>& _frame, bool _isBinary);
192  public:
198  const std::chrono::steady_clock::time_point& getLastTimeReceive() {
199  return m_connection.getLastTimeReceive();
200  }
206  const std::chrono::steady_clock::time_point& getLastTimeSend() {
207  return m_connection.getLastTimeSend();
208  }
209  private:
210  using ActionAsync = std::function<bool(WebServer* _interface)>;
211  std::mutex m_threadAsyncMutex;
212  std::thread* m_threadAsync;
213  bool m_threadAsyncRunning;
214  std::vector<ActionAsync> m_threadAsyncList;
215  std::vector<ActionAsync> m_threadAsyncList2;
216  private:
222  void threadAsyncCallback();
223  public:
229  void addAsync(ActionAsync _elem);
230  private:
236  zeus::FutureBase callBinary(uint64_t _transactionId,
238  zeus::FutureData::ObserverFinish _callback=nullptr,
239  const uint32_t& _service=0);
240  public: // section call direct
246  template<class... _ARGS>
247  zeus::FutureBase call(const std::string& _functionName, _ARGS&&... _args) {
248  uint16_t id = getId();
249  ememory::SharedPtr<zeus::BufferCall> callElem = zeus::createCall(id, _functionName, std::forward<_ARGS>(_args)...);
250  return callBinary(id, callElem);
251  }
257  template<class... _ARGS>
258  zeus::FutureBase callAction(const std::string& _functionName, _ARGS&&... _args, zeus::FutureData::ObserverFinish _callback) {
259  uint16_t id = getId();
260  ememory::SharedPtr<zeus::BufferCall> callElem = zeus::createCall(id, _functionName, std::forward<_ARGS>(_args)...);
261  return callBinary(id, callElem, _callback);
262  }
263  public: // section call with service ID / Client ID
264 
270  template<class... _ARGS>
271  zeus::FutureBase callService(uint32_t _serviceId, const std::string& _functionName, _ARGS&&... _args) {
272  uint16_t id = getId();
273  ememory::SharedPtr<zeus::BufferCall> callElem = zeus::createCallService(id, _serviceId, _functionName, std::forward<_ARGS>(_args)...);
274  return callBinary(id, callElem);
275  }
281  template<class... _ARGS>
282  zeus::FutureBase callServiceAction(uint32_t _serviceId, const std::string& _functionName, _ARGS&&... _args, zeus::FutureData::ObserverFinish _callback) {
283  uint16_t id = getId();
284  ememory::SharedPtr<zeus::BufferCall> callElem = zeus::createCallService(id, _serviceId, _functionName, std::forward<_ARGS>(_args)...);
285  return callBinary(id, callElem, _callback);
286  }
292  template<class... _ARGS>
293  zeus::FutureBase callClient(uint32_t _clientId,
294  const std::string& _functionName,
295  _ARGS&&... _args) {
296  return callService(_clientId, _functionName, _args...);
297  }
303  template<class... _ARGS>
304  zeus::FutureBase callClientAction(uint32_t _clientId,
305  const std::string& _functionName,
306  _ARGS&&... _args,
308  return callServiceAction(_clientId, _functionName, _args..., _callback);
309  }
315  zeus::FutureBase callForward(uint32_t _clientId,
317  uint64_t _singleReferenceId,
324  void callForwardMultiple(uint32_t _clientId,
326  uint64_t _singleReferenceId);
327  public: // answers ...
334  void answerProtocolError(uint32_t _transactionId, const std::string& _errorHelp);
340  template<class ZEUS_ARG>
341  void answerValue(uint64_t _clientTransactionId, ZEUS_ARG _value, uint32_t _clientId=0) {
343  answer->setTransactionId(_clientTransactionId);
344  answer->setClientId(_clientId);
345  answer->addAnswer(_value);
346  writeBinary(answer);
347  }
353  void answerVoid(uint64_t _clientTransactionId, uint32_t _clientId=0);
359  void answerError(uint64_t _clientTransactionId, const std::string& _errorValue, const std::string& _errorComment="", uint32_t _clientId=0);
360  };
361 }
362 
std::function< bool(zeus::FutureBase)> ObserverFinish
Define an Observer: function pointer.
Definition: FutureData.hpp:21
ememory::SharedPtr< zeus::BufferCall > createCallService(uint64_t _transactionId, const uint32_t &_serviceName, const std::string &_functionName, _ARGS &&... _args)
Definition: WebServer.hpp:78
void addAsync(ActionAsync _elem)
Definition: WebServer.hpp:90
void onReceiveData(std::vector< uint8_t > &_frame, bool _isBinary)
zeus::FutureBase callAction(const std::string &_functionName, _ARGS &&... _args, zeus::FutureData::ObserverFinish _callback)
Definition: WebServer.hpp:258
zeus::FutureBase callService(uint32_t _serviceId, const std::string &_functionName, _ARGS &&... _args)
Definition: WebServer.hpp:271
void addAnswer(const ZEUS_TYPE_DATA &_value)
set the answer of the call
Definition: BufferAnswer.hpp:46
static ememory::SharedPtr< zeus::BufferAnswer > create()
Create a shared pointer on the BufferAnswer.
ememory::SharedPtr< zeus::BufferCall > createBaseCall(uint64_t _transactionId, const std::string &_functionName, const uint32_t &_serviceId=0)
Generic zeus Future interface to get data asynchronously.
Definition: FutureBase.hpp:14
void addParameter()
Add parameter (or not.. this is the end of an auto resursive add parameter)
void setInterface(enet::Tcp _connection, bool _isServer)
zeus::FutureBase call(const std::string &_functionName, _ARGS &&... _args)
Definition: WebServer.hpp:247
void disconnect(bool _inThreadStop=false)
bool onReceiveUri(const std::string &_uri, const std::vector< std::string > &_protocols)
std::function< void(ememory::SharedPtr< zeus::Buffer >)> Observer
Define an Observer: function pointer.
Definition: WebServer.hpp:100
zeus::FutureBase callClientAction(uint32_t _clientId, const std::string &_functionName, _ARGS &&... _args, zeus::FutureData::ObserverFinish _callback)
Definition: WebServer.hpp:304
void createParam(int32_t _paramId, ememory::SharedPtr< zeus::BufferCall > _obj)
zeus::FutureBase callClient(uint32_t _clientId, const std::string &_functionName, _ARGS &&... _args)
Definition: WebServer.hpp:293
zeus::FutureBase callForward(uint32_t _clientId, ememory::SharedPtr< zeus::Buffer > _Buffer, uint64_t _singleReferenceId, zeus::FutureData::ObserverFinish _callback)
void answerError(uint64_t _clientTransactionId, const std::string &_errorValue, const std::string &_errorComment="", uint32_t _clientId=0)
void answerVoid(uint64_t _clientTransactionId, uint32_t _clientId=0)
void setInterfaceName(const std::string &_name)
virtual ~WebServer()
ememory::SharedPtr< zeus::BufferCall > createCall(uint64_t _transactionId, const std::string &_functionName, _ARGS &&... _args)
Definition: WebServer.hpp:64
int32_t writeBinary(ememory::SharedPtr< zeus::Buffer > _data)
void connect(CLASS_TYPE *_class, void(CLASS_TYPE::*_func)(ememory::SharedPtr< zeus::Buffer >))
Connect an function member on the signal with the shared_ptr object.
Definition: WebServer.hpp:108
Main zeus library namespace.
Definition: AbstractFunction.hpp:15
void setClientId(uint32_t _value)
Set the Client identifier of the packet.
const std::chrono::steady_clock::time_point & getLastTimeSend()
Definition: WebServer.hpp:206
const std::chrono::steady_clock::time_point & getLastTimeReceive()
Definition: WebServer.hpp:198
void setTransactionId(uint32_t _value)
Set the transaction identifier of the packet.
void answerValue(uint64_t _clientTransactionId, ZEUS_ARG _value, uint32_t _clientId=0)
Definition: WebServer.hpp:341
void answerProtocolError(uint32_t _transactionId, const std::string &_errorHelp)
zeus::FutureBase callServiceAction(uint32_t _serviceId, const std::string &_functionName, _ARGS &&... _args, zeus::FutureData::ObserverFinish _callback)
Definition: WebServer.hpp:282
void callForwardMultiple(uint32_t _clientId, ememory::SharedPtr< zeus::Buffer > _Buffer, uint64_t _singleReferenceId)
void newBuffer(ememory::SharedPtr< zeus::Buffer > _buffer)
bool isActive() const