File: SocketListener.d

package info (click to toggle)
gtk-d 3.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 20,152 kB
  • sloc: javascript: 565; sh: 71; makefile: 25
file content (530 lines) | stat: -rw-r--r-- 16,403 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
 * This file is part of gtkD.
 *
 * gtkD is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version, with
 * some exceptions, please read the COPYING file.
 *
 * gtkD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with gtkD; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 */

// generated automatically - do not change
// find conversion definition on APILookup.txt
// implement new conversion functionalities on the wrap.utils pakage


module gio.SocketListener;

private import gio.AsyncResultIF;
private import gio.Cancellable;
private import gio.Socket;
private import gio.SocketAddress;
private import gio.SocketConnection;
private import gio.c.functions;
public  import gio.c.types;
private import glib.ConstructionException;
private import glib.ErrorG;
private import glib.GException;
private import gobject.ObjectG;
private import gobject.Signals;
public  import gtkc.giotypes;
private import std.algorithm;


/**
 * A #GSocketListener is an object that keeps track of a set
 * of server sockets and helps you accept sockets from any of the
 * socket, either sync or async.
 * 
 * Add addresses and ports to listen on using g_socket_listener_add_address()
 * and g_socket_listener_add_inet_port(). These will be listened on until
 * g_socket_listener_close() is called. Dropping your final reference to the
 * #GSocketListener will not cause g_socket_listener_close() to be called
 * implicitly, as some references to the #GSocketListener may be held
 * internally.
 * 
 * If you want to implement a network server, also look at #GSocketService
 * and #GThreadedSocketService which are subclasses of #GSocketListener
 * that make this even easier.
 *
 * Since: 2.22
 */
public class SocketListener : ObjectG
{
	/** the main Gtk struct */
	protected GSocketListener* gSocketListener;

	/** Get the main Gtk struct */
	public GSocketListener* getSocketListenerStruct(bool transferOwnership = false)
	{
		if (transferOwnership)
			ownedRef = false;
		return gSocketListener;
	}

	/** the main Gtk struct as a void* */
	protected override void* getStruct()
	{
		return cast(void*)gSocketListener;
	}

	/**
	 * Sets our main struct and passes it to the parent class.
	 */
	public this (GSocketListener* gSocketListener, bool ownedRef = false)
	{
		this.gSocketListener = gSocketListener;
		super(cast(GObject*)gSocketListener, ownedRef);
	}


	/** */
	public static GType getType()
	{
		return g_socket_listener_get_type();
	}

	/**
	 * Creates a new #GSocketListener with no sockets to listen for.
	 * New listeners can be added with e.g. g_socket_listener_add_address()
	 * or g_socket_listener_add_inet_port().
	 *
	 * Returns: a new #GSocketListener.
	 *
	 * Since: 2.22
	 *
	 * Throws: ConstructionException GTK+ fails to create the object.
	 */
	public this()
	{
		auto __p = g_socket_listener_new();

		if(__p is null)
		{
			throw new ConstructionException("null returned by new");
		}

		this(cast(GSocketListener*) __p, true);
	}

	/**
	 * Blocks waiting for a client to connect to any of the sockets added
	 * to the listener. Returns a #GSocketConnection for the socket that was
	 * accepted.
	 *
	 * If @source_object is not %NULL it will be filled out with the source
	 * object specified when the corresponding socket or address was added
	 * to the listener.
	 *
	 * If @cancellable is not %NULL, then the operation can be cancelled by
	 * triggering the cancellable object from another thread. If the operation
	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
	 *
	 * Params:
	 *     sourceObject = location where #GObject pointer will be stored, or %NULL
	 *     cancellable = optional #GCancellable object, %NULL to ignore.
	 *
	 * Returns: a #GSocketConnection on success, %NULL on error.
	 *
	 * Since: 2.22
	 *
	 * Throws: GException on failure.
	 */
	public SocketConnection accept(out ObjectG sourceObject, Cancellable cancellable)
	{
		GObject* outsourceObject = null;
		GError* err = null;

		auto __p = g_socket_listener_accept(gSocketListener, &outsourceObject, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);

		if (err !is null)
		{
			throw new GException( new ErrorG(err) );
		}

		sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject);

		if(__p is null)
		{
			return null;
		}

