1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gio.NetworkAddress; 26 27 private import gio.SocketConnectable; 28 private import gio.SocketConnectableIF; 29 private import gio.SocketConnectableT; 30 private import glib.ConstructionException; 31 private import glib.ErrorG; 32 private import glib.GException; 33 private import glib.Str; 34 private import gobject.ObjectG; 35 private import gtkc.gio; 36 public import gtkc.giotypes; 37 38 39 /** 40 * #GNetworkAddress provides an easy way to resolve a hostname and 41 * then attempt to connect to that host, handling the possibility of 42 * multiple IP addresses and multiple address families. 43 * 44 * See #GSocketConnectable for and example of using the connectable 45 * interface. 46 */ 47 public class NetworkAddress : ObjectG, SocketConnectableIF 48 { 49 /** the main Gtk struct */ 50 protected GNetworkAddress* gNetworkAddress; 51 52 /** Get the main Gtk struct */ 53 public GNetworkAddress* getNetworkAddressStruct() 54 { 55 return gNetworkAddress; 56 } 57 58 /** the main Gtk struct as a void* */ 59 protected override void* getStruct() 60 { 61 return cast(void*)gNetworkAddress; 62 } 63 64 protected override void setStruct(GObject* obj) 65 { 66 gNetworkAddress = cast(GNetworkAddress*)obj; 67 super.setStruct(obj); 68 } 69 70 /** 71 * Sets our main struct and passes it to the parent class. 72 */ 73 public this (GNetworkAddress* gNetworkAddress, bool ownedRef = false) 74 { 75 this.gNetworkAddress = gNetworkAddress; 76 super(cast(GObject*)gNetworkAddress, ownedRef); 77 } 78 79 // add the SocketConnectable capabilities 80 mixin SocketConnectableT!(GNetworkAddress); 81 82 /** 83 */ 84 85 public static GType getType() 86 { 87 return g_network_address_get_type(); 88 } 89 90 /** 91 * Creates a new #GSocketConnectable for connecting to the given 92 * @hostname and @port. 93 * 94 * Params: 95 * hostname = the hostname 96 * port = the port 97 * 98 * Return: the new #GNetworkAddress 99 * 100 * Since: 2.22 101 * 102 * Throws: ConstructionException GTK+ fails to create the object. 103 */ 104 public this(string hostname, ushort port) 105 { 106 auto p = g_network_address_new(Str.toStringz(hostname), port); 107 108 if(p is null) 109 { 110 throw new ConstructionException("null returned by new"); 111 } 112 113 this(cast(GNetworkAddress*) p, true); 114 } 115 116 /** 117 * Creates a new #GSocketConnectable for connecting to the given 118 * @hostname and @port. May fail and return %NULL in case 119 * parsing @host_and_port fails. 120 * 121 * @host_and_port may be in any of a number of recognised formats; an IPv6 122 * address, an IPv4 address, or a domain name (in which case a DNS 123 * lookup is performed). Quoting with [] is supported for all address 124 * types. A port override may be specified in the usual way with a 125 * colon. 126 * 127 * If no port is specified in @host_and_port then @default_port will be 128 * used as the port number to connect to. 129 * 130 * In general, @host_and_port is expected to be provided by the user 131 * (allowing them to give the hostname, and a port overide if necessary) 132 * and @default_port is expected to be provided by the application. 133 * 134 * (The port component of @host_and_port can also be specified as a 135 * service name rather than as a numeric port, but this functionality 136 * is deprecated, because it depends on the contents of /etc/services, 137 * which is generally quite sparse on platforms other than Linux.) 138 * 139 * Params: 140 * hostAndPort = the hostname and optionally a port 141 * defaultPort = the default port if not in @host_and_port 142 * 143 * Return: the new #GNetworkAddress, or %NULL on error 144 * 145 * Since: 2.22 146 * 147 * Throws: GException on failure. 148 */ 149 public static SocketConnectableIF parse(string hostAndPort, ushort defaultPort) 150 { 151 GError* err = null; 152 153 auto p = g_network_address_parse(Str.toStringz(hostAndPort), defaultPort, &err); 154 155 if (err !is null) 156 { 157 throw new GException( new ErrorG(err) ); 158 } 159 160 if(p is null) 161 { 162 return null; 163 } 164 165 return ObjectG.getDObject!(SocketConnectable, SocketConnectableIF)(cast(GSocketConnectable*) p); 166 } 167 168 /** 169 * Creates a new #GSocketConnectable for connecting to the given 170 * @uri. May fail and return %NULL in case parsing @uri fails. 171 * 172 * Using this rather than g_network_address_new() or 173 * g_network_address_parse() allows #GSocketClient to determine 174 * when to use application-specific proxy protocols. 175 * 176 * Params: 177 * uri = the hostname and optionally a port 178 * defaultPort = The default port if none is found in the URI 179 * 180 * Return: the new #GNetworkAddress, or %NULL on error 181 * 182 * Since: 2.26 183 * 184 * Throws: GException on failure. 185 */ 186 public static SocketConnectableIF parseUri(string uri, ushort defaultPort) 187 { 188 GError* err = null; 189 190 auto p = g_network_address_parse_uri(Str.toStringz(uri), defaultPort, &err); 191 192 if (err !is null) 193 { 194 throw new GException( new ErrorG(err) ); 195 } 196 197 if(p is null) 198 { 199 return null; 200 } 201 202 return ObjectG.getDObject!(SocketConnectable, SocketConnectableIF)(cast(GSocketConnectable*) p); 203 } 204 205 /** 206 * Gets @addr's hostname. This might be either UTF-8 or ASCII-encoded, 207 * depending on what @addr was created with. 208 * 209 * Return: @addr's hostname 210 * 211 * Since: 2.22 212 */ 213 public string getHostname() 214 { 215 return Str.toString(g_network_address_get_hostname(gNetworkAddress)); 216 } 217 218 /** 219 * Gets @addr's port number 220 * 221 * Return: @addr's port (which may be 0) 222 * 223 * Since: 2.22 224 */ 225 public ushort getPort() 226 { 227 return g_network_address_get_port(gNetworkAddress); 228 } 229 230 /** 231 * Gets @addr's scheme 232 * 233 * Return: @addr's scheme (%NULL if not built from URI) 234 * 235 * Since: 2.26 236 */ 237 public string getScheme() 238 { 239 return Str.toString(g_network_address_get_scheme(gNetworkAddress)); 240 } 241 }