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.
var peers = net.get_peers()
if Peers.update(peers):
print(Peers.peerArray)
# Redraw peer tree.
peers_tree.clear()
var root = peers_tree.create_item()
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"] + ")")
else:
# Clear network information.

View file

@ -5,6 +5,7 @@ var _server := UDPServer.new()
var _userInfo: Dictionary
var _pendingPackets: Array
var _metadataTimer: Timer
var _lastPeers: Array
# This array is of dictionary elements that contain:
# "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 jsonIn = JSON.new()
var error = jsonIn.parse(raw)
# DEBUG
print("Raw: ", raw)
if error == OK:
# DEBUG
print("JSON In: ", jsonIn.data)
# Remote node wants our information.
if jsonIn.data["method"] == "query":
var payload = {}
@ -50,14 +55,19 @@ func _process(_delta):
peerArray[i]["refresh"] = true
break
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():
clear()
@ -73,7 +83,14 @@ func _sendTo(ipAddress, message):
pending["message"] = message
_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():
peerArray.clear()
@ -91,50 +108,44 @@ func stop_server():
func update(peers: Array):
var changed = false
var found
var newArray: Array = []
# Mark everyone as offline.
for i in range(0, peerArray.size() - 1):
peerArray[i]["online"] = false
print("Real peers: ", peers.size())
# Now go through everyone we know is connected and mark them online and update their type.
for peerEntry in peers:
peers.sort_custom(_sort_by_ip)
# Did the peer list change?
if Util.deep_equal(peers, _lastPeers):
# Does an existing peer need redrawn?
found = false
for i in range(0, peerArray.size() - 1):
if peerEntry["ip"] == peerArray[i]["ip"]:
peerArray[i]["online"] = true
if peerArray[i]["type"] != peerEntry["type"]:
peerArray[i]["type"] = peerEntry["type"]
changed = true
for peer in peerArray:
if peer["refresh"] == true:
peer["refresh"] = false
found = true
break
# Is this a new entry?
return found
# 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:
var newPeer = peerEntry
# Add all dictionary elements except "ip" and "type".
newPeer["user"] = ""
newPeer["online"] = true
newPeer["refresh"] = false
peerArray.append(newPeer)
changed = true
# We didn't have them. Add needed records.
peer["user"] = ""
peer["online"] = true
peer["refresh"] = false
# Add them to the new list.
newArray.append(peer)
# Use new array.
peerArray = newArray.duplicate(true)
# Delete everyone that is offline.
found = 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
# Remember this pass.
_lastPeers = peers.duplicate(true)
# Do we need to redraw the list?
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
return true

View file

@ -1,6 +1,46 @@
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):
for n in node.get_children():
node.remove_child(n)