Merge branch 'protocol' of https://github.com/Atlante45/hifi into atp

This commit is contained in:
Stephen Birarda 2015-07-27 15:59:19 -07:00
commit 7966dee4fe
8 changed files with 172 additions and 19 deletions

View file

@ -0,0 +1,56 @@
//
// Connection.cpp
//
//
// Created by Clement on 7/27/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 "Connection.h"
#include "../HifiSockAddr.h"
#include "ControlPacket.h"
#include "Packet.h"
#include "Socket.h"
using namespace udt;
Connection::Connection(Socket* parentSocket, HifiSockAddr destination) {
}
void Connection::send(std::unique_ptr<Packet> packet) {
if (_sendQueue) {
_sendQueue->queuePacket(std::move(packet));
}
}
void Connection::processReceivedSeqNum(SeqNum seq) {
if (udt::seqcmp(seq, _largestRecievedSeqNum + 1) > 0) {
// TODO: Add range to loss list
// TODO: Send loss report
}
if (seq > _largestRecievedSeqNum) {
_largestRecievedSeqNum = seq;
} else {
// TODO: Remove seq from loss list
}
}
void Connection::processControl(std::unique_ptr<ControlPacket> controlPacket) {
switch (controlPacket->getType()) {
case ControlPacket::Type::ACK:
break;
case ControlPacket::Type::ACK2:
break;
case ControlPacket::Type::NAK:
break;
case ControlPacket::Type::PacketPair:
break;
}
}

View file

@ -0,0 +1,45 @@
//
// Connection.h
//
//
// Created by Clement on 7/27/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_Connection_h
#define hifi_Connection_h
#include <memory>
#include "SendQueue.h"
class HifiSockAddr;
namespace udt {
class ControlPacket;
class Packet;
class Socket;
class Connection {
public:
Connection(Socket* parentSocket, HifiSockAddr destination);
void send(std::unique_ptr<Packet> packet);
void processReceivedSeqNum(SeqNum seq);
void processControl(std::unique_ptr<ControlPacket> controlPacket);
private:
SeqNum _largestRecievedSeqNum;
std::unique_ptr<SendQueue> _sendQueue;
};
}
#endif // hifi_Connection_h

View file

@ -42,6 +42,8 @@ public:
static qint64 localHeaderSize(); // Current level's header size
virtual qint64 totalHeadersSize() const; // Cumulated size of all the headers
Type getType() const { return _type; }
private:
ControlPacket(Type type, const SequenceNumberList& sequenceNumbers);
ControlPacket(quint64 timestamp);

View file

