diff --git a/src/marshconnector.cpp b/src/marshconnector.cpp index cac39ae..cdd5aca 100644 --- a/src/marshconnector.cpp +++ b/src/marshconnector.cpp @@ -6,7 +6,9 @@ #include "godot_cpp/classes/packet_peer_udp.hpp" #include "godot_cpp/core/math.hpp" #include "godot_cpp/core/memory.hpp" +#include "godot_cpp/core/object.hpp" #include "godot_cpp/core/print_string.hpp" +#include "godot_cpp/core/property_info.hpp" #include "godot_cpp/variant/basis.hpp" #include "godot_cpp/variant/packed_byte_array.hpp" #include "godot_cpp/variant/quaternion.hpp" @@ -34,6 +36,13 @@ void MarshConnector::_bind_methods() { ClassDB::bind_method(D_METHOD("get_manager_connected"), &MarshConnector::get_manager_connected); + // Properties + ClassDB::bind_method(D_METHOD("get_hostname"), &MarshConnector::get_hostname); + ClassDB::bind_method(D_METHOD("set_hostname", "p_hostname"), + &MarshConnector::set_hostname); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "hostname"), "set_hostname", + "get_hostname"); + // Timer callbacks ClassDB::bind_method(D_METHOD("send_heartbeat"), &MarshConnector::send_heartbeat); @@ -56,6 +65,7 @@ MarshConnector::MarshConnector() { socket = memnew(PacketPeerUDP); + manager_hostname = "127.0.0.1"; manager_connected = false; manager_timer = memnew(Timer); @@ -85,7 +95,7 @@ void MarshConnector::_process(double delta) { time_passed += delta; if (!socket->is_socket_connected()) { - socket->connect_to_host("192.168.1.2", 24400); + socket->connect_to_host(manager_hostname, 24400); } else { while (socket->get_available_packet_count() > 0) { const PackedByteArray data = socket->get_packet(); @@ -94,6 +104,19 @@ void MarshConnector::_process(double delta) { } } +String MarshConnector::get_hostname() const { return manager_hostname; } + +void MarshConnector::set_hostname(const String hostname) { + // Socket's destination address is not available, so discard useless updates + // and recreate socket when needed + if (hostname == manager_hostname) + return; + + manager_hostname = hostname; + if (socket->is_socket_connected()) + socket->close(); +} + Error MarshConnector::send_heartbeat() { // print_line("Sending HEARTBEAT at ", time_passed, " seconds"); diff --git a/src/marshconnector.h b/src/marshconnector.h index 9e86eac..e1a3e78 100644 --- a/src/marshconnector.h +++ b/src/marshconnector.h @@ -25,6 +25,9 @@ public: void _ready() override; void _process(double delta) override; + void set_hostname(const String hostname); + String get_hostname() const; + // Returns the error from put_packet Error send_heartbeat(); // Handle MAVLink messages fully contained in data @@ -109,6 +112,7 @@ private: Timer *heartbeat_timer; PacketPeerUDP *socket; + String manager_hostname; bool manager_connected; Timer *manager_timer; bool model_connected;