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