Skip to content

Commit d0b79ee

Browse files
committed
fix(transport): Fixed UNET transport when using non relay
1 parent a6806a7 commit d0b79ee

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

MLAPI/Data/Transports/UNET/RelayTransport.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,12 @@ public static bool QueueMessageForSending(int hostId, int connectionId, int chan
199199
return NetworkTransport.QueueMessageForSending(hostId, relayConnectionId, channelId, buffer, size, out error);
200200
}
201201

202-
public static bool SendQueuedMessages(int hostId, int connectionId, out byte error) => NetworkTransport.SendQueuedMessages(hostId, relayConnectionId, out error);
202+
public static bool SendQueuedMessages(int hostId, int connectionId, out byte error)
203+
{
204+
if (!Enabled) return NetworkTransport.SendQueuedMessages(hostId, connectionId, out error);
205+
206+
return NetworkTransport.SendQueuedMessages(hostId, relayConnectionId, out error);
207+
}
203208

204209
public static NetworkEventType ReceiveFromHost(int hostId, out int connectionId, out int channelId, byte[] buffer, int bufferSize, out int receivedSize, out byte error)
205210
{

MLAPI/Data/Transports/UNET/UnetTransport.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using MLAPI.Logging;
5+
using UnityEngine;
56
using UnityEngine.Networking;
67

78
namespace MLAPI.Transports.UNET
@@ -38,9 +39,7 @@ public class UnetTransport : Transport
3839
public override ulong ServerClientId => GetMLAPIClientId(0, 0, true);
3940

4041
public override void Send(ulong clientId, ArraySegment<byte> data, string channelName, bool skipQueue)
41-
{
42-
UpdateRelay();
43-
42+
{
4443
GetUnetConnectionDetails(clientId, out byte hostId, out ushort connectionId);
4544

4645
int channelId = channelNameToId[channelName];
@@ -87,19 +86,15 @@ public override void Send(ulong clientId, ArraySegment<byte> data, string channe
8786

8887
public override void FlushSendQueue(ulong clientId)
8988
{
90-
UpdateRelay();
91-
9289
GetUnetConnectionDetails(clientId, out byte hostId, out ushort connectionId);
9390

9491
RelayTransport.SendQueuedMessages(hostId, connectionId, out byte error);
9592
}
9693

9794
public override NetEventType PollEvent(out ulong clientId, out string channelName, out ArraySegment<byte> payload)
98-
{
99-
UpdateRelay();
100-
95+
{
10196
NetworkEventType eventType = RelayTransport.Receive(out int hostId, out int connectionId, out int channelId, messageBuffer, messageBuffer.Length, out int receivedSize, out byte error);
102-
97+
10398
clientId = GetMLAPIClientId((byte) hostId, (ushort) connectionId, false);
10499

105100
NetworkError networkError = (NetworkError) error;
@@ -128,7 +123,6 @@ public override NetEventType PollEvent(out ulong clientId, out string channelNam
128123

129124
channelName = channelIdToName[channelId];
130125

131-
132126
if (networkError == NetworkError.Timeout)
133127
{
134128
// In UNET. Timeouts are not disconnects. We have to translate that here.
@@ -154,18 +148,14 @@ public override NetEventType PollEvent(out ulong clientId, out string channelNam
154148
}
155149

156150
public override void StartClient()
157-
{
158-
UpdateRelay();
159-
151+
{
160152
serverHostId = RelayTransport.AddHost(new HostTopology(GetConfig(), 1), false);
161153

162154
serverConnectionId = RelayTransport.Connect(serverHostId, ConnectAddress, ConnectPort, 0, out byte error);
163155
}
164156

165157
public override void StartServer()
166158
{
167-
UpdateRelay();
168-
169159
HostTopology topology = new HostTopology(GetConfig(), MaxConnections);
170160

171161
if (SupportWebsocket)
@@ -186,17 +176,13 @@ public override void StartServer()
186176

187177
public override void DisconnectRemoteClient(ulong clientId)
188178
{
189-
UpdateRelay();
190-
191179
GetUnetConnectionDetails(clientId, out byte hostId, out ushort connectionId);
192180

193181
RelayTransport.Disconnect((int) hostId, (int) connectionId, out byte error);
194182
}
195183

196184
public override void DisconnectLocalClient()
197185
{
198-
UpdateRelay();
199-
200186
RelayTransport.Disconnect(serverHostId, serverConnectionId, out byte error);
201187
}
202188

@@ -221,6 +207,8 @@ public override void Shutdown()
221207

222208
public override void Init()
223209
{
210+
UpdateRelay();
211+
224212
messageBuffer = new byte[MessageBufferSize];
225213

226214
NetworkTransport.Init();

0 commit comments

Comments
 (0)