[649] | 1 |
|
---|
| 2 | /*!
|
---|
| 3 | @brief DNS名前解決をする関数群
|
---|
| 4 | @author OverTaker
|
---|
| 5 | @date 2008/10/19
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | Namespace System
|
---|
| 10 | Namespace Net
|
---|
| 11 | Namespace Dns
|
---|
| 12 |
|
---|
| 13 | /*!
|
---|
| 14 | @brief ホスト名からIPアドレスを取得します
|
---|
| 15 | @param ホスト名
|
---|
| 16 | @return IPAddressクラスが格納されたリスト
|
---|
| 17 | */
|
---|
| 18 | Function GetHostAddresses(hostname As String) As System.Collections.Generic.List<IPAddress>
|
---|
| 19 | Return GetHostEntry(hostname).IPAddressList
|
---|
| 20 | End Function
|
---|
| 21 |
|
---|
| 22 | /*!
|
---|
| 23 | @brief IPアドレスからホストの情報を取得します
|
---|
| 24 | @param IPアドレスを表すIPAddressクラス
|
---|
| 25 | @return ホスト情報が格納されたIPHostEntryクラス
|
---|
| 26 | */
|
---|
| 27 | Function GetHostEntry(ip As IPAddress) As IPHostEntry
|
---|
| 28 | Return GetHostEntry(ip.ToString())
|
---|
| 29 | End Function
|
---|
| 30 |
|
---|
| 31 | /*!
|
---|
| 32 | @brief ホスト名からホストの情報を取得します
|
---|
| 33 | @param ホスト名
|
---|
| 34 | @return ホスト情報が格納されたIPHostEntryクラス
|
---|
| 35 | */
|
---|
| 36 | Function GetHostEntry(hostname As String) As IPHostEntry
|
---|
| 37 | Return GetHostEntry(hostname, String.Empty)
|
---|
| 38 | End Function
|
---|
| 39 |
|
---|
| 40 | /*!
|
---|
| 41 | @brief ホスト名とポート番号からホストの情報を取得します
|
---|
| 42 | @param ホスト名
|
---|
| 43 | @param ポート番号
|
---|
| 44 | @return ホスト情報が格納されたIPHostEntryクラス
|
---|
| 45 | */
|
---|
| 46 | Function GetHostEntry(hostname As String, port As Word) As IPHostEntry
|
---|
| 47 | Return GetHostEntry(hostname, port.ToString())
|
---|
| 48 | End Function
|
---|
| 49 |
|
---|
| 50 | /*!
|
---|
| 51 | @brief ホスト名とサービス名からホストの情報を取得します
|
---|
| 52 | @param ホスト名
|
---|
| 53 | @param サービス名
|
---|
| 54 | @return ホスト情報が格納されたIPHostEntryクラス
|
---|
| 55 | */
|
---|
| 56 | Function GetHostEntry(hostname As String, service As String) As IPHostEntry
|
---|
| 57 | Dim results As *addrinfo
|
---|
| 58 | If getaddrinfo(ToTCStr(hostname), ToTCStr(service), ByVal 0, results) Then
|
---|
| 59 | Throw New System.Net.Sockets.SocketException(WSAGetLastError(), "Dns.GetHostEntry: Failed to getaddrinfo.")
|
---|
| 60 | End If
|
---|
| 61 | Return New Detail.IPHostEntryAddrinfo(results)
|
---|
| 62 | End Function
|
---|
| 63 |
|
---|
| 64 | /*!
|
---|
| 65 | @brief 自身のコンピュータのホスト名を取得します
|
---|
| 66 | @return ホスト名
|
---|
| 67 | */
|
---|
| 68 | Function GetHostName() As String
|
---|
| 69 | Return GetHostEntry(String.Empty, "").HostName
|
---|
| 70 | End Function
|
---|
| 71 |
|
---|
| 72 | /*!
|
---|
| 73 | @brief ホスト名からIPアドレスを非同期で取得します
|
---|
| 74 | @param ホスト名
|
---|
| 75 | @param コールバック
|
---|
| 76 | @param ユーザー情報
|
---|
| 77 | @return 非同期の操作をするIAsyncResultインターフェース
|
---|
| 78 | */
|
---|
| 79 | Function BeginGetHostAddresses(hostname As String, callback As AsyncCallback, state As Object) As IAsyncResult
|
---|
| 80 | Return BeginGetHostEntry(hostname, callback, state)
|
---|
| 81 | End Function
|
---|
| 82 |
|
---|
| 83 | /*!
|
---|
| 84 | @brief ホスト名からホストの情報を取得します
|
---|
| 85 | @param ホスト名
|
---|
| 86 | @param コールバック
|
---|
| 87 | @param ユーザー情報
|
---|
| 88 | @return 非同期の操作をするIAsyncResultインターフェース
|
---|
| 89 | */
|
---|
| 90 | Function BeginGetHostEntry(hostname As String, callback As AsyncCallback, state As Object) As IAsyncResult
|
---|
| 91 | Return BeginGetHostEntry(hostname, String.Empty, callback, state)
|
---|
| 92 | End Function
|
---|
| 93 |
|
---|
| 94 | /*!
|
---|
| 95 | @brief ホスト名とサービス名からホストの情報を取得します
|
---|
| 96 | @param ホスト名
|
---|
| 97 | @param サービス名
|
---|
| 98 | @param コールバック
|
---|
| 99 | @param ユーザー情報
|
---|
| 100 | @return 非同期の操作をするIAsyncResultインターフェース
|
---|
| 101 | */
|
---|
| 102 | Function BeginGetHostEntry(hostname As String, service As String, callback As AsyncCallback, state As Object) As IAsyncResult
|
---|
| 103 | Dim data As Detail.AsyncGetHostEntryData
|
---|
| 104 | data.Hostname = hostname
|
---|
| 105 | data.Service = service
|
---|
| 106 | data.Callback = callback
|
---|
| 107 |
|
---|
| 108 | Dim result = New Detail.AsyncGetHostEntryResult(state)
|
---|
| 109 | data.Result = result
|
---|
| 110 | Threading.ThreadPool.QueueUserWorkItem(AddressOf(Detail.AsyncGetHostEntry), data)
|
---|
| 111 | Return result
|
---|
| 112 | End Function
|
---|
| 113 |
|
---|
| 114 | /*!
|
---|
| 115 | @brief BeginGetHostAddressesの結果を待ち、取得します
|
---|
| 116 | @param BeginGetHostAddressesで取得したIAsyncResultインターフェース
|
---|
| 117 | @return IPAddressクラスが格納されたリスト
|
---|
| 118 | @note この関数は、結果が取得されるまでブロッキングが発生します
|
---|
| 119 | */
|
---|
| 120 | Function EndGetHostAddresses(ar As IAsyncResult) As System.Collections.Generic.List<IPAddress>
|
---|
| 121 | Dim result = ar As Detail.IAsyncGetResult
|
---|
| 122 | Return result.WaitAndGetResult().IPAddressList
|
---|
| 123 | End Function
|
---|
| 124 |
|
---|
| 125 | /*!
|
---|
| 126 | @brief BeginGetHostEntryの結果を待ち、取得します
|
---|
| 127 | @param BeginGetHostEntryで取得したIAsyncResultインターフェース
|
---|
| 128 | @return ホスト情報が格納されたIPHostEntryクラス
|
---|
| 129 | @note この関数は、結果が取得されるまでブロッキングが発生します
|
---|
| 130 | */
|
---|
| 131 | Function EndGetHostEntry(ar As IAsyncResult) As IPHostEntry
|
---|
| 132 | Dim result = ar As Detail.IAsyncGetResult
|
---|
| 133 | Return result.WaitAndGetResult()
|
---|
| 134 | End Function
|
---|
| 135 |
|
---|
| 136 | Namespace Detail
|
---|
| 137 |
|
---|
| 138 | Sub AsyncGetHostEntry(o As Object)
|
---|
| 139 | Dim data = o As AsyncGetHostEntryData
|
---|
| 140 | Dim results As *addrinfo
|
---|
| 141 | If getaddrinfo(ToTCStr(data.Hostname), ToTCStr(data.Service), ByVal 0, results) Then
|
---|
| 142 | data.Result.SetException(New ActiveBasic.Windows.WindowsException(WSAGetLastError(), "Dns.GetHostEntry: Failed to getaddrinfo."))
|
---|
| 143 | End If
|
---|
| 144 | data.Result.Result = New Detail.IPHostEntryAddrinfo(results)
|
---|
| 145 |
|
---|
| 146 | data.Result.AsyncEventWaitHandle.Set()
|
---|
| 147 | If Not ActiveBasic.IsNothing(data.Callback) Then
|
---|
| 148 | Dim c = data.Callback
|
---|
| 149 | c(data.Result)
|
---|
| 150 | End If
|
---|
| 151 | End Sub
|
---|
| 152 |
|
---|
| 153 | Class AsyncGetHostEntryData
|
---|
| 154 | hostname As String
|
---|
| 155 | service As String
|
---|
| 156 | Public
|
---|
| 157 | Callback As AsyncCallback
|
---|
| 158 | Result As AsyncGetHostEntryResult
|
---|
| 159 |
|
---|
| 160 | Function Hostname() As String
|
---|
| 161 | Return hostname
|
---|
| 162 | End Function
|
---|
| 163 |
|
---|
| 164 | Sub Hostname(name As String)
|
---|
| 165 | If Not String.IsNullOrEmpty(name) Then hostname = String.Copy(name)
|
---|
| 166 | End Sub
|
---|
| 167 |
|
---|
| 168 | Function Service() As String
|
---|
| 169 | Return service
|
---|
| 170 | End Function
|
---|
| 171 |
|
---|
| 172 | Sub Service(serv As String)
|
---|
| 173 | If Not String.IsNullOrEmpty(serv) Then service = String.Copy(serv)
|
---|
| 174 | End Sub
|
---|
| 175 | End Class
|
---|
| 176 |
|
---|
| 177 | Interface IAsyncGetResult
|
---|
| 178 | Inherits IAsyncResult
|
---|
| 179 | Function WaitAndGetResult() As IPHostEntry
|
---|
| 180 | End Interface
|
---|
| 181 |
|
---|
| 182 | Class AsyncGetHostEntryResult
|
---|
| 183 | Implements IAsyncGetResult
|
---|
| 184 | Public
|
---|
| 185 | Sub AsyncGetHostEntryResult(s As Object)
|
---|
| 186 | state = s
|
---|
| 187 | wait = New Threading.EventWaitHandle(False, System.Threading.EventResetMode.ManualReset)
|
---|
| 188 | End Sub
|
---|
| 189 |
|
---|
| 190 | Function AsyncState() As Object
|
---|
| 191 | Return state
|
---|
| 192 | End Function
|
---|
| 193 |
|
---|
| 194 | Function AsyncWaitHandle() As Threading.WaitHandle
|
---|
| 195 | Return wait
|
---|
| 196 | End Function
|
---|
| 197 |
|
---|
| 198 | Function AsyncEventWaitHandle() As Threading.EventWaitHandle
|
---|
| 199 | AsyncEventWaitHandle = wait
|
---|
| 200 | End Function
|
---|
| 201 |
|
---|
| 202 | Function CompletedSynchronously() As Boolean
|
---|
| 203 | Return False
|
---|
| 204 | End Function
|
---|
| 205 |
|
---|
| 206 | Function IsCompleted() As Boolean
|
---|
| 207 | Return AsyncWaitHandle.WaitOne(0)
|
---|
| 208 | End Function
|
---|
| 209 |
|
---|
| 210 | Override Function WaitAndGetResult() As IPHostEntry
|
---|
| 211 | AsyncWaitHandle.WaitOne()
|
---|
| 212 | If Not ActiveBasic.IsNothing(exception) Then
|
---|
| 213 | Throw exception
|
---|
| 214 | End If
|
---|
| 215 | Return Result
|
---|
| 216 | End Function
|
---|
| 217 |
|
---|
| 218 | Sub SetException(ex As Exception)
|
---|
| 219 | exception = ex
|
---|
| 220 | End Sub
|
---|
| 221 |
|
---|
| 222 | Result = Nothing As IPHostEntry
|
---|
| 223 | Private
|
---|
| 224 | state As Object
|
---|
| 225 | wait As Threading.EventWaitHandle
|
---|
| 226 | exception As Exception
|
---|
| 227 | End Class
|
---|
| 228 |
|
---|
| 229 | End Namespace
|
---|
| 230 |
|
---|
| 231 | End Namespace
|
---|
| 232 | End Namespace
|
---|
| 233 | End Namespace
|
---|