network.inc
network.inc
CPP1/****h* PawnLibs/network2 * Summary3 * A helper library for packing and unpacking network data.4 * Description5 * Pawn works with cells which has a size of 4 bytes. Native interfaces6 * 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 values8 * can be packed into a single cell. This library provides some convenient9 * functions to pack and unpack such values from array of cells which can be10 * broadcasted over network by core functions.11 * Example12 * TBD13 * History14 * * v6.0 - added15 ******/16#define CELL_BITS 3217#define BYTE_ON_CELL 418new network_pos = 0;1920new network_data[MAX_PACKET_SIZE];2122/****f* PawnLibs/network/NetWorkReset23 * Summary24 * Reset state before storing new data to pack.25 * Synopsis26 */27NetWorkReset()28/*29 * Source30 */31{32 network_pos = 0;33 for (new i = 0; i < MAX_PACKET_SIZE; i++) {34 network_data[i] = 0;35 }36}37/******/3839/****f* PawnLibs/network/NetWorkWriteValue40 * Summary41 * Store specified number of bits from some unsigned value.42 * Synopsis43 */44NetWorkWriteValue(value, bits)45/*46 * Inputs47 * * value - a value to store data from48 * * bits - a number of bits to store49 * Source50 */51{5253 if (NetworkCheckOverflow(bits)) return;5455 if (value < 0) {56 LOG_i("NetworkMessage::WriteInt: negative value %d may be sent incorrectly, use WriteSignedInt instead ", value);57 }5859 new freeBits = NetworkGetFreeBits();60 new intermediate_result;6162 if (freeBits >= bits) {63 network_data[NetworkGetCell()] |= value << NetworkGetBits();64 network_pos += bits;65 } else {66 intermediate_result = value >> freeBits67 new set = value - (intermediate_result << freeBits);68 network_data[NetworkGetCell()] |= set << NetworkGetBits();69 network_pos += freeBits;7071 set = (value - set) >> freeBits;72 network_data[NetworkGetCell()] |= set;73 network_pos += bits - freeBits;74 }7576}77/******/787980/****f* PawnLibs/network/NetWorkWriteSignedValue81 * Summary82 * Store specified number of bits from some signed value.83 * Synopsis84 */85NetWorkWriteSignedValue(value, bits)86/*87 * Inputs88 * * value - a value to store data from89 * * bits - a number of bits to store90 * Source91 */92{93 if (NetworkCheckOverflow(bits + 1)) return;9495 NetWorkWriteValue(value >>> (CELL_BITS - 1), 1);96 NetWorkWriteValue(ABS(value), bits);97}98/******/99100/****f* PawnLibs/network/NetWorkReadValue101 * Summary102 * Read specified number of bits of packed unsigned value.103 * Synopsis104 */105NetWorkReadValue(bits)106/*107 * Inputs108 * * bits - a number of bits to get109 * Return value110 * Unpacked unsigned value.111 * Source112 */113{114 if (NetworkCheckOverflow(bits)) return 0;115116 new value = 0;117 new freeBits = NetworkGetFreeBits();118 new intermediate_result;119120 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;127128 intermediate_result = (network_data[NetworkGetCell()] << (CELL_BITS - (bits - freeBits))) >>> (CELL_BITS - (bits - freeBits));129 value |= intermediate_result << freeBits;130 network_pos += bits - freeBits;131132 }133134 return value;135}136/******/137138/****f* PawnLibs/network/NetWorkReadSignedValue139 * Summary140 * Read specified number of bits of packed signed value.141 * Synopsis142 */143NetWorkReadSignedValue(bits)144/*145 * Inputs146 * * bits - a number of bits to get147 * Return value148 * Unpacked signed value.149 * Source150 */151{152 if (NetworkCheckOverflow(bits + 1)) return 0;153154 new value = NetWorkReadValue(1) == 0 ? 1 : - 1;155 value *= NetWorkReadValue(bits);156157 return value;158}159/******/160161/****f* PawnLibs/network/NetWorkSetData162 * Summary163 * Populate state with a new data to unpack.164 * Synopsis165 */166NetWorkSetData(const pkt[], size)167/*168 * Inputs169 * * pkt - packet data to unpack170 * Source171 */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 also178 * * ON_Packet()179 ******/180181/****f* PawnLibs/network/NetWorkGetData182 * Summary183 * Get packed data to broadcast.184 * Synopsis185 */186NetWorkGetData()187/*188 * Return value189 * Packed data to broadcast.190 * Source191 */192{193 return network_data;194}195/*196 * See also197 * * broadcastPacket()198 ******/199200/****f* PawnLibs/network/NetWorkGetDataSize201 * Summary202 * Get packed data to broadcast.203 * Synopsis204 */205NetWorkGetDataSize()206/*207 * Return value208 * Packed size to broadcast.209 * Source210 */211{212 return network_pos / CELL_BITS + (network_pos % CELL_BITS ? 1 : 0);213}214/*215 * See also216 * * broadcastPacket()217 ******/218219NetworkGetCell() {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}228229NetworkCheckOverflow(bits) {230 if (network_pos + bits > MAX_PACKET_SIZE * CELL_BITS) {231 LOG_i("NetworkMessage: maximum message size exceeded");232 return true;233 }234235 return false;236}237
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.