Skip to content

Commit

Permalink
Merge pull request #59 from MarcinZboralski/master
Browse files Browse the repository at this point in the history
fix(FizzySteamworks): Implemented OnServerConnectedWithAddress
  • Loading branch information
Chykary authored Aug 9, 2024
2 parents 18e3c3b + e7f17ce commit e53b074
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
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

0 comments on commit e53b074

Please sign in to comment.