Skip to content

Commit

Permalink
Configurable FastResend, CongestionWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
vis2k committed Oct 29, 2020
1 parent 55b4f22 commit a947b8e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
8 changes: 7 additions & 1 deletion kcp2k/Assets/kcp2k/MirrorTransport/KcpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class KcpTransport : Transport
[Tooltip("KCP internal update interval. 100ms is KCP default, but a lower interval is recommended to minimize latency and to scale to more networked entities.")]
public uint Interval = 10;
[Header("Advanced")]
[Tooltip("KCP fastresend parameter. Faster resend for the cost of higher bandwidth.")]
public int FastResend = 0;
[Tooltip("KCP congestion window can be disabled. This is necessary to Mirror 10k Benchmark. Disable this for high scale games if connections get chocked regularly.")]
public bool CongestionWindow = true; // KCP 'NoCongestionWindow' is false by default. here we negate it for ease of use.
[Tooltip("KCP window size can be modified to support higher loads. For example, Mirror Benchmark requires 128 for 4k monsters, 512 for 10k monsters")]
public uint SendWindowSize = 128; //Kcp.WND_SND; 32 by default. 128 is better for 4k Benchmark etc.
[Tooltip("KCP window size can be modified to support higher loads. For example, Mirror Benchmark requires 128 for 4k monsters, 512 for 10k monsters")]
Expand Down Expand Up @@ -44,6 +48,8 @@ void Awake()
(connectionId) => OnServerDisconnected.Invoke(connectionId),
NoDelay,
Interval,
FastResend,
CongestionWindow,
SendWindowSize,
ReceiveWindowSize
);
Expand All @@ -58,7 +64,7 @@ public override bool Available() =>
public override bool ClientConnected() => client.connected;
public override void ClientConnect(string address)
{
client.Connect(address, Port, NoDelay, Interval, SendWindowSize, ReceiveWindowSize);
client.Connect(address, Port, NoDelay, Interval, FastResend, CongestionWindow, SendWindowSize, ReceiveWindowSize);
}
public override bool ClientSend(int channelId, ArraySegment<byte> segment)
{
Expand Down
4 changes: 2 additions & 2 deletions kcp2k/Assets/kcp2k/highlevel/KcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public KcpClient(Action OnConnected, Action<ArraySegment<byte>> OnData, Action O
this.OnDisconnected = OnDisconnected;
}

public void Connect(string address, ushort port, bool noDelay, uint interval, uint SendWindowSize = Kcp.WND_SND, uint ReceiveWindowSize = Kcp.WND_RCV)
public void Connect(string address, ushort port, bool noDelay, uint interval, int fastResend = 0, bool congestionWindow = true, uint SendWindowSize = Kcp.WND_SND, uint ReceiveWindowSize = Kcp.WND_RCV)
{
if (connected)
{
Expand Down Expand Up @@ -54,7 +54,7 @@ public void Connect(string address, ushort port, bool noDelay, uint interval, ui
};

// connect
connection.Connect(address, port, noDelay, interval, SendWindowSize, ReceiveWindowSize);
connection.Connect(address, port, noDelay, interval, fastResend, congestionWindow, SendWindowSize, ReceiveWindowSize);
}

public void Send(ArraySegment<byte> segment)
Expand Down
4 changes: 2 additions & 2 deletions kcp2k/Assets/kcp2k/highlevel/KcpClientConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class KcpClientConnection : KcpConnection
{
readonly byte[] buffer = new byte[1500];

public void Connect(string host, ushort port, bool noDelay, uint interval = Kcp.INTERVAL, uint SendWindowSize = Kcp.WND_SND, uint ReceiveWindowSize = Kcp.WND_RCV)
public void Connect(string host, ushort port, bool noDelay, uint interval = Kcp.INTERVAL, int fastResend = 0, bool congestionWindow = true, uint SendWindowSize = Kcp.WND_SND, uint ReceiveWindowSize = Kcp.WND_RCV)
{
Debug.Log($"KcpClient: connect to {host}:{port}");
IPAddress[] ipAddress = Dns.GetHostAddresses(host);
Expand All @@ -18,7 +18,7 @@ public void Connect(string host, ushort port, bool noDelay, uint interval = Kcp.
remoteEndpoint = new IPEndPoint(ipAddress[0], port);
socket = new Socket(remoteEndpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
socket.Connect(remoteEndpoint);
SetupKcp(noDelay, interval, SendWindowSize, ReceiveWindowSize);
SetupKcp(noDelay, interval, fastResend, congestionWindow, SendWindowSize, ReceiveWindowSize);

// client should send handshake to server as very first message
SendHandshake();
Expand Down
6 changes: 4 additions & 2 deletions kcp2k/Assets/kcp2k/highlevel/KcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ public abstract class KcpConnection

// NoDelay, interval, window size are the most important configurations.
// let's force require the parameters so we don't forget it anywhere.
protected void SetupKcp(bool noDelay, uint interval = Kcp.INTERVAL, uint SendWindowSize = Kcp.WND_SND, uint ReceiveWindowSize = Kcp.WND_RCV)
protected void SetupKcp(bool noDelay, uint interval = Kcp.INTERVAL, int fastResend = 0, bool congestionWindow = true, uint SendWindowSize = Kcp.WND_SND, uint ReceiveWindowSize = Kcp.WND_RCV)
{
kcp = new Kcp(0, RawSend);
kcp.SetNoDelay(noDelay ? 1u : 0u, interval);
// set nodelay.
// note that kcp uses 'nocwnd' internally so we negate the parameter
kcp.SetNoDelay(noDelay ? 1u : 0u, interval, fastResend, !congestionWindow);
kcp.SetWindowSize(SendWindowSize, ReceiveWindowSize);
refTime.Start();
state = KcpState.Connected;
Expand Down
13 changes: 12 additions & 1 deletion kcp2k/Assets/kcp2k/highlevel/KcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public class KcpServer
// interval is recommended to minimize latency and to scale to more
// networked entities.
public uint Interval;
// KCP fastresend parameter. Faster resend for the cost of higher
// bandwidth.
public int FastResend;
// KCP 'NoCongestionWindow' is false by default. here we negate it for
// ease of use. This can be disabled for high scale games if connections
// choke regularly.
public bool CongestionWindow;
// KCP window size can be modified to support higher loads.
// for example, Mirror Benchmark requires:
// 128, 128 for 4k monsters
Expand All @@ -44,6 +51,8 @@ public KcpServer(Action<int> OnConnected,
Action<int> OnDisconnected,
bool NoDelay,
uint Interval,
int FastResend = 0,
bool CongestionWindow = true,
uint SendWindowSize = Kcp.WND_SND,
uint ReceiveWindowSize = Kcp.WND_RCV)
{
Expand All @@ -52,6 +61,8 @@ public KcpServer(Action<int> OnConnected,
this.OnDisconnected = OnDisconnected;
this.NoDelay = NoDelay;
this.Interval = Interval;
this.FastResend = FastResend;
this.CongestionWindow = CongestionWindow;
this.SendWindowSize = SendWindowSize;
this.ReceiveWindowSize = ReceiveWindowSize;
}
Expand Down Expand Up @@ -111,7 +122,7 @@ public void Tick()
if (!connections.TryGetValue(connectionId, out KcpServerConnection connection))
{
// create a new KcpConnection
connection = new KcpServerConnection(socket, newClientEP, NoDelay, Interval, SendWindowSize, ReceiveWindowSize);
connection = new KcpServerConnection(socket, newClientEP, NoDelay, Interval, FastResend, CongestionWindow, SendWindowSize, ReceiveWindowSize);

// DO NOT add to connections yet. only if the first message
// is actually the kcp handshake. otherwise it's either:
Expand Down
4 changes: 2 additions & 2 deletions kcp2k/Assets/kcp2k/highlevel/KcpServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace kcp2k
{
public class KcpServerConnection : KcpConnection
{
public KcpServerConnection(Socket socket, EndPoint remoteEndpoint, bool noDelay, uint interval = Kcp.INTERVAL, uint SendWindowSize = Kcp.WND_SND, uint ReceiveWindowSize = Kcp.WND_RCV)
public KcpServerConnection(Socket socket, EndPoint remoteEndpoint, bool noDelay, uint interval = Kcp.INTERVAL, int fastResend = 0, bool congestionWindow = true, uint SendWindowSize = Kcp.WND_SND, uint ReceiveWindowSize = Kcp.WND_RCV)
{
this.socket = socket;
this.remoteEndpoint = remoteEndpoint;
SetupKcp(noDelay, interval, SendWindowSize, ReceiveWindowSize);
SetupKcp(noDelay, interval, fastResend, congestionWindow, SendWindowSize, ReceiveWindowSize);
}

protected override void RawSend(byte[] data, int length)
Expand Down

0 comments on commit a947b8e

Please sign in to comment.