@@ -32,8 +32,7 @@ namespace Echo {
32
32
CHIP_ERROR EchoServer::Init (Messaging::ExchangeManager * exchangeMgr)
33
33
{
34
34
// Error if already initialized.
35
- if (mExchangeMgr != nullptr )
36
- return CHIP_ERROR_INCORRECT_STATE;
35
+ VerifyOrReturnError (mExchangeMgr == nullptr , CHIP_ERROR_INCORRECT_STATE);
37
36
38
37
mExchangeMgr = exchangeMgr;
39
38
OnEchoRequestReceived = nullptr ;
@@ -63,9 +62,6 @@ CHIP_ERROR EchoServer::OnUnsolicitedMessageReceived(const PayloadHeader & payloa
63
62
CHIP_ERROR EchoServer::OnMessageReceived (Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader,
64
63
System::PacketBufferHandle && payload)
65
64
{
66
- CHIP_ERROR err = CHIP_NO_ERROR;
67
- System::PacketBufferHandle response;
68
-
69
65
// NOTE: we already know this is an Echo Request message because we explicitly registered with the
70
66
// Exchange Manager for unsolicited Echo Requests.
71
67
@@ -75,6 +71,8 @@ CHIP_ERROR EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const
75
71
OnEchoRequestReceived (ec, payload.Retain ());
76
72
}
77
73
74
+ System::PacketBufferHandle response;
75
+
78
76
// Since we are re-using the inbound EchoRequest buffer to send the EchoResponse, if necessary,
79
77
// adjust the position of the payload within the buffer to ensure there is enough room for the
80
78
// outgoing network headers. This is necessary because in some network stack configurations,
@@ -86,12 +84,11 @@ CHIP_ERROR EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const
86
84
else
87
85
{
88
86
response = MessagePacketBuffer::NewWithData (payload->Start (), payload->DataLength ());
87
+ VerifyOrReturnError (!response.IsNull (), CHIP_ERROR_NO_MEMORY);
89
88
}
90
89
91
90
// Send an Echo Response back to the sender.
92
- err = ec->SendMessage (MsgType::EchoResponse, std::move (response));
93
-
94
- return err;
91
+ return ec->SendMessage (MsgType::EchoResponse, std::move (response));
95
92
}
96
93
97
94
} // namespace Echo
0 commit comments