Code:
                         / Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / Net / System / Net / SocketAddress.cs / 1 / SocketAddress.cs
                        
                        
                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
//  
//----------------------------------------------------------------------------- 
namespace System.Net { 
 
    using System;
    using System.Runtime.InteropServices; 
    using System.Net.Sockets;
    using System.Text;
    using System.Globalization;
 
    // a little perf app measured these times when comparing the internal
    // buffer implemented as a managed byte[] or unmanaged memory IntPtr 
    // that's why we use byte[] 
    // byte[] total ms:19656
    // IntPtr total ms:25671 
    /// 
    ///    
    ///       This class is used when subclassing EndPoint, and provides indication 
    ///       on how to format the memeory buffers that winsock uses for network addresses.
    ///      
    ///   
    public class SocketAddress {
 
        internal const int IPv6AddressSize = 28;
        internal const int IPv4AddressSize = 16;
        internal int m_Size; 
        internal byte[] m_Buffer;
 
        private const int WriteableOffset = 2; 
        private const int MaxSize = 32; // IrDA requires 32 bytes
        private bool m_changed = true; 
        private int m_hash;
        //
        // Address Family 
        //
        ///  
        ///    [To be supplied.]  
        ///  
        public AddressFamily Family { 
            get {
                int family;
#if BIGENDIAN
                family = ((int)m_Buffer[0]<<8) | m_Buffer[1]; 
#else
                family = m_Buffer[0] | ((int)m_Buffer[1]<<8); 
#endif 
                return (AddressFamily)family;
            } 
        }
        //
        // Size of this SocketAddress
        // 
        /// 
        ///    [To be supplied.]  
        ///   
        public int Size {
            get { 
                return m_Size;
            }
        }
 
        //
        // access to unmanaged serialized data. this doesn't 
        // allow access to the first 2 bytes of unmanaged memory 
        // that are supposed to contain the address family which
        // is readonly. 
        //
        //  you can still use negative offsets as a back door in case
        // winsock changes the way it uses SOCKADDR. maybe we want to prohibit it?
        // maybe we should make the class sealed to avoid potentially dangerous calls 
        // into winsock with unproperly formatted data?  
        // 
        ///  
        ///    [To be supplied.] 
        ///   
        public byte this[int offset] {
            get {
                //
                // access 
                //
                if (offset<0 || offset>=Size) { 
                    throw new IndexOutOfRangeException(); 
                }
                return m_Buffer[offset]; 
            }
            set {
                if (offset<0 || offset>=Size) {
                    throw new IndexOutOfRangeException(); 
                }
                if (m_Buffer[offset] != value) { 
                    m_changed = true; 
                }
                m_Buffer[offset] = value; 
            }
        }
        ///  
        ///    [To be supplied.] 
        ///   
        public SocketAddress(AddressFamily family) : this(family, MaxSize) { 
        }
 
        /// 
        ///    [To be supplied.] 
        ///  
        public SocketAddress(AddressFamily family, int size) { 
            if (size>8)); 
            m_Buffer[1] = unchecked((byte)((int)family   ));
#else 
            m_Buffer[0] = unchecked((byte)((int)family   ));
            m_Buffer[1] = unchecked((byte)((int)family>>8));
#endif
        } 
        //
        // For ReceiveFrom we need to pin address size, using reserved m_Buffer space 
        // 
        internal void CopyAddressSizeIntoBuffer()
        { 
            m_Buffer[m_Buffer.Length-IntPtr.Size]   = unchecked((byte)(m_Size));
            m_Buffer[m_Buffer.Length-IntPtr.Size+1] = unchecked((byte)(m_Size >> 8));
            m_Buffer[m_Buffer.Length-IntPtr.Size+2] = unchecked((byte)(m_Size >> 16));
            m_Buffer[m_Buffer.Length-IntPtr.Size+3] = unchecked((byte)(m_Size >> 24)); 
        }
        // 
        // Can be called after the above method did work 
        //
        internal int GetAddressSizeOffset() 
        {
            return m_Buffer.Length-IntPtr.Size;
        }
        // 
        //
        // For ReceiveFrom we need to update the address size upon IO return 
        // 
        internal unsafe void SetSize(IntPtr ptr)
        { 
            // Apparently it must be less or equal the original value since ReceiveFrom cannot reallocate the address buffer
            m_Size = *(int*)ptr;
        }
        public override bool Equals(object comparand) { 
            SocketAddress castedComparand = comparand as SocketAddress;
            if (castedComparand == null || this.Size != castedComparand.Size) { 
                return false; 
            }
            for(int i=0; iWriteableOffset) { 
                    bytes.Append(",");
                } 
                bytes.Append(this[i].ToString(NumberFormatInfo.InvariantInfo)); 
            }
            return Family.ToString() + ":" + Size.ToString(NumberFormatInfo.InvariantInfo) + ":{" + bytes.ToString() + "}"; 
        }
    } // class SocketAddress
 
} // namespace System.Net 
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
//  
//----------------------------------------------------------------------------- 
namespace System.Net { 
 
