Skip to content

Wire sending into UDP listener - #4019

Open
ryanwarfield wants to merge 6 commits into
masterfrom
ryan/wire-sending-in-udp-listener
Open

ryanwarfield wants to merge 6 commits into
masterfrom
ryan/wire-sending-in-udp-listener

Conversation

@ryanwarfield

@ryanwarfield ryanwarfield commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR:

  • Exposes the endpoint of the sender for the receive callback
  • Exposes a method to send from the listener socket (to respond to messages)

3rd party AIs sending messages might expect responses to come from the same socket that they sent their message to, so using the existing udp sender wasn't sufficient.

Testing Done

  • Unit tests

Resolved Issues

Part of stacked PR

Length Justification and Key Files to Review

Review Checklist

It is the reviewers responsibility to also make sure every item here has been covered

  • Function & Class comments: All function definitions (usually in the .h file) should have a javadoc style comment at the start of them. For examples, see the functions defined in thunderbots/software/geom. Similarly, all classes should have an associated Javadoc comment explaining the purpose of the class.
  • Remove all commented out code
  • Remove extra print statements: for example, those just used for testing
  • Resolve all TODO's: All TODO (or similar) statements should either be completed or associated with a github issue

@ryanwarfield
ryanwarfield added this pull request to stack #4020 October 18, 2026 20:35
@ryanwarfield
ryanwarfield force-pushed the ryan/wire-sending-in-udp-listener branch 2 times, most recently from ef5287a to 64b9702 Compare October 31, 2026 15:55
@ryanwarfield
ryanwarfield force-pushed the ryan/wire-sending-in-udp-listener branch from 64b9702 to 45f94e7 Compare November 3, 2026 01:24
@ryanwarfield
ryanwarfield marked this pull request as ready for review November 3, 2026 03:16
This code will always be a little bit insane. Its a callback within a
callback that needs to worry about lifetimes etc. But hopefuly this
makes it a little more legible.
// sockets aren't thread-safe, so the actual send needs to happen on the io_context
// thread
boost::asio::post(socket_.get_executor(),
[this, buf, destination]() { doSend(buf, destination); });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is it possible for this to be null/dangling? post is executed at a later date iirc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I suppose that's a risk. UdpListener holds a reference to an io_service, which is where doSend gets run. doSend uses its reference to the UdpListener to get the socket and send the message. So, destroying UdpListener first runs the risk of dangling like you said, while destroying the io_service first seems to leave a dangling pointer within the socket_ object. So, the only sane way of destruction is to first stop the io_service thread (to prevent a dangling this, then destroy the UdpListener, and then finally destroy the io_service.

This feels pretty awkward. Do you have any better ideas on how to structure this? Should I just document this order for the class?

@nycrat nycrat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code itself looks good, but I'm a bit worried about adding this send functionality to UdpListener. If it is possible, I would suggest moving this functionality to a different class, perhaps a child class of UdpListener?

#include <string>

typedef std::function<void(const char*, const size_t&)> ReceiveCallback;
typedef std::function<void(const char*, const size_t&,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

small nit: c++ using is preferred over typedef for some small reasons, but I think mostly readability

@ryanwarfield

Copy link
Copy Markdown
Contributor Author

Code itself looks good, but I'm a bit worried about adding this send functionality to UdpListener. If it is possible, I would suggest moving this functionality to a different class, perhaps a child class of UdpListener?

Lowkey I though this was gonna be really ugly, but it turned out well so I'll include it! Maybe C++ isn't so bad after all.

@nycrat nycrat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice work 🚀💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants