Templated read/write in Packet

This commit is contained in:
Atlante45 2015-07-08 13:45:12 -07:00
parent 10f843b197
commit 7329ad6334
5 changed files with 23 additions and 13 deletions

View file

@ -856,22 +856,22 @@ void AudioClient::handleAudioInput() {
_audioPacket->reset(); _audioPacket->reset();
// write sequence number // write sequence number
_audioPacket->write(reinterpret_cast<char*>(&_outgoingAvatarAudioSequenceNumber), sizeof(quint16)); _audioPacket->write(_outgoingAvatarAudioSequenceNumber);
if (_audioPacket->getPacketType() == PacketType::SilentAudioFrame) { if (_audioPacket->getPacketType() == PacketType::SilentAudioFrame) {
// pack num silent samples // pack num silent samples
quint16 numSilentSamples = numNetworkSamples; quint16 numSilentSamples = numNetworkSamples;
_audioPacket->write(reinterpret_cast<char*>(&numSilentSamples), sizeof(quint16)); _audioPacket->write(numSilentSamples);
} else { } else {
// set the mono/stereo byte // set the mono/stereo byte
_audioPacket->write(reinterpret_cast<char*>(&isStereo), sizeof(isStereo)); _audioPacket->write(isStereo);
} }
// pack the three float positions // pack the three float positions
_audioPacket->write(reinterpret_cast<char*>(&headPosition), sizeof(headPosition)); _audioPacket->write(headPosition);
// pack the orientation // pack the orientation
_audioPacket->write(reinterpret_cast<char*>(&headOrientation), sizeof(headOrientation)); _audioPacket->write(headOrientation);
if (_audioPacket->getPacketType() != PacketType::SilentAudioFrame) { if (_audioPacket->getPacketType() != PacketType::SilentAudioFrame) {
// audio samples have already been packed (written to networkAudioSamples) // audio samples have already been packed (written to networkAudioSamples)
@ -912,12 +912,12 @@ void AudioClient::sendMuteEnvironmentPacket() {
auto mutePacket = NLPacket::create(PacketType::MuteEnvironment, dataSize); auto mutePacket = NLPacket::create(PacketType::MuteEnvironment, dataSize);
float MUTE_RADIUS = 50; const float MUTE_RADIUS = 50;
glm::vec3 currentSourcePosition = _positionGetter(); glm::vec3 currentSourcePosition = _positionGetter();
mutePacket->write(reinterpret_cast<char*>(&currentSourcePosition), sizeof(currentSourcePosition)); mutePacket->write(currentSourcePosition);
mutePacket->write(reinterpret_cast<char*>(&MUTE_RADIUS), sizeof(MUTE_RADIUS)); mutePacket->write(MUTE_RADIUS);
// grab our audio mixer from the NodeList, if it exists // grab our audio mixer from the NodeList, if it exists
SharedNodePointer audioMixer = nodeList->soloNodeOfType(NodeType::AudioMixer); SharedNodePointer audioMixer = nodeList->soloNodeOfType(NodeType::AudioMixer);

View file

@ -216,17 +216,17 @@ void AudioInjector::injectToMixer() {
_loudness /= (float)(bytesToCopy / sizeof(int16_t)); _loudness /= (float)(bytesToCopy / sizeof(int16_t));
audioPacket->seek(positionOptionOffset); audioPacket->seek(positionOptionOffset);
audioPacket->write(reinterpret_cast<char*>(&_options.position), sizeof(_options.position)); audioPacket->write(_options.position);
audioPacket->write(reinterpret_cast<char*>(&_options.orientation), sizeof(_options.orientation)); audioPacket->write(_options.orientation);
volume = MAX_INJECTOR_VOLUME * _options.volume; volume = MAX_INJECTOR_VOLUME * _options.volume;
audioPacket->seek(volumeOptionOffset); audioPacket->seek(volumeOptionOffset);
audioPacket->write(reinterpret_cast<char*>(&volume), sizeof(volume)); audioPacket->write(volume);
audioPacket->seek(audioDataOffset); audioPacket->seek(audioDataOffset);
// pack the sequence number // pack the sequence number
audioPacket->write(reinterpret_cast<char*>(&outgoingInjectedAudioSequenceNumber), sizeof(quint16)); audioPacket->write(outgoingInjectedAudioSequenceNumber);
// copy the next NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL bytes to the packet // copy the next NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL bytes to the packet
audioPacket->write(_audioData.data() + _currentSendPosition, bytesToCopy); audioPacket->write(_audioData.data() + _currentSendPosition, bytesToCopy);

View file

@ -156,6 +156,14 @@ void Packet::writeSequenceNumber(SequenceNumber seqNum) {
&seqNum, sizeof(seqNum)); &seqNum, sizeof(seqNum));
} }
template<typename T> qint64 Packet::read(T* data) {
return QIODevice::read(reinterpret_cast<char*>(data), sizeof(T));
}
template<typename T> qint64 Packet::write(const T& data) {
return QIODevice::write(reinterpret_cast<const char*>(&data), sizeof(T));
}
static const qint64 PACKET_WRITE_ERROR = -1; static const qint64 PACKET_WRITE_ERROR = -1;
qint64 Packet::writeData(const char* data, qint64 maxSize) { qint64 Packet::writeData(const char* data, qint64 maxSize) {
// make sure we have the space required to write this block // make sure we have the space required to write this block

View file

@ -59,6 +59,9 @@ public:
virtual bool reset() { setSizeUsed(0); return QIODevice::reset(); } virtual bool reset() { setSizeUsed(0); return QIODevice::reset(); }
virtual qint64 size() const { return _capacity; } virtual qint64 size() const { return _capacity; }
template<typename T> qint64 read(T* data);
template<typename T> qint64 write(const T& data);
protected: protected:
Packet(PacketType::Value type, int64_t size); Packet(PacketType::Value type, int64_t size);
Packet(const Packet& other); Packet(const Packet& other);

View file

@ -28,7 +28,6 @@ JurisdictionSender::JurisdictionSender(JurisdictionMap* map, NodeType_t type) :
JurisdictionSender::~JurisdictionSender() { JurisdictionSender::~JurisdictionSender() {
} }
void JurisdictionSender::processPacket(const SharedNodePointer& sendingNode, const QByteArray& packet) { void JurisdictionSender::processPacket(const SharedNodePointer& sendingNode, const QByteArray& packet) {
if (packetTypeForPacket(packet) == PacketType::JurisdictionRequest) { if (packetTypeForPacket(packet) == PacketType::JurisdictionRequest) {
if (sendingNode) { if (sendingNode) {