Skip to content
This repository was archived by the owner on Aug 8, 2018. It is now read-only.

Commit fe81de4

Browse files
authored
Update RelayTransport.cs
1 parent b649c44 commit fe81de4

File tree

1 file changed

+30
-54
lines changed

1 file changed

+30
-54
lines changed

RelayTransport.cs

Lines changed: 30 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public static int Connect(int hostId, string serverAddress, int serverPort, int
3737
RelayTransport.port = (ushort)serverPort;
3838
relayConnectionId = NetworkTransport.Connect(hostId, RelayAddress, RelayPort, exceptionConnectionId, out error); // Requests connection
3939
return relayConnectionId;
40-
// Wait here until connect event is accepted
4140
}
4241

4342
public static int ConnectWithSimulator(int hostId, string serverAddress, int serverPort, int exceptionConnectionId, out byte error, ConnectionSimulatorConfig conf)
@@ -150,40 +149,33 @@ public static int AddWebsocketHost(HostTopology topology, int port, string ip, b
150149
return ret;
151150
}
152151

153-
private static byte[] disconnectBuffer = new byte[3] { (byte)MessageType.ClientDisconnect, 0, 0 };
152+
private static byte[] disconnectBuffer = new byte[] { 0, 0, (byte)MessageType.ClientDisconnect };
154153
public static bool Disconnect(int hostId, int connectionId, out byte error)
155154
{
156155
if (!Enabled) NetworkTransport.Disconnect(hostId, connectionId, out error);
157156

158157
if (!isClient)
159158
{
160-
disconnectBuffer.ToBytes((ushort)connectionId, 1);
159+
disconnectBuffer.ToBytes((ushort)connectionId); // Tell relay who to drop
161160
return NetworkTransport.Send(hostId, relayConnectionId, GetReliableChannel(), disconnectBuffer, 3, out error);
162161
}
163-
else
164-
{
165-
return NetworkTransport.Disconnect(hostId, connectionId, out error);
166-
}
162+
else return NetworkTransport.Disconnect(hostId, connectionId, out error);
167163
}
168164

169165
public static bool Send(int hostId, int connectionId, int channelId, byte[] buffer, int size, out byte error)
170166
{
171167
if (!Enabled) return NetworkTransport.Send(hostId, connectionId, channelId, buffer, size, out error);
172-
173-
if (isClient)
174-
{
175-
ForwardOffset(buffer, 1, size); // Offsets just the bytes we're sending
176-
++size;
177-
}
178-
else
168+
//ForwardOffset(buffer, 1, size); // Offsets just the bytes we're sending (isClient old)
169+
++size;
170+
if (!isClient)
179171
{
180-
ForwardOffset(buffer, 3, size); // Offsets just the bytes we're sending
181-
size += 3;
172+
//ForwardOffset(buffer, 3, size); // Offsets just the bytes we're sending
173+
size += 2;
182174

183-
buffer[1] = (byte)connectionId;
184-
buffer[2] = (byte)(connectionId >> 8);
175+
buffer[size-3] = (byte)connectionId;
176+
buffer[size-2] = (byte)(connectionId >> 8);
185177
}
186-
buffer[0] = (byte)MessageType.Data;
178+
buffer[size-1] = (byte)MessageType.Data;
187179

188180
return NetworkTransport.Send(hostId, relayConnectionId, channelId, buffer, size, out error);
189181
}
@@ -192,20 +184,16 @@ public static bool QueueMessageForSending(int hostId, int connectionId, int chan
192184
{
193185
if (!Enabled) return NetworkTransport.QueueMessageForSending(hostId, connectionId, channelId, buffer, size, out error);
194186

195-
if (isClient)
196-
{
197-
ForwardOffset(buffer, 1, size); // Offsets just the bytes we're sending
198-
++size;
199-
}
200-
else
187+
++size;
188+
if (!isClient)
201189
{
202-
ForwardOffset(buffer, 3, size); // Offsets just the bytes we're sending
203-
size += 3;
190+
//ForwardOffset(buffer, 3, size); // Offsets just the bytes we're sending
191+
size += 2;
204192

205-
buffer[1] = (byte)connectionId;
206-
buffer[2] = (byte)(connectionId >> 8);
193+
buffer[size - 3] = (byte)connectionId;
194+
buffer[size - 2] = (byte)(connectionId >> 8);
207195
}
208-
buffer[0] = (byte)MessageType.Data;
196+
buffer[size - 1] = (byte)MessageType.Data;
209197

210198
return NetworkTransport.QueueMessageForSending(hostId, relayConnectionId, channelId, buffer, size, out error);
211199
}
@@ -236,37 +224,26 @@ private static NetworkEventType BaseReceive(NetworkEventType @event, int hostId,
236224
switch (@event)
237225
{
238226
case NetworkEventType.DataEvent:
239-
MessageType messageType = (MessageType)buffer[0];
227+
MessageType messageType = (MessageType)buffer[receivedSize - 1];
240228
switch (messageType)
241229
{
242230
case MessageType.ConnectToServer: // Connection approved
243231
{
244232
if (!isClient)
245-
{
246-
connectionId = (ushort)(buffer[1] | (buffer[2] << 8)); // Parse connection id
247-
}
233+
connectionId = (ushort)(buffer[receivedSize - 3] | (buffer[receivedSize - 2] << 8)); // Parse connection id
248234
return NetworkEventType.ConnectEvent;
249235
}
250236
case MessageType.Data:
251237
{
252-
if (isClient)
253-
{
254-
// Remove our headers
255-
ReverseOffset(buffer, 1, receivedSize);
256-
--receivedSize;
257-
}
258-
else
259-
{
260-
// Remove our headers
261-
connectionId = buffer.FromBytes(1);
262-
ReverseOffset(buffer, 3, receivedSize);
263-
receivedSize -= 3;
264-
}
238+
// Implicitly remove header
239+
if (isClient) --receivedSize;
240+
else connectionId = buffer.FromBytes(receivedSize-=3);
241+
265242
return NetworkEventType.DataEvent;
266243
}
267244
case MessageType.ClientDisconnect:
268245
{
269-
connectionId = (ushort)(buffer[1] | (buffer[2] << 8)); // Parse connection id
246+
connectionId = (ushort)(buffer[0] | (buffer[1] << 8)); // Parse connection id
270247
return NetworkEventType.DisconnectEvent;
271248
}
272249
}
@@ -277,14 +254,13 @@ private static NetworkEventType BaseReceive(NetworkEventType @event, int hostId,
277254
{
278255
//Connect via relay
279256
string s = new StringBuilder(address).Append(':').Append(port).ToString();
280-
buffer[0] = (byte)MessageType.ConnectToServer;
281-
buffer[1] = (byte)s.Length;
257+
buffer[s.Length] = (byte)MessageType.ConnectToServer;
258+
259+
// Address data length is implied
282260
for (int i = 0; i < s.Length; i++)
283-
{
284-
buffer[i + 2] = (byte)s[i]; // Get ASCII characters
285-
}
261+
buffer[i] = (byte)s[i]; // Get ASCII characters
286262

287-
NetworkTransport.Send(hostId, connectionId, GetReliableChannel(), buffer, s.Length + 2, out error);
263+
NetworkTransport.Send(hostId, connectionId, GetReliableChannel(), buffer, s.Length + 1, out error);
288264
}
289265
else
290266
{

0 commit comments

Comments
 (0)