@@ -64,6 +64,10 @@ class LoopbackTransportDelegate
64
64
public:
65
65
virtual ~LoopbackTransportDelegate () {}
66
66
67
+ // Called by the loopback transport when a message is requested to be sent.
68
+ // This is called even if the message is subsequently rejected or dropped.
69
+ virtual void WillSendMessage (const Transport::PeerAddress & peer, const System::PacketBufferHandle & message) {}
70
+
67
71
// Called by the loopback transport when it drops one of a configurable number of messages (mDroppedMessageCount) after a
68
72
// configurable allowed number of messages (mNumMessagesToAllowBeforeDropping)
69
73
virtual void OnMessageDropped () {}
@@ -72,6 +76,18 @@ class LoopbackTransportDelegate
72
76
class LoopbackTransport : public Transport ::Base
73
77
{
74
78
public:
79
+ // In test scenarios using the loopback transport, we're only ever given
80
+ // the address we're sending to, but we don't have any information about
81
+ // what our local address is. Assume our fake addresses come in pairs of
82
+ // even and odd port numbers, so we can calculate one from the other by
83
+ // flipping the LSB of the port number.
84
+ static Transport::PeerAddress LoopbackPeer (const Transport::PeerAddress & address)
85
+ {
86
+ Transport::PeerAddress other (address);
87
+ other.SetPort (address.GetPort () ^ 1 );
88
+ return other;
89
+ }
90
+
75
91
void InitLoopbackTransport (System::Layer * systemLayer)
76
92
{
77
93
Reset ();
@@ -100,14 +116,19 @@ class LoopbackTransport : public Transport::Base
100
116
{
101
117
auto item = std::move (_this->mPendingMessageQueue .front ());
102
118
_this->mPendingMessageQueue .pop ();
103
- _this->HandleMessageReceived (item.mDestinationAddress , std::move (item.mPendingMessage ));
119
+ _this->HandleMessageReceived (LoopbackPeer ( item.mDestinationAddress ) , std::move (item.mPendingMessage ));
104
120
}
105
121
}
106
122
107
123
static constexpr uint32_t kUnlimitedMessageCount = std::numeric_limits<uint32_t >::max();
108
124
109
125
CHIP_ERROR SendMessage (const Transport::PeerAddress & address, System::PacketBufferHandle && msgBuf) override
110
126
{
127
+ if (mDelegate != nullptr )
128
+ {
129
+ mDelegate ->WillSendMessage (address, msgBuf);
130
+ }
131
+
111
132
if (mNumMessagesToAllowBeforeError == 0 )
112
133
{
113
134
ReturnErrorOnFailure (mMessageSendError );
0 commit comments