		return ObjectG.getDObject!(SocketConnection)(cast(GSocketConnection*) __p, true);
	}

	/**
	 * This is the asynchronous version of g_socket_listener_accept().
	 *
	 * When the operation is finished @callback will be
	 * called. You can then call g_socket_listener_accept_socket()
	 * to get the result of the operation.
	 *
	 * Params:
	 *     cancellable = a #GCancellable, or %NULL
	 *     callback = a #GAsyncReadyCallback
	 *     userData = user data for the callback
	 *
	 * Since: 2.22
	 */
	public void acceptAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
	{
		g_socket_listener_accept_async(gSocketListener, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
	}

	/**
	 * Finishes an async accept operation. See g_socket_listener_accept_async()
	 *
	 * Params:
	 *     result = a #GAsyncResult.
	 *     sourceObject = Optional #GObject identifying this source
	 *
	 * Returns: a #GSocketConnection on success, %NULL on error.
	 *
	 * Since: 2.22
	 *
	 * Throws: GException on failure.
	 */
	public SocketConnection acceptFinish(AsyncResultIF result, out ObjectG sourceObject)
	{
		GObject* outsourceObject = null;
		GError* err = null;

		auto __p = g_socket_listener_accept_finish(gSocketListener, (result is null) ? null : result.getAsyncResultStruct(), &outsourceObject, &err);

		if (err !is null)
		{
			throw new GException( new ErrorG(err) );
		}

		sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject);

		if(__p is null)
		{
			return null;
		}

		return ObjectG.getDObject!(SocketConnection)(cast(GSocketConnection*) __p, true);
	}

	/**
	 * Blocks waiting for a client to connect to any of the sockets added
	 * to the listener. Returns the #GSocket that was accepted.
	 *
	 * If you want to accept the high-level #GSocketConnection, not a #GSocket,
	 * which is often the case, then you should use g_socket_listener_accept()
	 * instead.
	 *
	 * If @source_object is not %NULL it will be filled out with the source
	 * object specified when the corresponding socket or address was added
	 * to the listener.
	 *
	 * If @cancellable is not %NULL, then the operation can be cancelled by
	 * triggering the cancellable object from another thread. If the operation
	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
	 *
	 * Params:
	 *     sourceObject = location where #GObject pointer will be stored, or %NULL.
	 *     cancellable = optional #GCancellable object, %NULL to ignore.
	 *
	 * Returns: a #GSocket on success, %NULL on error.
	 *
	 * Since: 2.22
	 *
	 * Throws: GException on failure.
	 */
	public Socket acceptSocket(out ObjectG sourceObject, Cancellable cancellable)
	{
		GObject* outsourceObject = null;
		GError* err = null;

		auto __p = g_socket_listener_accept_socket(gSocketListener, &outsourceObject, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);

		if (err !is null)
		{
			throw new GException( new ErrorG(err) );
		}

		sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject);

		if(__p is null)
		{
			return null;
		}

		return ObjectG.getDObject!(Socket)(cast(GSocket*) __p, true);
	}

	/**
	 * This is the asynchronous version of g_socket_listener_accept_socket().
	 *
	 * When the operation is finished @callback will be
	 * called. You can then call g_socket_listener_accept_socket_finish()
	 * to get the result of the operation.
	 *
	 * Params:
	 *     cancellable = a #GCancellable, or %NULL
	 *     callback = a #GAsyncReadyCallback
	 *     userData = user data for the callback
	 *
	 * Since: 2.22
	 */
	public void acceptSocketAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
	{
		g_socket_listener_accept_socket_async(gSocketListener, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
	}

	/**
	 * Finishes an async accept operation. See g_socket_listener_accept_socket_async()
	 *
	 * Params:
	 *     result = a #GAsyncResult.
	 *     sourceObject = Optional #GObject identifying this source
	 *
	 * Returns: a #GSocket on success, %NULL on error.
	 *
	 * Since: 2.22
	 *
	 * Throws: GException on failure.
	 */
	public Socket acceptSocketFinish(AsyncResultIF result, out ObjectG sourceObject)
	{
		GObject* outsourceObject = null;
		GError* err = null;

		auto __p = g_socket_listener_accept_socket_finish(gSocketListener, (result is null) ? null : result.getAsyncResultStruct(), &outsourceObject, &err);

		if (err !is null)
		{
			throw new GException( new ErrorG(err) );
		}

		sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject);

		if(__p is null)
		{
			return null;
		}

		return ObjectG.getDObject!(Socket)(cast(GSocket*) __p, true);
	}

	/**
	 * Creates a socket of type @type and protocol @protocol, binds
	 * it to @address and adds it to the set of sockets we're accepting
	 * sockets from.
	 *
	 * Note that adding an IPv6 address, depending on the platform,
	 * may or may not result in a listener that also accepts IPv4
	 * connections.  For more deterministic behavior, see
	 * g_socket_listener_add_inet_port().
	 *
	 * @source_object will be passed out in the various calls
	 * to accept to identify this particular source, which is
	 * useful if you're listening on multiple addresses and do
	 * different things depending on what address is connected to.
	 *
	 * If successful and @effective_address is non-%NULL then it will
	 * be set to the address that the binding actually occurred at.  This
	 * is helpful for determining the port number that was used for when
	 * requesting a binding to port 0 (ie: "any port").  This address, if
	 * requested, belongs to the caller and must be freed.
	 *
	 * Call g_socket_listener_close() to stop listening on @address; this will not
	 * be done automatically when you drop your final reference to @listener, as
	 * references may be held internally.
	 *
	 * Params:
	 *     address = a #GSocketAddress
	 *     type = a #GSocketType
	 *     protocol = a #GSocketProtocol
	 *     sourceObject = Optional #GObject identifying this source
	 *     effectiveAddress = location to store the address that was bound to, or %NULL.
	 *
	 * Returns: %TRUE on success, %FALSE on error.
	 *
	 * Since: 2.22
	 *
	 * Throws: GException on failure.
	 */
	public bool addAddress(SocketAddress address, GSocketType type, GSocketProtocol protocol, ObjectG sourceObject, out SocketAddress effectiveAddress)
	{
		GSocketAddress* outeffectiveAddress = null;
		GError* err = null;

		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;

		if (err !is null)
		{
			throw new GException( new ErrorG(err) );
		}

		effectiveAddress = ObjectG.getDObject!(SocketAddress)(outeffectiveAddress);

		return __p;
	}

	/**
	 * Listens for TCP connections on any available port number for both
	 * IPv6 and IPv4 (if each is available).
	 *
	 * This is useful if you need to have a socket for incoming connections
	 * but don't care about the specific port number.
	 *
	 * @source_object will be passed out in the various calls
	 * to accept to identify this particular source, which is
	 * useful if you're listening on multiple addresses and do
	 * different things depending on what address is connected to.
	 *
	 * Params:
	 *     sourceObject = Optional #GObject identifying this source
	 *
	 * Returns: the port number, or 0 in case of failure.
	 *
	 * Since: 2.24
	 *
	 * Throws: GException on failure.
	 */
	public ushort addAnyInetPort(ObjectG sourceObject)
	{
		GError* err = null;

		auto __p = g_socket_listener_add_any_inet_port(gSocketListener, (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err);

		if (err !is null)
		{
			throw new GException( new ErrorG(err) );
		}

		return __p;
	}

	/**
	 * Helper function for g_socket_listener_add_address() that
	 * creates a TCP/IP socket listening on IPv4 and IPv6 (if
	 * supported) on the specified port on all interfaces.
	 *
	 * @source_object will be passed out in the various calls
	 * to accept to identify this particular source, which is
	 * useful if you're listening on multiple addresses and do
	 * different things depending on what address is connected to.
	 *
	 * Call g_socket_listener_close() to stop listening on @port; this will not
	 * be done automatically when you drop your final reference to @listener, as
	 * references may be held internally.
	 *
	 * Params:
	 *     port = an IP port number (non-zero)
	 *     sourceObject = Optional #GObject identifying this source
	 *
	 * Returns: %TRUE on success, %FALSE on error.
	 *
	 * Since: 2.22
	 *
	 * Throws: GException on failure.
	 */
	public bool addInetPort(ushort port, ObjectG sourceObject)
	{
		GError* err = null;

		auto __p = g_socket_listener_add_inet_port(gSocketListener, port, (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err) != 0;

		if (err !is null)
		{
			throw new GException( new ErrorG(err) );
		}

		return __p;
	}

	/**
	 * Adds @socket to the set of sockets that we try to accept
	 * new clients from. The socket must be bound to a local
	 * address and listened to.
	 *
	 * @source_object will be passed out in the various calls
	 * to accept to identify this particular source, which is
	 * useful if you're listening on multiple addresses and do
	 * different things depending on what address is connected to.
	 *
	 * The @socket will not be automatically closed when the @listener is finalized
	 * unless the listener held the final reference to the socket. Before GLib 2.42,
	 * the @socket was automatically closed on finalization of the @listener, even
	 * if references to it were held elsewhere.
	 *
	 * Params:
	 *     socket = a listening #GSocket
	 *     sourceObject = Optional #GObject identifying this source
	 *
	 * Returns: %TRUE on success, %FALSE on error.
	 *
	 * Since: 2.22
	 *
	 * Throws: GException on failure.
	 */
	public bool addSocket(Socket socket, ObjectG sourceObject)
	{
		GError* err = null;

		auto __p = g_socket_listener_add_socket(gSocketListener, (socket is null) ? null : socket.getSocketStruct(), (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err) != 0;

		if (err !is null)
		{
			throw new GException( new ErrorG(err) );
		}

		return __p;
	}

	/**
	 * Closes all the sockets in the listener.
	 *
	 * Since: 2.22
	 */
	public void close()
	{
		g_socket_listener_close(gSocketListener);
	}

	/**
	 * Sets the listen backlog on the sockets in the listener. This must be called
	 * before adding any sockets, addresses or ports to the #GSocketListener (for
	 * example, by calling g_socket_listener_add_inet_port()) to be effective.
	 *
	 * See g_socket_set_listen_backlog() for details
	 *
	 * Params:
	 *     listenBacklog = an integer
	 *
	 * Since: 2.22
	 */
	public void setBacklog(int listenBacklog)
	{
		g_socket_listener_set_backlog(gSocketListener, listenBacklog);
	}

	/**
	 * Emitted when @listener's activity on @socket changes state.
	 * Note that when @listener is used to listen on both IPv4 and
	 * IPv6, a separate set of signals will be emitted for each, and
	 * the order they happen in is undefined.
	 *
	 * Params:
	 *     event = the event that is occurring
	 *     socket = the #GSocket the event is occurring on
	 *
	 * Since: 2.46
	 */
	gulong addOnEvent(void delegate(GSocketListenerEvent, Socket, SocketListener) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
	{
		return Signals.connect(this, "event", dlg, connectFlags ^ ConnectFlags.SWAPPED);
	}
}