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