File tree Expand file tree Collapse file tree 1 file changed +12
-23
lines changed
src/Shared/HttpSys/NativeInterop Expand file tree Collapse file tree 1 file changed +12
-23
lines changed Original file line number Diff line number Diff line change @@ -65,32 +65,21 @@ private IPAddress GetIPv4Address()
6565
6666 internal static unsafe SocketAddress ? CopyOutAddress ( IntPtr address )
6767 {
68- var addressFamily = * ( ( ushort * ) address ) ;
69- if ( addressFamily == ( ushort ) AddressFamily . InterNetwork )
68+ AddressFamily family = ( AddressFamily ) ( * ( ( ushort * ) address ) ) ;
69+ int ? addressSize = family switch
7070 {
71- var v4address = new SocketAddress ( AddressFamily . InterNetwork , IPv4AddressSize ) ;
72- fixed ( byte * pBuffer = v4address . _buffer )
73- {
74- for ( var index = 2 ; index < IPv4AddressSize ; index ++ )
75- {
76- pBuffer [ index ] = ( ( byte * ) address ) [ index ] ;
77- }
78- }
79- return v4address ;
80- }
81- if ( addressFamily == ( ushort ) AddressFamily . InterNetworkV6 )
71+ AddressFamily . InterNetwork => IPv4AddressSize ,
72+ AddressFamily . InterNetworkV6 => IPv6AddressSize ,
73+ _ => null
74+ } ;
75+
76+ if ( ! addressSize . HasValue )
8277 {
83- var v6address = new SocketAddress ( AddressFamily . InterNetworkV6 , IPv6AddressSize ) ;
84- fixed ( byte * pBuffer = v6address . _buffer )
85- {
86- for ( var index = 2 ; index < IPv6AddressSize ; index ++ )
87- {
88- pBuffer [ index ] = ( ( byte * ) address ) [ index ] ;
89- }
90- }
91- return v6address ;
78+ return null ;
9279 }
9380
94- return null ;
81+ var sockAddress = new SocketAddress ( family , addressSize . Value ) ;
82+ new ReadOnlySpan < byte > ( ( byte * ) address , addressSize . Value ) . Slice ( sizeof ( ushort ) ) . CopyTo ( sockAddress . _buffer . AsSpan ( sizeof ( ushort ) ) ) ;
83+ return sockAddress ;
9584 }
9685}
You can’t perform that action at this time.
0 commit comments