rpc_envelope.h
1 // Copyright 2018, Beeri 15. All rights reserved.
2 // Author: Roman Gershman (romange@gmail.com)
3 //
4 #pragma once
5 
6 #include "base/pod_array.h"
7 #include "util/asio/asio_utils.h"
8 
9 namespace util {
10 namespace rpc {
11 
12 typedef base::PODArray<uint8_t> BufferType;
13 
14 class Envelope {
15  public:
16  BufferType header, letter;
17 
18  Envelope() = default;
19 
20  Envelope(Envelope&& other) noexcept {
21  Swap(&other);
22  }
23 
24  Envelope(size_t hsz, size_t lsz) {
25  Resize(hsz, lsz);
26  }
27 
28  void Clear() {
29  header.clear();
30  letter.clear();
31  }
32 
33  void Resize(size_t hsz, size_t lsz) {
34  header.resize(hsz);
35  letter.resize(lsz);
36  }
37 
38  auto buf_seq() { return make_buffer_seq(header, letter); }
39 
40  void Swap(Envelope* other) {
41  if (other != this) {
42  other->header.swap(header);
43  other->letter.swap(letter);
44  }
45  }
46 
47  Envelope& operator=(Envelope&& other) noexcept {
48  Swap(&other);
49  return *this;
50  }
51 };
52 
53 } // namespace rpc
54 } // namespace util
55