From 94f4127f71d0358424bbaba3e849c761903a75a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 5 Jan 2020 15:33:55 +0100 Subject: Temporary and partial storage for network peers --- src/network.cpp | 59 ++++++++++++++++++++++++++++++--------------------------- src/network.h | 12 +++++++----- 2 files changed, 38 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/network.cpp b/src/network.cpp index b1beb5c..64f9ed3 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -88,11 +88,12 @@ void Server::Priv::doListen() if (ret < 0) throw std::system_error(errno, std::generic_category()); - if (auto dec = Object::decodePrefix(self.ref()->storage(), + auto peer = getPeer(paddr); + if (auto dec = PartialObject::decodePrefix(peer.partStorage, buf.begin(), buf.begin() + ret)) { - if (auto header = TransportHeader::load(std::get(*dec))) { + if (auto header = TransportHeader::load(std::get(*dec))) { scoped_lock hlock(dataMutex); - handlePacket(getPeer(paddr), *header); + handlePacket(peer, *header); } } @@ -113,7 +114,7 @@ void Server::Priv::doAnnounce() { TransportHeader::Type::AnnounceSelf, *self.ref() } }); - vector bytes = header.store(self.ref()->storage())->encode(); + vector bytes = header.toObject().encode(); for (const auto & in : bcastAddresses) { sockaddr_in sin = {}; @@ -136,7 +137,13 @@ Peer & Server::Priv::getPeer(const sockaddr_in & paddr) if (memcmp(&peer->addr, &paddr, sizeof paddr) == 0) return *peer; - Peer * peer = new Peer { .sock = sock, .addr = paddr }; + auto st = self.ref()->storage().deriveEphemeralStorage(); + Peer * peer = new Peer { + .sock = sock, + .addr = paddr, + .tempStorage = st, + .partStorage = st.derivePartialStorage(), + }; peers.emplace_back(peer); return *peer; } @@ -153,10 +160,12 @@ void Server::Priv::handlePacket(Peer & peer, const TransportHeader & header) break; case TransportHeader::Type::DataRequest: { - auto ref = std::get(item.value); - if (plaintextRefs.find(ref.digest()) != plaintextRefs.end()) { - TransportHeader::Item hitem { TransportHeader::Type::DataResponse, ref }; - peer.send(TransportHeader({ hitem }), { *ref }); + auto pref = std::get(item.value); + if (plaintextRefs.find(pref.digest()) != plaintextRefs.end()) { + if (auto ref = peer.tempStorage.ref(pref.digest())) { + TransportHeader::Item hitem { TransportHeader::Type::DataResponse, *ref }; + peer.send(TransportHeader({ hitem }), { **ref }); + } } break; } @@ -201,12 +210,12 @@ void Peer::send(const TransportHeader & header, const vector & objs) (sockaddr *) &addr, sizeof(addr)); } -optional TransportHeader::load(const Ref & ref) +optional TransportHeader::load(const PartialRef & ref) { return load(*ref); } -optional TransportHeader::load(const Object & obj) +optional TransportHeader::load(const PartialObject & obj) { auto rec = obj.asRecord(); if (!rec) @@ -274,38 +283,38 @@ optional TransportHeader::load(const Object & obj) return TransportHeader { .items = items }; } -Object TransportHeader::toObject() const +PartialObject TransportHeader::toObject() const { - vector ritems; + vector ritems; for (const auto & item : items) { switch (item.type) { case Type::Acknowledged: - ritems.emplace_back("ACK", std::get(item.value)); + ritems.emplace_back("ACK", std::get(item.value)); break; case Type::DataRequest: - ritems.emplace_back("REQ", std::get(item.value)); + ritems.emplace_back("REQ", std::get(item.value)); break; case Type::DataResponse: - ritems.emplace_back("RSP", std::get(item.value)); + ritems.emplace_back("RSP", std::get(item.value)); break; case Type::AnnounceSelf: - ritems.emplace_back("ANN", std::get(item.value)); + ritems.emplace_back("ANN", std::get(item.value)); break; case Type::AnnounceUpdate: - ritems.emplace_back("ANU", std::get(item.value)); + ritems.emplace_back("ANU", std::get(item.value)); break; case Type::ChannelRequest: - ritems.emplace_back("CRQ", std::get(item.value)); + ritems.emplace_back("CRQ", std::get(item.value)); break; case Type::ChannelAccept: - ritems.emplace_back("CAC", std::get(item.value)); + ritems.emplace_back("CAC", std::get(item.value)); break; case Type::ServiceType: @@ -313,16 +322,10 @@ Object TransportHeader::toObject() const break; case Type::ServiceRef: - ritems.emplace_back("SRF", std::get(item.value)); + ritems.emplace_back("SRF", std::get(item.value)); break; } } - return Object(Record(std::move(ritems))); -} - -Ref TransportHeader::store(const Storage & st) const -{ - - return st.storeObject(toObject()); + return PartialObject(PartialRecord(std::move(ritems))); } diff --git a/src/network.h b/src/network.h index ad29496..af202f6 100644 --- a/src/network.h +++ b/src/network.h @@ -28,6 +28,9 @@ struct Peer const int sock; const sockaddr_in addr; + Storage tempStorage; + PartialStorage partStorage; + void send(const struct TransportHeader &, const vector &); }; @@ -47,14 +50,13 @@ struct TransportHeader struct Item { const Type type; - const variant value; + const variant value; }; TransportHeader(const vector & items): items(items) {} - static optional load(const Ref &); - static optional load(const Object &); - Object toObject() const; - Ref store(const Storage & st) const; + static optional load(const PartialRef &); + static optional load(const PartialObject &); + PartialObject toObject() const; const vector items; }; -- cgit v1.2.3