    using System;
    using System.Runtime.InteropServices; 
    using System.Net.Sockets;
    using System.Text;
    using System.Globalization;
 
    // a little perf app measured these times when comparing the internal
    // buffer implemented as a managed byte[] or unmanaged memory IntPtr 
    // that's why we use byte[] 
    // byte[] total ms:19656
    // IntPtr total ms:25671 
    /// 
    ///    
    ///       This class is used when subclassing EndPoint, and provides indication 
    ///       on how to format the memeory buffers that winsock uses for network addresses.
    ///      
    ///   
    public class SocketAddress {
 
        internal const int IPv6AddressSize = 28;
        internal const int IPv4AddressSize = 16;
        internal int m_Size; 
        internal byte[] m_Buffer;
 
        private const int WriteableOffset = 2; 
        private const int MaxSize = 32; // IrDA requires 32 bytes
        private bool m_changed = true; 
        private int m_hash;
        //
        // Address Family 
        //
        ///  
        ///    [To be supplied.]  
        ///  
        public AddressFamily Family { 
            get {
                int family;
#if BIGENDIAN
                family = ((int)m_Buffer[0]<<8) | m_Buffer[1]; 
#else
                family = m_Buffer[0] | ((int)m_Buffer[1]<<8); 
#endif 
                return (AddressFamily)family;
            } 
        }
        //
        // Size of this SocketAddress
        // 
        /// 
        ///    [To be supplied.]  
        ///   
        public int Size {
            get { 
                return m_Size;
            }
        }
 
        //
        // access to unmanaged serialized data. this doesn't 
        // allow access to the first 2 bytes of unmanaged memory 
        // that are supposed to contain the address family which
        // is readonly. 
        //
        //  you can still use negative offsets as a back door in case
        // winsock changes the way it uses SOCKADDR. maybe we want to prohibit it?
        // maybe we should make the class sealed to avoid potentially dangerous calls 
        // into winsock with unproperly formatted data?  
        // 
        ///  
        ///    [To be supplied.] 
        ///   
        public byte this[int offset] {
            get {
                //
                // access 
                //
                if (offset<0 || offset>=Size) { 
                    throw new IndexOutOfRangeException(); 
                }
                return m_Buffer[offset]; 
            }
            set {
                if (offset<0 || offset>=Size) {
                    throw new IndexOutOfRangeException(); 
                }
                if (m_Buffer[offset] != value) { 
                    m_changed = true; 
                }
                m_Buffer[offset] = value; 
            }
        }
        ///  
        ///    [To be supplied.] 
        ///   
        public SocketAddress(AddressFamily family) : this(family, MaxSize) { 
        }
 
