Skip to content

Commit

Permalink
Convert mdns Builder classes to use PacketBufferHandle (#4094)
Browse files Browse the repository at this point in the history
* Convert mdns Builder classes to use PacketBufferHandle

#### Problem

Code should use `PacketBufferHandle` rather than `PacketBuffer`
directly.

#### Summary of Changes

Convert mdns::Minimal::QueryBuilder and mdns::Minimal::ResponseBuilder
to take and hold PacketBufferHandle.

Part of issue #2707 - Figure out a way to express PacketBuffer ownership in the type system

* Review: make QueryBuilder take const&

* WIP

* Revise to match ResponseBuilder in #4102

* Restyled by clang-format

* remove PacketReporter.cpp change

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
kpschoedel and restyled-commits authored Jan 8, 2021
1 parent f9748f3 commit f77bebf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions examples/minimal-mdns/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ void BroadcastPacket(mdns::Minimal::ServerBase * server)
QuerySplitter query;
query.Split(gOptions.query);

mdns::Minimal::QueryBuilder builder(buffer.Get_ForNow());
mdns::Minimal::QueryBuilder builder(std::move(buffer));

builder.Header().SetMessageId(kTestMessageId);
builder.AddQuery(query
Expand All @@ -277,7 +277,7 @@ void BroadcastPacket(mdns::Minimal::ServerBase * server)
return;
}

if (server->BroadcastSend(std::move(buffer), gOptions.querySendPort) != CHIP_NO_ERROR)
if (server->BroadcastSend(builder.ReleasePacket, gOptions.querySendPort) != CHIP_NO_ERROR)
{
printf("Error sending\n");
return;
Expand Down
28 changes: 20 additions & 8 deletions src/lib/mdns/minimal/QueryBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,39 @@ namespace Minimal {
class QueryBuilder
{
public:
QueryBuilder(chip::System::PacketBuffer * packet) : mPacket(packet), mHeader(mPacket->Start())
{
QueryBuilder() : mHeader(nullptr) {}
QueryBuilder(chip::System::PacketBufferHandle && packet) : mHeader(nullptr) { Reset(std::move(packet)); }

QueryBuilder & Reset(chip::System::PacketBufferHandle && packet)
{
mPacket = std::move(packet);
if (mPacket->AvailableDataLength() >= HeaderRef::kSizeBytes)
{
mPacket->SetDataLength(HeaderRef::kSizeBytes);
mHeader.Clear();
}
else
{
mQueryBuidOk = false;
mQueryBuildOk = false;
}

mHeader.SetFlags(mHeader.GetFlags().SetQuery());
return *this;
}

CHECK_RETURN_VALUE
chip::System::PacketBufferHandle && ReleasePacket()
{
mHeader = HeaderRef(nullptr);
mQueryBuildOk = false;
return std::move(mPacket);
}

HeaderRef & Header() { return mHeader; }

QueryBuilder & AddQuery(const Query & query)
{
if (!mQueryBuidOk)
if (!mQueryBuildOk)
{
return *this;
}
Expand All @@ -57,7 +69,7 @@ class QueryBuilder

if (!query.Append(mHeader, out))
{
mQueryBuidOk = false;
mQueryBuildOk = false;
}
else
{
Expand All @@ -66,12 +78,12 @@ class QueryBuilder
return *this;
}

bool Ok() const { return mQueryBuidOk; }
bool Ok() const { return mQueryBuildOk; }

private:
chip::System::PacketBuffer * mPacket;
chip::System::PacketBufferHandle mPacket;
HeaderRef mHeader;
bool mQueryBuidOk = true;
bool mQueryBuildOk = true;
};

} // namespace Minimal
Expand Down

0 comments on commit f77bebf

Please sign in to comment.