xref: /aosp_15_r20/external/openscreen/platform/base/udp_packet.cc (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 #include "platform/base/udp_packet.h"
6 
7 #include <cassert>
8 #include <sstream>
9 
10 namespace openscreen {
11 
12 // static
13 const UdpPacket::size_type UdpPacket::kUdpMaxPacketSize = 1 << 16;
14 
UdpPacket()15 UdpPacket::UdpPacket() : std::vector<uint8_t>() {}
16 
UdpPacket(size_type size,uint8_t fill_value)17 UdpPacket::UdpPacket(size_type size, uint8_t fill_value)
18     : std::vector<uint8_t>(size, fill_value) {
19   assert(size <= kUdpMaxPacketSize);
20 }
21 
22 UdpPacket::UdpPacket(UdpPacket&& other) noexcept = default;
23 
UdpPacket(std::initializer_list<uint8_t> init)24 UdpPacket::UdpPacket(std::initializer_list<uint8_t> init)
25     : std::vector<uint8_t>(init) {
26   assert(size() <= kUdpMaxPacketSize);
27 }
28 
29 UdpPacket::~UdpPacket() = default;
30 
31 UdpPacket& UdpPacket::operator=(UdpPacket&& other) = default;
32 
ToString() const33 std::string UdpPacket::ToString() const {
34   // TODO(issuetracker.google.com/158660166): Change to use shared hex-to-string
35   // method.
36   static constexpr char hex[] = "0123456789ABCDEF";
37   std::stringstream ss;
38   ss << "[";
39   for (auto it = begin(); it != end(); it++) {
40     ss << hex[*it / 16] << hex[*it % 16] << " ";
41   }
42   ss << "]";
43   return ss.str();
44 }
45 
46 }  // namespace openscreen
47