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