@ -14,7 +14,6 @@
#include <algorithm>
#include <QtCore/QThread>
#include <QtCore/QTimer>
#include <SharedUtil.h>
@ -29,8 +28,8 @@ std::unique_ptr<SendQueue> SendQueue::create(Socket* socket, HifiSockAddr dest)
// Setup queue private thread
QThread* thread = new QThread(queue.get());
thread->setObjectName("Networking: SendQueue"); // Name thread for easier debug
thread->connect(queue.get(), &QObject::destroyed, thread, &QThread::quit); // Thread auto cleanup
thread->connect(thread, &QThread::finished, thread, &QThread::deleteLater); // Thread auto cleanup
connect(queue.get(), &QObject::destroyed, thread, &QThread::quit); // Thread auto cleanup
connect(thread, &QThread::finished, thread, &QThread::deleteLater); // Thread auto cleanup
// Move queue to private thread and start it
queue->moveToThread(thread);
@ -51,6 +50,11 @@ SendQueue::SendQueue(Socket* socket, HifiSockAddr dest) :
_lastSendTimestamp = 0;
}
SendQueue::~SendQueue() {
assert(thread() == QThread::currentThread());
_sendTimer->stop();
}
void SendQueue::queuePacket(std::unique_ptr<Packet> packet) {
{
QWriteLocker locker(&_packetsLock);

View file

@ -15,15 +15,14 @@
#include <list>
#include <unordered_map>
#include <QObject>
#include <QReadWriteLock>
#include <QtCore/QObject>
#include <QtCore/QReadWriteLock>
#include <QtCore/QTimer>
#include "../HifiSockAddr.h"
#include "SeqNum.h"
class QTimer;
namespace udt {
class Socket;
@ -38,7 +37,7 @@ public:
static std::unique_ptr<SendQueue> create(Socket* socket, HifiSockAddr dest);
void queuePacket(std::unique_ptr<Packet> packet);
int getQueueSize() const { return _packets.size(); }
int getQueueSize() const { QReadLocker locker(&_packetsLock); return _packets.size(); }
quint64 getLastSendTimestamp() const { return _lastSendTimestamp; }
@ -57,11 +56,14 @@ private slots:
void sendNextPacket();
private:
friend struct std::default_delete<SendQueue>;
SendQueue(Socket* socket, HifiSockAddr dest);
SendQueue(SendQueue& other) = delete;
SendQueue(SendQueue&& other) = delete;
~SendQueue();
QReadWriteLock _packetsLock; // Protects the packets to be sent list.
mutable QReadWriteLock _packetsLock; // Protects the packets to be sent list.
std::list<std::unique_ptr<Packet>> _packets; // List of packets to be sent
std::unique_ptr<Packet> _nextPacket; // Next packet to be sent
@ -75,10 +77,10 @@ private:
std::atomic<quint64> _lastSendTimestamp { 0 }; // Record last time of packet departure
std::atomic<bool> _running { false };
QReadWriteLock _naksLock; // Protects the naks list.
mutable QReadWriteLock _naksLock; // Protects the naks list.
std::list<SeqNum> _naks; // Sequence numbers of packets to resend
QReadWriteLock _sentLock; // Protects the sent packet list
mutable QReadWriteLock _sentLock; // Protects the sent packet list
std::unordered_map<SeqNum, std::unique_ptr<Packet>> _sentPackets; // Packets waiting for ACK.
};

View file

@ -13,12 +13,12 @@
int udt::seqcmp(const SeqNum& seq1, const SeqNum& seq2) {
return (glm::abs(seq1._value - seq2._value) < SeqNum::THRESHOLD) ? (seq1._value - seq2._value)
: (seq2._value - seq1._value);
: (seq2._value - seq1._value);
}
int udt::seqlen(const SeqNum& seq1, const SeqNum& seq2) {
return (seq1._value <= seq2._value) ? (seq2._value - seq1._value + 1)
: (seq2._value - seq1._value + SeqNum::MAX + 2);
: (seq2._value - seq1._value + SeqNum::MAX + 2);
}
int udt::seqoff(const SeqNum& seq1, const SeqNum& seq2) {

View file

@ -74,6 +74,16 @@ public:
return _value != other._value;
}
friend bool operator<(const SeqNum& a, const SeqNum& b);
friend bool operator>(const SeqNum& a, const SeqNum& b);
friend bool operator<=(const SeqNum& a, const SeqNum& b);
friend bool operator>=(const SeqNum& a, const SeqNum& b);
friend SeqNum operator+(const SeqNum a, const Type& b);
friend SeqNum operator+(const Type& a, const SeqNum b);
friend SeqNum operator-(const SeqNum a, const Type& b);
friend SeqNum operator-(const Type& a, const SeqNum b);
friend int seqcmp(const SeqNum& seq1, const SeqNum& seq2);
friend int seqlen(const SeqNum& seq1, const SeqNum& seq2);
friend int seqoff(const SeqNum& seq1, const SeqNum& seq2);
@ -84,20 +94,54 @@ private:
friend struct std::hash<SeqNum>;
};
inline bool operator<(const SeqNum& a, const SeqNum& b) {
}
inline bool operator>(const SeqNum& a, const SeqNum& b) {
}
inline bool operator<=(const SeqNum& a, const SeqNum& b) {
}
inline bool operator>=(const SeqNum& a, const SeqNum& b) {
}
inline SeqNum operator+(SeqNum a, const SeqNum::Type& b) {
a += b;
return a;
}
inline SeqNum operator+(const SeqNum::Type& a, SeqNum b) {
b += a;
return b;
}
inline SeqNum operator-(SeqNum a, const SeqNum::Type& b) {
a -= b;
return a;
}
inline SeqNum operator-(const SeqNum::Type& a, SeqNum b) {
b -= a;
return b;
}
int seqcmp(const SeqNum& seq1, const SeqNum& seq2);
int seqlen(const SeqNum& seq1, const SeqNum& seq2);
int seqoff(const SeqNum& seq1, const SeqNum& seq2);
}
namespace std {
template<> struct hash<udt::SeqNum> {
template<> struct std::hash<udt::SeqNum> {
size_t operator()(const udt::SeqNum& seqNum) const {
return std::hash<unsigned long>()(seqNum._value);
}
};
}
#endif // hifi_SeqNum_h

View file

@ -22,13 +22,13 @@
#include <QtNetwork/QUdpSocket>
#include "../HifiSockAddr.h"
#include "SeqNum.h"
namespace udt {
class BasePacket;
class ControlSender;
class Packet;
class SeqNum;
using PacketFilterOperator = std::function<bool(const Packet&)>;