Before switch from enet to TCP.
This commit is contained in:
parent
b72ddcbed1
commit
80d2cf654b
1 changed files with 40 additions and 7 deletions
|
@ -46,7 +46,19 @@ var peerArray: Array
|
||||||
|
|
||||||
func _process(_delta):
|
func _process(_delta):
|
||||||
if _serverRunning:
|
if _serverRunning:
|
||||||
|
# Handle current connections.
|
||||||
_process_enet(_userInfo["enet"], _userInfo["id"])
|
_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):
|
func _process_enet(host: ENetConnection, id: int):
|
||||||
|
@ -89,6 +101,27 @@ func clear():
|
||||||
peerArray.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():
|
func is_running():
|
||||||
return _serverRunning
|
return _serverRunning
|
||||||
|
|
||||||
|
@ -135,6 +168,8 @@ func update(peersFromCPP: Array):
|
||||||
|
|
||||||
# Add everyone connected.
|
# Add everyone connected.
|
||||||
for peerCPP in peersFromCPP:
|
for peerCPP in peersFromCPP:
|
||||||
|
if peerCPP["ip4addr"] == "":
|
||||||
|
continue
|
||||||
# Get an IP without netmask.
|
# Get an IP without netmask.
|
||||||
peerCPP["ip"] = peerCPP["ip4addr"].get_slice("/", 0)
|
peerCPP["ip"] = peerCPP["ip4addr"].get_slice("/", 0)
|
||||||
# Do we have this peer's information from before?
|
# Do we have this peer's information from before?
|
||||||
|
@ -144,8 +179,7 @@ func update(peersFromCPP: Array):
|
||||||
found = true
|
found = true
|
||||||
peerCPP = oldPeer
|
peerCPP = oldPeer
|
||||||
# Update peer statistics. ***TODO*** These do not set 'changed' and are not refreshed.
|
# Update peer statistics. ***TODO*** These do not set 'changed' and are not refreshed.
|
||||||
if typeof(peerCPP["enet"]) == TYPE_OBJECT:
|
if is_peer_connected(peerCPP):
|
||||||
if peerCPP["enet"].get_state() == ENetPacketPeer.STATE_CONNECTED:
|
|
||||||
peerCPP["ping"] = peerCPP["enet"].get_statistic(ENetPacketPeer.PEER_ROUND_TRIP_TIME)
|
peerCPP["ping"] = peerCPP["enet"].get_statistic(ENetPacketPeer.PEER_ROUND_TRIP_TIME)
|
||||||
if !found:
|
if !found:
|
||||||
# We didn't have them. Add needed records.
|
# We didn't have them. Add needed records.
|
||||||
|
@ -155,11 +189,10 @@ func update(peersFromCPP: Array):
|
||||||
peerCPP["refresh"] = false
|
peerCPP["refresh"] = false
|
||||||
peerCPP["ping"] = 0.0
|
peerCPP["ping"] = 0.0
|
||||||
changed = true
|
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["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["port"] = _basePort + peerCPP["ip"].get_slice(".", 3).to_int()
|
||||||
peerCPP["enet"] = _userInfo["enet"].connect_to_host(peerCPP["ip"], peerCPP["port"], 0)
|
peerCPP["enet"] = null
|
||||||
print("Connecting to ", peerCPP["ip"], ":", peerCPP["port"])
|
|
||||||
# Sometimes the CPP code will return duplicates. Check.
|
# Sometimes the CPP code will return duplicates. Check.
|
||||||
# Also check if we need to redraw anyone.
|
# Also check if we need to redraw anyone.
|
||||||
found = false
|
found = false
|
||||||
|
@ -176,6 +209,6 @@ func update(peersFromCPP: Array):
|
||||||
# Sort new array.
|
# Sort new array.
|
||||||
peerArray.sort_custom(_sort_by_ip)
|
peerArray.sort_custom(_sort_by_ip)
|
||||||
|
|
||||||
#print("Peers After: ", peerArray)
|
print("Peers After: ", peerArray)
|
||||||
|
|
||||||
return changed
|
return changed
|
||||||
|
|
Loading…
Add table
Reference in a new issue