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  * Conversion parameters:
26  * inFile  = GSocketListener.html
27  * outPack = gio
28  * outFile = SocketListener
29  * strct   = GSocketListener
30  * realStrct=
31  * ctorStrct=
32  * clss    = SocketListener
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_socket_listener_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- glib.ErrorG
47  * 	- glib.GException
48  * 	- gio.AsyncResultIF
49  * 	- gio.Cancellable
50  * 	- gio.Socket
51  * 	- gio.SocketAddress
52  * 	- gio.SocketConnection
53  * structWrap:
54  * 	- GAsyncResult* -> AsyncResultIF
55  * 	- GCancellable* -> Cancellable
56  * 	- GObject* -> ObjectG
57  * 	- GSocket* -> Socket
58  * 	- GSocketAddress* -> SocketAddress
59  * 	- GSocketConnection* -> SocketConnection
60  * module aliases:
61  * local aliases:
62  * overrides:
63  */
64 
65 module gio.SocketListener;
66 
67 public  import gtkc.giotypes;
68 
69 private import gtkc.gio;
70 private import glib.ConstructionException;
71 private import gobject.ObjectG;
72 
73 private import glib.ErrorG;
74 private import glib.GException;
75 private import gio.AsyncResultIF;
76 private import gio.Cancellable;
77 private import gio.Socket;
78 private import gio.SocketAddress;
79 private import gio.SocketConnection;
80 
81 
82 private import gobject.ObjectG;
83 
84 /**
85  * A GSocketListener is an object that keeps track of a set
86  * of server sockets and helps you accept sockets from any of the
87  * socket, either sync or async.
88  *
89  * If you want to implement a network server, also look at GSocketService
90  * and GThreadedSocketService which are subclass of GSocketListener
91  * that makes this even easier.
92  */
93 public class SocketListener : ObjectG
94 {
95 	
96 	/** the main Gtk struct */
97 	protected GSocketListener* gSocketListener;
98 	
99 	
100 	/** Get the main Gtk struct */
101 	public GSocketListener* getSocketListenerStruct()
102 	{
103 		return gSocketListener;
104 	}
105 	
106 	
107 	/** the main Gtk struct as a void* */
108 	protected override void* getStruct()
109 	{
110 		return cast(void*)gSocketListener;
111 	}
112 	
113 	/**
114 	 * Sets our main struct and passes it to the parent class
115 	 */
116 	public this (GSocketListener* gSocketListener)
117 	{
118 		super(cast(GObject*)gSocketListener);
119 		this.gSocketListener = gSocketListener;
120 	}
121 	
122 	protected override void setStruct(GObject* obj)
123 	{
124 		super.setStruct(obj);
125 		gSocketListener = cast(GSocketListener*)obj;
126 	}
127 	
128 	/**
129 	 */
130 	
131 	/**
132 	 * Creates a new GSocketListener with no sockets to listen for.
133 	 * New listeners can be added with e.g. g_socket_listener_add_address()
134 	 * or g_socket_listener_add_inet_port().
135 	 * Since 2.22
136 	 * Throws: ConstructionException GTK+ fails to create the object.
137 	 */
138 	public this ()
139 	{
140 		// GSocketListener * g_socket_listener_new (void);
141 		auto p = g_socket_listener_new();
142 		if(p is null)
143 		{
144 			throw new ConstructionException("null returned by g_socket_listener_new()");
145 		}
146 		this(cast(GSocketListener*) p);
147 	}
148 	
149 	/**
150 	 * Adds socket to the set of sockets that we try to accept
151 	 * new clients from. The socket must be bound to a local
152 	 * address and listened to.
153 	 * source_object will be passed out in the various calls
154 	 * to accept to identify this particular source, which is
155 	 * useful if you're listening on multiple addresses and do
156 	 * different things depending on what address is connected to.
157 	 * Since 2.22
158 	 * Params:
159 	 * socket = a listening GSocket
160 	 * sourceObject = Optional GObject identifying this source. [allow-none]
161 	 * Returns: TRUE on success, FALSE on error.
162 	 * Throws: GException on failure.
163 	 */
164 	public int addSocket(Socket socket, ObjectG sourceObject)
165 	{
166 		// gboolean g_socket_listener_add_socket (GSocketListener *listener,  GSocket *socket,  GObject *source_object,  GError **error);
167 		GError* err = null;
168 		
169 		auto p = g_socket_listener_add_socket(gSocketListener, (socket is null) ? null : socket.getSocketStruct(), (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err);
170 		
171 		if (err !is null)
172 		{
173 			throw new GException( new ErrorG(err) );
174 		}
175 		
176 		return p;
177 	}
178 	
179 	/**
180 	 * Creates a socket of type type and protocol protocol, binds
181 	 * it to address and adds it to the set of sockets we're accepting
182 	 * sockets from.
183 	 * Note that adding an IPv6 address, depending on the platform,
184 	 * may or may not result in a listener that also accepts IPv4
185 	 * connections. For more deterministic behavior, see
186 	 * g_socket_listener_add_inet_port().
187 	 * source_object will be passed out in the various calls
188 	 * to accept to identify this particular source, which is
189 	 * useful if you're listening on multiple addresses and do
190 	 * different things depending on what address is connected to.
191 	 * If successful and effective_address is non-NULL then it will
192 	 * be set to the address that the binding actually occurred at. This
193 	 * is helpful for determining the port number that was used for when
194 	 * requesting a binding to port 0 (ie: "any port"). This address, if
195 	 * requested, belongs to the caller and must be freed.
196 	 * Since 2.22
197 	 * Params:
198 	 * address = a GSocketAddress
199 	 * type = a GSocketType
200 	 * protocol = a GSocketProtocol
201 	 * sourceObject = Optional GObject identifying this source. [allow-none]
202 	 * effectiveAddress = location to store the address that was bound to, or NULL. [out][allow-none]
203 	 * Returns: TRUE on success, FALSE on error.
204 	 * Throws: GException on failure.
205 	 */
206 	public int addAddress(SocketAddress address, GSocketType type, GSocketProtocol protocol, ObjectG sourceObject, out SocketAddress effectiveAddress)
207 	{
208 		// gboolean g_socket_listener_add_address (GSocketListener *listener,  GSocketAddress *address,  GSocketType type,  GSocketProtocol protocol,  GObject *source_object,  GSocketAddress **effective_address,  GError **error);
209 		GSocketAddress* outeffectiveAddress = null;
210 		GError* err = null;
211 		
212 		auto p = g_socket_listener_add_address(gSocketListener, (address is null) ? null : address.getSocketAddressStruct(), type, protocol, (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &outeffectiveAddress, &err);
213 		
214 		if (err !is null)
215 		{
216 			throw new GException( new ErrorG(err) );
217 		}
218 		
219 		effectiveAddress = ObjectG.getDObject!(SocketAddress)(outeffectiveAddress);
220 		return p;
221 	}
222 	
223 	/**
224 	 * Helper function for g_socket_listener_add_address() that
225 	 * creates a TCP/IP socket listening on IPv4 and IPv6 (if
226 	 * supported) on the specified port on all interfaces.
227 	 * source_object will be passed out in the various calls
228 	 * to accept to identify this particular source, which is
229 	 * useful if you're listening on multiple addresses and do
230 	 * different things depending on what address is connected to.
231 	 * Since 2.22
232 	 * Params:
233 	 * port = an IP port number (non-zero)
234 	 * sourceObject = Optional GObject identifying this source. [allow-none]
235 	 * Returns: TRUE on success, FALSE on error.
236 	 * Throws: GException on failure.
237 	 */
238 	public int addInetPort(ushort port, ObjectG sourceObject)
239 	{
240 		// gboolean g_socket_listener_add_inet_port (GSocketListener *listener,  guint16 port,  GObject *source_object,  GError **error);
241 		GError* err = null;
242 		
243 		auto p = g_socket_listener_add_inet_port(gSocketListener, port, (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err);
244 		
245 		if (err !is null)
246 		{
247 			throw new GException( new ErrorG(err) );
248 		}
249 		
250 		return p;
251 	}
252 	
253 	/**
254 	 * Listens for TCP connections on any available port number for both
255 	 * IPv6 and IPv4 (if each is available).
256 	 * This is useful if you need to have a socket for incoming connections
257 	 * but don't care about the specific port number.
258 	 * source_object will be passed out in the various calls
259 	 * to accept to identify this particular source, which is
260 	 * useful if you're listening on multiple addresses and do
261 	 * different things depending on what address is connected to.
262 	 * Since 2.24
263 	 * Params:
264 	 * sourceObject = Optional GObject identifying this source. [allow-none]
265 	 * Returns: the port number, or 0 in case of failure.
266 	 * Throws: GException on failure.
267 	 */
268 	public ushort addAnyInetPort(ObjectG sourceObject)
269 	{
270 		// guint16 g_socket_listener_add_any_inet_port (GSocketListener *listener,  GObject *source_object,  GError **error);
271 		GError* err = null;
272 		
273 		auto p = g_socket_listener_add_any_inet_port(gSocketListener, (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err);
274 		
275 		if (err !is null)
276 		{
277 			throw new GException( new ErrorG(err) );
278 		}
279 		
280 		return p;
281 	}
282 	
283 	/**
284 	 * Blocks waiting for a client to connect to any of the sockets added
285 	 * to the listener. Returns a GSocketConnection for the socket that was
286 	 * accepted.
287 	 * If source_object is not NULL it will be filled out with the source
288 	 * object specified when the corresponding socket or address was added
289 	 * to the listener.
290 	 * If cancellable is not NULL, then the operation can be cancelled by
291 	 * triggering the cancellable object from another thread. If the operation
292 	 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned.
293 	 * Since 2.22
294 	 * Params:
295 	 * sourceObject = location where GObject pointer will be stored, or NULL. [out][transfer none][allow-none]
296 	 * cancellable = optional GCancellable object, NULL to ignore. [allow-none]
297 	 * Returns: a GSocketConnection on success, NULL on error. [transfer full]
298 	 * Throws: GException on failure.
299 	 */
300 	public SocketConnection accept(out ObjectG sourceObject, Cancellable cancellable)
301 	{
302 		// GSocketConnection * g_socket_listener_accept (GSocketListener *listener,  GObject **source_object,  GCancellable *cancellable,  GError **error);
303 		GObject* outsourceObject = null;
304 		GError* err = null;
305 		
306 		auto p = g_socket_listener_accept(gSocketListener, &outsourceObject, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
307 		
308 		if (err !is null)
309 		{
310 			throw new GException( new ErrorG(err) );
311 		}
312 		
313 		sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject);
314 		
315 		if(p is null)
316 		{
317 			return null;
318 		}
319 		
320 		return ObjectG.getDObject!(SocketConnection)(cast(GSocketConnection*) p);
321 	}
322 	
323 	/**
324 	 * This is the asynchronous version of g_socket_listener_accept().
325 	 * When the operation is finished callback will be
326 	 * called. You can then call g_socket_listener_accept_socket()
327 	 * to get the result of the operation.
328 	 * Since 2.22
329 	 * Params:
330 	 * cancellable = a GCancellable, or NULL. [allow-none]
331 	 * callback = a GAsyncReadyCallback. [scope async]
332 	 * userData = user data for the callback. [closure]
333 	 */
334 	public void acceptAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
335 	{
336 		// void g_socket_listener_accept_async (GSocketListener *listener,  GCancellable *cancellable,  GAsyncReadyCallback callback,  gpointer user_data);
337 		g_socket_listener_accept_async(gSocketListener, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
338 	}
339 	
340 	/**
341 	 * Finishes an async accept operation. See g_socket_listener_accept_async()
342 	 * Since 2.22
343 	 * Params:
344 	 * result = a GAsyncResult.
345 	 * sourceObject = Optional GObject identifying this source. [out][transfer none][allow-none]
346 	 * Returns: a GSocketConnection on success, NULL on error. [transfer full]
347 	 * Throws: GException on failure.
348 	 */
349 	public SocketConnection acceptFinish(AsyncResultIF result, ref ObjectG sourceObject)
350 	{
351 		// GSocketConnection * g_socket_listener_accept_finish (GSocketListener *listener,  GAsyncResult *result,  GObject **source_object,  GError **error);
352 		GObject* outsourceObject = (sourceObject is null) ? null : sourceObject.getObjectGStruct();
353 		GError* err = null;
354 		
355 		auto p = g_socket_listener_accept_finish(gSocketListener, (result is null) ? null : result.getAsyncResultTStruct(), &outsourceObject, &err);
356 		
357 		if (err !is null)
358 		{
359 			throw new GException( new ErrorG(err) );
360 		}
361 		
362 		sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject);
363 		
364 		if(p is null)
365 		{
366 			return null;
367 		}
368 		
369 		return ObjectG.getDObject!(SocketConnection)(cast(GSocketConnection*) p);
370 	}
371 	
372 	/**
373 	 * Blocks waiting for a client to connect to any of the sockets added
374 	 * to the listener. Returns the GSocket that was accepted.
375 	 * If you want to accept the high-level GSocketConnection, not a GSocket,
376 	 * which is often the case, then you should use g_socket_listener_accept()
377 	 * instead.
378 	 * If source_object is not NULL it will be filled out with the source
379 	 * object specified when the corresponding socket or address was added
380 	 * to the listener.
381 	 * If cancellable is not NULL, then the operation can be cancelled by
382 	 * triggering the cancellable object from another thread. If the operation
383 	 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned.
384 	 * Since 2.22
385 	 * Params:
386 	 * sourceObject = location where GObject pointer will be stored, or NULL. [out][transfer none][allow-none]
387 	 * cancellable = optional GCancellable object, NULL to ignore. [allow-none]
388 	 * Returns: a GSocket on success, NULL on error. [transfer full]
389 	 * Throws: GException on failure.
390 	 */
391 	public Socket acceptSocket(out ObjectG sourceObject, Cancellable cancellable)
392 	{
393 		// GSocket * g_socket_listener_accept_socket (GSocketListener *listener,  GObject **source_object,  GCancellable *cancellable,  GError **error);
394 		GObject* outsourceObject = null;
395 		GError* err = null;
396 		
397 		auto p = g_socket_listener_accept_socket(gSocketListener, &outsourceObject, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
398 		
399 		if (err !is null)
400 		{
401 			throw new GException( new ErrorG(err) );
402 		}
403 		
404 		sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject);
405 		
406 		if(p is null)
407 		{
408 			return null;
409 		}
410 		
411 		return ObjectG.getDObject!(Socket)(cast(GSocket*) p);
412 	}
413 	
414 	/**
415 	 * This is the asynchronous version of g_socket_listener_accept_socket().
416 	 * When the operation is finished callback will be
417 	 * called. You can then call g_socket_listener_accept_socket_finish()
418 	 * to get the result of the operation.
419 	 * Since 2.22
420 	 * Params:
421 	 * cancellable = a GCancellable, or NULL. [allow-none]
422 	 * callback = a GAsyncReadyCallback. [scope async]
423 	 * userData = user data for the callback. [closure]
424 	 */
425 	public void acceptSocketAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
426 	{
427 		// void g_socket_listener_accept_socket_async  (GSocketListener *listener,  GCancellable *cancellable,  GAsyncReadyCallback callback,  gpointer user_data);
428 		g_socket_listener_accept_socket_async(gSocketListener, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
429 	}
430 	
431 	/**
432 	 * Finishes an async accept operation. See g_socket_listener_accept_socket_async()
433 	 * Since 2.22
434 	 * Params:
435 	 * result = a GAsyncResult.
436 	 * sourceObject = Optional GObject identifying this source. [out][transfer none][allow-none]
437 	 * Returns: a GSocket on success, NULL on error. [transfer full]
438 	 * Throws: GException on failure.
439 	 */
440 	public Socket acceptSocketFinish(AsyncResultIF result, ref ObjectG sourceObject)
441 	{
442 		// GSocket * g_socket_listener_accept_socket_finish  (GSocketListener *listener,  GAsyncResult *result,  GObject **source_object,  GError **error);
443 		GObject* outsourceObject = (sourceObject is null) ? null : sourceObject.getObjectGStruct();
444 		GError* err = null;
445 		
446 		auto p = g_socket_listener_accept_socket_finish(gSocketListener, (result is null) ? null : result.getAsyncResultTStruct(), &outsourceObject, &err);
447 		
448 		if (err !is null)
449 		{
450 			throw new GException( new ErrorG(err) );
451 		}
452 		
453 		sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject);
454 		
455 		if(p is null)
456 		{
457 			return null;
458 		}
459 		
460 		return ObjectG.getDObject!(Socket)(cast(GSocket*) p);
461 	}
462 	
463 	/**
464 	 * Closes all the sockets in the listener.
465 	 * Since 2.22
466 	 */
467 	public void close()
468 	{
469 		// void g_socket_listener_close (GSocketListener *listener);
470 		g_socket_listener_close(gSocketListener);
471 	}
472 	
473 	/**
474 	 * Sets the listen backlog on the sockets in the listener.
475 	 * See g_socket_set_listen_backlog() for details
476 	 * Since 2.22
477 	 * Params:
478 	 * listenBacklog = an integer
479 	 */
480 	public void setBacklog(int listenBacklog)
481 	{
482 		// void g_socket_listener_set_backlog (GSocketListener *listener,  int listen_backlog);
483 		g_socket_listener_set_backlog(gSocketListener, listenBacklog);
484 	}
485 }