]> git.tdb.fi Git - libs/net.git/blob - source/net/protocol.h
Use a static local variable for assigning packet IDs
[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() { }
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                 virtual std::string describe() const;
54                 virtual char *serialize(const C &, char *, char *) const;
55                 virtual const char *deserialize(C &, const char *, const char *) const;
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() { }
169                 virtual unsigned get_class_id() const = 0;
170                 unsigned get_id() const { return id; }
171                 virtual std::string describe() const = 0;
172                 virtual const char *dispatch(ReceiverBase &, const char *, const char *) const = 0;
173         };
174
175         template<typename P>
176         class PacketTypeDef: public PacketDefBase
177         {
178         private:
179                 CompoundTypeDef<P> *compound;
180
181         public:
182                 PacketTypeDef(unsigned);
183                 ~PacketTypeDef();
184
185                 virtual unsigned get_class_id() const { return get_packet_class_id<P>(); }
186
187                 template<typename S>
188                 void set_serializer(const S &);
189
190                 const CompoundTypeDef<P> &get_compound() const { return *compound; }
191
192                 virtual std::string describe() const { return compound->describe(); }
193                 char *serialize(const P &, char *, char *) const;
194                 const char *deserialize(P &, const char *, const char *) const;
195                 virtual const char *dispatch(ReceiverBase &, const char *, const char *) const;
196         };
197
198         template<typename P, typename S>
199         class PacketDefBuilder
200         {
201         private:
202                 const Protocol &protocol;
203                 PacketTypeDef<P> &pktdef;
204                 S serializer;
205
206         public:
207                 PacketDefBuilder(const Protocol &, PacketTypeDef<P> &, const S &);
208                 
209                 template<typename T>
210                 PacketDefBuilder<P, typename S::template Next<T>::Type> operator()(T P::*);
211         };
212
213         struct PacketHeader
214         {
215                 std::uint16_t type;
216                 std::uint16_t length;
217
218                 PacketHeader();
219                 PacketHeader(std::uint16_t, std::uint16_t);
220         };
221
222         typedef std::map<unsigned, PacketDefBase *> PacketMap;
223
224         PacketTypeDef<PacketHeader> header_def;
225         unsigned next_packet_id;
226         PacketMap packet_class_defs;
227         PacketMap packet_id_defs;
228
229 protected:
230         Protocol(unsigned = 1);
231 public:
232         ~Protocol();
233
234 private:
235         static unsigned get_next_packet_class_id();
236
237         template<typename P>
238         static unsigned get_packet_class_id();
239
240         void add_packet(PacketDefBase *);
241
242 protected:
243         template<typename P>
244         PacketDefBuilder<P, NullSerializer<P> > add(unsigned);
245
246         template<typename P>
247         PacketDefBuilder<P, NullSerializer<P> > add();
248
249         const PacketDefBase &get_packet_by_class_id(unsigned) const;
250         const PacketDefBase &get_packet_by_id(unsigned) const;
251
252         template<typename P>
253         const PacketTypeDef<P> &get_packet_by_class() const;
254
255 public:
256         template<typename P>
257         std::size_t serialize(const P &, char *, std::size_t) const;
258
259         std::size_t get_packet_size(const char *, std::size_t) const;
260         std::size_t dispatch(ReceiverBase &, const char *, std::size_t) const;
261
262         std::uint64_t get_hash() const;
263
264 private:
265         template<typename T>
266         static std::string get_type_signature();
267 };
268
269
270 template<typename P>
271 unsigned Protocol::get_packet_class_id()
272 {
273         static unsigned id = get_next_packet_class_id();
274         return id;
275 }
276
277 template<typename P>
278 Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add(unsigned id)
279 {
280         PacketTypeDef<P> *pdef = new PacketTypeDef<P>(id);
281         add_packet(pdef);
282         return PacketDefBuilder<P, NullSerializer<P> >(*this, *pdef, NullSerializer<P>());
283 }
284
285 template<typename P>
286 Protocol::PacketDefBuilder<P, Protocol::NullSerializer<P> > Protocol::add()
287 {
288         return add<P>(next_packet_id++);
289 }
290
291 template<typename P>
292 const Protocol::PacketTypeDef<P> &Protocol::get_packet_by_class() const
293 {
294         const PacketDefBase &pdef = get_packet_by_class_id(get_packet_class_id<P>());
295         return static_cast<const PacketTypeDef<P> &>(pdef);
296 }
297
298 template<typename P>
299 std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size) const
300 {
301         const PacketTypeDef<P> &pdef = get_packet_by_class<P>();
302         if(!pdef.get_id())
303                 throw std::invalid_argument("no packet id");
304         char *ptr = pdef.serialize(pkt, buf+4, buf+size);
305         size = ptr-buf;
306         header_def.serialize(PacketHeader(pdef.get_id(), size), buf, buf+4);
307         return size;
308 }
309
310 template<typename T>
311 std::string Protocol::get_type_signature()
312 {
313         const std::uint16_t sig = Traits<T>::signature;
314         std::string result;
315         result += sig&0xFF;
316         if(sig>=0x100)
317                 result += '0'+(sig>>8);
318         return result;
319 }
320
321
322 template<typename T, std::uint8_t K>
323 struct Protocol::BasicTraits
324 {
325         static const std::uint16_t signature = K | (sizeof(T)<<8);
326         typedef BasicSerializer<T> Serializer;
327 };
328
329 template<typename T>
330 struct Protocol::Traits
331 {
332         static const std::uint16_t signature = 'C';
333         typedef CompoundSerializer<T> Serializer;
334 };
335
336 template<> struct Protocol::Traits<bool>: BasicTraits<bool, 'B'> { };
337 template<> struct Protocol::Traits<std::int8_t>: BasicTraits<std::int8_t, 'I'> { };
338 template<> struct Protocol::Traits<std::uint8_t>: BasicTraits<std::uint8_t, 'U'> { };
339 template<> struct Protocol::Traits<std::int16_t>: BasicTraits<std::int16_t, 'I'> { };
340 template<> struct Protocol::Traits<std::uint16_t>: BasicTraits<std::uint16_t, 'U'> { };
341 template<> struct Protocol::Traits<std::int32_t>: BasicTraits<std::int32_t, 'I'> { };
342 template<> struct Protocol::Traits<std::uint32_t>: BasicTraits<std::uint32_t, 'U'> { };
343 template<> struct Protocol::Traits<std::int64_t>: BasicTraits<std::int64_t, 'I'> { };
344 template<> struct Protocol::Traits<std::uint64_t>: BasicTraits<std::uint64_t, 'U'> { };
345 template<> struct Protocol::Traits<float>: BasicTraits<float, 'F'> { };
346 template<> struct Protocol::Traits<double>: BasicTraits<double, 'F'> { };
347
348 template<> struct Protocol::Traits<std::string>
349 {
350         static const std::uint16_t signature = 'S';
351         typedef StringSerializer Serializer;
352 };
353
354 template<typename T>
355 struct Protocol::Traits<std::vector<T> >
356 {
357         static const std::uint16_t signature = 'A';
358         typedef ArraySerializer<std::vector<T> > Serializer;
359 };
360
361
362
363 template<typename C, typename S>
364 Protocol::CompoundDef<C, S>::CompoundDef(const S &s):
365         serializer(s)
366 { }
367
368 template<typename C, typename S>
369 std::string Protocol::CompoundDef<C, S>::describe() const
370 {
371         return "{"+serializer.describe()+"}";
372 }
373
374 template<typename C, typename S>
375 char *Protocol::CompoundDef<C, S>::serialize(const C &com, char *buf, char *end) const
376 {
377         return serializer.serialize(com, buf, end);
378 }
379
380 template<typename C, typename S>
381 const char *Protocol::CompoundDef<C, S>::deserialize(C &com, const char *buf, const char *end) const
382 {
383         return serializer.deserialize(com, buf, end);
384 }
385
386
387 template<typename A>
388 Protocol::ArraySerializer<A>::ArraySerializer(const Protocol &proto):
389         length_serializer(proto),
390         element_serializer(proto)
391 { }
392
393 template<typename A>
394 std::string Protocol::ArraySerializer<A>::describe() const
395 {
396         return "["+element_serializer.describe()+"]";
397 }
398
399 template<typename A>
400 char *Protocol::ArraySerializer<A>::serialize(const A &array, char *buf, char *end) const
401 {
402         buf = length_serializer.serialize(array.size(), buf, end);
403         for(typename A::const_iterator i=array.begin(); i!=array.end(); ++i)
404                 buf = element_serializer.serialize(*i, buf, end);
405         return buf;
406 }
407
408 template<typename A>
409 const char *Protocol::ArraySerializer<A>::deserialize(A &array, const char *buf, const char *end) const
410 {
411         std::uint16_t length;
412         buf = length_serializer.deserialize(length, buf, end);
413         array.resize(length);
414         for(unsigned i=0; i<length; ++i)
415                 buf = element_serializer.deserialize(array[i], buf, end);
416         return buf;
417 }
418
419
420 template<typename C>
421 Protocol::CompoundSerializer<C>::CompoundSerializer(const Protocol &proto):
422         def(proto.get_packet_by_class<C>().get_compound())
423 { }
424
425 template<typename C>
426 char *Protocol::CompoundSerializer<C>::serialize(const C &com, char *buf, char *end) const
427 {
428         return def.serialize(com, buf, end);
429 }
430
431 template<typename C>
432 const char *Protocol::CompoundSerializer<C>::deserialize(C &com, const char *buf, const char *end) const
433 {
434         return def.deserialize(com, buf, end);
435 }
436
437
438 template<typename P, typename Head, typename S>
439 Protocol::Serializer<P, Head, S>::Serializer(const Head &h, Pointer p, const Protocol &proto):
440         Head(h),
441         ptr(p),
442         ser(proto)
443 { }
444
445 template<typename P, typename Head, typename S>
446 std::string Protocol::Serializer<P, Head, S>::describe() const
447 {
448         return Head::describe()+ser.describe();
449 }
450
451 template<typename P, typename Head, typename S>
452 char *Protocol::Serializer<P, Head, S>::serialize(const P &pkt, char *buf, char *end) const
453 {
454         buf = Head::serialize(pkt, buf, end);
455         return ser.serialize(pkt.*ptr, buf, end);
456 }
457
458 template<typename P, typename Head, typename S>
459 const char *Protocol::Serializer<P, Head, S>::deserialize(P &pkt, const char *buf, const char *end) const
460 {
461         buf = Head::deserialize(pkt, buf, end);
462         return ser.deserialize(pkt.*ptr, buf, end);
463 }
464
465
466 template<typename P>
467 Protocol::PacketTypeDef<P>::PacketTypeDef(unsigned i):
468         PacketDefBase(i),
469         compound(new CompoundDef<P, NullSerializer<P> >(NullSerializer<P>()))
470 { }
471
472 template<typename P>
473 Protocol::PacketTypeDef<P>::~PacketTypeDef()
474 {
475         delete compound;
476 }
477
478 template<typename P>
479 template<typename S>
480 void Protocol::PacketTypeDef<P>::set_serializer(const S &ser)
481 {
482         delete compound;
483         compound = new CompoundDef<P, S>(ser);
484 }
485
486 template<typename P>
487 char *Protocol::PacketTypeDef<P>::serialize(const P &pkt, char *buf, char *end) const
488 {
489         return compound->serialize(pkt, buf, end);
490 }
491
492 template<typename P>
493 const char *Protocol::PacketTypeDef<P>::deserialize(P &pkt, const char *buf, const char *end) const
494 {
495         return compound->deserialize(pkt, buf, end);
496 }
497
498 template<typename P>
499 const char *Protocol::PacketTypeDef<P>::dispatch(ReceiverBase &rcv, const char *buf, const char *end) const
500 {
501         PacketReceiver<P> *prcv = dynamic_cast<PacketReceiver<P> *>(&rcv);
502         if(!prcv)
503                 throw bad_packet("unsupported");
504         P pkt;
505         buf = deserialize(pkt, buf, end);
506         prcv->receive(pkt);
507         return buf;
508 }
509
510
511 template<typename P, typename S>
512 Protocol::PacketDefBuilder<P, S>::PacketDefBuilder(const Protocol &p, PacketTypeDef<P> &k, const S &s):
513         protocol(p),
514         pktdef(k),
515         serializer(s)
516 { }
517
518 template<typename P, typename S>
519 template<typename T>
520 Protocol::PacketDefBuilder<P, typename S::template Next<T>::Type> Protocol::PacketDefBuilder<P, S>::operator()(T P::*ptr)
521 {
522         typename S::template Next<T>::Type next_ser(serializer, ptr, protocol);
523         pktdef.set_serializer(next_ser);
524         return PacketDefBuilder<P, typename S::template Next<T>::Type>(protocol, pktdef, next_ser);
525 }
526
527 } // namespace Net
528 } // namespace Msp
529
530 #endif