]> git.tdb.fi Git - libs/net.git/blob - source/net/protocol.h
Use the override specifier when overriding
[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 "receiver.h"
9
10 namespace Msp {
11 namespace Net {
12
13 class bad_packet: public std::runtime_error
14 {
15 public:
16         bad_packet(const std::string &w): std::runtime_error(w) { }
17 };
18
19
20 class buffer_error: public std::runtime_error
21 {
22 public:
23         buffer_error(const std::string &w): std::runtime_error(w) { }
24 };
25
26
27 class Protocol
28 {
29 private:
30         template<typename T, std::uint8_t K>
31         struct BasicTraits;
32
33         template<typename T>
34         struct Traits;
35
36         template<typename C>
37         struct CompoundTypeDef
38         {
39                 virtual ~CompoundTypeDef() = default;
40
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;
44         };
45
46         template<typename C, typename S>
47         struct CompoundDef: public CompoundTypeDef<C>
48         {
49                 S serializer;
50
51                 CompoundDef(const S &);
52
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;
56         };
57
58         template<typename T>
59         class BasicSerializer
60         {
61         public:
62                 typedef T ValueType;
63
64                 BasicSerializer(const Protocol &) { }
65
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;
69         };
70
71         class StringSerializer
72         {
73         public:
74                 typedef std::string ValueType;
75
76         private:
77                 BasicSerializer<std::uint16_t> length_serializer;
78
79         public:
80                 StringSerializer(const Protocol &);
81
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;
85         };
86
87         template<typename A>
88         class ArraySerializer
89         {
90         public:
91                 typedef A ValueType;
92
93         private:
94                 BasicSerializer<std::uint16_t> length_serializer;
95                 typename Traits<typename A::value_type>::Serializer element_serializer;
96
97         public:
98                 ArraySerializer(const Protocol &);
99
100                 std::string describe() const;
101                 char *serialize(const A &, char *, char *) const;
102                 const char *deserialize(A &, const char *, const char *) const;
103         };
104
105         template<typename C>
106         class CompoundSerializer
107         {
108         public:
109                 typedef C ValueType;
110
111         private:
112                 const CompoundTypeDef<C> &def;
113
114         public:
115                 CompoundSerializer(const Protocol &);
116
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;
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::string describe() 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::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; }
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::string describe() 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::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;
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;
217                 std::uint16_t length;
218
219                 PacketHeader();
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 private:
266         template<typename T>
267         static std::string get_type_signature();
268 };
269
270
271 template<typename P>
272 unsigned Protocol::get_packet_class_id()
273 {
274         static unsigned id = get_next_packet_class_id();
275         return id;
276 }
277
278 template<typename P>
279 Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add(unsigned id)
280 {
281         PacketTypeDef<P> *pdef = new PacketTypeDef<P>(id);
282         add_packet(pdef);
283         return PacketDefBuilder<P, NullSerializer<P> >(*this, *pdef, NullSerializer<P>());
284 }
285
286 template<typename P>
287 Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add()
288 {
289         return add<P>(next_packet_id++);
290 }
291
292 template<typename P>
293 const Protocol::PacketTypeDef<P> &Protocol::get_packet_by_class() const
294 {
295         const PacketDefBase &pdef = get_packet_by_class_id(get_packet_class_id<P>());
296         return static_cast<const PacketTypeDef<P> &>(pdef);
297 }
298
299 template<typename P>
300 std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size) const
301 {
302         const PacketTypeDef<P> &pdef = get_packet_by_class<P>();
303         if(!pdef.get_id())
304                 throw std::invalid_argument("no packet id");
305         char *ptr = pdef.serialize(pkt, buf+4, buf+size);
306         size = ptr-buf;
307         header_def.serialize(PacketHeader(pdef.get_id(), size), buf, buf+4);
308         return size;
309 }
310
311 template<typename T>
312 std::string Protocol::get_type_signature()
313 {
314         const std::uint16_t sig = Traits<T>::signature;
315         std::string result;
316         result += sig&0xFF;
317         if(sig>=0x100)
318                 result += '0'+(sig>>8);
319         return result;
320 }
321
322
323 template<typename T, std::uint8_t K>
324 struct Protocol::BasicTraits
325 {
326         static const std::uint16_t signature = K | (sizeof(T)<<8);
327         typedef BasicSerializer<T> Serializer;
328 };
329
330 template<typename T>
331 struct Protocol::Traits
332 {
333         static const std::uint16_t signature = 'C';
334         typedef CompoundSerializer<T> Serializer;
335 };
336
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'> { };
348
349 template<> struct Protocol::Traits<std::string>
350 {
351         static const std::uint16_t signature = 'S';
352         typedef StringSerializer Serializer;
353 };
354
355 template<typename T>
356 struct Protocol::Traits<std::vector<T> >
357 {
358         static const std::uint16_t signature = 'A';
359         typedef ArraySerializer<std::vector<T> > Serializer;
360 };
361
362
363
364 template<typename C, typename S>
365 Protocol::CompoundDef<C, S>::CompoundDef(const S &s):
366         serializer(s)
367 { }
368
369 template<typename C, typename S>
370 std::string Protocol::CompoundDef<C, S>::describe() const
371 {
372         return "{"+serializer.describe()+"}";
373 }
374
375 template<typename C, typename S>
376 char *Protocol::CompoundDef<C, S>::serialize(const C &com, char *buf, char *end) const
377 {
378         return serializer.serialize(com, buf, end);
379 }
380
381 template<typename C, typename S>
382 const char *Protocol::CompoundDef<C, S>::deserialize(C &com, const char *buf, const char *end) const
383 {
384         return serializer.deserialize(com, buf, end);
385 }
386
387
388 template<typename A>
389 Protocol::ArraySerializer<A>::ArraySerializer(const Protocol &proto):
390         length_serializer(proto),
391         element_serializer(proto)
392 { }
393
394 template<typename A>
395 std::string Protocol::ArraySerializer<A>::describe() const
396 {
397         return "["+element_serializer.describe()+"]";
398 }
399
400 template<typename A>
401 char *Protocol::ArraySerializer<A>::serialize(const A &array, char *buf, char *end) const
402 {
403         buf = length_serializer.serialize(array.size(), buf, end);
404         for(typename A::const_iterator i=array.begin(); i!=array.end(); ++i)
405                 buf = element_serializer.serialize(*i, buf, end);
406         return buf;
407 }
408
409 template<typename A>
410 const char *Protocol::ArraySerializer<A>::deserialize(A &array, const char *buf, const char *end) const
411 {
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);
417         return buf;
418 }
419
420
421 template<typename C>
422 Protocol::CompoundSerializer<C>::CompoundSerializer(const Protocol &proto):
423         def(proto.get_packet_by_class<C>().get_compound())
424 { }
425
426 template<typename C>
427 char *Protocol::CompoundSerializer<C>::serialize(const C &com, char *buf, char *end) const
428 {
429         return def.serialize(com, buf, end);
430 }
431
432 template<typename C>
433 const char *Protocol::CompoundSerializer<C>::deserialize(C &com, const char *buf, const char *end) const
434 {
435         return def.deserialize(com, buf, end);
436 }
437
438
439 template<typename P, typename Head, typename S>
440 Protocol::Serializer<P, Head, S>::Serializer(const Head &h, Pointer p, const Protocol &proto):
441         Head(h),
442         ptr(p),
443         ser(proto)
444 { }
445
446 template<typename P, typename Head, typename S>
447 std::string Protocol::Serializer<P, Head, S>::describe() const
448 {
449         return Head::describe()+ser.describe();
450 }
451
452 template<typename P, typename Head, typename S>
453 char *Protocol::Serializer<P, Head, S>::serialize(const P &pkt, char *buf, char *end) const
454 {
455         buf = Head::serialize(pkt, buf, end);
456         return ser.serialize(pkt.*ptr, buf, end);
457 }
458
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
461 {
462         buf = Head::deserialize(pkt, buf, end);
463         return ser.deserialize(pkt.*ptr, buf, end);
464 }
465
466
467 template<typename P>
468 Protocol::PacketTypeDef<P>::PacketTypeDef(unsigned i):
469         PacketDefBase(i),
470         compound(new CompoundDef<P, NullSerializer<P> >(NullSerializer<P>()))
471 { }
472
473 template<typename P>
474 Protocol::PacketTypeDef<P>::~PacketTypeDef()
475 {
476         delete compound;
477 }
478
479 template<typename P>
480 template<typename S>
481 void Protocol::PacketTypeDef<P>::set_serializer(const S &ser)
482 {
483         delete compound;
484         compound = new CompoundDef<P, S>(ser);
485 }
486
487 template<typename P>
488 char *Protocol::PacketTypeDef<P>::serialize(const P &pkt, char *buf, char *end) const
489 {
490         return compound->serialize(pkt, buf, end);
491 }
492
493 template<typename P>
494 const char *Protocol::PacketTypeDef<P>::deserialize(P &pkt, const char *buf, const char *end) const
495 {
496         return compound->deserialize(pkt, buf, end);
497 }
498
499 template<typename P>
500 const char *Protocol::PacketTypeDef<P>::dispatch(ReceiverBase &rcv, const char *buf, const char *end) const
501 {
502         PacketReceiver<P> *prcv = dynamic_cast<PacketReceiver<P> *>(&rcv);
503         if(!prcv)
504                 throw bad_packet("unsupported");
505         P pkt;
506         buf = deserialize(pkt, buf, end);
507         prcv->receive(pkt);
508         return buf;
509 }
510
511
512 template<typename P, typename S>
513 Protocol::PacketDefBuilder<P, S>::PacketDefBuilder(const Protocol &p, PacketTypeDef<P> &k, const S &s):
514         protocol(p),
515         pktdef(k),
516         serializer(s)
517 { }
518
519 template<typename P, typename S>
520 template<typename T>
521 Protocol::PacketDefBuilder<P, typename S::template Next<T>::Type> Protocol::PacketDefBuilder<P, S>::operator()(T P::*ptr)
522 {
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);
526 }
527
528 } // namespace Net
529 } // namespace Msp
530
531 #endif