gramods
Protocol.hh
1 
2 #ifndef GRAMODS_NETWORK_PROTOCOL
3 #define GRAMODS_NETWORK_PROTOCOL
4 
5 #include <gmNetwork/config.hh>
6 
7 #include <memory>
8 #include <mutex>
9 #include <vector>
10 #include <limits>
11 #include <set>
12 
13 BEGIN_NAMESPACE_GMNETWORK;
14 
15 class SyncNode;
16 
20 class Protocol
21  : public std::enable_shared_from_this<Protocol> {
22 
23 public:
24 
25  static const size_t HEADER_LENGTH;
26 
27  Protocol() {}
28 
29  virtual ~Protocol() {}
30 
37  virtual char getProtocolFlag() { return 0; }
38 
43  struct Message {
44 
48  Message() {}
49 
54  Message(std::vector<char> hdr);
55 
59  Message(char protocol, std::vector<char> data);
60 
65  std::vector<char> getHeader();
66 
71  size_t from_peer_idx = std::numeric_limits<size_t>::max();
72 
77  size_t to_peer_idx = std::numeric_limits<size_t>::max();
78 
82  char protocol = 0;
83 
89  size_t length = 0;
90 
94  std::vector<char> data;
95  };
96 
105  virtual void processMessage(Message m);
106 
111  virtual void lostPeer(size_t idx);
112 
113  void setSyncNode(SyncNode *sync_node);
114 
115 protected:
116 
121  void sendMessage(std::vector<char> data);
122 
129  size_t getLocalPeerIdx();
130 
135  std::set<size_t> getConnectedPeers();
136 
143 
147  std::mutex sync_node_lock;
148 };
149 
150 END_NAMESPACE_GMNETWORK;
151 
152 #endif
TODO: write this.
Definition: Protocol.hh:21
SyncNode * sync_node
The SyncNode instance this protocol communicates through or nullptr if it has gone out of scope.
Definition: Protocol.hh:142
virtual char getProtocolFlag()
Returns the byte sent in the message to indicate which protocol instance to call for interpretation a...
Definition: Protocol.hh:37
std::mutex sync_node_lock
Lock for synchronizing the sync_node pointer.
Definition: Protocol.hh:147
A handler of network communication with multiple peers.
Definition: SyncNode.hh:34
Data entity communicated by the connection to the designated protocol.
Definition: Protocol.hh:43
Message()
Create an empty message.
Definition: Protocol.hh:48
std::vector< char > data
The complete set of data sent by the peer.
Definition: Protocol.hh:94