diff --git a/hamncheese/Scripts/peers.gd b/hamncheese/Scripts/peers.gd index eb1e4bd..db5b2aa 100644 --- a/hamncheese/Scripts/peers.gd +++ b/hamncheese/Scripts/peers.gd @@ -46,7 +46,19 @@ var peerArray: Array func _process(_delta): if _serverRunning: + # Handle current connections. _process_enet(_userInfo["enet"], _userInfo["id"]) + # Manage peer connections. + for peer in peerArray: + # Do we need new connections? It looks funny that we check for both connected and + # disconnected but there are other states the peer could be in. + if !is_peer_valid(peer): + print("Connecting to ", peer["ip"], ":", peer["port"]) + peer["enet"] = _userInfo["enet"].connect_to_host(peer["ip"], peer["port"], 0) + else: + # Do we need to remove this one? + if is_peer_disconnected(peer): + peer["enet"] = null func _process_enet(host: ENetConnection, id: int): @@ -89,6 +101,27 @@ func clear(): peerArray.clear() +func is_peer_connected(peer): + if is_peer_valid(peer): + if peer["enet"].get_state() == ENetPacketPeer.STATE_CONNECTED: + return true + return false + + +func is_peer_disconnected(peer): + if is_peer_valid(peer): + if peer["enet"].get_state() == ENetPacketPeer.STATE_DISCONNECTED: + return true + return false + + +func is_peer_valid(peer): + if typeof(peer["enet"]) == TYPE_OBJECT: + if peer["enet"] != null: + return true + return false + + func is_running(): return _serverRunning @@ -135,6 +168,8 @@ func update(peersFromCPP: Array): # Add everyone connected. for peerCPP in peersFromCPP: + if peerCPP["ip4addr"] == "": + continue # Get an IP without netmask. peerCPP["ip"] = peerCPP["ip4addr"].get_slice("/", 0) # Do we have this peer's information from before? @@ -144,9 +179,8 @@ func update(peersFromCPP: Array): found = true peerCPP = oldPeer # Update peer statistics. ***TODO*** These do not set 'changed' and are not refreshed. - if typeof(peerCPP["enet"]) == TYPE_OBJECT: - if peerCPP["enet"].get_state() == ENetPacketPeer.STATE_CONNECTED: - peerCPP["ping"] = peerCPP["enet"].get_statistic(ENetPacketPeer.PEER_ROUND_TRIP_TIME) + if is_peer_connected(peerCPP): + peerCPP["ping"] = peerCPP["enet"].get_statistic(ENetPacketPeer.PEER_ROUND_TRIP_TIME) if !found: # We didn't have them. Add needed records. peerCPP["user"] = "" @@ -155,11 +189,10 @@ func update(peersFromCPP: Array): peerCPP["refresh"] = false peerCPP["ping"] = 0.0 changed = true - # Also create the ENet connection to them. + # Also create the ENet connection data. peerCPP["id"] = peerCPP["ip"].get_slice(".", 3).to_int() # Use last octet for our ID. peerCPP["port"] = _basePort + peerCPP["ip"].get_slice(".", 3).to_int() - peerCPP["enet"] = _userInfo["enet"].connect_to_host(peerCPP["ip"], peerCPP["port"], 0) - print("Connecting to ", peerCPP["ip"], ":", peerCPP["port"]) + peerCPP["enet"] = null # Sometimes the CPP code will return duplicates. Check. # Also check if we need to redraw anyone. found = false @@ -176,6 +209,6 @@ func update(peersFromCPP: Array): # Sort new array. peerArray.sort_custom(_sort_by_ip) - #print("Peers After: ", peerArray) + print("Peers After: ", peerArray) return changed