jsocketpp 1.0
A cross-platform C++20 socket library.
Loading...
Searching...
No Matches
DatagramPacket.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "common.hpp"
9
10#include <string>
11#include <vector>
12
13namespace jsocketpp
14{
15
48{
49 public:
56 std::vector<char> buffer;
57
63 std::string address{};
64
71
76 explicit DatagramPacket(const size_t size = 0) : buffer(size) {}
77
84 DatagramPacket(std::string_view data, std::string addr, const Port prt)
85 : buffer(data.begin(), data.end()), address(std::move(addr)), port(prt)
86 {
87 }
88
97 DatagramPacket(const char* data, const size_t len, std::string addr, const Port prt)
98 : buffer(data, data + len), address(std::move(addr)), port(prt)
99 {
100 }
101
102 // Default copy and move constructors/operators.
104 DatagramPacket(DatagramPacket&&) noexcept = default;
105 DatagramPacket& operator=(const DatagramPacket&) = default;
106 DatagramPacket& operator=(DatagramPacket&&) noexcept = default;
107
112 void resize(const size_t newSize) { buffer.resize(newSize); }
113
118 [[nodiscard]] size_t size() const noexcept { return buffer.size(); }
119
123 void clear()
124 {
125 buffer.clear();
126 address.clear();
127 port = 0;
128 }
129};
130
131} // namespace jsocketpp
DatagramPacket(const char *data, const size_t len, std::string addr, const Port prt)
Construct a DatagramPacket from a raw pointer/size and destination info.
Definition DatagramPacket.hpp:97
void resize(const size_t newSize)
Resize the packet's internal buffer.
Definition DatagramPacket.hpp:112
size_t size() const noexcept
Get the current size of the buffer (valid bytes for receive/send).
Definition DatagramPacket.hpp:118
DatagramPacket(std::string_view data, std::string addr, const Port prt)
Construct a DatagramPacket from a string_view and destination info.
Definition DatagramPacket.hpp:84
std::vector< char > buffer
Data buffer for the packet payload.
Definition DatagramPacket.hpp:56
DatagramPacket(DatagramPacket &&) noexcept=default
Port port
Remote UDP port for the destination/source.
Definition DatagramPacket.hpp:70
void clear()
Reset the packet (clears buffer, address, and port).
Definition DatagramPacket.hpp:123
DatagramPacket(const size_t size=0)
Construct an empty DatagramPacket with a specified buffer size.
Definition DatagramPacket.hpp:76
std::string address
Remote address (IPv4/IPv6) for the destination/source.
Definition DatagramPacket.hpp:63
DatagramPacket(const DatagramPacket &)=default
Common platform and utility includes for jsocketpp.
std::uint16_t Port
Type alias representing a TCP or UDP port number (1–65535).
Definition common.hpp:315
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:13