| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.ComponentModel; |
| | | 5 | | using System.Runtime.Serialization; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Sockets |
| | | 8 | | { |
| | | 9 | | /// <summary>Provides socket exceptions to the application.</summary> |
| | | 10 | | [Serializable] |
| | | 11 | | [System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5 |
| | | 12 | | public partial class SocketException : Win32Exception |
| | | 13 | | { |
| | | 14 | | /// <summary>The SocketError or Int32 specified when constructing the exception.</summary> |
| | | 15 | | /// <remarks>Based on platform, this may or may not be the same as the underlying NativeErrorCode.</remarks> |
| | | 16 | | private readonly SocketError _errorCode; |
| | | 17 | | |
| | | 18 | | /// <summary>Creates a new instance of the <see cref='System.Net.Sockets.SocketException'/> class with the speci |
| | 0 | 19 | | public SocketException(int errorCode) : this((SocketError)errorCode) |
| | 0 | 20 | | { |
| | | 21 | | // NOTE: SocketException(SocketError) isn't exposed publicly. As a result, code with a SocketError calls |
| | | 22 | | // this ctor, e.g. |
| | | 23 | | // SocketError error = ...; |
| | | 24 | | // throw new SocketException((int)error); |
| | | 25 | | // That means we need to assume the errorCode is a SocketError value, rather than a platform-specific error |
| | | 26 | | // Hence, no translation on the supplied code. This does mean on Unix there's a difference between: |
| | | 27 | | // new SocketException(); // will treat the last error as a native error code and translate it appropria |
| | | 28 | | // and: |
| | | 29 | | // new SocketException(Marshal.GetLastPInvokeError()); // will treat the last error as a SocketError, in |
| | | 30 | | // but that's the least bad option right now. |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary>Initializes a new instance of the <see cref='System.Net.Sockets.SocketException'/> class with the s |
| | 0 | 34 | | public SocketException(int errorCode, string? message) : this((SocketError)errorCode, message) |
| | 0 | 35 | | { |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary>Creates a new instance of the <see cref='System.Net.Sockets.SocketException'/> class with the speci |
| | 0 | 39 | | internal SocketException(SocketError socketError) : base(GetNativeErrorForSocketError(socketError)) |
| | 0 | 40 | | { |
| | 0 | 41 | | _errorCode = socketError; |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary>Initializes a new instance of the <see cref='System.Net.Sockets.SocketException'/> class with the s |
| | 0 | 45 | | internal SocketException(SocketError socketError, string? message) : base(GetNativeErrorForSocketError(socketErr |
| | 0 | 46 | | { |
| | 0 | 47 | | _errorCode = socketError; |
| | 0 | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | public override string Message => base.Message; |
| | | 51 | | |
| | 0 | 52 | | public SocketError SocketErrorCode => _errorCode; |
| | | 53 | | |
| | | 54 | | [Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlForma |
| | | 55 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 56 | | protected SocketException(SerializationInfo serializationInfo, StreamingContext streamingContext) |
| | 0 | 57 | | : base(serializationInfo, streamingContext) |
| | 0 | 58 | | { |
| | 0 | 59 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"{NativeErrorCode}:{Message}"); |
| | 0 | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | public override int ErrorCode => base.NativeErrorCode; |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |