Skip to content

Commit

Permalink
Make sure to pass NetPackets around as unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Ettinger committed Jun 7, 2022
1 parent 200a812 commit 4dc6496
Show file tree
Hide file tree
Showing 25 changed files with 445 additions and 547 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ depcomp
install-sh
missing
stamp-h1

.lvimrc


#----------------------------
Expand Down
70 changes: 9 additions & 61 deletions opensand-core/src/common/EncapPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ EncapPlugin::EncapPacketHandler::EncapPacketHandler(EncapPlugin &pl):

EncapPlugin::EncapPacketHandler::~EncapPacketHandler()
{
for(auto&& it : this->encap_packets)
{
delete it.first;
}
this->encap_packets.clear();
}

bool EncapPlugin::EncapPacketHandler::init()
Expand All @@ -105,68 +100,21 @@ std::list<std::string> EncapPlugin::EncapPacketHandler::getCallback()
}
*/

bool EncapPlugin::EncapPacketHandler::encapNextPacket(NetPacket *packet,
bool EncapPlugin::EncapPacketHandler::encapNextPacket(std::unique_ptr<NetPacket> packet,
std::size_t remaining_length,
bool UNUSED(new_burst),
bool &partial_encap,
NetPacket **encap_packet)
bool,
std::unique_ptr<NetPacket> &encap_packet,
std::unique_ptr<NetPacket> &remaining_data)
{
// Set default returned values
partial_encap = false;
*encap_packet = nullptr;

// Check there is a previous encapsulation of the packet
std::unique_ptr<NetPacket> packet_to_encap;
auto it = this->encap_packets.find(packet);
if(it == this->encap_packets.end())
{
packet_to_encap = std::unique_ptr<NetPacket>{new NetPacket(packet)};
}
else
{
packet_to_encap = std::move(it->second);
}
remaining_data.reset();

// get the part of the packet to send
std::unique_ptr<NetPacket> data;
std::unique_ptr<NetPacket> remaining_data;
bool success = this->getChunk(std::move(packet_to_encap),
remaining_length,
data, remaining_data);
if(!success || (!data && !remaining_data))
{
return false;
}

// Set the returned encap packet
if(data)
{
*encap_packet = data.release();
}
bool success = this->getChunk(std::move(packet),
remaining_length,
encap_packet, remaining_data);

// Check the remaining data
if(remaining_data)
{
partial_encap = true;
if(it == this->encap_packets.end())
{
// Insert the remaining data
this->encap_packets.insert({packet, std::move(remaining_data)});
}
else
{
// Modify the remaining data
it->second = std::move(remaining_data);
}
}
else if(it != this->encap_packets.end())
{
// Remove the remaining data
delete it->first;
this->encap_packets.erase(it);
}

return true;
return success && (encap_packet != nullptr || remaining_data != nullptr);
}

bool EncapPlugin::EncapPacketHandler::getEncapsulatedPackets(NetContainer *packet,
Expand Down
13 changes: 5 additions & 8 deletions opensand-core/src/common/EncapPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ class EncapPlugin: public StackPlugin
*
* @return true if success, false otherwise
*/
bool encapNextPacket(NetPacket *packet,
bool encapNextPacket(std::unique_ptr<NetPacket> packet,
std::size_t remaining_length,
bool new_burst,
bool &partial_encap,
NetPacket **encap_packet) override;
std::unique_ptr<NetPacket> &encap_packet,
std::unique_ptr<NetPacket> &remaining_data) override;

/**
* @brief Get encapsulated packet from payload
Expand All @@ -126,9 +126,9 @@ class EncapPlugin: public StackPlugin
std::vector<std::unique_ptr<NetPacket>> &decap_packets,
unsigned int decap_packet_count=0) override;

virtual bool getPacketForHeaderExtensions(const std::vector<NetPacket*>& packets, NetPacket ** selected_pkt) = 0;
virtual bool checkPacketForHeaderExtensions(std::unique_ptr<NetPacket> &packet) = 0;

virtual bool setHeaderExtensions(const NetPacket* packet,
virtual bool setHeaderExtensions(std::unique_ptr<NetPacket> packet,
std::unique_ptr<NetPacket>& new_packet,
tal_id_t tal_id_src,
tal_id_t tal_id_dst,
Expand Down Expand Up @@ -174,9 +174,6 @@ class EncapPlugin: public StackPlugin

/// map call back name
std::list<std::string> callback_name;

/// map packets being encapsulated
std::map<NetPacket *, std::unique_ptr<NetPacket>> encap_packets;
};

/**
Expand Down
9 changes: 6 additions & 3 deletions opensand-core/src/common/LanAdaptationPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,23 @@ bool LanAdaptationPlugin::LanAdaptationPacketHandler::init()
return true;
}


std::size_t LanAdaptationPlugin::LanAdaptationPacketHandler::getMinLength() const
{
assert(0);
}

bool LanAdaptationPlugin::LanAdaptationPacketHandler::encapNextPacket(NetPacket *,

bool LanAdaptationPlugin::LanAdaptationPacketHandler::encapNextPacket(std::unique_ptr<NetPacket>,
std::size_t,
bool,
bool &,
NetPacket **)
std::unique_ptr<NetPacket> &,
std::unique_ptr<NetPacket> &)
{
assert(0);
}


bool LanAdaptationPlugin::LanAdaptationPacketHandler::getEncapsulatedPackets(NetContainer *,
bool &,
std::vector<std::unique_ptr<NetPacket>> &,
Expand Down
28 changes: 14 additions & 14 deletions opensand-core/src/common/LanAdaptationPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,21 @@ class LanAdaptationPlugin: public StackPlugin
/* Allow packets to access LanAdaptationPlugin members */
LanAdaptationPacketHandler(LanAdaptationPlugin &pl);

/* the following functions should not be called */
std::size_t getMinLength() const;

virtual bool encapNextPacket(NetPacket *packet,
std::size_t remaining_length,
bool new_burst,
bool &partial_encap,
NetPacket **encap_packet);

virtual bool getEncapsulatedPackets(NetContainer *packet,
bool &partial_decap,
std::vector<std::unique_ptr<NetPacket>> &decap_packets,
unsigned int decap_packets_count) override;
bool init() override;

virtual bool init();
/* the following functions should not be called */
std::size_t getMinLength() const override;

bool encapNextPacket(std::unique_ptr<NetPacket> packet,
std::size_t remaining_length,
bool new_burst,
std::unique_ptr<NetPacket> &encap_packet,
std::unique_ptr<NetPacket> &remaining_packet) override;

bool getEncapsulatedPackets(NetContainer *packet,
bool &partial_decap,
std::vector<std::unique_ptr<NetPacket>> &decap_packets,
unsigned int decap_packets_count) override;
};

/**
Expand Down
56 changes: 28 additions & 28 deletions opensand-core/src/common/NetPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,56 +36,56 @@


NetPacket::NetPacket(const unsigned char *data, std::size_t length):
NetContainer(data, length),
type(NET_PROTO_ERROR),
qos(),
src_tal_id(),
dst_tal_id()
NetContainer{data, length},
type{NET_PROTO_ERROR},
qos{},
src_tal_id{},
dst_tal_id{}
{
this->name = "NetPacket";
}


NetPacket::NetPacket(const Data &data):
NetContainer(data),
type(NET_PROTO_ERROR),
qos(),
src_tal_id(),
dst_tal_id()
NetContainer{data},
type{NET_PROTO_ERROR},
qos{},
src_tal_id{},
dst_tal_id{}
{
this->name = "NetPacket";
}


NetPacket::NetPacket(const Data &data, std::size_t length):
NetContainer(data, length),
type(NET_PROTO_ERROR),
qos(),
src_tal_id(),
dst_tal_id()
NetContainer{data, length},
type{NET_PROTO_ERROR},
qos{},
src_tal_id{},
dst_tal_id{}
{
this->name = "NetPacket";
}


NetPacket::NetPacket(NetPacket *pkt):
NetContainer(pkt->getData(), pkt->getTotalLength()),
type(pkt->getType()),
qos(pkt->getQos()),
src_tal_id(pkt->getSrcTalId()),
dst_tal_id(pkt->getDstTalId())
NetPacket::NetPacket(const NetPacket &pkt):
NetContainer{pkt.getData(), pkt.getTotalLength()},
type{pkt.getType()},
qos{pkt.getQos()},
src_tal_id{pkt.getSrcTalId()},
dst_tal_id{pkt.getDstTalId()}
{
this->name = pkt->getName();
this->spot = pkt->getSpot();
this->name = pkt.getName();
this->spot = pkt.getSpot();
}


NetPacket::NetPacket():
NetContainer(),
type(NET_PROTO_ERROR),
qos(),
src_tal_id(),
dst_tal_id()
NetContainer{},
type{NET_PROTO_ERROR},
qos{},
src_tal_id{},
dst_tal_id{}
{
this->name = "NetPacket";
}
Expand Down
2 changes: 1 addition & 1 deletion opensand-core/src/common/NetPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class NetPacket: public NetContainer
* Build a network-layer packet
* @param pkt
*/
NetPacket(NetPacket *pkt);
NetPacket(const NetPacket &pkt);

/**
* Build an empty network-layer packet
Expand Down
6 changes: 3 additions & 3 deletions opensand-core/src/common/StackPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ class StackPlugin: public OpenSandPlugin
*
* @return true if success, false otherwise
*/
virtual bool encapNextPacket(NetPacket *packet,
virtual bool encapNextPacket(std::unique_ptr<NetPacket> packet,
std::size_t remaining_length,
bool new_burst,
bool &partial_encap,
NetPacket **encap_packet) = 0;
std::unique_ptr<NetPacket> &encap_packet,
std::unique_ptr<NetPacket> &remaining_data) = 0;

/**
* @brief Get encapsulated packet from payload
Expand Down
18 changes: 9 additions & 9 deletions opensand-core/src/common/tests/test_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ static void test_encap_and_decap(
for(auto plugit = encap_plug.begin(); plugit != encap_plug.end(); ++plugit)
{
encap_contexts_t encap_contexts;
std::string name = plugit->first;
std::string name_low;
std::string name = plugit->first;
std::string name_low;
EncapPlugin *plugin = NULL;
EncapPlugin::EncapContext *context;
int found;
Expand All @@ -256,7 +256,7 @@ static void test_encap_and_decap(
INFO("cannot set %s as upper layer for %s context, find another one\n",
pkt_hdl->getName().c_str(), name.c_str());

std::vector<std::string> upper = context->getAvailableUpperProto();
std::vector<std::string> upper = context->getAvailableUpperProto();
// try to add a supported upper layer
for(std::vector<std::string>::iterator iter = upper.begin();
iter != upper.end(); ++iter)
Expand Down Expand Up @@ -475,7 +475,7 @@ static bool test_iter(std::string src_filename, std::string encap_filename,
header_init = 1;
}

std::unique_ptr<NetPacket> net_packet{new NetPacket{packet + src_link_len,
std::unique_ptr<NetPacket> net_packet{new NetPacket{packet + src_link_len,
header.len - src_link_len}};
if(net_packet == NULL)
{
Expand Down Expand Up @@ -533,11 +533,11 @@ static bool test_iter(std::string src_filename, std::string encap_filename,
NetBurst *flushed = (*ctxit)->flushAll();
if(flushed && !flushed->empty())
{
// TODO: use back_inserter or something
for (auto&& p : *flushed)
{
encap_packets->push_back(std::move(p));
}
// TODO: use back_inserter or something
for (auto&& p : *flushed)
{
encap_packets->push_back(std::move(p));
}
}
}
if(!encap_packets)
Expand Down
2 changes: 1 addition & 1 deletion opensand-core/src/dvb/core/BlockDvb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,5 @@ bool BlockDvb::DvbDownward::onRcvEncapPacket(std::unique_ptr<NetPacket> packet,
time_ms_t fifo_delay)
{
//TODO: lift off the release call in favor of std::move
return this->pushInFifo(fifo, packet.release(), fifo_delay);
return this->pushInFifo(fifo, std::move(packet), fifo_delay);
}
Loading

0 comments on commit 4dc6496

Please sign in to comment.