first init

This commit is contained in:
TotMaxim
2025-11-10 19:35:17 +03:00
commit f6649907d4
17 changed files with 297 additions and 0 deletions

79
udp_broadcast.gd Normal file
View File

@@ -0,0 +1,79 @@
class_name UDPBroadcast
const addres_list: Dictionary = {
'ПРД-Н1': [50011, "10.1.1.11"],
'ПРД-В1': [50012, "10.1.1.12"],
'ПРД-К1': [50013, "10.1.1.13"],
'ПРД-Н2': [50021, "10.1.1.21"],
'ПРД-В2': [50022, "10.1.1.22"],
'ПРД-К2': [50023, "10.1.1.23"],
'ПРД-Н3': [50031, "10.1.1.31"],
'ПРД-В3': [50032, "10.1.1.32"],
'ПРД-К3': [50033, "10.1.1.33"],
'ПРД-Н4': [50041, "10.1.1.41"],
'ПРД-В4': [50042, "10.1.1.42"],
'ПРД-К4': [50043, "10.1.1.43"],
}
const broadcast_addr: Array = [50000, "10.1.1.255"]
#const bind_addr: Array = [50_000, "10.1.1.70"] TODO: Порт по умолчанию
var _socket: SocketUDP
var self_name: String
var _timer: Timer
func _init(name: String):
self_name = name
_socket = SocketUDP.new()
_timer = Timer.new()
_timer.wait_time = 1.0 / 3.0
_timer.timeout.connect(_on_timer_timeout)
if addres_list.has(name):
var port: int = addres_list[name][0]
var addr: String = addres_list[name][1]
bind_unit_addres(port, addr)
else:
push_error("Неизвестное устройство: %s" % name)
func bind_unit_addres(port_for_bind: int, addr_for_bind: String) -> void:
print('Привязка адреса: %s' % addr_for_bind)
var bind_result = _socket.bind(port_for_bind, addr_for_bind)
if bind_result != OK:
push_error("Не удалось привязаться к %s: %s" % [addr_for_bind, error_string(bind_result)])
func start_broadcast() -> void:
_timer.start()
func stop_broadcast() -> void:
_timer.stop()
func send_broadcast(message: String) -> bool:
_socket.set_broadcast_enabled(true)
var data: PackedByteArray = message.to_utf8_buffer()
_socket.send_to(broadcast_addr[1], broadcast_addr[0], data)
return true
func close_udp_unit() -> void:
_timer.stop()
_socket.close()
func add_timer_to_scene(parent: Node) -> void:
parent.add_child(_timer)
func _on_timer_timeout() -> void:
send_broadcast("hello")
class SocketUDP extends PacketPeerUDP:
func send_to(addr: String, port: int, data: PackedByteArray):
set_dest_address(addr, port)
var rc: = put_packet(data)
if rc != OK:
push_error('%s: %s:%s' % [error_string(rc), addr, port])