1 #ifndef MSP_NET_PROTOCOL_H_
2 #define MSP_NET_PROTOCOL_H_
13 class bad_packet: public std::runtime_error
16 bad_packet(const std::string &w): std::runtime_error(w) { }
20 class buffer_error: public std::runtime_error
23 buffer_error(const std::string &w): std::runtime_error(w) { }
30 template<typename T, std::uint8_t K>
37 struct CompoundTypeDef
39 virtual ~CompoundTypeDef() = default;
41 virtual std::string describe() const = 0;
42 virtual char *serialize(const C &, char *, char *) const = 0;
43 virtual const char *deserialize(C &, const char *, const char *) const = 0;
46 template<typename C, typename S>
47 struct CompoundDef: public CompoundTypeDef<C>
51 CompoundDef(const S &);
53 std::string describe() const override;
54 char *serialize(const C &, char *, char *) const override;
55 const char *deserialize(C &, const char *, const char *) const override;
64 BasicSerializer(const Protocol &) { }
66 std::string describe() const { return get_type_signature<T>(); }
67 char *serialize(const T &, char *, char *) const;
68 const char *deserialize(T &, const char *, const char *) const;
71 class StringSerializer
74 typedef std::string ValueType;
77 BasicSerializer<std::uint16_t> length_serializer;
80 StringSerializer(const Protocol &);
82 std::string describe() const { return get_type_signature<std::string>(); }
83 char *serialize(const std::string &, char *, char *) const;
84 const char *deserialize(std::string &, const char *, const char *) const;
94 BasicSerializer<std::uint16_t> length_serializer;
95 typename Traits<typename A::value_type>::Serializer element_serializer;
98 ArraySerializer(const Protocol &);
100 std::string describe() const;
101 char *serialize(const A &, char *, char *) const;
102 const char *deserialize(A &, const char *, const char *) const;
106 class CompoundSerializer
112 const CompoundTypeDef<C> &def;
115 CompoundSerializer(const Protocol &);
117 std::string describe() const { return def.describe(); }
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::string describe() 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::string describe() const { return std::string(); }
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::string describe() 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::string describe() const override { return compound->describe(); }
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;
267 static std::string get_type_signature();
272 unsigned Protocol::get_packet_class_id()
274 static unsigned id = get_next_packet_class_id();
279 Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add(unsigned id)
281 PacketTypeDef<P> *pdef = new PacketTypeDef<P>(id);
283 return PacketDefBuilder<P, NullSerializer<P> >(*this, *pdef, NullSerializer<P>());
287 Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add()
289 return add<P>(next_packet_id++);
293 const Protocol::PacketTypeDef<P> &Protocol::get_packet_by_class() const
295 const PacketDefBase &pdef = get_packet_by_class_id(get_packet_class_id<P>());
296 return static_cast<const PacketTypeDef<P> &>(pdef);
300 std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size) const
302 const PacketTypeDef<P> &pdef = get_packet_by_class<P>();
304 throw std::invalid_argument("no packet id");
305 char *ptr = pdef.serialize(pkt, buf+4, buf+size);
307 header_def.serialize(PacketHeader(pdef.get_id(), size), buf, buf+4);
312 std::string Protocol::get_type_signature()
314 const std::uint16_t sig = Traits<T>::signature;
318 result += '0'+(sig>>8);
323 template<typename T, std::uint8_t K>
324 struct Protocol::BasicTraits
326 static const std::uint16_t signature = K | (sizeof(T)<<8);
327 typedef BasicSerializer<T> Serializer;
331 struct Protocol::Traits
333 static const std::uint16_t signature = 'C';
334 typedef CompoundSerializer<T> Serializer;
337 template<> struct Protocol::Traits<bool>: BasicTraits<bool, 'B'> { };
338 template<> struct Protocol::Traits<std::int8_t>: BasicTraits<std::int8_t, 'I'> { };
339 template<> struct Protocol::Traits<std::uint8_t>: BasicTraits<std::uint8_t, 'U'> { };
340 template<> struct Protocol::Traits<std::int16_t>: BasicTraits<std::int16_t, 'I'> { };
341 template<> struct Protocol::Traits<std::uint16_t>: BasicTraits<std::uint16_t, 'U'> { };
342 template<> struct Protocol::Traits<std::int32_t>: BasicTraits<std::int32_t, 'I'> { };
343 template<> struct Protocol::Traits<std::uint32_t>: BasicTraits<std::uint32_t, 'U'> { };
344 template<> struct Protocol::Traits<std::int64_t>: BasicTraits<std::int64_t, 'I'> { };
345 template<> struct Protocol::Traits<std::uint64_t>: BasicTraits<std::uint64_t, 'U'> { };
346 template<> struct Protocol::Traits<float>: BasicTraits<float, 'F'> { };
347 template<> struct Protocol::Traits<double>: BasicTraits<double, 'F'> { };
349 template<> struct Protocol::Traits<std::string>
351 static const std::uint16_t signature = 'S';
352 typedef StringSerializer Serializer;
356 struct Protocol::Traits<std::vector<T> >
358 static const std::uint16_t signature = 'A';
359 typedef ArraySerializer<std::vector<T> > Serializer;
364 template<typename C, typename S>
365 Protocol::CompoundDef<C, S>::CompoundDef(const S &s):
369 template<typename C, typename S>
370 std::string Protocol::CompoundDef<C, S>::describe() const
372 return "{"+serializer.describe()+"}";
375 template<typename C, typename S>
376 char *Protocol::CompoundDef<C, S>::serialize(const C &com, char *buf, char *end) const
378 return serializer.serialize(com, buf, end);
381 template<typename C, typename S>
382 const char *Protocol::CompoundDef<C, S>::deserialize(C &com, const char *buf, const char *end) const
384 return serializer.deserialize(com, buf, end);
389 Protocol::ArraySerializer<A>::ArraySerializer(const Protocol &proto):
390 length_serializer(proto),
391 element_serializer(proto)
395 std::string Protocol::ArraySerializer<A>::describe() const
397 return "["+element_serializer.describe()+"]";
401 char *Protocol::ArraySerializer<A>::serialize(const A &array, char *buf, char *end) const
403 buf = length_serializer.serialize(array.size(), buf, end);
404 for(const auto &e: array)
405 buf = element_serializer.serialize(e, buf, end);
410 const char *Protocol::ArraySerializer<A>::deserialize(A &array, const char *buf, const char *end) const
412 std::uint16_t length;
413 buf = length_serializer.deserialize(length, buf, end);
414 array.resize(length);
415 for(unsigned i=0; i<length; ++i)
416 buf = element_serializer.deserialize(array[i], buf, end);
422 Protocol::CompoundSerializer<C>::CompoundSerializer(const Protocol &proto):
423 def(proto.get_packet_by_class<C>().get_compound())
427 char *Protocol::CompoundSerializer<C>::serialize(const C &com, char *buf, char *end) const
429 return def.serialize(com, buf, end);
433 const char *Protocol::CompoundSerializer<C>::deserialize(C &com, const char *buf, const char *end) const
435 return def.deserialize(com, buf, end);
439 template<typename P, typename Head, typename S>
440 Protocol::Serializer<P, Head, S>::Serializer(const Head &h, Pointer p, const Protocol &proto):
446 template<typename P, typename Head, typename S>
447 std::string Protocol::Serializer<P, Head, S>::describe() const
449 return Head::describe()+ser.describe();
452 template<typename P, typename Head, typename S>
453 char *Protocol::Serializer<P, Head, S>::serialize(const P &pkt, char *buf, char *end) const
455 buf = Head::serialize(pkt, buf, end);
456 return ser.serialize(pkt.*ptr, buf, end);
459 template<typename P, typename Head, typename S>
460 const char *Protocol::Serializer<P, Head, S>::deserialize(P &pkt, const char *buf, const char *end) const
462 buf = Head::deserialize(pkt, buf, end);
463 return ser.deserialize(pkt.*ptr, buf, end);
468 Protocol::PacketTypeDef<P>::PacketTypeDef(unsigned i):
470 compound(new CompoundDef<P, NullSerializer<P> >(NullSerializer<P>()))
474 Protocol::PacketTypeDef<P>::~PacketTypeDef()
481 void Protocol::PacketTypeDef<P>::set_serializer(const S &ser)
484 compound = new CompoundDef<P, S>(ser);
488 char *Protocol::PacketTypeDef<P>::serialize(const P &pkt, char *buf, char *end) const
490 return compound->serialize(pkt, buf, end);
494 const char *Protocol::PacketTypeDef<P>::deserialize(P &pkt, const char *buf, const char *end) const
496 return compound->deserialize(pkt, buf, end);
500 const char *Protocol::PacketTypeDef<P>::dispatch(ReceiverBase &rcv, const char *buf, const char *end) const
502 PacketReceiver<P> *prcv = dynamic_cast<PacketReceiver<P> *>(&rcv);
504 throw bad_packet("unsupported");
506 buf = deserialize(pkt, buf, end);
512 template<typename P, typename S>
513 Protocol::PacketDefBuilder<P, S>::PacketDefBuilder(const Protocol &p, PacketTypeDef<P> &k, const S &s):
519 template<typename P, typename S>
521 Protocol::PacketDefBuilder<P, typename S::template Next<T>::Type> Protocol::PacketDefBuilder<P, S>::operator()(T P::*ptr)
523 typename S::template Next<T>::Type next_ser(serializer, ptr, protocol);
524 pktdef.set_serializer(next_ser);
525 return PacketDefBuilder<P, typename S::template Next<T>::Type>(protocol, pktdef, next_ser);