From fe81de4aec00c28cc7a5ca97b66f8e92a2ce1d95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= <2108U9@gmail.com> Date: Thu, 14 Jun 2018 18:56:32 +0200 Subject: [PATCH] Update RelayTransport.cs --- RelayTransport.cs | 84 +++++++++++++++++------------------------------ 1 file changed, 30 insertions(+), 54 deletions(-) diff --git a/RelayTransport.cs b/RelayTransport.cs index ad9048c..0429d91 100644 --- a/RelayTransport.cs +++ b/RelayTransport.cs @@ -37,7 +37,6 @@ public static int Connect(int hostId, string serverAddress, int serverPort, int RelayTransport.port = (ushort)serverPort; relayConnectionId = NetworkTransport.Connect(hostId, RelayAddress, RelayPort, exceptionConnectionId, out error); // Requests connection return relayConnectionId; - // Wait here until connect event is accepted } 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 return ret; } - private static byte[] disconnectBuffer = new byte[3] { (byte)MessageType.ClientDisconnect, 0, 0 }; + private static byte[] disconnectBuffer = new byte[] { 0, 0, (byte)MessageType.ClientDisconnect }; public static bool Disconnect(int hostId, int connectionId, out byte error) { if (!Enabled) NetworkTransport.Disconnect(hostId, connectionId, out error); if (!isClient) { - disconnectBuffer.ToBytes((ushort)connectionId, 1); + disconnectBuffer.ToBytes((ushort)connectionId); // Tell relay who to drop return NetworkTransport.Send(hostId, relayConnectionId, GetReliableChannel(), disconnectBuffer, 3, out error); } - else - { - return NetworkTransport.Disconnect(hostId, connectionId, out error); - } + else return NetworkTransport.Disconnect(hostId, connectionId, out error); } public static bool Send(int hostId, int connectionId, int channelId, byte[] buffer, int size, out byte error) { if (!Enabled) return NetworkTransport.Send(hostId, connectionId, channelId, buffer, size, out error); - - if (isClient) - { - ForwardOffset(buffer, 1, size); // Offsets just the bytes we're sending - ++size; - } - else + //ForwardOffset(buffer, 1, size); // Offsets just the bytes we're sending (isClient old) + ++size; + if (!isClient) { - ForwardOffset(buffer, 3, size); // Offsets just the bytes we're sending - size += 3; + //ForwardOffset(buffer, 3, size); // Offsets just the bytes we're sending + size += 2; - buffer[1] = (byte)connectionId; - buffer[2] = (byte)(connectionId >> 8); + buffer[size-3] = (byte)connectionId; + buffer[size-2] = (byte)(connectionId >> 8); } - buffer[0] = (byte)MessageType.Data; + buffer[size-1] = (byte)MessageType.Data; return NetworkTransport.Send(hostId, relayConnectionId, channelId, buffer, size, out error); } @@ -192,20 +184,16 @@ public static bool QueueMessageForSending(int hostId, int connectionId, int chan { if (!Enabled) return NetworkTransport.QueueMessageForSending(hostId, connectionId, channelId, buffer, size, out error); - if (isClient) - { - ForwardOffset(buffer, 1, size); // Offsets just the bytes we're sending - ++size; - } - else + ++size; + if (!isClient) { - ForwardOffset(buffer, 3, size); // Offsets just the bytes we're sending - size += 3; + //ForwardOffset(buffer, 3, size); // Offsets just the bytes we're sending + size += 2; - buffer[1] = (byte)connectionId; - buffer[2] = (byte)(connectionId >> 8); + buffer[size - 3] = (byte)connectionId; + buffer[size - 2] = (byte)(connectionId >> 8); } - buffer[0] = (byte)MessageType.Data; + buffer[size - 1] = (byte)MessageType.Data; return NetworkTransport.QueueMessageForSending(hostId, relayConnectionId, channelId, buffer, size, out error); } @@ -236,37 +224,26 @@ private static NetworkEventType BaseReceive(NetworkEventType @event, int hostId, switch (@event) { case NetworkEventType.DataEvent: - MessageType messageType = (MessageType)buffer[0]; + MessageType messageType = (MessageType)buffer[receivedSize - 1]; switch (messageType) { case MessageType.ConnectToServer: // Connection approved { if (!isClient) - { - connectionId = (ushort)(buffer[1] | (buffer[2] << 8)); // Parse connection id - } + connectionId = (ushort)(buffer[receivedSize - 3] | (buffer[receivedSize - 2] << 8)); // Parse connection id return NetworkEventType.ConnectEvent; } case MessageType.Data: { - if (isClient) - { - // Remove our headers - ReverseOffset(buffer, 1, receivedSize); - --receivedSize; - } - else - { - // Remove our headers - connectionId = buffer.FromBytes(1); - ReverseOffset(buffer, 3, receivedSize); - receivedSize -= 3; - } + // Implicitly remove header + if (isClient) --receivedSize; + else connectionId = buffer.FromBytes(receivedSize-=3); + return NetworkEventType.DataEvent; } case MessageType.ClientDisconnect: { - connectionId = (ushort)(buffer[1] | (buffer[2] << 8)); // Parse connection id + connectionId = (ushort)(buffer[0] | (buffer[1] << 8)); // Parse connection id return NetworkEventType.DisconnectEvent; } } @@ -277,14 +254,13 @@ private static NetworkEventType BaseReceive(NetworkEventType @event, int hostId, { //Connect via relay string s = new StringBuilder(address).Append(':').Append(port).ToString(); - buffer[0] = (byte)MessageType.ConnectToServer; - buffer[1] = (byte)s.Length; + buffer[s.Length] = (byte)MessageType.ConnectToServer; + + // Address data length is implied for (int i = 0; i < s.Length; i++) - { - buffer[i + 2] = (byte)s[i]; // Get ASCII characters - } + buffer[i] = (byte)s[i]; // Get ASCII characters - NetworkTransport.Send(hostId, connectionId, GetReliableChannel(), buffer, s.Length + 2, out error); + NetworkTransport.Send(hostId, connectionId, GetReliableChannel(), buffer, s.Length + 1, out error); } else {