Simplify Usage Of ByteVector With Custom Types

dependabot/npm_and_yarn/Src/WebController/UI/websocket-extensions-0.1.4
Pawel Kurowski 2019-09-19 18:13:34 +02:00
parent 3f238b349f
commit 0955650262
8 changed files with 45 additions and 56 deletions

View File

@ -26,7 +26,7 @@ void MWR::C3::Core::ConnectorBridge::Detach()
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MWR::C3::Core::ConnectorBridge::PostCommandToBinder(ByteView binderId, ByteView command)
{
return GetGateRelay()->PostCommandToPeripheral(command, RouteId::FromByteView(binderId));
return GetGateRelay()->PostCommandToPeripheral(command, RouteId(binderId));
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -69,7 +69,7 @@ void MWR::C3::Core::Distributor::OnProtocolN2N(ByteView packet0, std::shared_ptr
throw std::invalid_argument{ OBF("N2N packet too short.") };
// Parse neighbor identifier and check whether is banned.
auto neighborRouteId = RouteId::FromByteView(packet0.SubString(1));
auto neighborRouteId = RouteId(packet0.SubString(1));
if (IsAgentBanned(neighborRouteId.GetAgentId()))
return Log({ OBF("Received packet from a banned Agent ") + neighborRouteId.ToString() + OBF("."), LogMessage::Severity::Warning });

View File

@ -47,11 +47,11 @@ void MWR::C3::Core::GateRelay::OnProtocolS2G(ByteView packet0, std::shared_ptr<D
try
{
auto decrypted = MWR::Crypto::DecryptFromAnonymous(packet0.SubString(1), m_AuthenticationKey, m_DecryptionKey);
auto [procedure, rid, timestamp] = ByteView{ decrypted }.Read<ProceduresUnderlyingType, Bytes<RouteId::BinarySize>, int32_t>();
if (!m_Profiler->Get().m_Gateway.ConnectionExist(RouteId::FromByteView(rid).GetAgentId()))
auto [procedure, rid, timestamp] = ByteView{ decrypted }.Read<ProceduresUnderlyingType, RouteId, int32_t>();
if (!m_Profiler->Get().m_Gateway.ConnectionExist(rid.GetAgentId()))
throw std::runtime_error{ "S2G packet received from not connected source." };
ProceduresS2G::RequestHandler::ParseRequestAndHandleIt(sender, procedure, RouteId::FromByteView(rid), timestamp, packet0.SubString(1));
ProceduresS2G::RequestHandler::ParseRequestAndHandleIt(sender, procedure, rid, timestamp, packet0.SubString(1));
}
catch (std::exception& exception)
{
@ -299,7 +299,7 @@ void MWR::C3::Core::GateRelay::On(ProceduresS2G::InitializeRouteQuery&& query)
auto readView = ByteView{ decryptedPacket };
// part of message created by parent Node
auto [procedureID, parentRid, timestamp, childRid, childSideDid] = readView.Read<ProceduresUnderlyingType, Bytes<RouteId::BinarySize>, int32_t, Bytes<RouteId::BinarySize>, Bytes<DeviceId::BinarySize>>();
auto [procedureID, parentRid, timestamp, childRid, childSideDid] = readView.Read<ProceduresUnderlyingType, RouteId, int32_t, RouteId, DeviceId>();
// part of message from new relay. encrypted once again because it was blob for parent relay.
auto childPacket = Crypto::DecryptFromAnonymous(readView, m_AuthenticationKey, m_DecryptionKey);
@ -315,17 +315,16 @@ void MWR::C3::Core::GateRelay::On(ProceduresS2G::InitializeRouteQuery&& query)
if (!receivedFrom)
return; // TODO signalize error
auto parent = RouteId::FromByteView(parentRid), child = RouteId::FromByteView(childRid);
//Add route.
AddRoute(child, receivedFrom);
AddRoute(childRid, receivedFrom);
//update profiler
m_Profiler->Get().m_Gateway.ReAddRemoteAgent(child, newRelayBuildId, newRelayPublicKey, RouteId{ parent.GetAgentId(), DeviceId(childSideDid) }, hash, lastSeen, hostInfo);
m_Profiler->Get().m_Gateway.UpdateRouteTimestamps(parent.GetAgentId(), timestamp);
m_Profiler->Get().m_Gateway.ConditionalUpdateChannelParameters({ parent.GetAgentId(), DeviceId(childSideDid) });
m_Profiler->Get().m_Gateway.ReAddRemoteAgent(childRid, newRelayBuildId, newRelayPublicKey, RouteId{ parentRid.GetAgentId(), childSideDid }, hash, lastSeen, hostInfo);
m_Profiler->Get().m_Gateway.UpdateRouteTimestamps(parentRid.GetAgentId(), timestamp);
m_Profiler->Get().m_Gateway.ConditionalUpdateChannelParameters({ parentRid.GetAgentId(), childSideDid });
//send update message across route.
auto&& packet = ProceduresG2X::AddRoute::Create(RouteId::FromByteView(parentRid), m_Signature, child.ToByteVector().Concat(childSideDid));
auto&& packet = ProceduresG2X::AddRoute::Create(parentRid, m_Signature, childRid.ToByteVector().Concat(childSideDid));
LockAndSendPacket(packet->ComposeQueryPacket(), receivedFrom);
}
@ -337,16 +336,16 @@ void MWR::C3::Core::GateRelay::On(ProceduresS2G::DeliverToBinder query)
auto readView = ByteView{ decryptedPacket };
auto unusedProtocolId = readView.Read<int8_t>();
auto senderRid = RouteId::FromByteView(readView.Read<Bytes<RouteId::BinarySize>>());
auto senderRid = readView.Read<RouteId>();
auto timestamp = readView.Read<int32_t>();
auto deviceId = readView.Read<Bytes<DeviceId::BinarySize>>();
auto deviceId = readView.Read<DeviceId>();
auto connectorHash = readView.Read<HashT>();
auto connector = m_Connectors.Find([&](auto const& e){ return e->GetNameHash() == connectorHash; });
if (!connector)
throw std::runtime_error{ "Connector not found" };
auto binder = RouteId{ senderRid.GetAgentId(), DeviceId{deviceId} }.ToByteVector();
auto binder = RouteId{ senderRid.GetAgentId(), deviceId }.ToByteVector();
connector->OnCommandFromBinder(binder, readView);
m_Profiler->Get().m_Gateway.UpdateRouteTimestamps(senderRid.GetAgentId(), timestamp);
@ -356,7 +355,7 @@ void MWR::C3::Core::GateRelay::On(ProceduresS2G::DeliverToBinder query)
if (!agent)
return;
auto peripheral = agent->m_Peripherals.Find(DeviceId{ deviceId });
auto peripheral = agent->m_Peripherals.Find(deviceId);
if (!peripheral || peripheral->m_StartupArguments.is_null())
return;
@ -372,7 +371,7 @@ void MWR::C3::Core::GateRelay::On(ProceduresS2G::AddDeviceResponse response)
{
auto decryptedPacket = response.GetQueryPacket(m_AuthenticationKey, m_DecryptionKey);
auto readView = ByteView{ decryptedPacket };
auto [unsusedProcedureNo, unusedSenderRouteId, timestamp, deviceId, deviceTypeHash, flags] = readView.Read<ProceduresUnderlyingType, Bytes<RouteId::BinarySize>, int32_t, DeviceId::UnderlyingIntegerType, HashT, std::uint8_t>();
auto [unsusedProcedureNo, unusedSenderRouteId, timestamp, deviceId, deviceTypeHash, flags] = readView.Read<ProceduresUnderlyingType, RouteId, int32_t, DeviceId, HashT, std::uint8_t>();
bool isChannel = flags & 1;
bool isNegotiationChannel = flags & (1 << 1);
@ -386,7 +385,7 @@ void MWR::C3::Core::GateRelay::On(ProceduresS2G::AddDeviceResponse response)
agent->ReAddPeripheral(deviceId, deviceTypeHash);
m_Profiler->Get().m_Gateway.UpdateRouteTimestamps(response.GetSenderRouteId().GetAgentId(), timestamp);
m_Profiler->Get().m_Gateway.ConditionalUpdateChannelParameters({ response.GetSenderRouteId().GetAgentId(), DeviceId{deviceId} });
m_Profiler->Get().m_Gateway.ConditionalUpdateChannelParameters({ response.GetSenderRouteId().GetAgentId(), deviceId });
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -426,7 +425,7 @@ void MWR::C3::Core::GateRelay::On(ProceduresS2G::NewNegotiatedChannelNotificatio
{
auto decryptedPacket = query.GetQueryPacket(m_AuthenticationKey, m_DecryptionKey);
auto readView = ByteView{ decryptedPacket };
auto [unsusedProcedureNo, unusedSenderRouteId, timestamp, newDeviceId, negotiatorId, inId, outId] = readView.Read<ProceduresUnderlyingType, Bytes<RouteId::BinarySize>, int32_t, DeviceId::UnderlyingIntegerType, DeviceId::UnderlyingIntegerType, std::string, std::string>();
auto [unsusedProcedureNo, unusedSenderRouteId, timestamp, newDeviceId, negotiatorId, inId, outId] = readView.Read<ProceduresUnderlyingType, RouteId, int32_t, DeviceId::UnderlyingIntegerType, DeviceId, std::string, std::string>();
auto agent = m_Profiler->Get().m_Gateway.m_Agents.Find(query.GetSenderRouteId().GetAgentId());
if (!agent)
@ -434,7 +433,7 @@ void MWR::C3::Core::GateRelay::On(ProceduresS2G::NewNegotiatedChannelNotificatio
auto negotiator = agent->m_Channels.Find(negotiatorId);
if (!negotiator)
throw std::runtime_error("Received new negotiated channel notification, but unknown with unknown Negotiator DeviceId" + DeviceId{ negotiatorId }.ToString());
throw std::runtime_error("Received new negotiated channel notification, but unknown with unknown Negotiator DeviceId" + negotiatorId.ToString());
agent->ReAddChannel(newDeviceId, negotiator->m_TypeHash, false, false);
agent->UpdateFromNegotiationChannel(negotiatorId, newDeviceId, inId, outId);
@ -448,7 +447,7 @@ void MWR::C3::Core::GateRelay::On(ProceduresS2G::Notification query)
{
auto decryptedPacket = query.GetQueryPacket(m_AuthenticationKey, m_DecryptionKey);
auto readView = ByteView{ decryptedPacket };
auto [unsusedProcedureNo, unusedSenderRouteId, timestamp, blob] = readView.Read<ProceduresUnderlyingType, Bytes<RouteId::BinarySize>, int32_t, ByteView>();
auto [unsusedProcedureNo, unusedSenderRouteId, timestamp, blob] = readView.Read<ProceduresUnderlyingType, RouteId, int32_t, ByteView>();
// blob has not defined structure. Currently Notification is used as ping response.
auto agent = m_Profiler->Get().m_Gateway.m_Agents.Find(query.GetSenderRouteId().GetAgentId());

View File

@ -66,16 +66,13 @@ void MWR::C3::Core::NodeRelay::OnProtocolG2A(ByteView packet0, std::shared_ptr<D
{
// Verify message.
auto veryfiedMessage = Crypto::VerifyMessage(packet0.SubString(1), m_GatewaySignature);
if (veryfiedMessage.size() < RouteId::BinarySize + 1 /* procedure number */)
throw std::invalid_argument{ OBF("G2A packet too short.") };
auto msgView = ByteView{ veryfiedMessage };
// Parse recipient identifier.
auto routeId = RouteId::FromByteView(veryfiedMessage);
if (routeId.GetAgentId() == m_AgentId)
if (auto routeId = msgView.Read<RouteId>(); routeId.GetAgentId() == m_AgentId)
{
// addressed to me
auto decryptedMessage = Crypto::DecryptAndAuthenticate({ veryfiedMessage, RouteId::BinarySize }, m_GatewayEncryptionKey, m_DecryptionKey);
auto decryptedMessage = Crypto::DecryptAndAuthenticate(msgView, m_GatewayEncryptionKey, m_DecryptionKey);
ProceduresG2X::RequestHandler::ParseRequestAndHandleIt(sender, routeId, decryptedMessage);
}
else
@ -96,12 +93,11 @@ void MWR::C3::Core::NodeRelay::OnProtocolG2R(ByteView packet0, std::shared_ptr<D
{
// Verify message.
auto veryfiedMessage = Crypto::VerifyMessage(packet0.SubString(1), m_GatewaySignature);
if (veryfiedMessage.size() < RouteId::BinarySize + 1 /* procedure number */)
throw std::invalid_argument{ OBF("G2A packet too short.") };
auto msgView = ByteView{ veryfiedMessage };
// Parse recipient identifier.
auto routeId = RouteId::FromByteView(veryfiedMessage);
ProceduresG2X::RequestHandler::ParseRequestAndHandleIt(sender, routeId, { veryfiedMessage, RouteId::BinarySize });
auto routeId = msgView.Read<RouteId>();
ProceduresG2X::RequestHandler::ParseRequestAndHandleIt(sender, routeId, msgView);
if (routeId.GetAgentId() != m_AgentId)
// I'm not the final recipient -> packet needs to be passed further.
@ -247,9 +243,7 @@ void MWR::C3::Core::NodeRelay::On(ProceduresG2X::RunCommandOnAgentQuery query)
void MWR::C3::Core::NodeRelay::On(ProceduresG2X::AddRoute query)
{
auto recipient = query.GetRecipientRouteId();
auto [newRouteBA, directionDidBA] = ByteView{ query.GetPacketBody() }.Read<Bytes<RouteId::BinarySize>, Bytes<DeviceId::BinarySize>>();
auto newRoute = RouteId::FromByteView(newRouteBA);
auto directionDid = DeviceId(directionDidBA);
auto [newRoute, directionDid] = ByteView{ query.GetPacketBody() }.Read<RouteId, DeviceId>();
std::shared_ptr<DeviceBridge> bridge;
if (recipient.GetAgentId() == GetAgentId())
{

View File

@ -641,8 +641,8 @@ void MWR::C3::Core::Profiler::Agent::RunCommand(ByteView commandWithArguments)
{
finalizer = [this, commandReadView]() mutable
{
auto [ridStr, didStr, isNbr] = commandReadView.Read<std::string, std::string, bool>();
ReAddRoute(RouteId::FromString(ridStr), DeviceId{ didStr }, isNbr);
auto [ridStr, didStr, isNbr] = commandReadView.Read<std::string_view, std::string_view, bool>();
ReAddRoute(ridStr, didStr, isNbr);
};
break;
}
@ -650,7 +650,7 @@ void MWR::C3::Core::Profiler::Agent::RunCommand(ByteView commandWithArguments)
{
finalizer = [this, commandReadView]() mutable
{
ReRemoveRoute(RouteId::FromString(commandReadView.Read<std::string>()));
ReRemoveRoute(commandReadView.Read<std::string_view>());
};
break;
}
@ -658,7 +658,7 @@ void MWR::C3::Core::Profiler::Agent::RunCommand(ByteView commandWithArguments)
{
finalizer = [this, commandReadView]() mutable
{
auto did = DeviceId{ commandReadView.Read<std::string>() };
auto did = DeviceId{ commandReadView.Read<std::string_view>() };
if (!m_Channels.Find(did))
throw std::runtime_error{ "Channel not found" };
@ -1014,14 +1014,14 @@ void MWR::C3::Core::Profiler::Gateway::RunCommand(ByteView commandWithArguments)
case GateRelay::Command::CreateRoute:
{
pin->CreateRoute(commandWithArguments);
auto [ridStr, didStr, isNbr] = commandWithArguments.Read<std::string, std::string, bool>();
ReAddRoute(RouteId::FromString(ridStr), DeviceId{ didStr }, isNbr);
auto [ridStr, didStr, isNbr] = commandWithArguments.Read<std::string_view, std::string_view, bool>();
ReAddRoute(ridStr, didStr, isNbr);
break;
}
case GateRelay::Command::RemoveRoute:
{
pin->RemoveRoute(commandWithArguments);
ReRemoveRoute(RouteId::FromString(commandWithArguments.Read<std::string>()));
ReRemoveRoute(commandWithArguments.Read<std::string_view>());
break;
}
case GateRelay::Command::ClearNetwork:

View File

@ -121,17 +121,17 @@ void MWR::C3::Core::Relay::Join()
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MWR::C3::Core::Relay::CreateRoute(ByteView args)
{
auto [ridStr, didStr] = args.Read<std::string, std::string>();
auto [ridStr, didStr] = args.Read<std::string_view, std::string_view>();
auto channel = m_Devices.Find([did = DeviceId{ didStr }](auto const& e) { auto l = e.lock(); return l ? l->GetDid() == did : false; }).lock();
if (!channel)
throw std::runtime_error{ OBF("Device not found") };
AddRoute(RouteId::FromString(ridStr), channel);
AddRoute(ridStr, channel);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MWR::C3::Core::Relay::RemoveRoute(ByteView args)
{
auto rid = RouteId::FromString(args.Read<std::string>());
RouteManager::RemoveRoute(rid);
auto ridStr = args.Read<std::string_view>();
RouteManager::RemoveRoute(ridStr);
}

View File

@ -1,9 +1,7 @@
#include "StdAfx.h"
#include "RouteId.h"
#include "Common/MWR/CppTools/ByteView.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const MWR::C3::RouteId MWR::C3::RouteId::Null{ AgentId::Null, DeviceId::Null };
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -14,15 +12,15 @@ constexpr MWR::C3::RouteId::RouteId(AgentId aid, DeviceId iid)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MWR::C3::RouteId MWR::C3::RouteId::FromString(std::string_view textId) noexcept(false)
MWR::C3::RouteId::RouteId(std::string_view textId)
: RouteId( textId.substr(0, AgentId::TextSize), textId.substr(AgentId::TextSize + 1)) // +1 for separator
{
return { AgentId{ textId.substr(0, AgentId::TextSize) }, DeviceId{ textId.substr(AgentId::TextSize + 1) } }; // +1 for separator
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MWR::C3::RouteId MWR::C3::RouteId::FromByteView(ByteView byteId)
MWR::C3::RouteId::RouteId(ByteView byteId)
: RouteId(byteId.SubString(0, AgentId::BinarySize), byteId.SubString(AgentId::BinarySize, DeviceId::BinarySize))
{
return { AgentId{ byteId.SubString(0, AgentId::BinarySize) }, DeviceId{ byteId.SubString(AgentId::BinarySize, DeviceId::BinarySize) } };
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -20,13 +20,11 @@ namespace MWR::C3
/// Creates a RouteId object from a hex string.
/// @param textId text containing the identifier.
/// @return Identifier object.
static RouteId FromString(std::string_view textId) noexcept(false);
RouteId(std::string_view textId);
/// Creates a RouteId object from a vector of bytes.
/// @param byteId a ByteView containing the identifier.
/// @return Identifier object.
static RouteId FromByteView(ByteView byteId);
RouteId(ByteView byteId);
/// Creates a RouteId object with a random ("unique") value.
/// @return Identifier object.
@ -102,7 +100,7 @@ namespace MWR
static C3::RouteId From(ByteView& bv)
{
auto ret = C3::RouteId::FromByteView(bv.SubString(0, C3::RouteId::BinarySize));
auto ret = C3::RouteId(bv.SubString(0, C3::RouteId::BinarySize));
bv.remove_prefix(C3::RouteId::BinarySize);
return ret;
}