Add property for manager hostname

This commit is contained in:
Marek S. Łukasiewicz 2025-03-03 11:06:49 +01:00
parent ad4bb45951
commit f8d769ebc6
2 changed files with 28 additions and 1 deletions

View file

@ -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");