Skip to content

Commit 8d23970

Browse files
Remove internal "state" from UnityTransport
1 parent c97f1be commit 8d23970

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
lines changed

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ public enum ProtocolType
114114
RelayUnityTransport,
115115
}
116116

117-
private enum State
118-
{
119-
Disconnected,
120-
Listening,
121-
Connected,
122-
}
123-
124117
/// <summary>
125118
/// The default maximum (receive) packet queue size
126119
/// </summary>
@@ -454,7 +447,6 @@ public NetworkEndpoint GetLocalEndpoint()
454447

455448
private PacketLossCache m_PacketLossCache = new PacketLossCache();
456449

457-
private State m_State = State.Disconnected;
458450
private NetworkSettings m_NetworkSettings;
459451
private ulong m_ServerClientId;
460452

@@ -582,8 +574,7 @@ private bool ClientBindAndConnect()
582574
return false;
583575
}
584576

585-
var serverConnection = Connect(serverEndpoint);
586-
m_ServerClientId = ParseClientId(serverConnection);
577+
Connect(serverEndpoint);
587578

588579
return true;
589580
}
@@ -623,7 +614,6 @@ private bool ServerBindAndListen(NetworkEndpoint endPoint)
623614
return false;
624615
}
625616

626-
m_State = State.Listening;
627617
return true;
628618
}
629619

@@ -904,26 +894,20 @@ private bool ProcessEvent()
904894
default,
905895
m_RealTimeProvider.RealTimeSinceStartup);
906896

907-
m_State = State.Connected;
897+
m_ServerClientId = clientId;
908898
return true;
909899
}
910900
case TransportNetworkEvent.Type.Disconnect:
911901
{
912-
// Handle cases where we're a client receiving a Disconnect event. The
913-
// meaning of the event depends on our current state. If we were connected
914-
// then it means we got disconnected. If we were disconnected means that our
915-
// connection attempt has failed.
916-
if (m_State == State.Connected)
917-
{
918-
m_State = State.Disconnected;
919-
m_ServerClientId = default;
920-
}
921-
else if (m_State == State.Disconnected)
902+
// If we're a client and had not yet set the server client ID, it means
903+
// our connection to the server failed to be established. Any other case
904+
// means a clean disconnect that doesn't require logging.
905+
if (!m_Driver.Listening && m_ServerClientId == default)
922906
{
923907
Debug.LogError("Failed to connect to server.");
924-
m_ServerClientId = default;
925908
}
926909

910+
m_ServerClientId = default;
927911
m_ReliableReceiveQueues.Remove(clientId);
928912
ClearSendQueuesForClientId(clientId);
929913

@@ -1179,13 +1163,13 @@ private void FlushSendQueuesForClientId(ulong clientId)
11791163
/// </summary>
11801164
public override void DisconnectLocalClient()
11811165
{
1182-
if (m_State == State.Connected)
1166+
if (m_ServerClientId != default)
11831167
{
11841168
FlushSendQueuesForClientId(m_ServerClientId);
11851169

11861170
if (m_Driver.Disconnect(ParseClientId(m_ServerClientId)) == 0)
11871171
{
1188-
m_State = State.Disconnected;
1172+
m_ServerClientId = default;
11891173

11901174
m_ReliableReceiveQueues.Remove(m_ServerClientId);
11911175
ClearSendQueuesForClientId(m_ServerClientId);
@@ -1208,14 +1192,14 @@ public override void DisconnectLocalClient()
12081192
public override void DisconnectRemoteClient(ulong clientId)
12091193
{
12101194
#if DEBUG
1211-
if (m_State != State.Listening)
1195+
if (!m_Driver.IsCreated)
12121196
{
12131197
Debug.LogWarning($"{nameof(DisconnectRemoteClient)} should only be called on a listening server!");
12141198
return;
12151199
}
12161200
#endif
12171201

1218-
if (m_State == State.Listening)
1202+
if (m_Driver.IsCreated)
12191203
{
12201204
FlushSendQueuesForClientId(clientId);
12211205

@@ -1514,10 +1498,9 @@ public override void Shutdown()
15141498
DisposeInternals();
15151499

15161500
m_ReliableReceiveQueues.Clear();
1517-
m_State = State.Disconnected;
15181501

15191502
// We must reset this to zero because UTP actually re-uses clientIds if there is a clean disconnect
1520-
m_ServerClientId = 0;
1503+
m_ServerClientId = default;
15211504
}
15221505

15231506
private void ConfigureSimulator()

com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ public IEnumerator DisconnectOnReliableSendQueueOverflow()
311311

312312
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
313313

314+
var serverClientId = m_Client1.ServerClientId;
315+
314316
m_Server.Shutdown();
315317

316318
var numSends = (maxSendQueueSize / 1024);
@@ -322,7 +324,7 @@ public IEnumerator DisconnectOnReliableSendQueueOverflow()
322324
}
323325

324326
LogAssert.Expect(LogType.Error, "Couldn't add payload of size 1024 to reliable send queue. " +
325-
$"Closing connection {m_Client1.ServerClientId} as reliability guarantees can't be maintained.");
327+
$"Closing connection {serverClientId} as reliability guarantees can't be maintained.");
326328

327329
Assert.AreEqual(2, m_Client1Events.Count);
328330
Assert.AreEqual(NetworkEvent.Disconnect, m_Client1Events[1].Type);

0 commit comments

Comments
 (0)