WOWCube Docs logo
WOWCube Docs
Mission Control
Section Shortcuts
APIExamplesSourceWOWConnectChangelog
Filters
SDK and language defaults persist in cookies.
SDK version
Navigation Tree
Collapsed by default, focused on the active path.
Made byMcKay Seamons
GitHub
  1. Home
  2. Docs
  3. Source
  4. network.inc
Mission NodeSDK 6.1Pawnnetwork.inc

network.inc

SDK Source File: network.inc

Source / SDK 6.1 / Pawn / Core

network.inc

network.inc
CPP
1/****h* PawnLibs/network
2 * Summary
3 * A helper library for packing and unpacking network data.
4 * Description
5 * Pawn works with cells which has a size of 4 bytes. Native interfaces
6 * handling generic data like network messages have size limits for that data.
7 * If application does not need to work with a large numbers, several values
8 * can be packed into a single cell. This library provides some convenient
9 * functions to pack and unpack such values from array of cells which can be
10 * broadcasted over network by core functions.
11 * Example
12 * TBD
13 * History
14 * * v6.0 - added
15 ******/
16#define CELL_BITS 32
17#define BYTE_ON_CELL 4
18new network_pos = 0;
19
20new network_data[MAX_PACKET_SIZE];
21
22/****f* PawnLibs/network/NetWorkReset
23 * Summary
24 * Reset state before storing new data to pack.
25 * Synopsis
26 */
27NetWorkReset()
28/*
29 * Source
30 */
31{
32 network_pos = 0;
33 for (new i = 0; i < MAX_PACKET_SIZE; i++) {
34 network_data[i] = 0;
35 }
36}
37/******/
38
39/****f* PawnLibs/network/NetWorkWriteValue
40 * Summary
41 * Store specified number of bits from some unsigned value.
42 * Synopsis
43 */
44NetWorkWriteValue(value, bits)
45/*
46 * Inputs
47 * * value - a value to store data from
48 * * bits - a number of bits to store
49 * Source
50 */
51{
52
53 if (NetworkCheckOverflow(bits)) return;
54
55 if (value < 0) {
56 LOG_i("NetworkMessage::WriteInt: negative value %d may be sent incorrectly, use WriteSignedInt instead ", value);
57 }
58
59 new freeBits = NetworkGetFreeBits();
60 new intermediate_result;
61
62 if (freeBits >= bits) {
63 network_data[NetworkGetCell()] |= value << NetworkGetBits();
64 network_pos += bits;
65 } else {
66 intermediate_result = value >> freeBits
67 new set = value - (intermediate_result << freeBits);
68 network_data[NetworkGetCell()] |= set << NetworkGetBits();
69 network_pos += freeBits;
70
71 set = (value - set) >> freeBits;
72 network_data[NetworkGetCell()] |= set;
73 network_pos += bits - freeBits;
74 }
75
76}
77/******/
78
79
80/****f* PawnLibs/network/NetWorkWriteSignedValue
81 * Summary
82 * Store specified number of bits from some signed value.
83 * Synopsis
84 */
85NetWorkWriteSignedValue(value, bits)
86/*
87 * Inputs
88 * * value - a value to store data from
89 * * bits - a number of bits to store
90 * Source
91 */
92{
93 if (NetworkCheckOverflow(bits + 1)) return;
94
95 NetWorkWriteValue(value >>> (CELL_BITS - 1), 1);
96 NetWorkWriteValue(ABS(value), bits);
97}
98/******/
99
100/****f* PawnLibs/network/NetWorkReadValue
101 * Summary
102 * Read specified number of bits of packed unsigned value.
103 * Synopsis
104 */
105NetWorkReadValue(bits)
106/*
107 * Inputs
108 * * bits - a number of bits to get
109 * Return value
110 * Unpacked unsigned value.
111 * Source
112 */
113{
114 if (NetworkCheckOverflow(bits)) return 0;
115
116 new value = 0;
117 new freeBits = NetworkGetFreeBits();
118 new intermediate_result;
119
120 if (NetworkGetFreeBits() >= bits) {
121 value = network_data[NetworkGetCell()] << (NetworkGetFreeBits() - bits);
122 value = value >>> (NetworkGetFreeBits() - bits + NetworkGetBits());
123 network_pos += bits;
124 } else {
125 value = network_data[NetworkGetCell()] >>> (NetworkGetBits());
126 network_pos += freeBits;
127
128 intermediate_result = (network_data[NetworkGetCell()] << (CELL_BITS - (bits - freeBits))) >>> (CELL_BITS - (bits - freeBits));
129 value |= intermediate_result << freeBits;
130 network_pos += bits - freeBits;
131
132 }
133
134 return value;
135}
136/******/
137
138/****f* PawnLibs/network/NetWorkReadSignedValue
139 * Summary
140 * Read specified number of bits of packed signed value.
141 * Synopsis
142 */
143NetWorkReadSignedValue(bits)
144/*
145 * Inputs
146 * * bits - a number of bits to get
147 * Return value
148 * Unpacked signed value.
149 * Source
150 */
151{
152 if (NetworkCheckOverflow(bits + 1)) return 0;
153
154 new value = NetWorkReadValue(1) == 0 ? 1 : - 1;
155 value *= NetWorkReadValue(bits);
156
157 return value;
158}
159/******/
160
161/****f* PawnLibs/network/NetWorkSetData
162 * Summary
163 * Populate state with a new data to unpack.
164 * Synopsis
165 */
166NetWorkSetData(const pkt[], size)
167/*
168 * Inputs
169 * * pkt - packet data to unpack
170 * Source
171 */
172{
173 for (new i = 0; i < size / BYTE_ON_CELL + (size % BYTE_ON_CELL ? 1 : 0); i++)
174 network_data[i] = pkt[i];
175}
176/*
177 * See also
178 * * ON_Packet()
179 ******/
180
181/****f* PawnLibs/network/NetWorkGetData
182 * Summary
183 * Get packed data to broadcast.
184 * Synopsis
185 */
186NetWorkGetData()
187/*
188 * Return value
189 * Packed data to broadcast.
190 * Source
191 */
192{
193 return network_data;
194}
195/*
196 * See also
197 * * broadcastPacket()
198 ******/
199
200/****f* PawnLibs/network/NetWorkGetDataSize
201 * Summary
202 * Get packed data to broadcast.
203 * Synopsis
204 */
205NetWorkGetDataSize()
206/*
207 * Return value
208 * Packed size to broadcast.
209 * Source
210 */
211{
212 return network_pos / CELL_BITS + (network_pos % CELL_BITS ? 1 : 0);
213}
214/*
215 * See also
216 * * broadcastPacket()
217 ******/
218
219NetworkGetCell() {
220 return network_pos / CELL_BITS;
221}
222NetworkGetFreeBits() {
223 return CELL_BITS - network_pos % CELL_BITS;
224}
225NetworkGetBits() {
226 return network_pos % CELL_BITS;
227}
228
229NetworkCheckOverflow(bits) {
230 if (network_pos + bits > MAX_PACKET_SIZE * CELL_BITS) {
231 LOG_i("NetworkMessage: maximum message size exceeded");
232 return true;
233 }
234
235 return false;
236}
237
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.
Context Rail

Related nodes

fixed.inc
Source / SDK 6.1 / Pawn / Core
graphics.inc
Source / SDK 6.1 / Pawn / Core
leaderboard.inc
Source / SDK 6.1 / Pawn / Core
log.inc
Source / SDK 6.1 / Pawn / Core
Previous Node
motion sensor.inc
Source / SDK 6.1 / Pawn / Core
Next Node
physics.inc
Source / SDK 6.1 / Pawn / Core