ComSDK
 Указатель Классы Пространства имен Функции Переменные Определения типов Перечисления Элементы перечислений Друзья Группы Страницы
node_protocol.h
1 //========================================================================
2 #ifndef comfrm_cls_NodeProtocolH
3 #define comfrm_cls_NodeProtocolH
4 //========================================================================
5 #include "anymap.h"
6 #include "streamtools.h"
7 
8 #include <string>
9 
10 #define MAX_APP_SID_SIZE 100
11 //========================================================================
12 namespace com {
13  namespace interproc {
14  #pragma pack(push, 1)
16  {
17  char m_version;
18  char m_messageType;
19  unsigned int m_bodySize;
20  };
21  #pragma pack(pop)
22 
24  {
26  : m_appSid("")
27  {}
28 
29  NodeHandshake( const std::string& p_appSid )
30  : m_appSid(p_appSid)
31  {}
32 
33  friend std::ostream& operator<<(std::ostream& o_out, const NodeHandshake& p_reg);
34  friend std::istream& operator>>(std::istream& o_in, NodeHandshake& o_reg);
35 
36  std::string m_appSid;
37  };
38 
39  // TODO: по сути это набор функций, оформленных в класс. Нужно что-то сделать с этим
40  // Как вариант, все засунуть в namespace com::interproc::protocol
42  {
43  // TODO: C++11 specific
44  enum ProtocolVersion : char
45  {
46  pv01 = 0x01
47  };
48 
49  public:
50  enum MessageType : char
51  {
52  mtHandshake,
53  mtAnyMap,
54  };
55 
56  NodeProtocol();
57  ~NodeProtocol();
58 
59  bool readHeader(std::vector< char >& p_bytearray, NodeProtocolHeader& o_header);
60 
61  template< typename T >
62  size_t read(std::vector< char >& p_bytearray, std::string& o_senderAppSid, std::string& o_receiverAppSid, T& o_data) // возаращет кол-во прочитанных байт
63  {
64  VectorStream vs(p_bytearray);
65  NodeProtocolHeader header;
66  vs.read((char*) &header, sizeof(header));
67  if (header.m_messageType != getTypeByData< T >())
68  return 0;
69 
70  // Вычитываем sender app sid
71  int senderAppSidSize;
72  vs.read((char*) &senderAppSidSize, sizeof(senderAppSidSize));
73  char* senderAppSidBuf = new char[senderAppSidSize];
74  vs.read(senderAppSidBuf, senderAppSidSize);
75  o_senderAppSid = std::string(senderAppSidBuf, senderAppSidSize);
76  delete[] senderAppSidBuf;
77 
78  // Вычитываем receiver app sid
79  int receiverAppSidSize;
80  vs.read((char*) &receiverAppSidSize, sizeof(receiverAppSidSize));
81  char* receiverAppSidBuf = new char[receiverAppSidSize];
82  vs.read(receiverAppSidBuf, receiverAppSidSize);
83  o_receiverAppSid = std::string(receiverAppSidBuf, receiverAppSidSize);
84  delete[] receiverAppSidBuf;
85 
86  // Вычитываем собственно данные
87  vs >> o_data;
88  return p_bytearray.size() - vs.bytesAvailable();
89  }
90 
91  template< typename T >
92  void write(const std::string& p_senderAppSid, const std::string& p_receiverAppSid, const T& p_data, std::vector< char >& o_bytearray)
93  {
94  NodeProtocolHeader header = {pv01, getTypeByData< T >(), 0}; // bodySize = 0, т.к. он будет потом перезаписан
95  VectorStream vs(o_bytearray);
96  vs.write((const char*) &header, sizeof(header));
97 
98  int senderAppSidSize = p_senderAppSid.size();
99  vs.write((const char*) &senderAppSidSize, sizeof(senderAppSidSize));
100  vs.write(p_senderAppSid.c_str(), senderAppSidSize);
101 
102  int receiverAppSidSize = p_receiverAppSid.size();
103  vs.write((const char*) &receiverAppSidSize, sizeof(receiverAppSidSize));
104  vs.write(p_receiverAppSid.c_str(), receiverAppSidSize);
105 
106  vs << p_data;
107  vs.flush();
108 
109  finalize(o_bytearray);
110  }
111 
112  private:
113  template< typename T >
114  inline MessageType getTypeByData();
115 
116  void finalize(std::vector< char >& o_bytearray);
117  };
118  }
119 }
120 //========================================================================
121 #endif
122 //========================================================================
Definition: node_protocol.h:41
Векторный поток
Definition: streamtools.h:35
Definition: node_protocol.h:15
Definition: node_protocol.h:23
size_t bytesAvailable()
Definition: streamtools.h:50