xref: /aosp_15_r20/external/openscreen/platform/base/udp_packet.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef PLATFORM_BASE_UDP_PACKET_H_
6 #define PLATFORM_BASE_UDP_PACKET_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include <utility>
12 #include <vector>
13 
14 #include "platform/base/ip_address.h"
15 
16 namespace openscreen {
17 
18 class UdpSocket;
19 
20 // A move-only std::vector of bytes that may not exceed the maximum possible
21 // size of a UDP packet. Implicit copy construction/assignment is disabled to
22 // prevent hidden copies (i.e., those not explicitly coded).
23 class UdpPacket : public std::vector<uint8_t> {
24  public:
25   // C++14 vector constructors, sans Allocator foo, and no copy ctor.
26   UdpPacket();
27   explicit UdpPacket(size_type size, uint8_t fill_value = {});
28   template <typename InputIt>
UdpPacket(InputIt first,InputIt last)29   UdpPacket(InputIt first, InputIt last) : std::vector<uint8_t>(first, last) {}
30   UdpPacket(UdpPacket&& other) noexcept;
31   UdpPacket(std::initializer_list<uint8_t> init);
32 
33   ~UdpPacket();
34 
35   UdpPacket& operator=(UdpPacket&& other);
36 
source()37   const IPEndpoint& source() const { return source_; }
set_source(IPEndpoint endpoint)38   void set_source(IPEndpoint endpoint) { source_ = std::move(endpoint); }
39 
destination()40   const IPEndpoint& destination() const { return destination_; }
set_destination(IPEndpoint endpoint)41   void set_destination(IPEndpoint endpoint) {
42     destination_ = std::move(endpoint);
43   }
44 
socket()45   UdpSocket* socket() const { return socket_; }
set_socket(UdpSocket * socket)46   void set_socket(UdpSocket* socket) { socket_ = socket; }
47 
48   std::string ToString() const;
49 
50   static const size_type kUdpMaxPacketSize;
51 
52  private:
53   IPEndpoint source_ = {};
54   IPEndpoint destination_ = {};
55   UdpSocket* socket_ = nullptr;
56 
57   OSP_DISALLOW_COPY_AND_ASSIGN(UdpPacket);
58 };
59 
60 }  // namespace openscreen
61 
62 #endif  // PLATFORM_BASE_UDP_PACKET_H_
63