aboutsummaryrefslogtreecommitdiff
path: root/platform/base/udp_packet.h
blob: b25cb8c1c87ea18c311f8992fc23256a11777829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file

#ifndef PLATFORM_BASE_UDP_PACKET_H_
#define PLATFORM_BASE_UDP_PACKET_H_

#include <stdint.h>

#include <algorithm>
#include <utility>
#include <vector>

#include "platform/base/ip_address.h"

namespace openscreen {

class UdpSocket;

// A move-only std::vector of bytes that may not exceed the maximum possible
// size of a UDP packet. Implicit copy construction/assignment is disabled to
// prevent hidden copies (i.e., those not explicitly coded).
class UdpPacket : public std::vector<uint8_t> {
 public:
  // C++14 vector constructors, sans Allocator foo, and no copy ctor.
  UdpPacket();
  explicit UdpPacket(size_type size);
  explicit UdpPacket(size_type size, uint8_t fill_value);
  template <typename InputIt>
  UdpPacket(InputIt first, InputIt last)
      : UdpPacket(std::distance(first, last)) {
    std::copy(first, last, begin());
  }
  UdpPacket(UdpPacket&& other);
  UdpPacket(std::initializer_list<uint8_t> init);

  ~UdpPacket();

  UdpPacket& operator=(UdpPacket&& other);

  const IPEndpoint& source() const { return source_; }
  void set_source(IPEndpoint endpoint) { source_ = std::move(endpoint); }

  const IPEndpoint& destination() const { return destination_; }
  void set_destination(IPEndpoint endpoint) {
    destination_ = std::move(endpoint);
  }

  UdpSocket* socket() const { return socket_; }
  void set_socket(UdpSocket* socket) { socket_ = socket; }

  static constexpr size_type kUdpMaxPacketSize = 1 << 16;

 private:
  IPEndpoint source_ = {};
  IPEndpoint destination_ = {};
  UdpSocket* socket_ = nullptr;

  OSP_DISALLOW_COPY_AND_ASSIGN(UdpPacket);
};

}  // namespace openscreen

#endif  // PLATFORM_BASE_UDP_PACKET_H_