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