Bomberman Multiplayer
Authoritative multiplayer networking layer for Bomberman.
Loading...
Searching...
No Matches
NetSend.h
Go to the documentation of this file.
1#ifndef BOMBERMAN_NET_NETSEND_H
2#define BOMBERMAN_NET_NETSEND_H
3
4#include <array>
5#include <cstddef>
6
7#include <enet/enet.h>
8
9#include "NetCommon.h"
10#include "Util/Log.h"
11
17namespace bomberman::net
18{
19 // This layer is transport-focused: queue onto channels and flush explicitly.
20
29 template<std::size_t N>
30 [[nodiscard]]
31 bool queueOnChannel(ENetPeer* peer, EChannel channel, uint32_t flags, const std::array<uint8_t, N>& bytes)
32 {
33 const uint8_t channelId = static_cast<uint8_t>(channel);
34 ENetPacket* pkt = enet_packet_create(bytes.data(), bytes.size(), flags);
35 if (pkt == nullptr)
36 {
37 LOG_NET_PACKET_ERROR("Failed to allocate packet ({} bytes) for channel {}",
38 N, channelName(channelId));
39 return false;
40 }
41
42 if (enet_peer_send(peer, channelId, pkt) != 0)
43 {
44 LOG_NET_PACKET_ERROR("Failed to queue packet on channel {}", channelName(channelId));
45 enet_packet_destroy(pkt);
46 return false;
47 }
48
49 return true;
50 }
51
53 inline void flush(ENetHost* host)
54 {
55 if (host == nullptr) return;
56
57 enet_host_flush(host);
58 }
59
61 template<std::size_t N>
62 [[nodiscard]]
63 bool queueReliableControl(ENetPeer* peer, const std::array<uint8_t, N>& bytes)
64 {
65 return queueOnChannel(peer, EChannel::ControlReliable, ENET_PACKET_FLAG_RELIABLE, bytes);
66 }
67
69 template<std::size_t N>
70 [[nodiscard]]
71 bool queueReliableGame(ENetPeer* peer, const std::array<uint8_t, N>& bytes)
72 {
73 return queueOnChannel(peer, EChannel::GameplayReliable, ENET_PACKET_FLAG_RELIABLE, bytes);
74 }
75
77 template<std::size_t N>
78 [[nodiscard]]
79 bool queueUnreliableInput(ENetPeer* peer, const std::array<uint8_t, N>& bytes)
80 {
81 return queueOnChannel(peer, EChannel::InputUnreliable, 0, bytes);
82 }
83
85 template<std::size_t N>
86 [[nodiscard]]
87 bool queueUnreliableSnapshot(ENetPeer* peer, const std::array<uint8_t, N>& bytes)
88 {
89 return queueOnChannel(peer, EChannel::SnapshotUnreliable, 0, bytes);
90 }
91
93 template<std::size_t N>
94 [[nodiscard]]
95 bool queueUnreliableCorrection(ENetPeer* peer, const std::array<uint8_t, N>& bytes)
96 {
97 return queueOnChannel(peer, EChannel::CorrectionUnreliable, 0, bytes);
98 }
99
100} // namespace bomberman::net
101
102#endif // BOMBERMAN_NET_NETSEND_H
Shared client/server wire contract for the multiplayer protocol.
Shared multiplayer protocol types and transport-facing wire helpers.
Definition ClientPrediction.cpp:13
bool queueUnreliableInput(ENetPeer *peer, const std::array< uint8_t, N > &bytes)
Queues an unreliable input packet.
Definition NetSend.h:79
bool queueOnChannel(ENetPeer *peer, EChannel channel, uint32_t flags, const std::array< uint8_t, N > &bytes)
Queues a pre-serialized byte buffer on an explicit channel.
Definition NetSend.h:31
EChannel
ENet channel identifiers used by the current protocol.
Definition NetCommon.h:108
constexpr std::string_view channelName(uint8_t id)
Returns a human-readable name for a channel ID.
Definition NetCommon.h:118
void flush(ENetHost *host)
Flushes all currently queued packets for this host.
Definition NetSend.h:53
bool queueReliableGame(ENetPeer *peer, const std::array< uint8_t, N > &bytes)
Queues a reliable gameplay packet.
Definition NetSend.h:71
bool queueUnreliableSnapshot(ENetPeer *peer, const std::array< uint8_t, N > &bytes)
Queues an unreliable snapshot packet.
Definition NetSend.h:87
bool queueReliableControl(ENetPeer *peer, const std::array< uint8_t, N > &bytes)
Queues a reliable control packet.
Definition NetSend.h:63
bool queueUnreliableCorrection(ENetPeer *peer, const std::array< uint8_t, N > &bytes)
Queues an unreliable owner-correction packet.
Definition NetSend.h:95