Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Followups from #6561 Generalize socket-based event loop #7799

Merged
merged 3 commits into from
Jun 23, 2021

Conversation

kpschoedel
Copy link
Contributor

@kpschoedel kpschoedel commented Jun 21, 2021

Problem

Followups from #6561 Generalize socket-based event loop

Change overview

Fixes #7758 Convert MDNS to WatchableSocket.

Testing

Manual sanity check of mDNS using chip-device-ctrl on Linux.
Otherwise, no change to functionality is intended.

src/platform/Linux/MdnsImpl.cpp Outdated Show resolved Hide resolved
@@ -145,19 +172,43 @@ AvahiWatch * Poller::WatchNew(int fd, AvahiWatchEvent event, AvahiWatchCallback
{
VerifyOrDie(callback != nullptr && fd >= 0);

mWatches.emplace_back(new AvahiWatch{ fd, event, 0, callback, context, this });
AvahiWatch * const watch = new AvahiWatch;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is deletecalled for these objects?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of std::vector<std::unique_ptr<AvahiWatch>> mWatches;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto watch = std::make_unique<AvahiWatch>() ?

@@ -35,8 +35,8 @@
namespace chip {
namespace Mdns {
// TODO(#5556): Convert MDNS to WatchableSocket.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these TODOs still relevant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they're not; the remaining part is timer-related and pending #7725

@@ -145,19 +172,43 @@ AvahiWatch * Poller::WatchNew(int fd, AvahiWatchEvent event, AvahiWatchCallback
{
VerifyOrDie(callback != nullptr && fd >= 0);

mWatches.emplace_back(new AvahiWatch{ fd, event, 0, callback, context, this });
AvahiWatch * const watch = new AvahiWatch;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto watch = std::make_unique<AvahiWatch>() ?

@@ -424,7 +424,7 @@ INET_ERROR RawEndPoint::Listen(IPEndPointBasis::OnMessageReceivedFunct onMessage

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
// Wait for ability to read on this endpoint.
mSocket.SetCallback(HandlePendingIO, this);
mSocket.SetCallback(HandlePendingIO, reinterpret_cast<intptr_t>(this));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires a more casts and stronger casts, I wonder if it is worth it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a request in the previous review, so you and Boris can fight it out :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a suggestion; Boris said he was OK either way. Above was just a question. Ultimately I think it is up to you.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, given what this diff looks like and that we have no non-pointer use cases for this closure, we should probably leave it as void* for now. If we ever end up with non-pointer use cases, we can think about how to be best address those then...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to my TODO list.


struct AvahiWatch
{
#if 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean up dead else?

Followups from project-chip#6561 Generalize socket-based event loop

#### Change overview

* mDNS: Convert `platform/Linux/MdnsImpl`; remove Darwin stubs.
* Change `mCallbackData` type to `intptr_t` (review feedback).
* Comment for `System::WakeEvent` (this is minimal since it's
  likely to change soon for issue project-chip#7725).

Fixes project-chip#7758 _Convert MDNS to WatchableSocket._

#### Testing

Manual sanity check of mDNS using chip-device-ctrl on Linux.
Otherwise, no change to functionality is intended.
@@ -424,7 +424,7 @@ INET_ERROR RawEndPoint::Listen(IPEndPointBasis::OnMessageReceivedFunct onMessage

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
// Wait for ability to read on this endpoint.
mSocket.SetCallback(HandlePendingIO, this);
mSocket.SetCallback(HandlePendingIO, reinterpret_cast<intptr_t>(this));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a suggestion; Boris said he was OK either way. Above was just a question. Ultimately I think it is up to you.

@@ -167,6 +208,7 @@ void Poller::WatchFree(AvahiWatch * watch)

void Poller::WatchFree(AvahiWatch & watch)
{
(void) watch.mSocket.ReleaseFD();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a confused by this. WatchableSocket is normally expected to own the fd but not in the case of avahi?

If WatchableSocket is owning, why doesn't it close in its destructor?

If WatchableSocket is non-owning, why does it close the file descriptor in Close()?

If WatchableSocket non-owning but Close() is just a helper for release-and-close, should "release" call WatchableSocket::OnClose which does the actual detach-from-WatchableEventManager?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a confused by this. WatchableSocket is normally expected to own the fd but not in the case of avahi?

Right. In the other cases (Inet and System::WakeEvent) the WatchableSocket effectively owns the fd, and replaces the fd from the point of view of the holder. In the Avahi case, Avahi itself owns the fd and lends it via AvahiPoll.watch_new and watch_free.

If WatchableSocket is owning, why doesn't it close in its destructor?

For the normal (Inet) case, it's contained inside an IPEndPointBasis which is not constructible or destructible. It's contained in order to avoid requiring separate allocation. It may be worth revisiting that if/when the System/Inet virtualization and pool allocation improvements happen.

If WatchableSocket is non-owning, why does it close the file descriptor in Close()?

Convenience for the common case.

If WatchableSocket non-owning but Close() is just a helper for release-and-close, should "release" call WatchableSocket::OnClose which does the actual detach-from-WatchableEventManager?

Yes, OnClose() should be renamed OnRelease().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a confused by this. WatchableSocket is normally expected to own the fd but not in the case of avahi?

Right. In the other cases (Inet and System::WakeEvent) the WatchableSocket effectively owns the fd, and replaces the fd from the point of view of the holder. In the Avahi case, Avahi itself owns the fd and lends it via AvahiPoll.watch_new and watch_free.

Having a non-owning watcher separate from fd-owner makes the most sense to me and would fit well with avahi but understood there are other constraints here.

If WatchableSocket is owning, why doesn't it close in its destructor?

For the normal (Inet) case, it's contained inside an IPEndPointBasis which is not constructible or destructible. It's contained in order to avoid requiring separate allocation. It may be worth revisiting that if/when the System/Inet virtualization and pool allocation improvements happen.

Ahh, the pool should really run destructors on slots released back to it :(

If WatchableSocket is non-owning, why does it close the file descriptor in Close()?

Convenience for the common case.

If WatchableSocket non-owning but Close() is just a helper for release-and-close, should "release" call WatchableSocket::OnClose which does the actual detach-from-WatchableEventManager?

Yes, OnClose() should be renamed OnRelease().

Ah, I see that it does I was just confused by the name. Rename sounds good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a non-owning watcher separate from fd-owner makes the most sense to me and would fit well with avahi but understood there are other constraints here.

I'll take a look at this when I do the remaining test and timer TODOs, which likely will be together with #7725. I don't know that it will make much difference; since we don't have weak fds, a watcher must be informed before a file is closed.

Yes, OnClose() should be renamed OnRelease().

Ah, I see that it does I was just confused by the name. Rename sounds good.

Ack, added to my TODO list.

@@ -424,7 +424,7 @@ INET_ERROR RawEndPoint::Listen(IPEndPointBasis::OnMessageReceivedFunct onMessage

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
// Wait for ability to read on this endpoint.
mSocket.SetCallback(HandlePendingIO, this);
mSocket.SetCallback(HandlePendingIO, reinterpret_cast<intptr_t>(this));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, given what this diff looks like and that we have no non-pointer use cases for this closure, we should probably leave it as void* for now. If we ever end up with non-pointer use cases, we can think about how to be best address those then...

@woody-apple
Copy link
Contributor

@andy31415 andy31415 merged commit 9692e9a into project-chip:master Jun 23, 2021
@kpschoedel kpschoedel deleted the x5556-2 branch July 13, 2021 15:51
nikita-s-wrk pushed a commit to nikita-s-wrk/connectedhomeip that referenced this pull request Sep 23, 2021
…roject-chip#7799)

* #### Problem

Followups from project-chip#6561 Generalize socket-based event loop

#### Change overview

* mDNS: Convert `platform/Linux/MdnsImpl`; remove Darwin stubs.
* Change `mCallbackData` type to `intptr_t` (review feedback).
* Comment for `System::WakeEvent` (this is minimal since it's
  likely to change soon for issue project-chip#7725).

Fixes project-chip#7758 _Convert MDNS to WatchableSocket._

#### Testing

Manual sanity check of mDNS using chip-device-ctrl on Linux.
Otherwise, no change to functionality is intended.

* review

* review
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(#5556): Convert MDNS to WatchableSocket.
6 participants