vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
udp_socket.h
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <vector>
6
7#include "platform/net/net_address.h"
8
9namespace Vkm::Engine {
10
17class UdpSocket {
18 public:
26 static constexpr size_t MAX_DATAGRAM = 1200;
27
28 public:
29 UdpSocket() = default;
30 ~UdpSocket();
31
32 UdpSocket(const UdpSocket& other) = delete;
33 UdpSocket& operator=(const UdpSocket& other) = delete;
34
35 UdpSocket(UdpSocket && other) = delete;
36 UdpSocket& operator=(UdpSocket && other) = delete;
37
38 public:
45 [[nodiscard]] bool open(uint16_t port);
46
48 void close();
49
61 bool send(const NetAddress& to, const uint8_t* bytes, size_t size);
62
73 [[nodiscard]] bool receive(std::vector<uint8_t>& out, NetAddress& from);
74
83 NetAddress localAddress() const { return m_local; }
84
85 bool isOpen() const { return m_handle != INVALID; }
86
87 private:
91 static constexpr intptr_t INVALID = -1;
92
93 private:
99 void noteOversized();
100
101 private:
102 intptr_t m_handle = INVALID;
103
104 bool m_readFailed = false;
105 bool m_oversized = false;
106 bool m_writeFailed = false;
107 NetAddress m_local;
108};
109
110} // namespace Vkm::Engine
void close()
Close it. Safe to call on one that was never opened.
bool open(uint16_t port)
Take a port and start listening on every interface.
bool receive(std::vector< uint8_t > &out, NetAddress &from)
Take the next datagram, if one is waiting.
NetAddress localAddress() const
Loopback and the port this is bound to, including one the system picked for zero.
Definition udp_socket.h:83
static constexpr size_t MAX_DATAGRAM
The largest datagram this will send.
Definition udp_socket.h:26
bool send(const NetAddress &to, const uint8_t *bytes, size_t size)
Send size bytes to to.
Where a datagram came from, or is going to: an IPv4 host and a port.
Definition net_address.h:13