From e7f17ce4e86ccfb052ba3d9f64d3c921cba7b7e4 Mon Sep 17 00:00:00 2001 From: ReMake <32996203+MarcinZboralski@users.noreply.github.com> Date: Fri, 9 Aug 2024 01:12:46 +0200 Subject: [PATCH] fix(FizzySteamworks): Implemented OnServerConnectedWithAddress --- com.mirror.steamworks.net/LegacyServer.cs | 24 ++++++++++++----------- com.mirror.steamworks.net/NextServer.cs | 20 ++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/com.mirror.steamworks.net/LegacyServer.cs b/com.mirror.steamworks.net/LegacyServer.cs index 03495d4..485caf8 100644 --- a/com.mirror.steamworks.net/LegacyServer.cs +++ b/com.mirror.steamworks.net/LegacyServer.cs @@ -8,7 +8,7 @@ namespace Mirror.FizzySteam { public class LegacyServer : LegacyCommon, IServer { - private event Action OnConnected; + private event Action OnConnectedWithAddress; private event Action OnReceivedData; private event Action OnDisconnected; private event Action OnReceivedError; @@ -17,14 +17,16 @@ public class LegacyServer : LegacyCommon, IServer private int maxConnections; private int nextConnectionID; + private static LegacyServer server; + public static LegacyServer CreateServer(FizzySteamworks transport, int maxConnections) { - LegacyServer s = new LegacyServer(transport, maxConnections); - - s.OnConnected += (id) => transport.OnServerConnected.Invoke(id); - s.OnDisconnected += (id) => transport.OnServerDisconnected.Invoke(id); - s.OnReceivedData += (id, data, channel) => transport.OnServerDataReceived.Invoke(id, new ArraySegment(data), channel); - s.OnReceivedError += (id, error, reason) => transport.OnServerError.Invoke(id, error, reason); + server = new LegacyServer(transport, maxConnections); + + server.OnConnectedWithAddress += (id,addres) => transport.OnServerConnectedWithAddress.Invoke(id,addres); + server.OnDisconnected += (id) => transport.OnServerDisconnected.Invoke(id); + server.OnReceivedData += (id, data, channel) => transport.OnServerDataReceived.Invoke(id, new ArraySegment(data), channel); + server.OnReceivedError += (id, error, reason) => transport.OnServerError.Invoke(id, error, reason); try { @@ -39,7 +41,7 @@ public static LegacyServer CreateServer(FizzySteamworks transport, int maxConnec Debug.LogError("SteamWorks not initialized."); } - return s; + return server; } private LegacyServer(FizzySteamworks transport, int maxConnections) : base(transport) @@ -81,7 +83,7 @@ protected override void OnReceiveInternalData(InternalMessages type, CSteamID cl int connectionId = nextConnectionID++; steamToMirrorIds.Add(clientSteamID, connectionId); - OnConnected.Invoke(connectionId); + OnConnectedWithAddress.Invoke(connectionId,server.ServerGetClientAddress(connectionId)); Debug.Log($"Client with SteamID {clientSteamID} connected. Assigning connection id {connectionId}"); break; case InternalMessages.DISCONNECT: @@ -115,11 +117,11 @@ protected override void OnReceiveData(byte[] data, CSteamID clientSteamID, int c OnReceivedError.Invoke(-1, TransportError.DnsResolve, "ERROR Unknown SteamID"); } } - catch(Exception ex) + catch (Exception ex) { Debug.LogError($"Error while recive data {ex.Message}"); Shutdown(); - } + } } public void Disconnect(int connectionId) diff --git a/com.mirror.steamworks.net/NextServer.cs b/com.mirror.steamworks.net/NextServer.cs index e81bfb9..82ea44e 100644 --- a/com.mirror.steamworks.net/NextServer.cs +++ b/com.mirror.steamworks.net/NextServer.cs @@ -8,7 +8,7 @@ namespace Mirror.FizzySteam { public class NextServer : NextCommon, IServer { - private event Action OnConnected; + private event Action OnConnectedWithAddress; private event Action OnReceivedData; private event Action OnDisconnected; private event Action OnReceivedError; @@ -21,6 +21,8 @@ public class NextServer : NextCommon, IServer private HSteamListenSocket listenSocket; private Callback c_onConnectionChange = null; + + private static NextServer server; private NextServer(int maxConnections) { this.maxConnections = maxConnections; @@ -32,12 +34,12 @@ private NextServer(int maxConnections) public static NextServer CreateServer(FizzySteamworks transport, int maxConnections) { - NextServer s = new NextServer(maxConnections); + server = new NextServer(maxConnections); - s.OnConnected += (id) => transport.OnServerConnected.Invoke(id); - s.OnDisconnected += (id) => transport.OnServerDisconnected.Invoke(id); - s.OnReceivedData += (id, data, ch) => transport.OnServerDataReceived.Invoke(id, new ArraySegment(data), ch); - s.OnReceivedError += (id, error, reason) => transport.OnServerError.Invoke(id, error, reason); + server.OnConnectedWithAddress += (id,addres) => transport.OnServerConnectedWithAddress.Invoke(id,addres); + server.OnDisconnected += (id) => transport.OnServerDisconnected.Invoke(id); + server.OnReceivedData += (id, data, ch) => transport.OnServerDataReceived.Invoke(id, new ArraySegment(data), ch); + server.OnReceivedError += (id, error, reason) => transport.OnServerError.Invoke(id, error, reason); try { @@ -52,9 +54,9 @@ public static NextServer CreateServer(FizzySteamworks transport, int maxConnecti Debug.LogException(ex); } - s.Host(); + server.Host(); - return s; + return server; } private void Host() @@ -103,7 +105,7 @@ private void OnConnectionStatusChanged(SteamNetConnectionStatusChangedCallback_t int connectionId = nextConnectionID++; connToMirrorID.Add(param.m_hConn, connectionId); steamIDToMirrorID.Add(param.m_info.m_identityRemote.GetSteamID(), connectionId); - OnConnected?.Invoke(connectionId); + OnConnectedWithAddress?.Invoke(connectionId,server.ServerGetClientAddress(connectionId)); Debug.Log($"Client with SteamID {clientSteamID} connected. Assigning connection id {connectionId}"); } else if (param.m_info.m_eState == ESteamNetworkingConnectionState.k_ESteamNetworkingConnectionState_ClosedByPeer || param.m_info.m_eState == ESteamNetworkingConnectionState.k_ESteamNetworkingConnectionState_ProblemDetectedLocally)