@@ -15,6 +15,9 @@ public class KcpServer
15
15
public Action < int > OnDisconnected ;
16
16
17
17
// configuration
18
+ // DualMode uses both IPv6 and IPv4. not all platforms support it.
19
+ // (Nintendo Switch, etc.)
20
+ public bool DualMode ;
18
21
// NoDelay is recommended to reduce latency. This also scales better
19
22
// without buffers getting full.
20
23
public bool NoDelay ;
@@ -41,12 +44,8 @@ public class KcpServer
41
44
42
45
// state
43
46
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
+
50
49
// IMPORTANT: raw receive buffer always needs to be of 'MTU' size, even
51
50
// if MaxMessageSize is larger. kcp always sends in MTU
52
51
// segments and having a buffer smaller than MTU would
@@ -60,6 +59,7 @@ public class KcpServer
60
59
public KcpServer ( Action < int > OnConnected ,
61
60
Action < int , ArraySegment < byte > > OnData ,
62
61
Action < int > OnDisconnected ,
62
+ bool DualMode ,
63
63
bool NoDelay ,
64
64
uint Interval ,
65
65
int FastResend = 0 ,
@@ -71,13 +71,19 @@ public KcpServer(Action<int> OnConnected,
71
71
this . OnConnected = OnConnected ;
72
72
this . OnData = OnData ;
73
73
this . OnDisconnected = OnDisconnected ;
74
+ this . DualMode = DualMode ;
74
75
this . NoDelay = NoDelay ;
75
76
this . Interval = Interval ;
76
77
this . FastResend = FastResend ;
77
78
this . CongestionWindow = CongestionWindow ;
78
79
this . SendWindowSize = SendWindowSize ;
79
80
this . ReceiveWindowSize = ReceiveWindowSize ;
80
81
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 ) ;
81
87
}
82
88
83
89
public bool IsActive ( ) => socket != null ;
@@ -91,15 +97,19 @@ public void Start(ushort port)
91
97
}
92
98
93
99
// 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
+ }
103
113
}
104
114
105
115
public void Send ( int connectionId , ArraySegment < byte > segment , KcpChannel channel )
0 commit comments