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