FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_protocol.inl
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17namespace fge::net
18{
19
20inline ProtocolPacket::ProtocolPacket(Header header, Realm realmId, CountId countId)
21{
22 this->operator<<(header) << realmId << countId;
23}
24
25inline bool ProtocolPacket::haveCorrectHeaderSize() const
26{
27 return this->getDataSize() >= HeaderSize;
28}
29inline std::optional<ProtocolPacket::Header> ProtocolPacket::retrieveHeader() const
30{
31 if (this->haveCorrectHeaderSize())
32 {
33 Header header;
34 this->unpack(HeaderIdPosition, &header, sizeof(Header));
35 return header;
36 }
37 return std::nullopt;
38}
39inline std::optional<ProtocolPacket::Header> ProtocolPacket::retrieveHeaderId() const
40{
41 if (this->haveCorrectHeaderSize())
42 {
43 Header headerId;
44 this->unpack(HeaderIdPosition, &headerId, sizeof(Header));
45 return headerId & ~FGE_NET_HEADER_FLAGS_MASK;
46 }
47 return std::nullopt;
48}
49inline std::optional<ProtocolPacket::Realm> ProtocolPacket::retrieveRealm() const
50{
51 if (this->haveCorrectHeaderSize())
52 {
53 Realm realm;
54 this->unpack(RealmPosition, &realm, sizeof(Realm));
55 return realm;
56 }
57 return std::nullopt;
58}
59inline std::optional<ProtocolPacket::CountId> ProtocolPacket::retrieveCountId() const
60{
61 if (this->haveCorrectHeaderSize())
62 {
63 CountId countId;
64 this->unpack(CountIdPosition, &countId, sizeof(CountId));
65 return countId;
66 }
67 return std::nullopt;
68}
69
70inline void ProtocolPacket::setHeader(Header header)
71{
72 this->pack(HeaderIdPosition, &header, sizeof(Header));
73}
74inline void ProtocolPacket::setHeaderId(Header headerId)
75{
76 if (this->haveCorrectHeaderSize())
77 {
78 Header header;
79 this->unpack(HeaderIdPosition, &header, sizeof(Header));
80 header = (header & FGE_NET_HEADER_FLAGS_MASK) | (headerId & ~FGE_NET_HEADER_FLAGS_MASK);
81 this->pack(HeaderIdPosition, &header, sizeof(Header));
82 }
83}
84inline void ProtocolPacket::setHeaderFlags(Header headerFlags)
85{
86 if (this->haveCorrectHeaderSize())
87 {
88 Header header;
89 this->unpack(HeaderIdPosition, &header, sizeof(Header));
90 header = (header & ~FGE_NET_HEADER_FLAGS_MASK) | (headerFlags & FGE_NET_HEADER_FLAGS_MASK);
91 this->pack(HeaderIdPosition, &header, sizeof(Header));
92 }
93}
94inline void ProtocolPacket::addHeaderFlags(Header headerFlags)
95{
96 if (this->haveCorrectHeaderSize())
97 {
98 Header headerId;
99 this->unpack(HeaderIdPosition, &headerId, sizeof(Header));
100 headerId |= headerFlags & FGE_NET_HEADER_FLAGS_MASK;
101 this->pack(HeaderIdPosition, &headerId, sizeof(Header));
102 }
103}
104inline void ProtocolPacket::removeHeaderFlags(Header headerFlags)
105{
106 if (this->haveCorrectHeaderSize())
107 {
108 Header headerId;
109 this->unpack(HeaderIdPosition, &headerId, sizeof(Header));
110 headerId &= ~headerFlags & FGE_NET_HEADER_FLAGS_MASK;
111 this->pack(HeaderIdPosition, &headerId, sizeof(Header));
112 }
113}
114inline void ProtocolPacket::setRealm(Realm realmId)
115{
116 this->pack(RealmPosition, &realmId, sizeof(Realm));
117}
118inline void ProtocolPacket::setCountId(CountId countId)
119{
120 this->pack(CountIdPosition, &countId, sizeof(CountId));
121}
122
123} // namespace fge::net