You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// NoDelay is recommended to reduce latency. This also scales better
@@ -94,6 +97,43 @@ public KcpServer(Action<int> OnConnected,
94
97
95
98
publicboolIsActive()=>socket!=null;
96
99
100
+
// if connections drop under heavy load, increase to OS limit.
101
+
// if still not enough, increase the OS limit.
102
+
voidConfigureSocketBufferSizes()
103
+
{
104
+
if(MaximizeSendReceiveBuffersToOSLimit)
105
+
{
106
+
// log initial size for comparison.
107
+
// remember initial size for log comparison
108
+
intinitialReceive=socket.ReceiveBufferSize;
109
+
intinitialSend=socket.SendBufferSize;
110
+
111
+
// 100k attempts of 1 KB increases = default + 100 MB max
112
+
constintattempts=100_000;
113
+
constintincrease=1024;
114
+
115
+
// setting a too large size throws a socket exception.
116
+
// so let's keep increasing until we encounter it.
117
+
for(inti=0;i<attempts;++i)
118
+
{
119
+
// increase in 1 KB steps
120
+
try{socket.ReceiveBufferSize+=increase;}
121
+
catch(SocketException){break;}
122
+
}
123
+
124
+
for(inti=0;i<attempts;++i)
125
+
{
126
+
// increase in 1 KB steps
127
+
try{socket.SendBufferSize+=increase;}
128
+
catch(SocketException){break;}
129
+
}
130
+
131
+
Log.Info($"KcpServer: RecvBuf = {initialReceive}=>{socket.ReceiveBufferSize} ({socket.ReceiveBufferSize/initialReceive}x) SendBuf = {initialSend}=>{socket.SendBufferSize} ({socket.SendBufferSize/initialSend}x) increased to OS limits!");
132
+
}
133
+
// otherwise still log the defaults for info.
134
+
elseLog.Info($"KcpServer: RecvBuf = {socket.ReceiveBufferSize} SendBuf = {socket.SendBufferSize}. If connections drop under heavy load, enable {nameof(MaximizeSendReceiveBuffersToOSLimit)} to increase it to OS limit. If they still drop, increase the OS limit.");
135
+
}
136
+
97
137
publicvoidStart(ushortport)
98
138
{
99
139
// only start once
@@ -117,10 +157,8 @@ public void Start(ushort port)
117
157
socket.Bind(newIPEndPoint(IPAddress.Any,port));
118
158
}
119
159
120
-
// show socket buffer size.
121
-
// if connections drop under heavy load, increase to OS limit.
122
-
// if still not enough, increase the OS limit.
123
-
Log.Info($"KcpServer: RecvBuf = {socket.ReceiveBufferSize} SendBuf = {socket.SendBufferSize}. If connections drop under heavy load, increase to OS limit. If they still drop, increase the OS limit.");
0 commit comments