Skip to content

Commit

Permalink
Ensure LoopbackTransport always is able to schedule a new message sen…
Browse files Browse the repository at this point in the history
…d without interfering with any previous message send
  • Loading branch information
andy31415 committed Sep 2, 2022
1 parent ca63e43 commit fade8ee
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/transport/raw/tests/NetworkTestHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,16 @@ class LoopbackTransportDelegate
// configurable allowed number of messages (mNumMessagesToAllowBeforeDropping)
virtual void OnMessageDropped() {}
};

class LoopbackTransport : public Transport::Base
{
private:
// Use unique pointers for work callbacks, so that one callback does not cancel another.
struct LoopbackWork
{
LoopbackTransport * self;
LoopbackWork(LoopbackTransport * transport) : self(transport) {}
};

public:
void InitLoopbackTransport(System::Layer * systemLayer) { mSystemLayer = systemLayer; }
void ShutdownLoopbackTransport()
Expand All @@ -89,7 +96,9 @@ class LoopbackTransport : public Transport::Base

static void OnMessageReceived(System::Layer * aSystemLayer, void * aAppState)
{
LoopbackTransport * _this = static_cast<LoopbackTransport *>(aAppState);
LoopbackWork * work = static_cast<LoopbackWork *>(aAppState);
LoopbackTransport * _this = work->self;
delete work;

while (!_this->mPendingMessageQueue.empty())
{
Expand Down Expand Up @@ -129,7 +138,7 @@ class LoopbackTransport : public Transport::Base
{
System::PacketBufferHandle receivedMessage = msgBuf.CloneData();
mPendingMessageQueue.push(PendingMessageItem(address, std::move(receivedMessage)));
mSystemLayer->ScheduleWork(OnMessageReceived, this);
mSystemLayer->ScheduleWork(OnMessageReceived, new LoopbackWork(this));
}

return CHIP_NO_ERROR;
Expand Down

0 comments on commit fade8ee

Please sign in to comment.