ENet mesh theoretically working.

This commit is contained in:
Scott Duensing 2023-09-11 17:55:22 -05:00
parent ac145b6d6b
commit 6a6169aef5
2 changed files with 47 additions and 25 deletions

View file

@ -87,11 +87,7 @@ func _go_online():
Dialog.alert("Error Starting Network", message)
else:
# Successful connection.
var userInfo = {}
userInfo["ip"] = net.get_ip()
userInfo["user"] = user_name_line_edit.text
userInfo["uuid"] = uuid
Peers.start_server(net.get_ip(), PEER_BASE_PORT, userInfo)
Peers.start_server(net.get_ip(), PEER_BASE_PORT, user_name_line_edit.text, uuid)
func _notification(what):

View file

@ -1,13 +1,13 @@
extends Node
const CHANNELS_MAX = 256
var _enet := ENetMultiplayerPeer.new()
var _basePort: int
var _serverRunning: bool
# This array is of dictionary elements that contain:
# "enet": ENet listening connnection.
# "id": Unique ID of this peer (1-254) based on IP.
# "ip": IP address of this peer.
# "port": Listen port of this peer.
# "user": User name on this IP.
@ -15,12 +15,13 @@ var _basePort: int
var _userInfo: Dictionary
# This array is of dictionary elements that contain:
# "connection": ENet connection to this host.
# "online": Will be true except when used to update the array.
# "port": Listen port of this peer.
# "user": User name on this IP.
# "uuid": UUID for this peer.
# "refresh": Do we need to redraw this entry?
# "enet": ENet connection to this host.
# "id": Unique ID of this peer (1-254) based on IP.
# "online": Will be true except when used to update the array.
# "port": Listen port of this peer.
# "user": User name on this IP.
# "uuid": UUID for this peer.
# "refresh": Do we need to redraw this entry?
#
# In addition, it contains all the properties of the C++ peer list:
# "type": Either "pending" or "known", which is confusing.
@ -37,10 +38,25 @@ func _peer_disconnected(id):
func _process(_delta):
pass
if _serverRunning:
#_process_enet(_userInfo["enet"], _userInfo["id"])
for peer in peerArray:
_process_enet(peer["enet"], peer["id"])
_enet.poll()
func _process_enet(host: ENetConnection, id: int):
var ret = host.service()
if ret[0] == host.EVENT_CONNECT:
print("Adding host %d" % id)
_enet.add_mesh_peer(id, host)
elif ret[0] != host.EVENT_NONE:
print("Mesh peer error %d" % id)
#***TODO*** Attempt disconnect/reconnect
func _ready():
_serverRunning = false
clear()
multiplayer.peer_connected.connect(self._peer_connected)
multiplayer.peer_disconnected.connect(self._peer_disconnected)
@ -57,17 +73,25 @@ func clear():
peerArray.clear()
func start_server(address, port, userInfo):
func start_server(address, port, user, uuid):
_basePort = port
_userInfo = userInfo
_enet.create_mesh(Util.ip_string_to_int(_userInfo["ip"]))
_userInfo["ip"] = address
_userInfo["user"] = user
_userInfo["uuid"] = uuid
_userInfo["id"] = _userInfo["ip"].get_slice(".", 3).to_int() # Use last octet for our ID.
_userInfo["port"] = _basePort + _userInfo["id"]
# Create mesh network.
_enet.create_mesh(_userInfo["id"])
multiplayer.set_multiplayer_peer(_enet)
_userInfo["port"] = _basePort + _userInfo["ip"].get_slice(".", 3).to_int()
var conn = ENetConnection.new()
conn.create_host_bound(_userInfo["ip"], _userInfo["port"], 256, CHANNELS_MAX)
# Set up our listening port.
_userInfo["enet"] = ENetConnection.new()
_userInfo["enet"].create_host_bound(_userInfo["ip"], _userInfo["port"], 256, 0)
_serverRunning = true
func stop_server():
# Tear down network.
_serverRunning = false
multiplayer.set_multiplayer_peer(null)
@ -101,10 +125,12 @@ func update(peersFromCPP: Array):
peerCPP["refresh"] = false
changed = true
# Also create the ENet connection to them.
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()
var conn = ENetConnection.new()
conn.create_host(1)
conn.connect_to_host(peerCPP["ip"], peerCPP["port"], CHANNELS_MAX)
peerCPP["enet"] = ENetConnection.new()
peerCPP["enet"].create_host(1)
peerCPP["enet"].connect_to_host(peerCPP["ip"], peerCPP["port"], 0)
print("Connecting to ", peerCPP["ip"], ":", peerCPP["port"])
# Sometimes the CPP code will return duplicates. Check.
# Also check if we need to redraw anyone.
found = false