21
21
// distribute, sublicense, and/or sell copies of the Software, and to
22
22
// permit persons to whom the Software is furnished to do so, subject to
23
23
// the following conditions:
24
- //
24
+ //
25
25
// The above copyright notice and this permission notice shall be
26
26
// included in all copies or substantial portions of the Software.
27
- //
27
+ //
28
28
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29
29
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30
30
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -46,6 +46,19 @@ namespace BeardedManStudios.Forge.Networking
46
46
{
47
47
public class CachedUdpClient : IDisposable
48
48
{
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
+ /// https://stackoverflow.com/questions/7201862/an-existing-connection-was-forcibly-closed-by-the-remote-host/7478498#7478498
57
+ /// uint IOC_IN = 0x80000000;
58
+ /// uint IOC_VENDOR = 0x18000000;
59
+ /// uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
60
+ private const int SIO_UDP_CONNRESET = - 1744830452 ;
61
+
49
62
public const char HOST_PORT_CHARACTER_SEPARATOR = '+' ;
50
63
private bool disposed = false ;
51
64
private bool active = false ;
@@ -143,6 +156,7 @@ private void InitSocket(EndPoint localEP)
143
156
recBuffer . SetSize ( 65536 ) ;
144
157
}
145
158
159
+
146
160
#region Close
147
161
public void Close ( )
148
162
{
@@ -226,6 +240,17 @@ public void Connect(string hostname, int port)
226
240
}
227
241
}
228
242
}
243
+
244
+ /// <summary>
245
+ /// Enable/disable whether the socket should disregard ICMP Port unreachable errors
246
+ /// </summary>
247
+ /// <param name="enabled"></param>
248
+ public void IgnoreICMPErrors ( bool enabled )
249
+ {
250
+ // set socket to disregard ICMP errors.
251
+ socket . IOControl ( SIO_UDP_CONNRESET , new byte [ ] { Convert . ToByte ( ! enabled ) } , null ) ;
252
+ }
253
+
229
254
#endregion
230
255
#region Multicast methods
231
256
public void DropMulticastGroup ( IPAddress multicastAddr )
0 commit comments