source: appstream-generator/build/girepo/gio/SocketListener.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

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