2019-09-02 10:28:14 +00:00
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "QualityOfService.h"
|
|
|
|
#include "RouteId.h"
|
2020-03-05 15:30:50 +00:00
|
|
|
#include "Common/FSecure/CppTools/ScopeGuard.h"
|
2019-09-02 10:28:14 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2020-03-05 15:30:50 +00:00
|
|
|
uint32_t FSecure::C3::QualityOfService::GetOutgouingPacketId()
|
2019-09-02 10:28:14 +00:00
|
|
|
{
|
|
|
|
return m_OutgouingPacketId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2020-03-05 15:30:50 +00:00
|
|
|
FSecure::ByteVector FSecure::C3::QualityOfService::GetNextPacket()
|
2019-09-02 10:28:14 +00:00
|
|
|
{
|
|
|
|
auto it = std::find_if(m_ReciveQueue.begin(), m_ReciveQueue.end(), [](auto& e) {return e.second.IsReady(); });
|
|
|
|
if (it == m_ReciveQueue.end())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto ret = it->second.Read();
|
|
|
|
m_ReciveQueue.erase(it);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2020-03-05 15:30:50 +00:00
|
|
|
void FSecure::C3::QualityOfService::PushReceivedChunk(ByteView chunkWithHeader)
|
2019-09-02 10:28:14 +00:00
|
|
|
{
|
2020-05-19 15:59:34 +00:00
|
|
|
if (chunkWithHeader.size() <= QualityOfService::s_HeaderSize) // Data is to short to even be chunk of packet.
|
2019-09-02 10:28:14 +00:00
|
|
|
return; // skip this chunk. there is nothing that can be done with it. If sender knows it pushed chunk to short it will retransmit it.
|
|
|
|
|
|
|
|
auto [mId, cId, expectedSize] = chunkWithHeader.Read<uint32_t, uint32_t, uint32_t>();
|
|
|
|
PushReceivedChunk(mId, cId, expectedSize, chunkWithHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2020-03-05 15:30:50 +00:00
|
|
|
void FSecure::C3::QualityOfService::PushReceivedChunk(uint32_t packetId, uint32_t chunkId, uint32_t expectedSize, ByteView chunk)
|
2019-09-02 10:28:14 +00:00
|
|
|
{
|
2020-05-21 12:20:01 +00:00
|
|
|
if (chunk.size() < QualityOfService::s_MinBodySize && chunk.size() != expectedSize)
|
|
|
|
return;
|
|
|
|
|
2019-09-02 10:28:14 +00:00
|
|
|
auto it = m_ReciveQueue.find(packetId);
|
|
|
|
if (it == m_ReciveQueue.end())
|
2020-05-21 12:20:01 +00:00
|
|
|
it = m_ReciveQueue.emplace(packetId, Packet{ chunkId, expectedSize, ByteVector{ chunk } }).first;
|
2019-09-02 10:28:14 +00:00
|
|
|
else
|
|
|
|
it->second.PushNextChunk(chunkId, expectedSize, ByteVector{ chunk });
|
2020-05-21 12:20:01 +00:00
|
|
|
|
|
|
|
if (chunkId == 0)
|
|
|
|
it->second.SetExpectedSize(expectedSize);
|
2019-09-02 10:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2020-03-05 15:30:50 +00:00
|
|
|
FSecure::C3::QualityOfService::Packet::Packet(uint32_t chunkId, uint32_t expectedSize, ByteVector chunk)
|
2019-09-02 10:28:14 +00:00
|
|
|
{
|
2020-05-21 12:20:01 +00:00
|
|
|
m_Size += static_cast<uint32_t>(chunk.size());
|
|
|
|
m_Chunks.emplace(chunkId, std::move(chunk));
|
2019-09-02 10:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2020-03-05 15:30:50 +00:00
|
|
|
void FSecure::C3::QualityOfService::Packet::PushNextChunk(uint32_t chunkId, uint32_t expectedSize, ByteVector chunk)
|
2019-09-02 10:28:14 +00:00
|
|
|
{
|
2020-05-21 12:20:01 +00:00
|
|
|
if (auto it = m_Chunks.find(chunkId); it != m_Chunks.end())
|
2019-09-02 10:28:14 +00:00
|
|
|
throw std::runtime_error{ OBF("QoS error. Received chunk of packet was already set") };
|
|
|
|
|
2020-05-21 12:20:01 +00:00
|
|
|
m_Size += static_cast<uint32_t>(chunk.size());
|
|
|
|
m_Chunks.emplace(chunkId, std::move(chunk));
|
2019-09-02 10:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2020-03-05 15:30:50 +00:00
|
|
|
FSecure::ByteVector FSecure::C3::QualityOfService::Packet::Read()
|
2019-09-02 10:28:14 +00:00
|
|
|
{
|
|
|
|
if (!IsReady())
|
|
|
|
throw std::runtime_error{ OBF("QoS error. Packet is not ready") };
|
|
|
|
|
|
|
|
ByteVector ret;
|
|
|
|
ret.resize(m_ExpectedSize);
|
|
|
|
auto data = ret.data();
|
|
|
|
for (auto&& e : m_Chunks)
|
|
|
|
{
|
|
|
|
memcpy(data, e.second.data(), e.second.size());
|
|
|
|
data += e.second.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2020-03-05 15:30:50 +00:00
|
|
|
bool FSecure::C3::QualityOfService::Packet::IsReady()
|
2019-09-02 10:28:14 +00:00
|
|
|
{
|
2020-05-21 12:20:01 +00:00
|
|
|
if (m_ExpectedSize == 0) // packet nr 0 was not set.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (m_Size > m_ExpectedSize) // sanity check
|
2019-09-02 10:28:14 +00:00
|
|
|
throw std::runtime_error{ OBF("QoS error. Packet size is longer than expected, wrong chunks must been set.") };
|
|
|
|
|
|
|
|
return m_Size == m_ExpectedSize;
|
|
|
|
}
|
2020-05-19 15:59:34 +00:00
|
|
|
|
2020-05-21 12:20:01 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void FSecure::C3::QualityOfService::Packet::SetExpectedSize(uint32_t size)
|
|
|
|
{
|
|
|
|
m_ExpectedSize = size;
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:04:10 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
FSecure::C3::QualityOfService::PacketSplitter FSecure::C3::QualityOfService::GetPacketSplitter(ByteView data)
|
|
|
|
{
|
|
|
|
return { data, GetOutgouingPacketId() };
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
FSecure::C3::QualityOfService::PacketSplitter::PacketSplitter(ByteView data, uint32_t id)
|
2020-05-21 12:20:01 +00:00
|
|
|
: m_Data{ data }, m_PacketId{ id }, m_ChunkId{ 0 }
|
2020-05-19 15:59:34 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:04:10 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool FSecure::C3::QualityOfService::PacketSplitter::Update(size_t sent)
|
2020-05-19 15:59:34 +00:00
|
|
|
{
|
|
|
|
auto dataSent = sent - QualityOfService::s_HeaderSize;
|
|
|
|
if (sent < QualityOfService::s_MinFrameSize && dataSent != m_Data.size() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++m_ChunkId;
|
|
|
|
m_Data.remove_prefix(dataSent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:04:10 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
FSecure::ByteVector FSecure::C3::QualityOfService::PacketSplitter::NextChunk() const
|
2020-05-19 15:59:34 +00:00
|
|
|
{
|
2020-05-21 12:20:01 +00:00
|
|
|
return ByteVector::Create(m_PacketId, m_ChunkId, static_cast<uint32_t>(m_Data.size())).Concat(m_Data);
|
2020-05-19 15:59:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:04:10 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
bool FSecure::C3::QualityOfService::PacketSplitter::HasMore() const
|
2020-05-19 15:59:34 +00:00
|
|
|
{
|
|
|
|
return !m_Data.empty();
|
|
|
|
}
|