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

Allow multiple UDP clients to connect/disconnect/reconnect #26163

Merged
merged 7 commits into from
Jun 21, 2024

Conversation

bugobliterator
Copy link
Member

Tested on a variant of CubePilot CANMod. The issue was that we were using connected UDP Server socket. Meaning once we connect to an application, then we shutdown that application and then restart the application the listen port on the application can change. But the UDP Server will be stuck with old IP:Port and will not even listen to the new application.

This PR moves to using sendto command with IP:Port from last received packet.
This is the case for example with Mavproxy, or any other client where the data is sent without binding to specific port.

@tridge tridge removed the DevCallEU label Feb 7, 2024
@bugobliterator bugobliterator force-pushed the pr-udp-multiple-clients branch 2 times, most recently from 4a15a6a to d7b9839 Compare February 8, 2024 02:02
Copy link
Contributor

@magicrub magicrub left a comment

Choose a reason for hiding this comment

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

This is a lot of code. I recently needed the same feature for the same reason and just commented out the locking.

@bugobliterator
Copy link
Member Author

bugobliterator commented Feb 9, 2024

This is a lot of code. I recently needed the same feature for the same reason and just commented out the locking.

Most of the code is to do things right i.e. use uint32_t instead of string parsing at every packet.

@tridge tridge removed the DevCallEU label Feb 14, 2024
@bugobliterator bugobliterator force-pushed the pr-udp-multiple-clients branch 2 times, most recently from 1df9242 to aba91f6 Compare February 14, 2024 11:09
@tridge tridge removed the DevCallEU label Feb 21, 2024
@bugobliterator bugobliterator force-pushed the pr-udp-multiple-clients branch 2 times, most recently from 833ad7e to 86f60c2 Compare June 2, 2024 06:35
@bugobliterator bugobliterator force-pushed the pr-udp-multiple-clients branch from 86f60c2 to 39a4095 Compare June 18, 2024 06:33
last_udp_connect_port != 0) {
ret = sock->sendto(buf, n, last_udp_connect_address, last_udp_connect_port);
} else {
ret = sock->send(buf, n);
Copy link
Contributor

Choose a reason for hiding this comment

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

this send makes no sense without having received a pkt when we are a UDP server

Copy link
Member Author

Choose a reason for hiding this comment

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

made the condition more tied in with non-UDP_SERVER sends. And added comments for more readability

Copy link
Contributor

@peterbarker peterbarker left a comment

Choose a reason for hiding this comment

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

Add timeout? Would stop the bouncing back-and-forwards between two clients problem.

Maybe a 3 second timeout to then allow bouncing

Comment on lines 2 to 3
#include <AP_HAL/AP_HAL.h>
#include <GCS_MAVLink/GCS_config.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

These includes don't look right - don't need GCS_config and probably want AP_HAL_/AP_HAL_Boards.h for the other

Copy link
Member Author

Choose a reason for hiding this comment

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

It needs to be GCS_Config, the reason is due to the include pattern being:

GCS_Config -- includes --> AP_HAL/AP_HAL_Boards.h

HAL_ENABLE_MAVLINK_PACKETISE is defined in AP_HAL_Boards.h but relies on the definition of HAL_GCS_ENABLED which is defined in GCS_Config.h

The solution I think might be to move HAL_GCS_ENABLED one layer up into AP_HAL_Boards.h ?

Copy link
Contributor

Choose a reason for hiding this comment

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

.... but nothing in this header file is using anything related to either of those headers.

Heck, you could probably just include stdint.h and add class in front of bytebuffer and need no other include.

... actually, now I see the thing.

You're not actually using HAL_ENABLE_MAVLINK_PACKETISE in the header.

Perhaps rename to AP_MAVLINK_PACKETISE_ENABLED, move the definition of that into this header inside some infdefs and include GCS_config.h and whatever else is required to decide on whether to enable the feature...

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, done. please check

last_udp_connect_port != 0) {
ret = sock->sendto(buf, n, last_udp_connect_address, last_udp_connect_port);
} else {
ret = sock->send(buf, n);
Copy link
Contributor

Choose a reason for hiding this comment

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

Work out whether this line is actually sensible; this only makes sense if the socket has been connect()'d somewhere. Perhaps we can't actually cross this line?

Copy link
Member Author

Choose a reason for hiding this comment

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

made the condition more tied in with non-UDP_SERVER sends. And added comments for more readability

@bugobliterator bugobliterator force-pushed the pr-udp-multiple-clients branch from 39a4095 to d458cd3 Compare June 20, 2024 02:32
#include <AP_HAL/AP_HAL.h>
#include <GCS_MAVLink/GCS_config.h>

#ifndef AP_MAVLINK_PACKETISE_ENABLE
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#ifndef AP_MAVLINK_PACKETISE_ENABLE
#ifndef AP_MAVLINK_PACKETISE_ENABLED

It's a niggle but also what we're trying to standardise on

... also, do you still need HAL.h?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@bugobliterator bugobliterator force-pushed the pr-udp-multiple-clients branch from 91de39a to c2f7e2a Compare June 21, 2024 01:00
Copy link
Contributor

@tridge tridge left a comment

Choose a reason for hiding this comment

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

Thanks Sid!

@tridge tridge dismissed stale reviews from magicrub and peterbarker June 21, 2024 10:30

Stale

@tridge tridge merged commit 96682b1 into ArduPilot:master Jun 21, 2024
93 checks passed
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.

5 participants