1 #Region "Microsoft.VisualBasic::6e47a19b6cb16f9d2375a56a2da21154, Microsoft.VisualBasic.Core\Net\Tcp\IPTools\IPEndPoint.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class IPEndPoint
35     
36     '         Properties: IPAddress, IsValid, Port, uid
37     
38     '         Constructor: (+4 OverloadsSub New
39     '         FunctionGetIPEndPoint, GetValue, ToString
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports System.ComponentModel
47 Imports System.Runtime.CompilerServices
48 Imports System.Xml.Serialization
49
50 Namespace Net
51
52     ''' <summary>
53     ''' The object of <see cref="System.Net.IPEndPoint"/> can not be Xml serialization.
54     ''' (系统自带的<see cref="System.Net.IPEndPoint"></see>不能够进行Xml序列化)
55     ''' </summary>
56     ''' <remarks></remarks>
57     Public Class IPEndPoint
58
59 #Region "Public Property"
60
61         ''' <summary>
62         ''' Guid value of this portal information on the server registry.
63         ''' </summary>
64         ''' <returns></returns>
65         <Browsable(True)>
66         <Description("Guid value of this portal information on the server registry.")>
67         <XmlAttribute> Public Property uid As String
68
69         ''' <summary>
70         ''' IPAddress of the services instance.
71         ''' </summary>
72         ''' <returns></returns>
73         <Browsable(True)>
74         <Description("IPAddress of the services instance.")>
75         <XmlAttribute> Public Property IPAddress As String
76
77         ''' <summary>
78         ''' Data port of the services instance.
79         ''' </summary>
80         ''' <returns></returns>
81         <Browsable(True)>
82         <Description("Data port of the services instance.")>
83         <XmlAttribute> Public Property Port As Integer
84 #End Region
85
86         ''' <summary>
87         ''' This parameterless constructor is required for the xml serialization.(XML序列化所需要的)
88         ''' </summary>
89         ''' <remarks></remarks>
90         Public Sub New()
91         End Sub
92
93         ''' <summary>
94         '''
95         ''' </summary>
96         ''' <param name="IPAddress">IPAddress string using for create object using method <see cref="System.Net.IPAddress.Parse(String)"/></param>
97         ''' <param name="Port"><see cref="System.Net.IPEndPoint.Port"/></param>
98         Sub New(IPAddress As String, Port As Integer)
99             Me.Port = Port
100             Me.IPAddress = IPAddress
101         End Sub
102
103         ''' <summary>
104         '''
105         ''' </summary>
106         ''' <param name="str">Required format string: ``IPAddress:Port``</param>
107         ''' <remarks></remarks>
108         Sub New(str As String)
109             Dim Tokens As String() = str.Split(":"c)
110
111             If Tokens.IsNullOrEmpty OrElse Tokens.Length < 2 Then
112                 Throw New DataException(str & " is not a valid IPEndPoint string value!")
113             End If
114
115             IPAddress = Tokens.First
116             Port = CInt(Val(Tokens(1)))
117         End Sub
118
119         Sub New(ipEnd As System.Net.IPEndPoint)
120             Call Me.New(ipEnd.ToString)
121         End Sub
122
123         ''' <summary>
124         ''' http://IPAddress:&lt;Port>/
125         ''' </summary>
126         ''' <returns></returns>
127         Public Overrides Function ToString() As String
128             Return $"http://{IPAddress}:{Port}/"
129         End Function
130
131         ''' <summary>
132         ''' Convert this networking end point DDM into the <see cref="System.Net.IPEndPoint"/>
133         ''' </summary>
134         ''' <returns></returns>
135         Public Function GetIPEndPoint() As System.Net.IPEndPoint
136             Return New System.Net.IPEndPoint(System.Net.IPAddress.Parse(ipString:=IPAddress), Port)
137         End Function
138
139         Public Function GetValue() As String
140             Return IPAddress & ":" & Port.ToString
141         End Function
142
143         ''' <summary>
144         ''' 格式是否正确
145         ''' </summary>
146         ''' <returns></returns>
147         <SoapIgnore> Public ReadOnly Property IsValid As Boolean
148             <MethodImpl(MethodImplOptions.AggressiveInlining)>
149             Get
150                 Return Port > 0 AndAlso System.Net.IPAddress.TryParse(IPAddress, Nothing)
151             End Get
152         End Property
153
154         Public Shared Narrowing Operator CType(ep As IPEndPoint) As System.Net.IPEndPoint
155             Return ep.GetIPEndPoint
156         End Operator
157     End Class
158 End Namespace