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