        /// 
        ///    [To be supplied.] 
        ///  
        public SocketAddress(AddressFamily family, int size) { 
            if (size>8)); 
            m_Buffer[1] = unchecked((byte)((int)family   ));
#else 
            m_Buffer[0] = unchecked((byte)((int)family   ));
            m_Buffer[1] = unchecked((byte)((int)family>>8));
#endif
        } 
        //
        // For ReceiveFrom we need to pin address size, using reserved m_Buffer space 
        // 
        internal void CopyAddressSizeIntoBuffer()
        { 
            m_Buffer[m_Buffer.Length-IntPtr.Size]   = unchecked((byte)(m_Size));
            m_Buffer[m_Buffer.Length-IntPtr.Size+1] = unchecked((byte)(m_Size >> 8));
            m_Buffer[m_Buffer.Length-IntPtr.Size+2] = unchecked((byte)(m_Size >> 16));
            m_Buffer[m_Buffer.Length-IntPtr.Size+3] = unchecked((byte)(m_Size >> 24)); 
        }
        // 
        // Can be called after the above method did work 
        //
        internal int GetAddressSizeOffset() 
        {
            return m_Buffer.Length-IntPtr.Size;
        }
        // 
        //
        // For ReceiveFrom we need to update the address size upon IO return 
        // 
        internal unsafe void SetSize(IntPtr ptr)
        { 
            // Apparently it must be less or equal the original value since ReceiveFrom cannot reallocate the address buffer
            m_Size = *(int*)ptr;
        }
        public override bool Equals(object comparand) { 
            SocketAddress castedComparand = comparand as SocketAddress;
            if (castedComparand == null || this.Size != castedComparand.Size) { 
                return false; 
            }
            for(int i=0; iWriteableOffset) { 
                    bytes.Append(",");
                } 
                bytes.Append(this[i].ToString(NumberFormatInfo.InvariantInfo)); 
            }
            return Family.ToString() + ":" + Size.ToString(NumberFormatInfo.InvariantInfo) + ":{" + bytes.ToString() + "}"; 
        }
    } // class SocketAddress
 
} // namespace System.Net 
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                            
                        
                        
                        
                    Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WebPartVerbCollection.cs
 - SqlUtil.cs
 - LoginNameDesigner.cs
 - ListViewItemSelectionChangedEvent.cs
 - QueryResponse.cs
 - RuleInfoComparer.cs
 - Viewport3DAutomationPeer.cs
 - SqlProviderServices.cs
 - TabPanel.cs
 - Avt.cs
 - SubclassTypeValidator.cs
 - Tuple.cs
 - ScrollBarAutomationPeer.cs
 - DocumentSequenceHighlightLayer.cs
 - ObjectSecurity.cs
 - StyleCollection.cs
 - HttpModuleCollection.cs
 - ProfileParameter.cs
 - KeyTimeConverter.cs
 - TextServicesCompartmentEventSink.cs
 - ping.cs
 - VisualBasicExpressionConverter.cs
 - SemanticValue.cs
 - XmlLangPropertyAttribute.cs
 - ILGenerator.cs
 - Collection.cs
 - MappingException.cs
 - DataContext.cs
 - CreateUserWizard.cs
 - HttpListenerContext.cs
 - FontFamily.cs
 - GreaterThan.cs
 - FacetDescription.cs
 - SurrogateEncoder.cs
 - SHA256Cng.cs
 - Itemizer.cs
 - TreeNodeCollection.cs
 - DateTimeOffsetConverter.cs
 - HtmlInputCheckBox.cs
 - ChannelSinkStacks.cs
 - DragCompletedEventArgs.cs
 - XPathDocumentNavigator.cs
 - HashCodeCombiner.cs
 - IntranetCredentialPolicy.cs
 - FileUpload.cs
 - ObjectListCommand.cs
 - BitVector32.cs
 - MediaSystem.cs
 - TemplateParser.cs
 - X509CertificateCollection.cs
 - PathNode.cs
 - ColorConvertedBitmap.cs
 - DataGridViewUtilities.cs
 - XmlILIndex.cs
 - PhysicalAddress.cs
 - _HeaderInfoTable.cs
 - ReadWriteSpinLock.cs
 - XmlDigitalSignatureProcessor.cs
 - EpmContentSerializerBase.cs
 - ServiceDesigner.cs
 - ResourceContainer.cs
 - DeploymentExceptionMapper.cs
 - SoapWriter.cs
 - XAMLParseException.cs
 - StaticDataManager.cs
 - HGlobalSafeHandle.cs
 - SocketElement.cs
 - CriticalHandle.cs
 - DateTimeEditor.cs
 - WmlTextBoxAdapter.cs
 - FlowDocumentReaderAutomationPeer.cs
 - DivideByZeroException.cs
 - WinInetCache.cs
 - Process.cs
 - ReachDocumentSequenceSerializerAsync.cs
 - UInt16.cs
 - RegexStringValidator.cs
 - AssemblyGen.cs
 - XsdBuildProvider.cs
 - SectionInformation.cs
 - DateTimeConverter2.cs
 - IdleTimeoutMonitor.cs
 - TrackingProfile.cs
 - Selector.cs
 - SafeBitVector32.cs
 - Authorization.cs
 - Debugger.cs
 - Range.cs
 - ObjectDataSourceEventArgs.cs
 - StylusPointProperty.cs
 - XamlRtfConverter.cs
 - ContainerActivationHelper.cs
 - FeatureSupport.cs
 - BulletedList.cs
 - ExecutorLocksHeldException.cs
 - DescendentsWalker.cs
 - mediaeventshelper.cs
 - ButtonRenderer.cs
 - AssemblyBuilderData.cs
 - IntegerValidatorAttribute.cs