]> git.tdb.fi Git - libs/net.git/blob - source/net/protocol.h
Use default member initializers where possible
[libs/net.git] / source / net / protocol.h
1 #ifndef MSP_NET_PROTOCOL_H_
2 #define MSP_NET_PROTOCOL_H_
3
4 #include <cstdint>
5 #include <map>
6 #include <stdexcept>
7 #include <vector>
8 #include <msp/core/hash.h>
9 #include "receiver.h"
10
11 namespace Msp {
12 namespace Net {
13
14 class bad_packet: public std::runtime_error
15 {
16 public:
17         bad_packet(const std::string &w): std::runtime_error(w) { }
18 };
19
20
21 class buffer_error: public std::runtime_error
22 {
23 public:
24         buffer_error(const std::string &w): std::runtime_error(w) { }
25 };
26
27
28 class Protocol
29 {
30 private:
31         template<typename T, std::uint8_t K>
32         struct BasicTraits;
33
34         template<typename T>
35         struct Traits;
36
37         template<typename C>
38         struct CompoundTypeDef
39         {
40                 virtual ~CompoundTypeDef() = default;
41
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;
45         };
46
47         template<typename C, typename S>
48         struct CompoundDef: public CompoundTypeDef<C>
49         {
50                 S serializer;
51
52                 CompoundDef(const S &);
53
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;
57         };
58
59         template<typename T>
60         class BasicSerializer
61         {
62         public:
63                 typedef T ValueType;
64
65                 BasicSerializer(const Protocol &) { }
66
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;
70         };
71
72         class StringSerializer
73         {
74         public:
75                 typedef std::string ValueType;
76
77         private:
78                 BasicSerializer<std::uint16_t> length_serializer;
79
80         public:
81                 StringSerializer(const Protocol &);
82
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;
86         };
87
88         template<typename A>
89         class ArraySerializer
90         {
91         public:
92                 typedef A ValueType;
93
94         private:
95                 BasicSerializer<std::uint16_t> length_serializer;
96                 typename Traits<typename A::value_type>::Serializer element_serializer;
97
98         public:
99                 ArraySerializer(const Protocol &);
100
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;
104         };
105
106         template<typename C>
107         class CompoundSerializer
108         {
109         public:
110                 typedef C ValueType;
111
112         private:
113                 const CompoundTypeDef<C> &def;
114
115         public:
116                 CompoundSerializer(const Protocol &);
117
118                 char *serialize(const C &, char *, char *) const;
119                 const char *deserialize(C &, const char *, const char *) const;
120         };
121
122         template<typename P, typename Head, typename S>
123         class Serializer: public Head
124         {
125         public:
126                 template<typename N>
127                 struct Next
128                 {
129                         typedef Serializer<P, Serializer<P, Head, S>, typename Traits<N>::Serializer> Type;
130                 };
131
132         private:
133                 typedef typename S::ValueType P::*Pointer;
134
135                 Pointer ptr;
136                 S ser;
137
138         public:
139                 Serializer(const Head &, Pointer, const Protocol &);
140
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;
144         };
145
146         template<typename P>
147         class NullSerializer
148         {
149         public:
150                 template<typename N>
151                 struct Next
152                 {
153                         typedef Serializer<P, NullSerializer, typename Traits<N>::Serializer> Type;
154                 };
155
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; }
159         };
160
161         class PacketDefBase
162         {
163         protected:
164                 unsigned id;
165
166                 PacketDefBase(unsigned);
167         public:
168                 virtual ~PacketDefBase() = default;
169
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;
174         };
175
176         template<typename P>
177         class PacketTypeDef: public PacketDefBase
178         {
179         private:
180                 CompoundTypeDef<P> *compound;
181
182         public:
183                 PacketTypeDef(unsigned);
184                 ~PacketTypeDef();
185
186                 unsigned get_class_id() const override { return get_packet_class_id<P>(); }
187
188                 template<typename S>
189                 void set_serializer(const S &);
190
191                 const CompoundTypeDef<P> &get_compound() const { return *compound; }
192
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;
197         };
198
199         template<typename P, typename S>
200         class PacketDefBuilder
201         {
202         private:
203                 const Protocol &protocol;
204                 PacketTypeDef<P> &pktdef;
205                 S serializer;
206
207         public:
208                 PacketDefBuilder(const Protocol &, PacketTypeDef<P> &, const S &);
209                 
210                 template<typename T>
211                 PacketDefBuilder<P, typename S::template Next<T>::Type> operator()(T P::*);
212         };
213
214         struct PacketHeader
215         {
216                 std::uint16_t type = 0;
217                 std::uint16_t length = 0;
218
219                 PacketHeader() = default;
220                 PacketHeader(std::uint16_t, std::uint16_t);
221         };
222
223         typedef std::map<unsigned, PacketDefBase *> PacketMap;
224
225         PacketTypeDef<PacketHeader> header_def;
226         unsigned next_packet_id;
227         PacketMap packet_class_defs;
228         PacketMap packet_id_defs;
229
230 protected:
231         Protocol(unsigned = 1);
232 public:
233         ~Protocol();
234
235 private:
236         static unsigned get_next_packet_class_id();
237
238         template<typename P>
239         static unsigned get_packet_class_id();
240
241         void add_packet(PacketDefBase *);
242
243 protected:
244         template<typename P>
245         PacketDefBuilder<P, NullSerializer<P> > add(unsigned);
246
247         template<typename P>
248         PacketDefBuilder<P, NullSerializer<P> > add();
249
250         const PacketDefBase &get_packet_by_class_id(unsigned) const;
251         const PacketDefBase &get_packet_by_id(unsigned) const;
252
253         template<typename P>
254         const PacketTypeDef<P> &get_packet_by_class() const;
255
256 public:
257         template<typename P>
258         std::size_t serialize(const P &, char *, std::size_t) const;
259
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;
262
263         std::uint64_t get_hash() const;
264 };
265
266
267 template<typename P>
268 unsigned Protocol::get_packet_class_id()
269 {
270         static unsigned id = get_next_packet_class_id();
271         return id;
272 }
273
274 template<typename P>
275 Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add(unsigned id)
276 {
277         PacketTypeDef<P> *pdef = new PacketTypeDef<P>(id);
278         add_packet(pdef);
279         return PacketDefBuilder<P, NullSerializer<P> >(*this, *pdef, NullSerializer<P>());
280 }
281
282 template<typename P>
283 Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add()
284 {
285         return add<P>(next_packet_id++);
286 }
287
288 template<typename P>
289 const Protocol::PacketTypeDef<P> &Protocol::get_packet_by_class() const
290 {
291         const PacketDefBase &pdef = get_packet_by_class_id(get_packet_class_id<P>());
292         return static_cast<const PacketTypeDef<P> &>(pdef);
293 }
294
295 template<typename P>
296 std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size) const
297 {
298         const PacketTypeDef<P> &pdef = get_packet_by_class<P>();
299         if(!pdef.get_id())
300                 throw std::invalid_argument("no packet id");
301         char *ptr = pdef.serialize(pkt, buf+4, buf+size);
302         size = ptr-buf;
303         header_def.serialize(PacketHeader(pdef.get_id(), size), buf, buf+4);
304         return size;
305 }
306
307
308 template<typename T, std::uint8_t K>
309 struct Protocol::BasicTraits
310 {
311         static const std::uint16_t signature = K | (sizeof(T)<<8);
312         typedef BasicSerializer<T> Serializer;
313 };
314
315 template<typename T>
316 struct Protocol::Traits
317 {
318         static const std::uint16_t signature = 'C';
319         typedef CompoundSerializer<T> Serializer;
320 };
321
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'> { };
333
334 template<> struct Protocol::Traits<std::string>
335 {
336         static const std::uint16_t signature = 'S';
337         typedef StringSerializer Serializer;
338 };
339
340 template<typename T>
341 struct Protocol::Traits<std::vector<T> >
342 {
343         static const std::uint16_t signature = 'A';
344         typedef ArraySerializer<std::vector<T> > Serializer;
345 };
346
347
348
349 template<typename C, typename S>
350 Protocol::CompoundDef<C, S>::CompoundDef(const S &s):
351         serializer(s)
352 { }
353
354 template<typename C, typename S>
355 std::uint64_t Protocol::CompoundDef<C, S>::get_hash() const
356 {
357         return hash_round<64>(serializer.get_hash(), 'C');
358 }
359
360 template<typename C, typename S>
361 char *Protocol::CompoundDef<C, S>::serialize(const C &com, char *buf, char *end) const
362 {
363         return serializer.serialize(com, buf, end);
364 }
365
366 template<typename C, typename S>
367 const char *Protocol::CompoundDef<C, S>::deserialize(C &com, const char *buf, const char *end) const
368 {
369         return serializer.deserialize(com, buf, end);
370 }
371
372
373 inline std::uint64_t Protocol::StringSerializer::get_hash() const
374 {
375         return Traits<std::string>::signature;
376 }
377
378
379 template<typename A>
380 Protocol::ArraySerializer<A>::ArraySerializer(const Protocol &proto):
381         length_serializer(proto),
382         element_serializer(proto)
383 { }
384
385 template<typename A>
386 std::uint64_t Protocol::ArraySerializer<A>::get_hash() const
387 {
388         return hash_round<64>(element_serializer.get_hash(), 'A');
389 }
390
391 template<typename A>
392 char *Protocol::ArraySerializer<A>::serialize(const A &array, char *buf, char *end) const
393 {
394         buf = length_serializer.serialize(array.size(), buf, end);
395         for(const auto &e: array)
396                 buf = element_serializer.serialize(e, buf, end);
397         return buf;
398 }
399
400 template<typename A>
401 const char *Protocol::ArraySerializer<A>::deserialize(A &array, const char *buf, const char *end) const
402 {
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);
408         return buf;
409 }
410
411
412 template<typename C>
413 Protocol::CompoundSerializer<C>::CompoundSerializer(const Protocol &proto):
414         def(proto.get_packet_by_class<C>().get_compound())
415 { }
416
417 template<typename C>
418 char *Protocol::CompoundSerializer<C>::serialize(const C &com, char *buf, char *end) const
419 {
420         return def.serialize(com, buf, end);
421 }
422
423 template<typename C>
424 const char *Protocol::CompoundSerializer<C>::deserialize(C &com, const char *buf, const char *end) const
425 {
426         return def.deserialize(com, buf, end);
427 }
428
429
430 template<typename P, typename Head, typename S>
431 Protocol::Serializer<P, Head, S>::Serializer(const Head &h, Pointer p, const Protocol &proto):
432         Head(h),
433         ptr(p),
434         ser(proto)
435 { }
436
437 template<typename P, typename Head, typename S>
438 std::uint64_t Protocol::Serializer<P, Head, S>::get_hash() const
439 {
440         return hash_update<64>(Head::get_hash(), ser.get_hash());
441 }
442
443 template<typename P, typename Head, typename S>
444 char *Protocol::Serializer<P, Head, S>::serialize(const P &pkt, char *buf, char *end) const
445 {
446         buf = Head::serialize(pkt, buf, end);
447         return ser.serialize(pkt.*ptr, buf, end);
448 }
449
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
452 {
453         buf = Head::deserialize(pkt, buf, end);
454         return ser.deserialize(pkt.*ptr, buf, end);
455 }
456
457
458 template<typename P>
459 Protocol::PacketTypeDef<P>::PacketTypeDef(unsigned i):
460         PacketDefBase(i),
461         compound(new CompoundDef<P, NullSerializer<P> >(NullSerializer<P>()))
462 { }
463
464 template<typename P>
465 Protocol::PacketTypeDef<P>::~PacketTypeDef()
466 {
467         delete compound;
468 }
469
470 template<typename P>
471 template<typename S>
472 void Protocol::PacketTypeDef<P>::set_serializer(const S &ser)
473 {
474         delete compound;
475         compound = new CompoundDef<P, S>(ser);
476 }
477
478 template<typename P>
479 char *Protocol::PacketTypeDef<P>::serialize(const P &pkt, char *buf, char *end) const
480 {
481         return compound->serialize(pkt, buf, end);
482 }
483
484 template<typename P>
485 const char *Protocol::PacketTypeDef<P>::deserialize(P &pkt, const char *buf, const char *end) const
486 {
487         return compound->deserialize(pkt, buf, end);
488 }
489
490 template<typename P>
491 const char *Protocol::PacketTypeDef<P>::dispatch(ReceiverBase &rcv, const char *buf, const char *end) const
492 {
493         PacketReceiver<P> *prcv = dynamic_cast<PacketReceiver<P> *>(&rcv);
494         if(!prcv)
495                 throw bad_packet("unsupported");
496         P pkt;
497         buf = deserialize(pkt, buf, end);
498         prcv->receive(pkt);
499         return buf;
500 }
501
502
503 template<typename P, typename S>
504 Protocol::PacketDefBuilder<P, S>::PacketDefBuilder(const Protocol &p, PacketTypeDef<P> &k, const S &s):
505         protocol(p),
506         pktdef(k),
507         serializer(s)
508 { }
509
510 template<typename P, typename S>
511 template<typename T>
512 Protocol::PacketDefBuilder<P, typename S::template Next<T>::Type> Protocol::PacketDefBuilder<P, S>::operator()(T P::*ptr)
513 {
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);
517 }
518
519 } // namespace Net
520 } // namespace Msp
521
522 #endif