Skip to content

Commit fd250fb

Browse files
authored
PublishAcknowledgementHandle fully opaque struct (#833)
1 parent 3551f42 commit fd250fb

File tree

2 files changed

+17
-36
lines changed

2 files changed

+17
-36
lines changed

include/aws/crt/mqtt/private/Mqtt5ClientCore.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,6 @@ namespace Aws
1919
{
2020
namespace Mqtt5
2121
{
22-
/**
23-
* An opaque handle representing manual control over a publish acknowledgement for a received PUBLISH
24-
* packet.
25-
*
26-
* Obtained by calling acquirePublishAcknowledgement() within the OnPublishReceivedHandler callback.
27-
* Pass this handle to Mqtt5Client::InvokePublishAcknowledgement() at any later time to send the publish
28-
* acknowledgement.
29-
*
30-
* @note acquirePublishAcknowledgement() must be called within the OnPublishReceivedHandler callback.
31-
* Calling it after the callback returns will return nullptr.
32-
*/
33-
struct AWS_CRT_CPP_API PublishAcknowledgementHandle
34-
{
35-
friend class Mqtt5ClientCore;
36-
37-
private:
38-
explicit PublishAcknowledgementHandle(uint64_t controlId) noexcept : m_controlId(controlId) {}
39-
40-
/**
41-
* Creates a PublishAcknowledgementHandle.
42-
*/
43-
static ScopedResource<PublishAcknowledgementHandle> s_create(
44-
Allocator *allocator,
45-
uint64_t controlId) noexcept;
46-
47-
uint64_t m_controlId;
48-
};
49-
5022
/**
5123
* The Mqtt5ClientCore is an internal class for Mqtt5Client. The class is used to handle communication
5224
* between Mqtt5Client and underlying c mqtt5 client. This class should only be used internally by

source/mqtt/Mqtt5ClientCore.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,27 @@ namespace Aws
2020
{
2121
namespace Mqtt5
2222
{
23-
ScopedResource<PublishAcknowledgementHandle> PublishAcknowledgementHandle::s_create(
23+
/**
24+
* We keep the definition of PublishAcknowledgementHandle here so it remains an incomplete type in all
25+
* headers. This makes the type opaque to users. They can hold a
26+
* ScopedResource<PublishAcknowledgementHandle> but cannot inspect, modify, or construct one directly.
27+
*/
28+
struct PublishAcknowledgementHandle
29+
{
30+
explicit PublishAcknowledgementHandle(uint64_t controlId) noexcept : controlId(controlId) {}
31+
uint64_t controlId;
32+
};
33+
34+
static ScopedResource<PublishAcknowledgementHandle> s_createPublishAcknowledgementHandle(
2435
Allocator *allocator,
2536
uint64_t controlId) noexcept
2637
{
27-
/* Manually call aws_mem_acquire here because PublishAcknowledgeHandle only has a private constructor
28-
* and cannot be used with Aws::Crt::New. Aws::Crt::Delete still clears memory appropriately. */
29-
void *mem = aws_mem_acquire(allocator, sizeof(PublishAcknowledgementHandle));
30-
if (!mem)
38+
PublishAcknowledgementHandle *handle =
39+
Aws::Crt::New<PublishAcknowledgementHandle>(allocator, controlId);
40+
if (!handle)
3141
{
3242
return ScopedResource<PublishAcknowledgementHandle>(nullptr, [](PublishAcknowledgementHandle *) {});
3343
}
34-
PublishAcknowledgementHandle *handle = new (mem) PublishAcknowledgementHandle(controlId);
3544
return ScopedResource<PublishAcknowledgementHandle>(
3645
handle, [allocator](PublishAcknowledgementHandle *p) { Aws::Crt::Delete(p, allocator); });
3746
}
@@ -246,7 +255,7 @@ namespace Aws
246255

247256
if (publishAcknowledgementId != 0)
248257
{
249-
functor.handle = PublishAcknowledgementHandle::s_create(
258+
functor.handle = s_createPublishAcknowledgementHandle(
250259
client_core->m_allocator, publishAcknowledgementId);
251260
/* std::function requires a copyable callable so we wrap the move only functor
252261
* into a shared_ptr so the lambda can be copyable. */
@@ -716,7 +725,7 @@ namespace Aws
716725
return false;
717726
}
718727
return aws_mqtt5_client_invoke_publish_acknowledgement(
719-
m_client, publishAcknowledgementHandle->m_controlId, nullptr) == AWS_OP_SUCCESS;
728+
m_client, publishAcknowledgementHandle->controlId, nullptr) == AWS_OP_SUCCESS;
720729
}
721730

722731
void Mqtt5ClientCore::Close() noexcept

0 commit comments

Comments
 (0)