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 	/**
80 	 * Sets our main struct and passes it to the parent class.
81 	 */
82 	public this (GSocketConnection* gSocketConnection, bool ownedRef = false)
83 	{
84 		this.gSocketConnection = gSocketConnection;
85 		super(cast(GIOStream*)gSocketConnection, ownedRef);
86 	}
87 
88 
89 	/** */
90 	public static GType getType()
91 	{
92 		return g_socket_connection_get_type();
93 	}
94 
95 	/**
96 	 * Looks up the #GType to be used when creating socket connections on
97 	 * sockets with the specified @family, @type and @protocol_id.
98 	 *
99 	 * If no type is registered, the #GSocketConnection base type is returned.
100 	 *
101 	 * Params:
102 	 *     family = a #GSocketFamily
103 	 *     type = a #GSocketType
104 	 *     protocolId = a protocol id
105 	 *
106 	 * Returns: a #GType
107 	 *
108 	 * Since: 2.22
109 	 */
110 	public static GType factoryLookupType(GSocketFamily family, GSocketType type, int protocolId)
111 	{
112 		return g_socket_connection_factory_lookup_type(family, type, protocolId);
113 	}
114 
115 	/**
116 	 * Looks up the #GType to be used when creating socket connections on
117 	 * sockets with the specified @family, @type and @protocol.
118 	 *
119 	 * If no type is registered, the #GSocketConnection base type is returned.
120 	 *
121 	 * Params:
122 	 *     gType = a #GType, inheriting from %G_TYPE_SOCKET_CONNECTION
123 	 *     family = a #GSocketFamily
124 	 *     type = a #GSocketType
125 	 *     protocol = a protocol id
126 	 *
127 	 * Since: 2.22
128 	 */
129 	public static void factoryRegisterType(GType gType, GSocketFamily family, GSocketType type, int protocol)
130 	{
131 		g_socket_connection_factory_register_type(gType, family, type, protocol);
132 	}
133 
134 	/**
135 	 * Connect @connection to the specified remote address.
136 	 *
137 	 * Params:
138 	 *     address = a #GSocketAddress specifying the remote address.
139 	 *     cancellable = a %GCancellable or %NULL
140 	 *
141 	 * Returns: %TRUE if the connection succeeded, %FALSE on error
142 	 *
143 	 * Since: 2.32
144 	 *
145 	 * Throws: GException on failure.
146 	 */
147 	public bool connect(SocketAddress address, Cancellable cancellable)
148 	{
149 		GError* err = null;
150 
151 		auto __p = g_socket_connection_connect(gSocketConnection, (address is null) ? null : address.getSocketAddressStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
152 
153 		if (err !is null)
154 		{
155 			throw new GException( new ErrorG(err) );
156 		}
157 
158 		return __p;
159 	}
160 
161 	/**
162 	 * Asynchronously connect @connection to the specified remote address.
163 	 *
164 	 * This clears the #GSocket:blocking flag on @connection's underlying
165 	 * socket if it is currently set.
166 	 *
167 	 * Use g_socket_connection_connect_finish() to retrieve the result.
168 	 *
169 	 * Params:
170 	 *     address = a #GSocketAddress specifying the remote address.
171 	 *     cancellable = a %GCancellable or %NULL
172 	 *     callback = a #GAsyncReadyCallback
173 	 *     userData = user data for the callback
174 	 *
175 	 * Since: 2.32
176 	 */
177 	public void connectAsync(SocketAddress address, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
178 	{
179 		g_socket_connection_connect_async(gSocketConnection, (address is null) ? null : address.getSocketAddressStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
180 	}
181 
182 	/**
183 	 * Gets the result of a g_socket_connection_connect_async() call.
184 	 *
185 	 * Params:
186 	 *     result = the #GAsyncResult
187 	 *
188 	 * Returns: %TRUE if the connection succeeded, %FALSE on error
189 	 *
190 	 * Since: 2.32
191 	 *
192 	 * Throws: GException on failure.
193 	 */
194 	public bool connectFinish(AsyncResultIF result)
195 	{
196 		GError* err = null;
197 
198 		auto __p = g_socket_connection_connect_finish(gSocketConnection, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
199 
200 		if (err !is null)
201 		{
202 			throw new GException( new ErrorG(err) );
203 		}
204 
205 		return __p;
206 	}
207 
208 	/**
209 	 * Try to get the local address of a socket connection.
210 	 *
211 	 * Returns: a #GSocketAddress or %NULL on error.
212 	 *     Free the returned object with g_object_unref().
213 	 *
214 	 * Since: 2.22
215 	 *
216 	 * Throws: GException on failure.
217 	 */
218 	public SocketAddress getLocalAddress()
219 	{
220 		GError* err = null;
221 
222 		auto __p = g_socket_connection_get_local_address(gSocketConnection, &err);
223 
224 		if (err !is null)
225 		{
226 			throw new GException( new ErrorG(err) );
227 		}
228 
229 		if(__p is null)
230 		{
231 			return null;
232 		}
233 
234 		return ObjectG.getDObject!(SocketAddress)(cast(GSocketAddress*) __p, true);
235 	}
236 
237 	/**
238 	 * Try to get the remote address of a socket connection.
239 	 *
240 	 * Since GLib 2.40, when used with g_socket_client_connect() or
241 	 * g_socket_client_connect_async(), during emission of
242 	 * %G_SOCKET_CLIENT_CONNECTING, this function will return the remote
243 	 * address that will be used for the connection.  This allows
244 	 * applications to print e.g. "Connecting to example.com
245 	 * (10.42.77.3)...".
246 	 *
247 	 * Returns: a #GSocketAddress or %NULL on error.
248 	 *     Free the returned object with g_object_unref().
249 	 *
250 	 * Since: 2.22
251 	 *
252 	 * Throws: GException on failure.
253 	 */
254 	public SocketAddress getRemoteAddress()
255 	{
256 		GError* err = null;
257 
258 		auto __p = g_socket_connection_get_remote_address(gSocketConnection, &err);
259 
260 		if (err !is null)
261 		{
262 			throw new GException( new ErrorG(err) );
263 		}
264 
265 		if(__p is null)
266 		{
267 			return null;
268 		}
269 
270 		return ObjectG.getDObject!(SocketAddress)(cast(GSocketAddress*) __p, true);
271 	}
272 
273 	/**
274 	 * Gets the underlying #GSocket object of the connection.
275 	 * This can be useful if you want to do something unusual on it
276 	 * not supported by the #GSocketConnection APIs.
277 	 *
278 	 * Returns: a #GSocket or %NULL on error.
279 	 *
280 	 * Since: 2.22
281 	 */
282 	public Socket getSocket()
283 	{
284 		auto __p = g_socket_connection_get_socket(gSocketConnection);
285 
286 		if(__p is null)
287 		{
288 			return null;
289 		}
290 
291 		return ObjectG.getDObject!(Socket)(cast(GSocket*) __p);
292 	}
293 
294 	/**
295 	 * Checks if @connection is connected. This is equivalent to calling
296 	 * g_socket_is_connected() on @connection's underlying #GSocket.
297 	 *
298 	 * Returns: whether @connection is connected
299 	 *
300 	 * Since: 2.32
301 	 */
302 	public bool isConnected()
303 	{
304 		return g_socket_connection_is_connected(gSocketConnection) != 0;
305 	}
306 }