Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Commit

Permalink
fix: updating transport for mirror v26
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Oct 29, 2020
1 parent 2b15b22 commit 9b6bbe0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 17 deletions.
7 changes: 5 additions & 2 deletions DebugScripts/DebugServer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Mirror.SimpleWeb
Expand Down Expand Up @@ -48,7 +47,11 @@ public void OnGUI()
{
if (GUILayout.Button("Send Message"))
{
transport.ServerSend(new List<int>() { 1 }, Channels.DefaultReliable, new ArraySegment<byte>(new byte[] { 1, 2, 4, 8 }));
#if MIRROR_26_0_OR_NEWER
transport.ServerSend(1, Channels.DefaultReliable, new ArraySegment<byte>(new byte[] { 1, 2, 4, 8 }));
#else
transport.ServerSend(new System.Collections.Generic.List<int>() { 1 }, Channels.DefaultReliable, new ArraySegment<byte>(new byte[] { 1, 2, 4, 8 }));
#endif
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions source/Server/SimpleWebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public void SendAll(List<int> connectionIds, ArraySegment<byte> source)
server.Send(id, buffer);
}
}
public void SendOne(int connectionId, ArraySegment<byte> source)
{
ArrayBuffer buffer = bufferPool.Take(source.Count);
buffer.CopyFrom(source);

server.Send(connectionId, buffer);
}

public bool KickClient(int connectionId)
{
Expand Down
56 changes: 53 additions & 3 deletions source/SimpleWebTransport.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Authentication;
using UnityEngine;
Expand Down Expand Up @@ -116,7 +115,7 @@ public void ProcessMessages()
string GetScheme() => sslEnabled ? SecureScheme : NormalScheme;
public override bool ClientConnected()
{
// not null and not NotConnected (we want to return true if connecting or disconnecting)
// not null and not NotConnected (we want to return true if connecting or disconnecting)
return client != null && client.ConnectionState != ClientState.NotConnected;
}

Expand Down Expand Up @@ -164,6 +163,30 @@ public override void ClientDisconnect()
client?.Disconnect();
}

#if MIRROR_26_0_OR_NEWER
public override void ClientSend(int channelId, ArraySegment<byte> segment)
{
if (!ClientConnected())
{
Debug.LogError("Not Connected");
return;
}

if (segment.Count > maxMessageSize)
{
Log.Error("Message greater than max size");
return;
}

if (segment.Count == 0)
{
Log.Error("Message count was zero");
return;
}

client.Send(segment);
}
#else
public override bool ClientSend(int channelId, ArraySegment<byte> segment)
{
if (!ClientConnected())
Expand All @@ -187,6 +210,7 @@ public override bool ClientSend(int channelId, ArraySegment<byte> segment)
client.Send(segment);
return true;
}
#endif
#endregion

#region Server
Expand Down Expand Up @@ -235,7 +259,32 @@ public override bool ServerDisconnect(int connectionId)
return server.KickClient(connectionId);
}

public override bool ServerSend(List<int> connectionIds, int channelId, ArraySegment<byte> segment)
#if MIRROR_26_0_OR_NEWER
public override void ServerSend(int connectionId, int channelId, ArraySegment<byte> segment)
{
if (!ServerActive())
{
Debug.LogError("SimpleWebServer Not Active");
return;
}

if (segment.Count > maxMessageSize)
{
Log.Error("Message greater than max size");
return;
}

if (segment.Count == 0)
{
Log.Error("Message count was zero");
return;
}

server.SendOne(connectionId, segment);
return;
}
#else
public override bool ServerSend(System.Collections.Generic.List<int> connectionIds, int channelId, ArraySegment<byte> segment)
{
if (!ServerActive())
{
Expand All @@ -258,6 +307,7 @@ public override bool ServerSend(List<int> connectionIds, int channelId, ArraySeg
server.SendAll(connectionIds, segment);
return true;
}
#endif

public override string ServerGetClientAddress(int connectionId)
{
Expand Down
8 changes: 2 additions & 6 deletions tests/Runtime/Client/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,7 @@ public IEnumerator ErrorWhenMessageTooBig()
ArraySegment<byte> segment = new ArraySegment<byte>(new byte[70_000]);

LogAssert.Expect(LogType.Error, "ERROR: <color=red>Message greater than max size</color>");
bool result = client.ClientSend(Channels.DefaultReliable, segment);

Assert.IsFalse(result);
client.ClientSend(Channels.DefaultReliable, segment);
}

[UnityTest]
Expand All @@ -248,9 +246,7 @@ public IEnumerator ErrorWhenMessageTooSmall()
ArraySegment<byte> segment = new ArraySegment<byte>();

LogAssert.Expect(LogType.Error, "ERROR: <color=red>Message count was zero</color>");
bool result = client.ClientSend(Channels.DefaultReliable, segment);

Assert.IsFalse(result);
client.ClientSend(Channels.DefaultReliable, segment);
}
}
}
8 changes: 2 additions & 6 deletions tests/Runtime/Server/SendMessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ public IEnumerator ErrorWhenMessageTooBig()
ArraySegment<byte> segment = new ArraySegment<byte>(new byte[70_000]);

LogAssert.Expect(LogType.Error, "ERROR: <color=red>Message greater than max size</color>");
bool result = server.ServerSend(new List<int> { 1 }, Channels.DefaultReliable, segment);

Assert.IsFalse(result);
server.ServerSend(new List<int> { 1 }, Channels.DefaultReliable, segment);
}

[UnityTest]
Expand All @@ -135,9 +133,7 @@ public IEnumerator ErrorWhenMessageTooSmall()
ArraySegment<byte> segment = new ArraySegment<byte>();

LogAssert.Expect(LogType.Error, "ERROR: <color=red>Message count was zero</color>");
bool result = server.ServerSend(new List<int> { 1 }, Channels.DefaultReliable, segment);

Assert.IsFalse(result);
server.ServerSend(new List<int> { 1 }, Channels.DefaultReliable, segment);
}
}
}
8 changes: 8 additions & 0 deletions tests/Runtime/TestInstances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public void Init()
}

public WaitUntil WaitForConnection => new WaitUntil(() => onConnect.Count >= 1);

public void ServerSend(System.Collections.Generic.List<int> connectionIds, int channelId, ArraySegment<byte> segment)
{
foreach (int id in connectionIds)
{
ServerSend(id, channelId, segment);
}
}
}
public class ClientTestInstance : SimpleWebTransport, NeedInitTestInstance
{
Expand Down

0 comments on commit 9b6bbe0

Please sign in to comment.