@@ -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