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.SocketConnection;
26 
27 private import gio.AsyncResultIF;
28 private import gio.Cancellable;
29 private import gio.IOStream;
30 private import gio.Socket;
31 private import gio.SocketAddress;
32 private import gio.c.functions;
33 public  import gio.c.types;
34 private import glib.ErrorG;
35 private import glib.GException;
36 private import gobject.ObjectG;
37 public  import gtkc.giotypes;
38 
39 
40 /**
41  * #GSocketConnection is a #GIOStream for a connected socket. They
42  * can be created either by #GSocketClient when connecting to a host,
43  * or by #GSocketListener when accepting a new client.
44  * 
45  * The type of the #GSocketConnection object returned from these calls
46  * depends on the type of the underlying socket that is in use. For
47  * instance, for a TCP/IP connection it will be a #GTcpConnection.
48  * 
49  * Choosing what type of object to construct is done with the socket
50  * connection factory, and it is possible for 3rd parties to register
51  * custom socket connection types for specific combination of socket
52  * family/type/protocol using g_socket_connection_factory_register_type().
53  * 
54  * To close a #GSocketConnection, use g_io_stream_close(). Closing both
55  * substreams of the #GIOStream separately will not close the underlying
56  * #GSocket.
57  *
58  * Since: 2.22
59  */
60 public class SocketConnection : IOStream
61 {
62 	/** the main Gtk struct */
63 	protected GSocketConnection* gSocketConnection;
64 
65 	/** Get the main Gtk struct */
66 	public GSocketConnection* getSocketConnectionStruct(bool transferOwnership = false)
67 	{
68 		if (transferOwnership)
69 			ownedRef = false;
70 		return gSocketConnection;
71 	}
72 
73 	/** the main Gtk struct as a void* */
74 	protected override void* getStruct()
75 	{
76 		return cast(void*)gSocketConnection;
77 	}
78 
79 	protected override void setStruct(GObject* obj)
80 	{
81 		gSocketConnection = cast(GSocketConnection*)obj;
82 		super.setStruct(obj);
83 	}
84 
85 	/**
86 	 * Sets our main struct and passes it to the parent class.
87 	 */
88 	public this (GSocketConnection* gSocketConnection, bool ownedRef = false)
89 	{
90 		this.gSocketConnection = gSocketConnection;
91 		super(cast(GIOStream*)gSocketConnection, ownedRef);
92 	}
93 
94 
95 	/** */
96 	public static GType getType()
97 	{
98 		return g_socket_connection_get_type();
99 	}
100 
101 	/**
102 	 * Looks up the #GType to be used when creating socket connections on
103 	 * sockets with the specified @family, @type and @protocol_id.
104 	 *
105 	 * If no type is registered, the #GSocketConnection base type is returned.
106 	 *
107 	 * Params:
108 	 *     family = a #GSocketFamily
109 	 *     type = a #GSocketType
110 	 *     protocolId = a protocol id
111 	 *
112 	 * Returns: a #GType
113 	 *
114 	 * Since: 2.22
115 	 */
116 	public static GType factoryLookupType(GSocketFamily family, GSocketType type, int protocolId)
117 	{
118 		return g_socket_connection_factory_lookup_type(family, type, protocolId);
119 	}
120 
121 	/**
122 	 * Looks up the #GType to be used when creating socket connections on
123 	 * sockets with the specified @family, @type and @protocol.
124 	 *
125 	 * If no type is registered, the #GSocketConnection base type is returned.
126 	 *
127 	 * Params:
128 	 *     gType = a #GType, inheriting from %G_TYPE_SOCKET_CONNECTION
129 	 *     family = a #GSocketFamily
130 	 *     type = a #GSocketType
131 	 *     protocol = a protocol id
132 	 *
133 	 * Since: 2.22
134 	 */
135 	public static void factoryRegisterType(GType gType, GSocketFamily family, GSocketType type, int protocol)
136 	{
137 		g_socket_connection_factory_register_type(gType, family, type, protocol);
138 	}
139 
140 	/**
141 	 * Connect @connection to the specified remote address.
142 	 *
143 	 * Params:
144 	 *     address = a #GSocketAddress specifying the remote address.
145 	 *     cancellable = a %GCancellable or %NULL
146 	 *
147 	 * Returns: %TRUE if the connection succeeded, %FALSE on error
148 	 *
149 	 * Since: 2.32
150 	 *
151 	 * Throws: GException on failure.
152 	 */
153 	public bool connect(SocketAddress address, Cancellable cancellable)
154 	{
155 		GError* err = null;
156 
157 		auto p = g_socket_connection_connect(gSocketConnection, (address is null) ? null : address.getSocketAddressStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
158 
159 		if (err !is null)
160 		{
161 			throw new GException( new ErrorG(err) );
162 		}
163 
164 		return p;
165 	}
166 
167 	/**
168 	 * Asynchronously connect @connection to the specified remote address.
169 	 *
170 	 * This clears the #GSocket:blocking flag on @connection's underlying
171 	 * socket if it is currently set.
172 	 *
173 	 * Use g_socket_connection_connect_finish() to retrieve the result.
174 	 *
175 	 * Params:
176 	 *     address = a #GSocketAddress specifying the remote address.
177 	 *     cancellable = a %GCancellable or %NULL
178 	 *     callback = a #GAsyncReadyCallback
179 	 *     userData = user data for the callback
180 	 *
181 	 * Since: 2.32
182 	 */
183 	public void connectAsync(SocketAddress address, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
184 	{
185 		g_socket_connection_connect_async(gSocketConnection, (address is null) ? null : address.getSocketAddressStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
186 	}
187 
188 	/**
189 	 * Gets the result of a g_socket_connection_connect_async() call.
190 	 *
191 	 * Params:
192 	 *     result = the #GAsyncResult
193 	 *
194 	 * Returns: %TRUE if the connection succeeded, %FALSE on error
195 	 *
196 	 * Since: 2.32
197 	 *
198 	 * Throws: GException on failure.
199 	 */
200 	public bool connectFinish(AsyncResultIF result)
201 	{
202 		GError* err = null;
203 
204 		auto p = g_socket_connection_connect_finish(gSocketConnection, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
205 
206 		if (err !is null)
207 		{
208 			throw new GException( new ErrorG(err) );
209 		}
210 
211 		return p;
212 	}
213 
214 	/**
215 	 * Try to get the local address of a socket connection.
216 	 *
217 	 * Returns: a #GSocketAddress or %NULL on error.
218 	 *     Free the returned object with g_object_unref().
219 	 *
220 	 * Since: 2.22
221 	 *
222 	 * Throws: GException on failure.
223 	 */
224 	public SocketAddress getLocalAddress()
225 	{
226 		GError* err = null;
227 
228 		auto p = g_socket_connection_get_local_address(gSocketConnection, &err);
229 
230 		if (err !is null)
231 		{
232 			throw new GException( new ErrorG(err) );
233 		}
234 
235 		if(p is null)
236 		{
237 			return null;
238 		}
239 
240 		return ObjectG.getDObject!(SocketAddress)(cast(GSocketAddress*) p, true);
241 	}
242 
243 	/**
244 	 * Try to get the remote address of a socket connection.
245 	 *
246 	 * Since GLib 2.40, when used with g_socket_client_connect() or
247 	 * g_socket_client_connect_async(), during emission of
248 	 * %G_SOCKET_CLIENT_CONNECTING, this function will return the remote
249 	 * address that will be used for the connection.  This allows
250 	 * applications to print e.g. "Connecting to example.com
251 	 * (10.42.77.3)...".
252 	 *
253 	 * Returns: a #GSocketAddress or %NULL on error.
254 	 *     Free the returned object with g_object_unref().
255 	 *
256 	 * Since: 2.22
257 	 *
258 	 * Throws: GException on failure.
259 	 */
260 	public SocketAddress getRemoteAddress()
261 	{
262 		GError* err = null;
263 
264 		auto p = g_socket_connection_get_remote_address(gSocketConnection, &err);
265 
266 		if (err !is null)
267 		{
268 			throw new GException( new ErrorG(err) );
269 		}
270 
271 		if(p is null)
272 		{
273 			return null;
274 		}
275 
276 		return ObjectG.getDObject!(SocketAddress)(cast(GSocketAddress*) p, true);
277 	}
278 
279 	/**
280 	 * Gets the underlying #GSocket object of the connection.
281 	 * This can be useful if you want to do something unusual on it
282 	 * not supported by the #GSocketConnection APIs.
283 	 *
284 	 * Returns: a #GSocket or %NULL on error.
285 	 *
286 	 * Since: 2.22
287 	 */
288 	public Socket getSocket()
289 	{
290 		auto p = g_socket_connection_get_socket(gSocketConnection);
291 
292 		if(p is null)
293 		{
294 			return null;
295 		}
296 
297 		return ObjectG.getDObject!(Socket)(cast(GSocket*) p);
298 	}
299 
300 	/**
301 	 * Checks if @connection is connected. This is equivalent to calling
302 	 * g_socket_is_connected() on @connection's underlying #GSocket.
303 	 *
304 	 * Returns: whether @connection is connected
305 	 *
306 	 * Since: 2.32
307 	 */
308 	public bool isConnected()
309 	{
310 		return g_socket_connection_is_connected(gSocketConnection) != 0;
311 	}
312 }