Skip to content

Commit

Permalink
Improved documentation. Added support for domains as connection address.
Browse files Browse the repository at this point in the history
  • Loading branch information
ooonush committed Nov 7, 2024
1 parent 096c8d8 commit b3f6022
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions Assets/FishNet/Plugins/FishyUnityTransport/FishyUnityTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using UnityEngine;
using TransportNetworkEvent = Unity.Networking.Transport.NetworkEvent;
using Unity.Burst;
Expand Down Expand Up @@ -141,11 +142,9 @@ public INetworkStreamDriverConstructor DriverConstructor
[SerializeField]
private ProtocolType m_ProtocolType;

#if UTP_TRANSPORT_2_0_ABOVE
[Tooltip("Per default the client/server will communicate over UDP. Set to true to communicate with WebSocket.")]
[SerializeField]
#if !UTP_TRANSPORT_2_0_ABOVE
[HideInInspector]
#endif
private bool m_UseWebSockets = false;

public bool UseWebSockets
Expand All @@ -159,15 +158,13 @@ public bool UseWebSockets
/// </summary>
[Tooltip("Per default the client/server communication will not be encrypted. Select true to enable DTLS for UDP and TLS for Websocket.")]
[SerializeField]
#if !UTP_TRANSPORT_2_0_ABOVE
[HideInInspector]
#endif
private bool m_UseEncryption = false;
public bool UseEncryption
{
get => m_UseEncryption;
set => m_UseEncryption = value;
}
#endif

[Tooltip("The maximum amount of packets that can be in the internal send/receive queues. Basically this is how many packets can be sent/received in a single update/frame.")]
[SerializeField]
Expand All @@ -181,11 +178,12 @@ public int MaxPacketQueueSize
set => m_MaxPacketQueueSize = value;
}

[Tooltip("The maximum size of an unreliable payload that can be handled by the transport.")]
[Tooltip("The maximum size of an unreliable payload that can be handled by the transport. The memory for MaxPayloadSize is allocated once per connection and is released when the connection is closed.")]
[SerializeField]
private int m_MaxPayloadSize = InitialMaxPayloadSize;

/// <summary>The maximum size of an unreliable payload that can be handled by the transport.</summary>
/// <remarks>The memory for MaxPayloadSize is allocated once per connection and is released when the connection is closed.</remarks>
public int MaxPayloadSize
{
get => m_MaxPayloadSize;
Expand Down Expand Up @@ -288,19 +286,25 @@ public struct ConnectionAddressData
[SerializeField]
public string ServerListenAddress;

private static NetworkEndpoint ParseNetworkEndpoint(string ip, ushort port, bool silent = false)
private static NetworkEndpoint ParseNetworkEndpoint(string address, ushort port, bool silent = false)
{
NetworkEndpoint endpoint = default;

if (!NetworkEndpoint.TryParse(ip, port, out endpoint, NetworkFamily.Ipv4) &&
!NetworkEndpoint.TryParse(ip, port, out endpoint, NetworkFamily.Ipv6))
if (!NetworkEndpoint.TryParse(address, port, out endpoint, NetworkFamily.Ipv4) &&
!NetworkEndpoint.TryParse(address, port, out endpoint, NetworkFamily.Ipv6))
{
if (!silent)
IPAddress[] ipList = Dns.GetHostAddresses(address);
if (ipList.Length > 0)
{
Debug.LogError($"Invalid network endpoint: {ip}:{port}.");
endpoint = ParseNetworkEndpoint(ipList[0].ToString(), port, true);
}
}

if (endpoint == default && !silent)
{
Debug.LogError($"Invalid network endpoint: {address}:{port}.");
}

return endpoint;
}

Expand Down Expand Up @@ -381,6 +385,7 @@ public struct SimulatorParameters
/// - packet jitter (variances in latency, see: https://en.wikipedia.org/wiki/Jitter)
/// - packet drop rate (packet loss)
/// </summary>

#if UTP_TRANSPORT_2_0_ABOVE
[HideInInspector]
[Obsolete("DebugSimulator is no longer supported and has no effect. Use Network Simulator from the Multiplayer Tools package.", false)]
Expand All @@ -400,6 +405,9 @@ public struct SimulatorParameters
private NetworkPipeline m_UnreliableSequencedFragmentedPipeline;
private NetworkPipeline m_ReliableSequencedPipeline;

/// <summary>
/// The current ProtocolType used by the transport
/// </summary>
public ProtocolType Protocol => m_ProtocolType;

private RelayServerData m_RelayServerData;
Expand Down Expand Up @@ -635,9 +643,11 @@ public void SetConnectionData(NetworkEndpoint endPoint, NetworkEndpoint listenEn
/// <param name="packetDelay">Packet delay in milliseconds.</param>
/// <param name="packetJitter">Packet jitter in milliseconds.</param>
/// <param name="dropRate">Packet drop percentage.</param>

#if UTP_TRANSPORT_2_0_ABOVE
[Obsolete("SetDebugSimulatorParameters is no longer supported and has no effect. Use Network Simulator from the Multiplayer Tools package.", false)]
#endif

public void SetDebugSimulatorParameters(int packetDelay, int packetJitter, int dropRate)
{
if (m_Driver.IsCreated)
Expand Down Expand Up @@ -753,6 +763,7 @@ private bool AcceptConnection()

HandleRemoteConnectionState(RemoteConnectionState.Started, ParseClientId(connection));
return true;

}

private void ReceiveMessages(ulong clientId, NetworkPipeline pipeline, DataStreamReader dataReader)
Expand Down Expand Up @@ -823,6 +834,10 @@ private bool ProcessEvent()
}
else
{
if (m_ClientState == LocalConnectionState.Starting)
{
Debug.LogError("Failed to connect to server.");
}
SetClientConnectionState(LocalConnectionState.Stopping);
DisposeInternals();
SetClientConnectionState(LocalConnectionState.Stopped);
Expand Down Expand Up @@ -879,6 +894,11 @@ private void IterateIncoming()
}
}

private void OnDestroy()
{
Shutdown();
}

private int ExtractRtt(NetworkConnection networkConnection)
{
if (m_Driver.GetConnectionState(networkConnection) != NetworkConnection.State.Connected)
Expand Down Expand Up @@ -1157,7 +1177,6 @@ private bool StartClient()
}
SetClientConnectionState(LocalConnectionState.Stopped);
}

return succeeded;
}

Expand Down Expand Up @@ -1332,6 +1351,7 @@ public void CreateDriver(FishyUnityTransport transport, out NetworkDriver driver
ConfigureSimulatorForUtp1();
#endif
bool asServer = m_ServerState == LocalConnectionState.Starting;

m_NetworkSettings.WithNetworkConfigParameters(
maxConnectAttempts: transport.m_MaxConnectAttempts,
connectTimeoutMS: transport.m_ConnectTimeoutMS,
Expand Down Expand Up @@ -1501,7 +1521,6 @@ private void SetupPipelinesForUtp2(NetworkDriver driver,
);
}
#endif

// -------------- Utility Types -------------------------------------------------------------------------------


Expand Down Expand Up @@ -1547,11 +1566,6 @@ public override int GetHashCode()
public override event Action<ServerConnectionStateArgs> OnServerConnectionState;
public override event Action<RemoteConnectionStateArgs> OnRemoteConnectionState;

private void OnDestroy()
{
Shutdown();
}

public override string GetConnectionAddress(int connectionId)
{
bool isServer = m_ServerState == LocalConnectionState.Started;
Expand Down Expand Up @@ -1584,7 +1598,7 @@ public override string GetConnectionAddress(int connectionId)
}
return string.Empty;

NetworkEndPoint GetLocalEndPoint()
NetworkEndpoint GetLocalEndPoint()
{
#if UTP_TRANSPORT_2_0_ABOVE
return m_Driver.GetLocalEndpoint();
Expand Down

0 comments on commit b3f6022

Please sign in to comment.