Showing the changes to interface/src/DataServerClient.cpp (1 of 3 files). Show all
Unified · Split
| @@ -6,15 +6,18 @@ · 5 unmodified lines | ||
| 6 | 6 | // Copyright (c) 2013 HighFidelity, Inc. All rights reserved. |
| 7 | 7 | // |
| 8 | 8 | |
| 9 | +#include <QtCore/QUrl> | |
| 10 | + | |
| 9 | 11 | #include <NodeList.h> |
| 10 | 12 | #include <PacketHeaders.h> |
| 11 | 13 | #include <UDPSocket.h> |
| 12 | 14 | #include <UUID.h> |
| 13 | 15 | |
| 16 | +#include "Application.h" | |
| 14 | 17 | #include "DataServerClient.h" |
| 15 | 18 | |
| 16 | 19 | QString DataServerClient::_clientUsername; |
| 17 | −std::vector<unsigned char*> DataServerClient::_unconfirmedPackets; | |
| 20 | +std::vector<unsigned char*> DataServerClient::_unmatchedPackets; | |
| 18 | 21 | |
| 19 | 22 | const char DATA_SERVER_HOSTNAME[] = "127.0.0.1"; |
| 20 | 23 | const unsigned short DATA_SERVER_PORT = 3282; |
| @@ -27,9 +30,10 @@ void DataServerClient::putValueForKey(const char* key, const char* value) { · 6 unmodified lines | ||
| 27 | 30 | // setup the header for this packet |
| 28 | 31 | int numPacketBytes = populateTypeAndVersion(putPacket, PACKET_TYPE_DATA_SERVER_PUT); |
| 29 | 32 | |
| 30 | − // pack the client UUID | |
| 33 | + // pack the client UUID, null terminated | |
| 31 | 34 | memcpy(putPacket + numPacketBytes, _clientUsername.toLocal8Bit().constData(), _clientUsername.toLocal8Bit().size()); |
| 32 | 35 | numPacketBytes += _clientUsername.toLocal8Bit().size(); |
| 36 | + putPacket[numPacketBytes++] = '\0'; | |
| 33 | 37 | |
| 34 | 38 | // pack the key, null terminated |
| 35 | 39 | strcpy((char*) putPacket + numPacketBytes, key); |
| @@ -42,7 +46,7 @@ void DataServerClient::putValueForKey(const char* key, const char* value) { · 6 unmodified lines | ||
| 42 | 46 | putPacket[numPacketBytes++] = '\0'; |
| 43 | 47 | |
| 44 | 48 | // add the putPacket to our vector of unconfirmed packets, will be deleted once put is confirmed |
| 45 | − _unconfirmedPackets.push_back(putPacket); | |
| 49 | + _unmatchedPackets.push_back(putPacket); | |
| 46 | 50 | |
| 47 | 51 | // send this put request to the data server |
| 48 | 52 | NodeList::getInstance()->getNodeSocket()->send((sockaddr*) &DATA_SERVER_SOCKET, putPacket, numPacketBytes); |
| @@ -62,9 +66,10 @@ void DataServerClient::getValueForKeyAndUserString(const char* key, QString& use · 13 unmodified lines | ||
| 62 | 66 | // setup the header for this packet |
| 63 | 67 | int numPacketBytes = populateTypeAndVersion(getPacket, PACKET_TYPE_DATA_SERVER_GET); |
| 64 | 68 | |
| 65 | − // pack the user string (could be username or UUID string) | |
| 69 | + // pack the user string (could be username or UUID string), null-terminate | |
| 66 | 70 | memcpy(getPacket + numPacketBytes, userString.toLocal8Bit().constData(), userString.toLocal8Bit().size()); |
| 67 | 71 | numPacketBytes += userString.toLocal8Bit().size(); |
| 72 | + getPacket[numPacketBytes++] = '\0'; | |
| 68 | 73 | |
| 69 | 74 | // pack the key, null terminated |
| 70 | 75 | strcpy((char*) getPacket + numPacketBytes, key); |
| @@ -77,14 +82,14 @@ void DataServerClient::getValueForKeyAndUserString(const char* key, QString& use · 6 unmodified lines | ||
| 77 | 82 | |
| 78 | 83 | void DataServerClient::processConfirmFromDataServer(unsigned char* packetData, int numPacketBytes) { |
| 79 | 84 | |
| 80 | − for (std::vector<unsigned char*>::iterator unconfirmedPacket = _unconfirmedPackets.begin(); | |
| 81 | − unconfirmedPacket != _unconfirmedPackets.end(); | |
| 85 | + for (std::vector<unsigned char*>::iterator unconfirmedPacket = _unmatchedPackets.begin(); | |
| 86 | + unconfirmedPacket != _unmatchedPackets.end(); | |
| 82 | 87 | ++unconfirmedPacket) { |
| 83 | 88 | if (memcmp(*unconfirmedPacket + sizeof(PACKET_TYPE), |
| 84 | 89 | packetData + sizeof(PACKET_TYPE), |
| 85 | 90 | numPacketBytes - sizeof(PACKET_TYPE)) == 0) { |
| 86 | 91 | // this is a match - remove the confirmed packet from the vector so it isn't sent back out |
| 87 | − _unconfirmedPackets.erase(unconfirmedPacket); | |
| 92 | + _unmatchedPackets.erase(unconfirmedPacket); | |
| 88 | 93 | |
| 89 | 94 | // we've matched the packet - bail out |
| 90 | 95 | break; |
| @@ -95,6 +100,46 @@ void DataServerClient::processConfirmFromDataServer(unsigned char* packetData, i · 4 unmodified lines | ||
| 95 | 100 | } |
| 96 | 101 | } |
| 97 | 102 | |
| 98 | −void DataServerClient::processGetFromDataServer(unsigned char* packetData, int numPacketBytes) { | |
| 103 | +void DataServerClient::processSendFromDataServer(unsigned char* packetData, int numPacketBytes) { | |
| 104 | + // pull the user string from the packet so we know who to associate this with | |
| 105 | + int numHeaderBytes = numBytesForPacketHeader(packetData); | |
| 106 | + | |
| 107 | + QString userString(QByteArray((char*) packetData + numHeaderBytes, strlen((char*) packetData + numHeaderBytes))); | |
| 108 | + | |
| 109 | + QUuid userUUID(userString); | |
| 99 | 110 | |
| 111 | + if (userUUID.isNull()) { | |
| 112 | + // the user string was a username | |
| 113 | + // for now assume this means that it is for our avatar | |
| 114 | + | |
| 115 | + char* dataKeyPosition = (char*) packetData + numHeaderBytes + sizeof(userString); | |
| 116 | + | |
| 117 | + if (strcmp(dataKeyPosition, DataServerKey::FaceMeshURL) == 0) { | |
| 118 | + // pull the user's face mesh and set it on the Avatar instance | |
| 119 | + char* faceMeshPosition = dataKeyPosition + strlen(dataKeyPosition) + sizeof(char); | |
| 120 | + | |
| 121 | + QUrl faceMeshURL(QByteArray(faceMeshPosition, | |
| 122 | + numPacketBytes - ((unsigned char*) faceMeshPosition - packetData))); | |
| 123 | + | |
| 124 | + qDebug("Changing user's face model URL to %s\n", faceMeshURL.toString().toLocal8Bit().constData()); | |
| 125 | + QMetaObject::invokeMethod(&Application::getInstance()->getAvatar()->getHead().getBlendFace(), | |
| 126 | + "setModelURL", | |
| 127 | + Q_ARG(QUrl, faceMeshURL)); | |
| 128 | + } | |
| 129 | + } else { | |
| 130 | + // user string was UUID, find matching avatar and associate data | |
| 131 | + } | |
| 132 | +} | |
| 133 | + | |
| 134 | +void DataServerClient::processMessageFromDataServer(unsigned char* packetData, int numPacketBytes) { | |
| 135 | + switch (packetData[0]) { | |
| 136 | + case PACKET_TYPE_DATA_SERVER_SEND: | |
| 137 | + processSendFromDataServer(packetData, numPacketBytes); | |
| 138 | + break; | |
| 139 | + case PACKET_TYPE_DATA_SERVER_CONFIRM: | |
| 140 | + processConfirmFromDataServer(packetData, numPacketBytes); | |
| 141 | + break; | |
| 142 | + default: | |
| 143 | + break; | |
| 144 | + } | |
| 100 | 145 | } |