Buffer.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 #include <etk/types.hpp>
8 #include <enet/WebSocket.hpp>
9 #include <zeus/ParamType.hpp>
10 
11 namespace zeus {
12  class WebServer;
13  // define basic async call element ...
14  using ActionAsyncClient = std::function<bool(WebServer* _interface, const uint32_t& _serviceId, uint64_t _transactionId, uint64_t _part)>;
15 }
16 
17 
18 
19 namespace zeus {
20  class BufferData;
21  //U32 message lenght
22  #pragma pack(push,1)
23  /*
24  struct headerBin {
25  //uint16_t versionProtocol; // protocol Version (might be 1)
26  uint32_t transactionID;
27  uint32_t clientID; // same as sevice ID
28  int16_t partID; // if < 0 the partId ifs the last (start at 0 if multiple or 0x8000 if single message)
29  uint16_t typeMessage; //TypeMessgae (1:call, 2:Answer, 4:event)
30  uint16_t numberOfParameter;
31  };
32  */
33  struct headerBin {
34  //uint16_t versionProtocol; // protocol Version (might be 1)
35  uint32_t transactionID;
36  uint32_t clientID; // same as sevice ID
37  uint8_t flags; // List of flags & type message:
38  // - 0-2: Type of the message
39  // - 3-5: Reserved
40  // - 6: message data is zipped
41  // - 7: message finished
42  };
43  #pragma pack(pop)
44  /*
45  // not needed ==> can be deduced with parameter number ... U16 Offset String call Name (start of the buffer) end with \0
46  ======================
47  == call
48  ======================
49  U16 param count
50  U16[param count] parameters offset (first offset is the "callName" and limit size of the number of parameter
51  CALL Name (funtion name)
52  [param 1]
53  [param 2]
54  [param 3]
55  [param 4]
56  ======================
57  == Answer
58  ======================
59  U16 param count = 3
60  U16[param count]
61  [param 1] (return value)
62  [param 2] (error value)
63  [param 3] (error help)
64  [error] (constituated with 2 strings (error type and comment)
65  ======================
66  == event
67  ======================
68  U16[param count] parameters offset (first offset is the "callName" and limit size of the number of parameter
69  event Name
70  [param 1]
71  [param 2]
72  [param 3]
73  [param 4]
74  ---------------------------
75  parameter and return value is contituated like :
76  TYPE,DATAs....(in raw)
77  Type is write in ascii in the list end with '\0':
78  - void
79  - bool
80  - float
81  - double
82  - int64
83  - int32
84  - int16
85  - int8
86  - uint64
87  - uint32
88  - uint16
89  - uint8
90  - string
91  - vector:bool
92  - vector:float
93  - vector:double
94  - vector:int64
95  - vector:int32
96  - vector:int16
97  - vector:int8
98  - vector:uint64
99  - vector:uint32
100  - vector:uint16
101  - vector:uint8
102  - vector:string
103  - obj:file
104  */
105  #define ZEUS_BUFFER_FLAG_FINISH (0x80)
106  #define ZEUS_BUFFER_FLAG_TYPE_MESSAGE (0x07)
107 
110  class Buffer {
111  friend std::ostream& operator<<(std::ostream&, zeus::Buffer*);
112  protected:
116  Buffer();
117  public:
121  virtual ~Buffer() = default;
126  static ememory::SharedPtr<zeus::Buffer> create();
132  static ememory::SharedPtr<zeus::Buffer> create(const std::vector<uint8_t>& _buffer);
133  protected:
135  std::vector<zeus::ActionAsyncClient> m_multipleSend;
136  public:
141  bool haveAsync() const {
142  return m_multipleSend.size() != 0;
143  }
148  std::vector<zeus::ActionAsyncClient> moveAsync() {
149  return std::move(m_multipleSend);
150  }
155  void appendBuffer(ememory::SharedPtr<zeus::Buffer> _obj);
156  virtual void appendBufferData(ememory::SharedPtr<zeus::BufferData> _obj);
157  protected:
163  virtual void composeWith(const uint8_t* _buffer, uint32_t _lenght);
164  public:
168  void clear();
173  uint32_t getTransactionId() const;
178  void setTransactionId(uint32_t _value);
183  uint32_t getClientId() const;// this is the same as serviceId
188  void setClientId(uint32_t _value);
193  uint32_t getServiceId() const {
194  return getClientId();
195  }
200  void setServiceId(uint32_t _value) {
201  setClientId(_value);
202  }
207  bool getPartFinish() const;
212  void setPartFinish(bool _value);
216  enum class typeMessage {
217  unknow = 0x0000,
218  call = 0x0001,
219  answer = 0x0002,
220  data = 0x0003,
221  event = 0x0004,
222  };
227  virtual enum typeMessage getType() const;
228  public:
234  virtual bool writeOn(enet::WebSocket& _interface);
235  virtual void generateDisplay(std::ostream& _os) const ;
236  };
243  std::ostream& operator <<(std::ostream& _os, enum zeus::Buffer::typeMessage _value);
250  std::ostream& operator <<(std::ostream& _os, ememory::SharedPtr<zeus::Buffer> _obj);
251 
252  // internal:
258  void addType(std::vector<uint8_t>& _data, zeus::ParamType _type);
264  void addTypeObject(std::vector<uint8_t>& _data, const std::string _type);
269  void addTypeRaw(std::vector<uint8_t>& _data);
270 }
271 
std::vector< zeus::ActionAsyncClient > m_multipleSend
Async element to send data on the webinterface when too big ...
Definition: Buffer.hpp:135
void addType(std::vector< uint8_t > &_data, zeus::ParamType _type)
Add a parameter generic type in the buffer.
std::vector< zeus::ActionAsyncClient > moveAsync()
Get the list of async data to send.
Definition: Buffer.hpp:148
headerBin m_header
header of the protocol
Definition: Buffer.hpp:134
void clear()
void setServiceId(uint32_t _value)
Set the Service identifier of the packet (same as client)
Definition: Buffer.hpp:200
void addTypeRaw(std::vector< uint8_t > &_data)
Add a parameter RAW type in the buffer.
Protocol buffer to transmit datas.
Definition: Buffer.hpp:110
Main zeus library namespace.
Definition: AbstractFunction.hpp:15
bool haveAsync() const
Check if async element are present on this buffer.
Definition: Buffer.hpp:141
Definition: Buffer.hpp:33
void addTypeObject(std::vector< uint8_t > &_data, const std::string _type)
Add a parameter object type in the buffer.
generisation of type of the type of the parameter
Definition: ParamType.hpp:14
typeMessage
Type of the massage send or receive.
Definition: Buffer.hpp:216
uint32_t getServiceId() const
Get the Service identifier of the packet (same as client)
Definition: Buffer.hpp:193