Rewrote peer update code. Peers are currently vanishing when the UDP packet is sent to them.

This commit is contained in:
Scott Duensing 2023-08-29 21:17:27 -05:00
parent 56ddb29c56
commit e622341f95
3 changed files with 101 additions and 48 deletions

View file

@ -153,10 +153,12 @@ func _on_timer_timeout():
# Update list of known peers. # Update list of known peers.
var peers = net.get_peers() var peers = net.get_peers()
if Peers.update(peers): if Peers.update(peers):
print(Peers.peerArray)
# Redraw peer tree. # Redraw peer tree.
peers_tree.clear() peers_tree.clear()
var root = peers_tree.create_item()
for peer in Peers.peerArray: for peer in Peers.peerArray:
var child = peers_tree.create_item(peers_tree.get_root()) var child = peers_tree.create_item(root)
child.set_text(0, peer["user"] + " (" + peer["ip"] + ")") child.set_text(0, peer["user"] + " (" + peer["ip"] + ")")
else: else:
# Clear network information. # Clear network information.

View file

@ -5,6 +5,7 @@ var _server := UDPServer.new()
var _userInfo: Dictionary var _userInfo: Dictionary
var _pendingPackets: Array var _pendingPackets: Array
var _metadataTimer: Timer var _metadataTimer: Timer
var _lastPeers: Array
# This array is of dictionary elements that contain: # This array is of dictionary elements that contain:
# "online": Will be true except when used to update the array. # "online": Will be true except when used to update the array.
@ -35,7 +36,11 @@ func _process(_delta):
var raw = packet.get_string_from_utf8() var raw = packet.get_string_from_utf8()
var jsonIn = JSON.new() var jsonIn = JSON.new()
var error = jsonIn.parse(raw) var error = jsonIn.parse(raw)
# DEBUG
print("Raw: ", raw)
if error == OK: if error == OK:
# DEBUG
print("JSON In: ", jsonIn.data)
# Remote node wants our information. # Remote node wants our information.
if jsonIn.data["method"] == "query": if jsonIn.data["method"] == "query":
var payload = {} var payload = {}
@ -50,14 +55,19 @@ func _process(_delta):
peerArray[i]["refresh"] = true peerArray[i]["refresh"] = true
break break
client.close() client.close()
# Handle outgoing data.
for outbound in _pendingPackets:
var destination := PacketPeerUDP.new()
destination.connect_to_host(outbound["ip"], _server.get_local_port())
destination.put_packet(JSON.stringify(outbound["message"]).to_utf8_buffer())
destination.close()
_pendingPackets.clear()
# Handle outgoing data.
for outbound in _pendingPackets:
# DEBUG
print("Sending: ", outbound)
var destination := PacketPeerUDP.new()
destination.connect_to_host(outbound["ip"], _server.get_local_port())
var error = destination.put_packet(JSON.stringify(outbound["message"]).to_utf8_buffer())
if error:
print("Sending packet error: ", error)
destination.close()
_pendingPackets.clear()
func _ready(): func _ready():
clear() clear()
@ -73,7 +83,14 @@ func _sendTo(ipAddress, message):
pending["message"] = message pending["message"] = message
_pendingPackets.append(pending) _pendingPackets.append(pending)
func _sort_by_ip(a, b):
#***TODO*** Sort numerically against last three digits.
if a["ip"] < b["ip"]:
return true
return false
func clear(): func clear():
peerArray.clear() peerArray.clear()
@ -91,50 +108,44 @@ func stop_server():
func update(peers: Array): func update(peers: Array):
var changed = false
var found var found
var newArray: Array = []
# Mark everyone as offline. print("Real peers: ", peers.size())
for i in range(0, peerArray.size() - 1):
peerArray[i]["online"] = false
# Now go through everyone we know is connected and mark them online and update their type. peers.sort_custom(_sort_by_ip)
for peerEntry in peers:
# Did the peer list change?
if Util.deep_equal(peers, _lastPeers):
# Does an existing peer need redrawn?
found = false found = false
for i in range(0, peerArray.size() - 1): for peer in peerArray:
if peerEntry["ip"] == peerArray[i]["ip"]: if peer["refresh"] == true:
peerArray[i]["online"] = true peer["refresh"] = false
if peerArray[i]["type"] != peerEntry["type"]:
peerArray[i]["type"] = peerEntry["type"]
changed = true
found = true found = true
break return found
# Is this a new entry?
# Add everyone connected.
for peer in peers:
# Do we have this peer's information?
found = false
for oldPeer in peerArray:
if oldPeer["ip"] == peer["ip"]:
print("Updating ", peer)
found = true
peer = oldPeer
if !found: if !found:
var newPeer = peerEntry # We didn't have them. Add needed records.
# Add all dictionary elements except "ip" and "type". peer["user"] = ""
newPeer["user"] = "" peer["online"] = true
newPeer["online"] = true peer["refresh"] = false
newPeer["refresh"] = false # Add them to the new list.
peerArray.append(newPeer) newArray.append(peer)
changed = true
# Use new array.
peerArray = newArray.duplicate(true)
# Delete everyone that is offline. # Remember this pass.
found = true _lastPeers = peers.duplicate(true)
while found:
found = false
for i in range(0, peerArray.size() - 1):
if peerArray[i]["online"] == false:
found = true
peerArray.remove_at(i)
changed = true
break
# Do we need to redraw the list? return true
for i in range(0, peerArray.size() - 1):
if peerArray[i]["refresh"] == true:
peerArray[i]["refresh"] = false
changed = true
# Did the array change?
return changed

View file

@ -1,6 +1,46 @@
extends Node extends Node
func _deep_equal_one_way(a, b):
if typeof(a) == TYPE_DICTIONARY:
if not typeof(b) == TYPE_DICTIONARY:
return false
for key in a:
if not b.has(key):
return false
var val_a = a[key]
var val_b = b[key]
var entry_equal
if typeof(val_a) == TYPE_DICTIONARY or typeof(val_a) == TYPE_ARRAY:
entry_equal = _deep_equal_one_way(val_a, val_b)
else:
entry_equal = val_a == val_b
if not entry_equal:
return false
elif typeof(a) == TYPE_ARRAY:
if not typeof(b) == TYPE_ARRAY:
return false
if a.size() != b.size():
return false
for i in range(a.size()):
var val_a = a[i]
var val_b = b[i]
var entry_equal
if typeof(val_a) == TYPE_DICTIONARY or typeof(val_a) == TYPE_ARRAY:
entry_equal = _deep_equal_one_way(val_a, val_b)
else:
entry_equal = val_a == val_b
if not entry_equal:
return false
else:
return a == b
return true
func deep_equal(a, b):
return _deep_equal_one_way(a, b) && _deep_equal_one_way(b, a)
func delete_children(node): func delete_children(node):
for n in node.get_children(): for n in node.get_children():
node.remove_child(n) node.remove_child(n)