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) Dialog.alert("Error Starting Network", message)
else: else:
# Successful connection. # Successful connection.
var userInfo = {} Peers.start_server(net.get_ip(), PEER_BASE_PORT, user_name_line_edit.text, uuid)
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)
func _notification(what): func _notification(what):

View file

@ -1,13 +1,13 @@
extends Node extends Node
const CHANNELS_MAX = 256
var _enet := ENetMultiplayerPeer.new() var _enet := ENetMultiplayerPeer.new()
var _basePort: int var _basePort: int
var _serverRunning: bool
# This array is of dictionary elements that contain: # 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. # "ip": IP address of this peer.
# "port": Listen port of this peer. # "port": Listen port of this peer.
# "user": User name on this IP. # "user": User name on this IP.
@ -15,12 +15,13 @@ var _basePort: int
var _userInfo: Dictionary var _userInfo: Dictionary
# This array is of dictionary elements that contain: # This array is of dictionary elements that contain:
# "connection": ENet connection to this host. # "enet": ENet connection to this host.
# "online": Will be true except when used to update the array. # "id": Unique ID of this peer (1-254) based on IP.
# "port": Listen port of this peer. # "online": Will be true except when used to update the array.
# "user": User name on this IP. # "port": Listen port of this peer.
# "uuid": UUID for this peer. # "user": User name on this IP.
# "refresh": Do we need to redraw this entry? # "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: # In addition, it contains all the properties of the C++ peer list:
# "type": Either "pending" or "known", which is confusing. # "type": Either "pending" or "known", which is confusing.
@ -37,10 +38,25 @@ func _peer_disconnected(id):
func _process(_delta): 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(): func _ready():
_serverRunning = false
clear() clear()
multiplayer.peer_connected.connect(self._peer_connected) multiplayer.peer_connected.connect(self._peer_connected)
multiplayer.peer_disconnected.connect(self._peer_disconnected) multiplayer.peer_disconnected.connect(self._peer_disconnected)
@ -57,17 +73,25 @@ func clear():
peerArray.clear() peerArray.clear()
func start_server(address, port, userInfo): func start_server(address, port, user, uuid):
_basePort = port _basePort = port
_userInfo = userInfo _userInfo["ip"] = address
_enet.create_mesh(Util.ip_string_to_int(_userInfo["ip"])) _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) multiplayer.set_multiplayer_peer(_enet)
_userInfo["port"] = _basePort + _userInfo["ip"].get_slice(".", 3).to_int() # Set up our listening port.
var conn = ENetConnection.new() _userInfo["enet"] = ENetConnection.new()
conn.create_host_bound(_userInfo["ip"], _userInfo["port"], 256, CHANNELS_MAX) _userInfo["enet"].create_host_bound(_userInfo["ip"], _userInfo["port"], 256, 0)
_serverRunning = true
func stop_server(): func stop_server():
# Tear down network.
_serverRunning = false
multiplayer.set_multiplayer_peer(null) multiplayer.set_multiplayer_peer(null)
@ -101,10 +125,12 @@ func update(peersFromCPP: Array):
peerCPP["refresh"] = false peerCPP["refresh"] = false
changed = true changed = true
# Also create the ENet connection to them. # 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() peerCPP["port"] = _basePort + peerCPP["ip"].get_slice(".", 3).to_int()
var conn = ENetConnection.new() peerCPP["enet"] = ENetConnection.new()
conn.create_host(1) peerCPP["enet"].create_host(1)
conn.connect_to_host(peerCPP["ip"], peerCPP["port"], CHANNELS_MAX) 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. # 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