| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:_mojo/application.dart'; | 5 import 'dart:_mojo/application.dart'; |
| 6 import 'dart:_mojo/bindings.dart'; | 6 import 'dart:_mojo/bindings.dart'; |
| 7 import 'dart:_mojo/core.dart'; | 7 import 'dart:_mojo/core.dart'; |
| 8 import 'dart:_mojo/mojo/network_error.mojom.dart'; | 8 import 'dart:_mojo/mojo/network_error.mojom.dart'; |
| 9 import 'dart:_mojo_services/mojo/host_resolver.mojom.dart'; | 9 import 'dart:_mojo_services/mojo/host_resolver.mojom.dart'; |
| 10 import 'dart:_mojo_services/mojo/net_address.mojom.dart'; | 10 import 'dart:_mojo_services/mojo/net_address.mojom.dart'; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 } | 107 } |
| 108 return r; | 108 return r; |
| 109 } | 109 } |
| 110 | 110 |
| 111 /// Convert from [NetAddress] to [InternetAddress]. | 111 /// Convert from [NetAddress] to [InternetAddress]. |
| 112 static InternetAddress _fromNetAddress(NetAddress netAddress) { | 112 static InternetAddress _fromNetAddress(NetAddress netAddress) { |
| 113 if (netAddress == null) { | 113 if (netAddress == null) { |
| 114 return null; | 114 return null; |
| 115 } | 115 } |
| 116 var address; | 116 var address; |
| 117 if (netAddress.family == NetAddressFamily.ipV4) { | 117 if (netAddress.family == NetAddressFamily.ipv4) { |
| 118 address = netAddress.ipv4.addr; | 118 address = netAddress.ipv4.addr; |
| 119 } else if (netAddress.family == NetAddressFamily.ipV6) { | 119 } else if (netAddress.family == NetAddressFamily.ipv6) { |
| 120 address = netAddress.ipv6.addr; | 120 address = netAddress.ipv6.addr; |
| 121 } else { | 121 } else { |
| 122 return null; | 122 return null; |
| 123 } | 123 } |
| 124 assert(address != null); | 124 assert(address != null); |
| 125 var addressString = _addressToString(address); | 125 var addressString = _addressToString(address); |
| 126 return new InternetAddress(addressString); | 126 return new InternetAddress(addressString); |
| 127 } | 127 } |
| 128 | 128 |
| 129 static int _portFromNetAddress(NetAddress netAddress) { | 129 static int _portFromNetAddress(NetAddress netAddress) { |
| 130 if (netAddress == null) { | 130 if (netAddress == null) { |
| 131 return null; | 131 return null; |
| 132 } | 132 } |
| 133 if (netAddress.family == NetAddressFamily.ipV4) { | 133 if (netAddress.family == NetAddressFamily.ipv4) { |
| 134 return netAddress.ipv4.port; | 134 return netAddress.ipv4.port; |
| 135 } else if (netAddress.family == NetAddressFamily.ipV6) { | 135 } else if (netAddress.family == NetAddressFamily.ipv6) { |
| 136 return netAddress.ipv6.port; | 136 return netAddress.ipv6.port; |
| 137 } else { | 137 } else { |
| 138 return null; | 138 return null; |
| 139 } | 139 } |
| 140 } | 140 } |
| 141 | 141 |
| 142 /// Convert from [InternetAddress] to [NetAddress]. | 142 /// Convert from [InternetAddress] to [NetAddress]. |
| 143 static NetAddress _fromInternetAddress(InternetAddress internetAddress, | 143 static NetAddress _fromInternetAddress(InternetAddress internetAddress, |
| 144 [port]) { | 144 [port]) { |
| 145 if (internetAddress == null) { | 145 if (internetAddress == null) { |
| 146 return null; | 146 return null; |
| 147 } | 147 } |
| 148 var netAddress = new NetAddress(); | 148 var netAddress = new NetAddress(); |
| 149 var rawAddress = internetAddress.rawAddress; | 149 var rawAddress = internetAddress.rawAddress; |
| 150 if (rawAddress.length == 4) { | 150 if (rawAddress.length == 4) { |
| 151 netAddress.family = NetAddressFamily.ipV4; | 151 netAddress.family = NetAddressFamily.ipv4; |
| 152 netAddress.ipv4 = new NetAddressIPv4(); | 152 netAddress.ipv4 = new NetAddressIPv4(); |
| 153 netAddress.ipv4.addr = new List.from(rawAddress, growable: false); | 153 netAddress.ipv4.addr = new List.from(rawAddress, growable: false); |
| 154 if (port != null) { | 154 if (port != null) { |
| 155 netAddress.ipv4.port = port; | 155 netAddress.ipv4.port = port; |
| 156 } | 156 } |
| 157 } else { | 157 } else { |
| 158 assert(rawAddress.length == 16); | 158 assert(rawAddress.length == 16); |
| 159 netAddress.family = NetAddressFamily.ipV6; | 159 netAddress.family = NetAddressFamily.ipv6; |
| 160 netAddress.ipv6 = new NetAddressIPv6(); | 160 netAddress.ipv6 = new NetAddressIPv6(); |
| 161 netAddress.ipv6.addr = new List.from(rawAddress, growable: false); | 161 netAddress.ipv6.addr = new List.from(rawAddress, growable: false); |
| 162 if (port != null) { | 162 if (port != null) { |
| 163 netAddress.ipv6.port = port; | 163 netAddress.ipv6.port = port; |
| 164 } | 164 } |
| 165 } | 165 } |
| 166 return netAddress; | 166 return netAddress; |
| 167 } | 167 } |
| 168 } | 168 } |
| 169 | 169 |
| 170 /// Static utility methods for dealing with 'mojo:network_service'. | 170 /// Static utility methods for dealing with 'mojo:network_service'. |
| 171 class _NetworkService { | 171 class _NetworkService { |
| 172 /// Return a [NetAddress] for localhost:port. | 172 /// Return a [NetAddress] for localhost:port. |
| 173 static NetAddress _localhostIpv4([int port = 0]) { | 173 static NetAddress _localhostIpv4([int port = 0]) { |
| 174 var addr = new NetAddress(); | 174 var addr = new NetAddress(); |
| 175 addr.family = NetAddressFamily.ipV4; | 175 addr.family = NetAddressFamily.ipv4; |
| 176 addr.ipv4 = new NetAddressIPv4(); | 176 addr.ipv4 = new NetAddressIPv4(); |
| 177 addr.ipv4.addr = [127, 0, 0, 1]; | 177 addr.ipv4.addr = [127, 0, 0, 1]; |
| 178 addr.ipv4.port = port; | 178 addr.ipv4.port = port; |
| 179 return addr; | 179 return addr; |
| 180 } | 180 } |
| 181 | 181 |
| 182 /// Return a [NetAddress] for localhost:port. | 182 /// Return a [NetAddress] for localhost:port. |
| 183 static NetAddress _localHostIpv6([int port = 0]) { | 183 static NetAddress _localHostIpv6([int port = 0]) { |
| 184 var addr = new NetAddress(); | 184 var addr = new NetAddress(); |
| 185 addr.family = NetAddressFamily.ipV6; | 185 addr.family = NetAddressFamily.ipv6; |
| 186 addr.ipv6 = new NetAddressIPv6(); | 186 addr.ipv6 = new NetAddressIPv6(); |
| 187 addr.ipv6.addr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; | 187 addr.ipv6.addr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; |
| 188 addr.ipv6.port = port; | 188 addr.ipv6.port = port; |
| 189 return addr; | 189 return addr; |
| 190 } | 190 } |
| 191 | 191 |
| 192 static bool _okay(NetworkError error) { | 192 static bool _okay(NetworkError error) { |
| 193 if (error == null) { | 193 if (error == null) { |
| 194 return true; | 194 return true; |
| 195 } | 195 } |
| 196 return error.code == 0; | 196 return error.code == 0; |
| 197 } | 197 } |
| 198 | 198 |
| 199 static _throwOnError(NetworkError error) { | 199 static _throwOnError(NetworkError error) { |
| 200 if (_okay(error)) { | 200 if (_okay(error)) { |
| 201 return; | 201 return; |
| 202 } | 202 } |
| 203 throw new OSError(error.description, error.code); | 203 throw new OSError(error.description, error.code); |
| 204 } | 204 } |
| 205 } | 205 } |
| 206 | 206 |
| OLD | NEW |