Jurisdiction map uses new packet API

This commit is contained in:
Atlante45 2015-07-08 11:19:57 -07:00
parent 6bf05fbf1f
commit fef51682d5
4 changed files with 24 additions and 47 deletions

View file

@ -75,6 +75,8 @@ typedef struct ty_gverb ty_gverb;
typedef glm::vec3 (*AudioPositionGetter)();
typedef glm::quat (*AudioOrientationGetter)();
class NLPacket;
class AudioClient : public AbstractAudioInterface, public Dependency {
Q_OBJECT
SINGLETON_DEPENDENCY

View file

@ -268,49 +268,35 @@ bool JurisdictionMap::writeToFile(const char* filename) {
return true;
}
int JurisdictionMap::packEmptyJurisdictionIntoMessage(NodeType_t type, unsigned char* destinationBuffer, int availableBytes) {
unsigned char* bufferStart = destinationBuffer;
int headerLength = DependencyManager::get<NodeList>()->populatePacketHeader(reinterpret_cast<char*>(destinationBuffer),
PacketType::Jurisdiction);
destinationBuffer += headerLength;
std::unique_ptr<NLPacket> JurisdictionMap::packEmptyJurisdictionIntoMessage(NodeType_t type) {
int bytes = 0;
auto packet = NLPacket::create(PacketType::Jurisdiction);
// Pack the Node Type in first byte
memcpy(destinationBuffer, &type, sizeof(type));
destinationBuffer += sizeof(type);
packet->write(reinterpret_cast<char*>(&type), sizeof(type));
// No root or end node details to pack!
int bytes = 0;
memcpy(destinationBuffer, &bytes, sizeof(bytes));
destinationBuffer += sizeof(bytes);
packet->write(reinterpret_cast<char*>(&bytes), sizeof(bytes));
return destinationBuffer - bufferStart; // includes header!
return std::move(packet); // includes header!
}
int JurisdictionMap::packIntoMessage(unsigned char* destinationBuffer, int availableBytes) {
unsigned char* bufferStart = destinationBuffer;
int headerLength = DependencyManager::get<NodeList>()->populatePacketHeader(reinterpret_cast<char*>(destinationBuffer),
PacketType::Jurisdiction);
destinationBuffer += headerLength;
std::unique_ptr<NLPacket> JurisdictionMap::packIntoMessage() {
auto packet = NLPacket::create(PacketType::Jurisdiction);
// Pack the Node Type in first byte
NodeType_t type = getNodeType();
memcpy(destinationBuffer, &type, sizeof(type));
destinationBuffer += sizeof(type);
packet->write(reinterpret_cast<char*>(&type), sizeof(type));
// add the root jurisdiction
if (_rootOctalCode) {
size_t bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(_rootOctalCode));
memcpy(destinationBuffer, &bytes, sizeof(bytes));
destinationBuffer += sizeof(bytes);
memcpy(destinationBuffer, _rootOctalCode, bytes);
destinationBuffer += bytes;
// No root or end node details to pack!
packet->write(reinterpret_cast<char*>(&bytes), sizeof(bytes));
packet->write(reinterpret_cast<char*>(_rootOctalCode), bytes);
// if and only if there's a root jurisdiction, also include the end nodes
int endNodeCount = _endNodes.size();
memcpy(destinationBuffer, &endNodeCount, sizeof(endNodeCount));
destinationBuffer += sizeof(endNodeCount);
packet->write(reinterpret_cast<char*>(&endNodeCount), sizeof(endNodeCount));
for (int i=0; i < endNodeCount; i++) {
unsigned char* endNodeCode = _endNodes[i];
@ -318,18 +304,15 @@ int JurisdictionMap::packIntoMessage(unsigned char* destinationBuffer, int avail
if (endNodeCode) {
bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(endNodeCode));
}
memcpy(destinationBuffer, &bytes, sizeof(bytes));
destinationBuffer += sizeof(bytes);
memcpy(destinationBuffer, endNodeCode, bytes);
destinationBuffer += bytes;
packet->write(reinterpret_cast<char*>(&bytes), sizeof(bytes));
packet->write(reinterpret_cast<char*>(endNodeCode), bytes);
}
} else {
int bytes = 0;
memcpy(destinationBuffer, &bytes, sizeof(bytes));
destinationBuffer += sizeof(bytes);
packet->write(reinterpret_cast<char*>(&bytes), sizeof(bytes));
}
return destinationBuffer - bufferStart; // includes header!
return std::move(packet); // includes header!
}
int JurisdictionMap::unpackFromMessage(const unsigned char* sourceBuffer, int availableBytes) {

View file

@ -61,10 +61,10 @@ public:
void copyContents(unsigned char* rootCodeIn, const std::vector<unsigned char*>& endNodesIn);
int unpackFromMessage(const unsigned char* sourceBuffer, int availableBytes);
int packIntoMessage(unsigned char* destinationBuffer, int availableBytes);
std::unique_ptr<NLPacket> packIntoMessage();
/// Available to pack an empty or unknown jurisdiction into a network packet, used when no JurisdictionMap is available
static int packEmptyJurisdictionIntoMessage(NodeType_t type, unsigned char* destinationBuffer, int availableBytes);
static std::unique_ptr<NLPacket> packEmptyJurisdictionIntoMessage(NodeType_t type);
void displayDebugDetails() const;

View file

@ -44,16 +44,8 @@ bool JurisdictionSender::process() {
// call our ReceivedPacketProcessor base class process so we'll get any pending packets
if (continueProcessing && (continueProcessing = ReceivedPacketProcessor::process())) {
// add our packet to our own queue, then let the PacketSender class do the rest of the work.
static unsigned char buffer[MAX_PACKET_SIZE];
unsigned char* bufferOut = &buffer[0];
int sizeOut = 0;
if (_jurisdictionMap) {
sizeOut = _jurisdictionMap->packIntoMessage(bufferOut, MAX_PACKET_SIZE);
} else {
sizeOut = JurisdictionMap::packEmptyJurisdictionIntoMessage(getNodeType(), bufferOut, MAX_PACKET_SIZE);
}
auto packet = (_jurisdictionMap) ? _jurisdictionMap->packIntoMessage()
: JurisdictionMap::packEmptyJurisdictionIntoMessage(getNodeType());
int nodeCount = 0;
lockRequestingNodes();
@ -64,7 +56,7 @@ bool JurisdictionSender::process() {
SharedNodePointer node = DependencyManager::get<NodeList>()->nodeWithUUID(nodeUUID);
if (node && node->getActiveSocket()) {
_packetSender.queuePacketForSending(node, QByteArray(reinterpret_cast<char *>(bufferOut), sizeOut));
_packetSender.queuePacketForSending(node, packet);
nodeCount++;
}
}