Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit d6e020d

Browse files
committed
Set UDP Client socket to disregard ICMP errors and forcibly disconnect.
1 parent eacb104 commit d6e020d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

ForgeUnity/Assets/BeardedManStudios/Scripts/Networking/Forge/Networking/CachedUdpClient.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
// distribute, sublicense, and/or sell copies of the Software, and to
2222
// permit persons to whom the Software is furnished to do so, subject to
2323
// the following conditions:
24-
//
24+
//
2525
// The above copyright notice and this permission notice shall be
2626
// included in all copies or substantial portions of the Software.
27-
//
27+
//
2828
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2929
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
3030
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -46,6 +46,16 @@ namespace BeardedManStudios.Forge.Networking
4646
{
4747
public class CachedUdpClient : IDisposable
4848
{
49+
/// <summary>
50+
/// Winsock ioctl code which will disable ICMP errors from being propagated to a UDP socket.
51+
/// This can occur if a UDP packet is sent to a valid destination but there is no socket
52+
/// registered to listen on the given port.
53+
/// </summary>
54+
/// http://msdn.microsoft.com/en-us/library/cc242275.aspx
55+
/// http://msdn.microsoft.com/en-us/library/bb736550(VS.85).aspx
56+
/// 0x9800000C == 2550136844 (uint) == -1744830452 (int) == 0x9800000C
57+
const int SIO_UDP_CONNRESET = -1744830452;
58+
4959
public const char HOST_PORT_CHARACTER_SEPARATOR = '+';
5060
private bool disposed = false;
5161
private bool active = false;
@@ -140,6 +150,9 @@ private void InitSocket(EndPoint localEP)
140150
if (localEP != null)
141151
socket.Bind(localEP);
142152

153+
// set socket to disregard ICMP errors.
154+
socket.IOControl((IOControlCode)SIO_UDP_CONNRESET, new byte[] { 0, 0, 0, 0 }, null);
155+
143156
recBuffer.SetSize(65536);
144157
}
145158

0 commit comments

Comments
 (0)