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