void Packet::writePacketTypeAndVersion(PacketType::Value type) { // Pack the packet type auto offset = packArithmeticallyCodedValue(type, _packet.get()); // Pack the packet version auto version = versionForPacketType(type); memcpy(_packet.get() + offset, &version, sizeof(version)); }
int packArithmeticallyCodedValue(int value, char* destination) { if (value < 255) { // less than 255, just pack our value destination[0] = (uchar) value; return 1; } else { // pack 255 and then recursively pack on ((unsigned char*)destination)[0] = 255; return 1 + packArithmeticallyCodedValue(value - 255, destination + 1); } }
int populatePacketHeader(char* packet, PacketType type, const QUuid& connectionUUID) { int numTypeBytes = packArithmeticallyCodedValue(type, packet); packet[numTypeBytes] = versionForPacketType(type); QUuid packUUID = connectionUUID.isNull() ? NodeList::getInstance()->getOwnerUUID() : connectionUUID; QByteArray rfcUUID = packUUID.toRfc4122(); memcpy(packet + numTypeBytes + sizeof(PacketVersion), rfcUUID.constData(), NUM_BYTES_RFC4122_UUID); // return the number of bytes written for pointer pushing return numTypeBytes + sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID; }
int populatePacketHeader(char* packet, PacketType type, const QUuid& connectionUUID) { int numTypeBytes = packArithmeticallyCodedValue(type, packet); packet[numTypeBytes] = versionForPacketType(type); char* position = packet + numTypeBytes + sizeof(PacketVersion); QUuid packUUID = connectionUUID.isNull() ? NodeList::getInstance()->getSessionUUID() : connectionUUID; QByteArray rfcUUID = packUUID.toRfc4122(); memcpy(position, rfcUUID.constData(), NUM_BYTES_RFC4122_UUID); position += NUM_BYTES_RFC4122_UUID; // pack 16 bytes of zeros where the md5 hash will be placed one data is packed memset(position, 0, NUM_BYTES_MD5_HASH); position += NUM_BYTES_MD5_HASH; // return the number of bytes written for pointer pushing return position - packet; }