Skip to content

Commit 6f47d9c

Browse files
author
vis2k
committed
feature: KcpServer.DualMode is now configurable in the constructor instead of using #if UNITY_SWITCH
1 parent 195ebb5 commit 6f47d9c

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

kcp2k/Assets/Example/TestServer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class TestServer : MonoBehaviour
1414
(connectionId) => {},
1515
(connectionId, message) => Debug.Log($"KCP: OnServerDataReceived({connectionId}, {BitConverter.ToString(message.Array, message.Offset, message.Count)})"),
1616
(connectionId) => {},
17+
false,
1718
true,
1819
10
1920
);

kcp2k/Assets/Tests/Editor/ClientServerTests.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class ClientServerTests
1414
// this way UpdateSeveralTimes() doesn't need to wait very long and
1515
// tests run a lot faster.
1616
const ushort Port = 7777;
17+
// not all platforms support DualMode.
18+
// run tests without it so they work on all platforms.
19+
const bool DualMode = false;
1720
const bool NoDelay = true;
1821
const uint Interval = 1; // 1ms so at interval code at least runs.
1922
const int Timeout = 2000;
@@ -65,6 +68,7 @@ public void SetUp()
6568
(connectionId) => {},
6669
ServerOnData,
6770
(connectionId) => {},
71+
DualMode,
6872
NoDelay,
6973
Interval,
7074
0,
@@ -746,7 +750,10 @@ public void ServerGetClientAddress()
746750
ConnectClientBlocking();
747751
int connectionId = ServerFirstConnectionId();
748752

749-
Assert.That(server.GetClientAddress(connectionId), Is.EqualTo("::ffff:127.0.0.1"));
753+
if (server.DualMode)
754+
Assert.That(server.GetClientAddress(connectionId), Is.EqualTo("::ffff:127.0.0.1"));
755+
else
756+
Assert.That(server.GetClientAddress(connectionId), Is.EqualTo("127.0.0.1"));
750757
}
751758

752759
[Test]

kcp2k/Assets/kcp2k/highlevel/KcpServer.cs

+25-15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public class KcpServer
1515
public Action<int> OnDisconnected;
1616

1717
// configuration
18+
// DualMode uses both IPv6 and IPv4. not all platforms support it.
19+
// (Nintendo Switch, etc.)
20+
public bool DualMode;
1821
// NoDelay is recommended to reduce latency. This also scales better
1922
// without buffers getting full.
2023
public bool NoDelay;
@@ -41,12 +44,8 @@ public class KcpServer
4144

4245
// state
4346
Socket socket;
44-
#if UNITY_SWITCH
45-
// switch does not support ipv6
46-
EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
47-
#else
48-
EndPoint newClientEP = new IPEndPoint(IPAddress.IPv6Any, 0);
49-
#endif
47+
EndPoint newClientEP;
48+
5049
// IMPORTANT: raw receive buffer always needs to be of 'MTU' size, even
5150
// if MaxMessageSize is larger. kcp always sends in MTU
5251
// segments and having a buffer smaller than MTU would
@@ -60,6 +59,7 @@ public class KcpServer
6059
public KcpServer(Action<int> OnConnected,
6160
Action<int, ArraySegment<byte>> OnData,
6261
Action<int> OnDisconnected,
62+
bool DualMode,
6363
bool NoDelay,
6464
uint Interval,
6565
int FastResend = 0,
@@ -71,13 +71,19 @@ public KcpServer(Action<int> OnConnected,
7171
this.OnConnected = OnConnected;
7272
this.OnData = OnData;
7373
this.OnDisconnected = OnDisconnected;
74+
this.DualMode = DualMode;
7475
this.NoDelay = NoDelay;
7576
this.Interval = Interval;
7677
this.FastResend = FastResend;
7778
this.CongestionWindow = CongestionWindow;
7879
this.SendWindowSize = SendWindowSize;
7980
this.ReceiveWindowSize = ReceiveWindowSize;
8081
this.Timeout = Timeout;
82+
83+
// create newClientEP either IPv4 or IPv6
84+
newClientEP = DualMode
85+
? new IPEndPoint(IPAddress.IPv6Any, 0)
86+
: new IPEndPoint(IPAddress.Any, 0);
8187
}
8288

8389
public bool IsActive() => socket != null;
@@ -91,15 +97,19 @@ public void Start(ushort port)
9197
}
9298

9399
// listen
94-
#if UNITY_SWITCH
95-
// Switch does not support ipv6
96-
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
97-
socket.Bind(new IPEndPoint(IPAddress.Any, port));
98-
#else
99-
socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
100-
socket.DualMode = true;
101-
socket.Bind(new IPEndPoint(IPAddress.IPv6Any, port));
102-
#endif
100+
if (DualMode)
101+
{
102+
// IPv6 socket with DualMode
103+
socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
104+
socket.DualMode = true;
105+
socket.Bind(new IPEndPoint(IPAddress.IPv6Any, port));
106+
}
107+
else
108+
{
109+
// IPv4 socket
110+
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
111+
socket.Bind(new IPEndPoint(IPAddress.Any, port));
112+
}
103113
}
104114

105115
public void Send(int connectionId, ArraySegment<byte> segment, KcpChannel channel)

0 commit comments

Comments
 (0)