Skip to content

Any way to use udpin with SO_REUSEADDR? #352

Open

Description

@amir-hossein-zarei

This is default behavior in pymavlink please do the same way in this project.
Until then any suggestion?

Activity

  1. pv42 commented on Jul 24, 2025

    @pv42
    Contributor

    Rust's std network implementation does not seem to support SO_REUSEADDR right now, therefore there is no easy way to do that right now.

    Your best current option seems to be using socket2 and manually handle the connection only using mavlink for message parsing/building.

  2. joaoantoniocardoso commented on Jul 24, 2025

    @joaoantoniocardoso
    Contributor

    Probably not what you want to hear, but before branching to set socket2's SO_REUSEADDR, check whether your limitation isn't caused by a resource not being freed.

  3. amir-hossein-zarei commented on Jul 24, 2025

    @amir-hossein-zarei
    Author

    Thank you guys for your great suggestions. I'm gonna fork and (at least try to) implement socket2 in rust-mavlink.
    I will send a pull request if it goes well.

    @joaoantoniocardoso It's actually the case but that'ss my desired outcome. here is a bit of context:
    The code I'm working on is written as guide in a workshop/competition for underwater vehicles, is undoubtedly complicated.
    It consist of three parts:

    1. a custom autopilot written in python using pymavlink to control different ROVs, mainly written by Mech. Eng. and Elect. Eng. students
    2. another python code to command these ROVs written in python using pymavlink, mainly CS students
    3. a ground station (Bluerobotic's Cockpit) that connect to the ROV using mavlink2rest

    Right now process (1) runs on a raspberry pi and broadcasts udp packets to the network on port 14550 the other two processes (2) and (3) run on another machine (students' laptop) but students can't run them simultaneously because they're listening on the same udp port.
    SO_REUSEADDR is enabled on python code, I'm sure of that but the mavlink2rest code do not enable this socket option.
    Any suggestion would be highly appreciated. Thanks in advance :)

  4. joaoantoniocardoso commented on Jul 25, 2025

    @joaoantoniocardoso
    Contributor

    Oh, I see..

    Well, you don't need to use this approach. UDP broadcasting is not trivial.

    IMHO, a simpler approach would be to write a custom piece of software that connects to the autopilot and sends the packets to many other UDP ports, giving each one of the students a different UDP port.

    Simpler than that, you can just use mavlink-server to have all those endpoints available for your students. It acts as a dumb switch, sending MAVLink packages from anyone to everyone.

    If you prefer to keep your current approach, if you know the IP:PORT mavlink2rest will use, you can bind to it by yourself with a Python script before the mavlink2rest uses it. Note that the script needs to keep the socket alive throughout mavlink2rest's life, and you can do it by calling mavlink2rest from this same script. This is kinda hacky, but might be easier than modifying the lib and rebuilding mavlink2rest with a rust-mavlink fork.

  5. amir-hossein-zarei commented on Jul 29, 2025

    @amir-hossein-zarei
    Author

    Although mavlink-server was a great suggestion, I ended up forking this repository and patching in socket2 support. This change could be helpful for users coming from a pymavlink background who expect similar socket behavior.

    I’d like to contribute this to the main branch and would appreciate your guidance on how to do that. Currently, I've added a heavy dependency just to support this small fix, which isn’t ideal. I can think of two ways to add this feature to the main branch:

    1. Reimplement the set_reuseaddr behavior from socket2 internally, avoiding the dependency altogether. (Not ideal.)
    2. Add socket2 as an optional dependency behind a Cargo feature flag that users can enable if needed.

    I’d appreciate your thoughts and feedback. Thanks in advance!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions