1 #ifndef MSP_NET_PROTOCOL_H_
2 #define MSP_NET_PROTOCOL_H_
8 #include <msp/core/hash.h>
14 class bad_packet: public std::runtime_error
17 bad_packet(const std::string &w): std::runtime_error(w) { }
21 class buffer_error: public std::runtime_error
24 buffer_error(const std::string &w): std::runtime_error(w) { }
31 template<typename T, std::uint8_t K>
38 struct CompoundTypeDef
40 virtual ~CompoundTypeDef() = default;
42 virtual std::uint64_t get_hash() const = 0;
43 virtual char *serialize(const C &, char *, char *) const = 0;
44 virtual const char *deserialize(C &, const char *, const char *) const = 0;
47 template<typename C, typename S>
48 struct CompoundDef: public CompoundTypeDef<C>
52 CompoundDef(const S &);
54 std::uint64_t get_hash() const override;
55 char *serialize(const C &, char *, char *) const override;
56 const char *deserialize(C &, const char *, const char *) const override;
65 BasicSerializer(const Protocol &) { }
67 std::uint64_t get_hash() const { return Traits<T>::signature; }
68 char *serialize(const T &, char *, char *) const;
69 const char *deserialize(T &, const char *, const char *) const;
72 class StringSerializer
75 typedef std::string ValueType;
78 BasicSerializer<std::uint16_t> length_serializer;
81 StringSerializer(const Protocol &);
83 std::uint64_t get_hash() const;
84 char *serialize(const std::string &, char *, char *) const;
85 const char *deserialize(std::string &, const char *, const char *) const;
95 BasicSerializer<std::uint16_t> length_serializer;
96 typename Traits<typename A::value_type>::Serializer element_serializer;
99 ArraySerializer(const Protocol &);
101 std::uint64_t get_hash() const;
102 char *serialize(const A &, char *, char *) const;
103 const char *deserialize(A &, const char *, const char *) const;
107 class CompoundSerializer
113 const CompoundTypeDef<C> &def;
116 CompoundSerializer(const Protocol &);
118 char *serialize(const C &, char *, char *) const;
119 const char *deserialize(C &, const char *, const char *) const;
122 template<typename P, typename Head, typename S>
123 class Serializer: public Head
129 typedef Serializer<P, Serializer<P, Head, S>, typename Traits<N>::Serializer> Type;
133 typedef typename S::ValueType P::*Pointer;
139 Serializer(const Head &, Pointer, const Protocol &);
141 std::uint64_t get_hash() const;
142 char *serialize(const P &, char *, char *) const;
143 const char *deserialize(P &, const char *, const char *) const;
153 typedef Serializer<P, NullSerializer, typename Traits<N>::Serializer> Type;
156 std::uint64_t get_hash() const { return 0; }
157 char *serialize(const P &, char *b, char *) const { return b; }
158 const char *deserialize(P &, const char *b, const char *) const { return b; }
166 PacketDefBase(unsigned);
168 virtual ~PacketDefBase() = default;
170 virtual unsigned get_class_id() const = 0;
171 unsigned get_id() const { return id; }
172 virtual std::uint64_t get_hash() const = 0;
173 virtual const char *dispatch(ReceiverBase &, const char *, const char *) const = 0;
177 class PacketTypeDef: public PacketDefBase
180 CompoundTypeDef<P> *compound;
183 PacketTypeDef(unsigned);
186 unsigned get_class_id() const override { return get_packet_class_id<P>(); }
189 void set_serializer(const S &);
191 const CompoundTypeDef<P> &get_compound() const { return *compound; }
193 std::uint64_t get_hash() const override { return compound->get_hash(); }
194 char *serialize(const P &, char *, char *) const;
195 const char *deserialize(P &, const char *, const char *) const;
196 const char *dispatch(ReceiverBase &, const char *, const char *) const override;
199 template<typename P, typename S>
200 class PacketDefBuilder
203 const Protocol &protocol;
204 PacketTypeDef<P> &pktdef;
208 PacketDefBuilder(const Protocol &, PacketTypeDef<P> &, const S &);
211 PacketDefBuilder<P, typename S::template Next<T>::Type> operator()(T P::*);
217 std::uint16_t length;
220 PacketHeader(std::uint16_t, std::uint16_t);
223 typedef std::map<unsigned, PacketDefBase *> PacketMap;
225 PacketTypeDef<PacketHeader> header_def;
226 unsigned next_packet_id;
227 PacketMap packet_class_defs;
228 PacketMap packet_id_defs;
231 Protocol(unsigned = 1);
236 static unsigned get_next_packet_class_id();
239 static unsigned get_packet_class_id();
241 void add_packet(PacketDefBase *);
245 PacketDefBuilder<P, NullSerializer<P> > add(unsigned);
248 PacketDefBuilder<P, NullSerializer<P> > add();
250 const PacketDefBase &get_packet_by_class_id(unsigned) const;
251 const PacketDefBase &get_packet_by_id(unsigned) const;
254 const PacketTypeDef<P> &get_packet_by_class() const;
258 std::size_t serialize(const P &, char *, std::size_t) const;
260 std::size_t get_packet_size(const char *, std::size_t) const;
261 std::size_t dispatch(ReceiverBase &, const char *, std::size_t) const;
263 std::uint64_t get_hash() const;
268 unsigned Protocol::get_packet_class_id()
270 static unsigned id = get_next_packet_class_id();
275 Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add(unsigned id)
277 PacketTypeDef<P> *pdef = new PacketTypeDef<P>(id);
279 return PacketDefBuilder<P, NullSerializer<P> >(*this, *pdef, NullSerializer<P>());
283 Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add()
285 return add<P>(next_packet_id++);
289 const Protocol::PacketTypeDef<P> &Protocol::get_packet_by_class() const
291 const PacketDefBase &pdef = get_packet_by_class_id(get_packet_class_id<P>());
292 return static_cast<const PacketTypeDef<P> &>(pdef);
296 std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size) const
298 const PacketTypeDef<P> &pdef = get_packet_by_class<P>();
300 throw std::invalid_argument("no packet id");
301 char *ptr = pdef.serialize(pkt, buf+4, buf+size);
303 header_def.serialize(PacketHeader(pdef.get_id(), size), buf, buf+4);
308 template<typename T, std::uint8_t K>
309 struct Protocol::BasicTraits
311 static const std::uint16_t signature = K | (sizeof(T)<<8);
312 typedef BasicSerializer<T> Serializer;
316 struct Protocol::Traits
318 static const std::uint16_t signature = 'C';
319 typedef CompoundSerializer<T> Serializer;
322 template<> struct Protocol::Traits<bool>: BasicTraits<bool, 'B'> { };
323 template<> struct Protocol::Traits<std::int8_t>: BasicTraits<std::int8_t, 'I'> { };
324 template<> struct Protocol::Traits<std::uint8_t>: BasicTraits<std::uint8_t, 'U'> { };
325 template<> struct Protocol::Traits<std::int16_t>: BasicTraits<std::int16_t, 'I'> { };
326 template<> struct Protocol::Traits<std::uint16_t>: BasicTraits<std::uint16_t, 'U'> { };
327 template<> struct Protocol::Traits<std::int32_t>: BasicTraits<std::int32_t, 'I'> { };
328 template<> struct Protocol::Traits<std::uint32_t>: BasicTraits<std::uint32_t, 'U'> { };
329 template<> struct Protocol::Traits<std::int64_t>: BasicTraits<std::int64_t, 'I'> { };
330 template<> struct Protocol::Traits<std::uint64_t>: BasicTraits<std::uint64_t, 'U'> { };
331 template<> struct Protocol::Traits<float>: BasicTraits<float, 'F'> { };
332 template<> struct Protocol::Traits<double>: BasicTraits<double, 'F'> { };
334 template<> struct Protocol::Traits<std::string>
336 static const std::uint16_t signature = 'S';
337 typedef StringSerializer Serializer;
341 struct Protocol::Traits<std::vector<T> >
343 static const std::uint16_t signature = 'A';
344 typedef ArraySerializer<std::vector<T> > Serializer;
349 template<typename C, typename S>
350 Protocol::CompoundDef<C, S>::CompoundDef(const S &s):
354 template<typename C, typename S>
355 std::uint64_t Protocol::CompoundDef<C, S>::get_hash() const
357 return hash_round<64>(serializer.get_hash(), 'C');
360 template<typename C, typename S>
361 char *Protocol::CompoundDef<C, S>::serialize(const C &com, char *buf, char *end) const
363 return serializer.serialize(com, buf, end);
366 template<typename C, typename S>
367 const char *Protocol::CompoundDef<C, S>::deserialize(C &com, const char *buf, const char *end) const
369 return serializer.deserialize(com, buf, end);
373 inline std::uint64_t Protocol::StringSerializer::get_hash() const
375 return Traits<std::string>::signature;
380 Protocol::ArraySerializer<A>::ArraySerializer(const Protocol &proto):
381 length_serializer(proto),
382 element_serializer(proto)
386 std::uint64_t Protocol::ArraySerializer<A>::get_hash() const
388 return hash_round<64>(element_serializer.get_hash(), 'A');
392 char *Protocol::ArraySerializer<A>::serialize(const A &array, char *buf, char *end) const
394 buf = length_serializer.serialize(array.size(), buf, end);
395 for(const auto &e: array)
396 buf = element_serializer.serialize(e, buf, end);
401 const char *Protocol::ArraySerializer<A>::deserialize(A &array, const char *buf, const char *end) const
403 std::uint16_t length;
404 buf = length_serializer.deserialize(length, buf, end);
405 array.resize(length);
406 for(unsigned i=0; i<length; ++i)
407 buf = element_serializer.deserialize(array[i], buf, end);
413 Protocol::CompoundSerializer<C>::CompoundSerializer(const Protocol &proto):
414 def(proto.get_packet_by_class<C>().get_compound())
418 char *Protocol::CompoundSerializer<C>::serialize(const C &com, char *buf, char *end) const
420 return def.serialize(com, buf, end);
424 const char *Protocol::CompoundSerializer<C>::deserialize(C &com, const char *buf, const char *end) const
426 return def.deserialize(com, buf, end);
430 template<typename P, typename Head, typename S>
431 Protocol::Serializer<P, Head, S>::Serializer(const Head &h, Pointer p, const Protocol &proto):
437 template<typename P, typename Head, typename S>
438 std::uint64_t Protocol::Serializer<P, Head, S>::get_hash() const
440 return hash_update<64>(Head::get_hash(), ser.get_hash());
443 template<typename P, typename Head, typename S>
444 char *Protocol::Serializer<P, Head, S>::serialize(const P &pkt, char *buf, char *end) const
446 buf = Head::serialize(pkt, buf, end);
447 return ser.serialize(pkt.*ptr, buf, end);
450 template<typename P, typename Head, typename S>
451 const char *Protocol::Serializer<P, Head, S>::deserialize(P &pkt, const char *buf, const char *end) const
453 buf = Head::deserialize(pkt, buf, end);
454 return ser.deserialize(pkt.*ptr, buf, end);
459 Protocol::PacketTypeDef<P>::PacketTypeDef(unsigned i):
461 compound(new CompoundDef<P, NullSerializer<P> >(NullSerializer<P>()))
465 Protocol::PacketTypeDef<P>::~PacketTypeDef()
472 void Protocol::PacketTypeDef<P>::set_serializer(const S &ser)
475 compound = new CompoundDef<P, S>(ser);
479 char *Protocol::PacketTypeDef<P>::serialize(const P &pkt, char *buf, char *end) const
481 return compound->serialize(pkt, buf, end);
485 const char *Protocol::PacketTypeDef<P>::deserialize(P &pkt, const char *buf, const char *end) const
487 return compound->deserialize(pkt, buf, end);
491 const char *Protocol::PacketTypeDef<P>::dispatch(ReceiverBase &rcv, const char *buf, const char *end) const
493 PacketReceiver<P> *prcv = dynamic_cast<PacketReceiver<P> *>(&rcv);
495 throw bad_packet("unsupported");
497 buf = deserialize(pkt, buf, end);
503 template<typename P, typename S>
504 Protocol::PacketDefBuilder<P, S>::PacketDefBuilder(const Protocol &p, PacketTypeDef<P> &k, const S &s):
510 template<typename P, typename S>
512 Protocol::PacketDefBuilder<P, typename S::template Next<T>::Type> Protocol::PacketDefBuilder<P, S>::operator()(T P::*ptr)
514 typename S::template Next<T>::Type next_ser(serializer, ptr, protocol);
515 pktdef.set_serializer(next_ser);
516 return PacketDefBuilder<P, typename S::template Next<T>::Type>(protocol, pktdef, next_ser);