mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 18:13:05 +02:00
make PacketPayload a QIODevice subclass
This commit is contained in:
parent
8dc385bd62
commit
57d76f488c
5 changed files with 148 additions and 40 deletions
|
@ -617,11 +617,16 @@ void DomainServer::handleConnectRequest(const QByteArray& packet, const HifiSock
|
||||||
QString reason;
|
QString reason;
|
||||||
if (!isAssignment && !shouldAllowConnectionFromNode(username, usernameSignature, senderSockAddr, reason)) {
|
if (!isAssignment && !shouldAllowConnectionFromNode(username, usernameSignature, senderSockAddr, reason)) {
|
||||||
// this is an agent and we've decided we won't let them connect - send them a packet to deny connection
|
// this is an agent and we've decided we won't let them connect - send them a packet to deny connection
|
||||||
QByteArray connectionDeniedByteArray = limitedNodeList->byteArrayWithPopulatedHeader(PacketTypeDomainConnectionDenied);
|
|
||||||
QDataStream out(&connectionDeniedByteArray, QIODevice::WriteOnly | QIODevice::Append);
|
QByteArray utfString = reason.toUtf8();
|
||||||
out << reason;
|
int payloadSize = utfString.size();
|
||||||
|
|
||||||
|
auto connectionDeniedPacket = NodeListPacket::make(PacketType::DomainConnectionDenied, payloadSize);
|
||||||
|
|
||||||
|
memcpy(connectionDeniedPacket.payload().data(), utfString.data(), utfString.size());
|
||||||
|
|
||||||
// tell client it has been refused.
|
// tell client it has been refused.
|
||||||
DependencyManager::get<LimitedNodeList>()->writeUnverifiedDatagram(connectionDeniedByteArray, senderSockAddr);
|
limitedNodeList->sendPacket(std::move(connectionDeniedPacket, senderSockAddr);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -921,10 +926,11 @@ int DomainServer::parseNodeDataFromByteArray(QDataStream& packetStream, NodeType
|
||||||
void DomainServer::sendDomainListToNode(const SharedNodePointer& node, const HifiSockAddr &senderSockAddr,
|
void DomainServer::sendDomainListToNode(const SharedNodePointer& node, const HifiSockAddr &senderSockAddr,
|
||||||
const NodeSet& nodeInterestSet) {
|
const NodeSet& nodeInterestSet) {
|
||||||
auto limitedNodeList = DependencyManager::get<LimitedNodeList>();
|
auto limitedNodeList = DependencyManager::get<LimitedNodeList>();
|
||||||
QByteArray broadcastPacket = limitedNodeList->byteArrayWithPopulatedHeader(PacketTypeDomainList);
|
|
||||||
|
auto listPacket = NodeListPacket::make(PacketType::DomainList);
|
||||||
|
|
||||||
// always send the node their own UUID back
|
// always send the node their own UUID back
|
||||||
QDataStream broadcastDataStream(&broadcastPacket, QIODevice::Append);
|
QDataStream broadcastDataStream(&listPacket.payload(), QIODevice::Append);
|
||||||
broadcastDataStream << node->getUUID();
|
broadcastDataStream << node->getUUID();
|
||||||
broadcastDataStream << node->getCanAdjustLocks();
|
broadcastDataStream << node->getCanAdjustLocks();
|
||||||
broadcastDataStream << node->getCanRez();
|
broadcastDataStream << node->getCanRez();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
//
|
//
|
||||||
// PacketByteArray.cpp
|
// PacketPayload.cpp
|
||||||
// libraries/networking/src
|
// libraries/networking/src
|
||||||
//
|
//
|
||||||
// Created by Stephen Birarda on 07/06/15.
|
// Created by Stephen Birarda on 07/06/15.
|
||||||
|
@ -9,16 +9,16 @@
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "PacketByteArray.h"
|
#include "PacketPayload.h"
|
||||||
|
|
||||||
PacketByteArray::PacketByteArray(char* data, int maxBytes) :
|
PacketPayload::PacketPayload(char* data, int maxBytes) :
|
||||||
_data(data)
|
_data(data)
|
||||||
_maxBytes(maxBytes)
|
_maxBytes(maxBytes)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int PacketByteArray::append(const char* src, int srcBytes) {
|
int PacketPayload::append(const char* src, int srcBytes) {
|
||||||
// this is a call to write at the current index
|
// this is a call to write at the current index
|
||||||
int numWrittenBytes = write(src, srcBytes, _index);
|
int numWrittenBytes = write(src, srcBytes, _index);
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ int PacketByteArray::append(const char* src, int srcBytes) {
|
||||||
|
|
||||||
const int PACKET_WRITE_ERROR = -1;
|
const int PACKET_WRITE_ERROR = -1;
|
||||||
|
|
||||||
int PacketByteArray::write(const char* src, int srcBytes, int index) {
|
int PacketPayload::write(const char* src, int srcBytes, int index) {
|
||||||
if (index >= _maxBytes) {
|
if (index >= _maxBytes) {
|
||||||
// we were passed a bad index, return -1
|
// we were passed a bad index, return -1
|
||||||
return PACKET_WRITE_ERROR;
|
return PACKET_WRITE_ERROR;
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
//
|
|
||||||
// PacketByteArray.h
|
|
||||||
// libraries/networking/src
|
|
||||||
//
|
|
||||||
// Created by Stephen Birarda on 07/06/15.
|
|
||||||
// Copyright 2015 High Fidelity, Inc.
|
|
||||||
//
|
|
||||||
// Distributed under the Apache License, Version 2.0.
|
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef hifi_PacketByteArray_h
|
|
||||||
#define hifi_PacketByteArray_h
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
class PacketByteArray {
|
|
||||||
public:
|
|
||||||
PacketByteArray(char* data, int maxBytes);
|
|
||||||
|
|
||||||
int write(const char* src, int srcBytes, int index = 0);
|
|
||||||
int append(const char* src, int srcBytes)
|
|
||||||
private:
|
|
||||||
char* _data;
|
|
||||||
int _index = 0;
|
|
||||||
int _maxBytes = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // hifi_PacketByteArray_h
|
|
89
libraries/networking/src/PacketPayload.cpp
Normal file
89
libraries/networking/src/PacketPayload.cpp
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
//
|
||||||
|
// PacketPayload.cpp
|
||||||
|
// libraries/networking/src
|
||||||
|
//
|
||||||
|
// Created by Stephen Birarda on 07/06/15.
|
||||||
|
// Copyright 2015 High Fidelity, Inc.
|
||||||
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "PacketPayload.h"
|
||||||
|
|
||||||
|
PacketPayload::PacketPayload(char* data, qint64 maxBytes) :
|
||||||
|
_data(data)
|
||||||
|
_maxBytes(maxBytes)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
qint64 PacketPayload::append(const char* src, qint64 srcBytes) {
|
||||||
|
// this is a call to write at the current index
|
||||||
|
qint64 numWrittenBytes = write(src, srcBytes, _index);
|
||||||
|
|
||||||
|
if (numWrittenBytes > 0) {
|
||||||
|
// we wrote some bytes, push the index
|
||||||
|
_index += numWrittenBytes;
|
||||||
|
return numWrittenBytes;
|
||||||
|
} else {
|
||||||
|
return numWrittenBytes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const int PACKET_READ_ERROR = -1;
|
||||||
|
|
||||||
|
qint64 PacketPayload::write(const char* src, qint64 srcBytes, qint64 index) {
|
||||||
|
if (index >= _maxBytes) {
|
||||||
|
// we were passed a bad index, return error
|
||||||
|
return PACKET_WRITE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure we have the space required to write this block
|
||||||
|
qint64 bytesAvailable = _maxBytes - index;
|
||||||
|
|
||||||
|
if (bytesAvailable < srcBytes) {
|
||||||
|
// good to go - write the data
|
||||||
|
memcpy(_data + index, src, srcBytes);
|
||||||
|
|
||||||
|
// should this cause us to push our index (is this the farthest we've written in data)?
|
||||||
|
_writeIndex = std::max(_data + index + srcBytes, _writeIndex);
|
||||||
|
|
||||||
|
// return the number of bytes written
|
||||||
|
return srcBytes;
|
||||||
|
} else {
|
||||||
|
// not enough space left for this write - return an error
|
||||||
|
return PACKET_WRITE_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
qint64 PacketPayload::readNext(char* dest, qint64 maxSize) {
|
||||||
|
// call read at the current _readIndex
|
||||||
|
int numBytesRead = read(dest, maxSize, _readIndex);
|
||||||
|
|
||||||
|
if (numBytesRead > 0) {
|
||||||
|
// we read some data, push the read index
|
||||||
|
_readIndex += numBytesRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
return numBytesRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
const qint64 PACKET_READ_ERROR = -1;
|
||||||
|
|
||||||
|
qint64 PacketPayload::read(char* dest, qint64 maxSize, qint64 index) {
|
||||||
|
if (index >= _maxBytes) {
|
||||||
|
// we were passed a bad index, return error
|
||||||
|
return PACKET_READ_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// we're either reading what is left from the index or what was asked to be read
|
||||||
|
qint64 numBytesToRead = std::min(_maxSize - index, maxSize);
|
||||||
|
|
||||||
|
if (numBytesToRead > 0) {
|
||||||
|
// read out the data
|
||||||
|
memcpy(dest, _data + index, numBytesToRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
return numBytesToRead;
|
||||||
|
}
|
42
libraries/networking/src/PacketPayload.h
Normal file
42
libraries/networking/src/PacketPayload.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
//
|
||||||
|
// PacketPayload.h
|
||||||
|
// libraries/networking/src
|
||||||
|
//
|
||||||
|
// Created by Stephen Birarda on 07/06/15.
|
||||||
|
// Copyright 2015 High Fidelity, Inc.
|
||||||
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef hifi_PacketPayload_h
|
||||||
|
#define hifi_PacketPayload_h
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QtCore/QIODevice>
|
||||||
|
|
||||||
|
class PacketPayload : public QIODevice {
|
||||||
|
public:
|
||||||
|
PacketPayload(char* data, qint64 maxBytes);
|
||||||
|
|
||||||
|
qint64 write(const char* src, qint64 srcBytes, qint64 index = 0);
|
||||||
|
qint64 append(const char* src, qint64 srcBytes);
|
||||||
|
|
||||||
|
qint64 read(char* dest, qint64 maxSize, qint64 index = 0);
|
||||||
|
qint64 readNext(char* dest, qint64 maxSize);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual qint64 writeData(const char* data, qint64 maxSize) { return append(data, maxSize) };
|
||||||
|
virtual qint64 readData(const char* data, qint64 maxSize) { return readNext(data, maxSize) };
|
||||||
|
|
||||||
|
private:
|
||||||
|
char* _data;
|
||||||
|
|
||||||
|
qint64 _writeIndex = 0;
|
||||||
|
qint64 _readIndex = 0;
|
||||||
|
|
||||||
|
qint64 _maxBytes = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // hifi_PacketByteArray_h
|
Loading…
Reference in a new issue