Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(FizzySteamworks): Implemented OnServerConnectedWithAddress #59

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions com.mirror.steamworks.net/LegacyServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Mirror.FizzySteam
{
public class LegacyServer : LegacyCommon, IServer
{
private event Action<int> OnConnected;
private event Action<int,String> OnConnectedWithAddress;
private event Action<int, byte[], int> OnReceivedData;
private event Action<int> OnDisconnected;
private event Action<int, TransportError, string> OnReceivedError;
Expand All @@ -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<byte>(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<byte>(data), channel);
server.OnReceivedError += (id, error, reason) => transport.OnServerError.Invoke(id, error, reason);

try
{
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 11 additions & 9 deletions com.mirror.steamworks.net/NextServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Mirror.FizzySteam
{
public class NextServer : NextCommon, IServer
{
private event Action<int> OnConnected;
private event Action<int,string> OnConnectedWithAddress;
private event Action<int, byte[], int> OnReceivedData;
private event Action<int> OnDisconnected;
private event Action<int, TransportError, string> OnReceivedError;
Expand All @@ -21,6 +21,8 @@ public class NextServer : NextCommon, IServer
private HSteamListenSocket listenSocket;

private Callback<SteamNetConnectionStatusChangedCallback_t> c_onConnectionChange = null;

private static NextServer server;
private NextServer(int maxConnections)
{
this.maxConnections = maxConnections;
Expand All @@ -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<byte>(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<byte>(data), ch);
server.OnReceivedError += (id, error, reason) => transport.OnServerError.Invoke(id, error, reason);

try
{
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down