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

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

Initial release

File size: 7.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.SocketService;
22
23private import gi.gio;
24public  import gi.giotypes;
25private import gio.SocketConnection;
26private import gio.SocketListener;
27private import glib.ConstructionException;
28private import gobject.ObjectG;
29private import gobject.Signals;
30private import std.algorithm;
31
32
33/**
34 * A #GSocketService is an object that represents a service that
35 * is provided to the network or over local sockets.  When a new
36 * connection is made to the service the #GSocketService::incoming
37 * signal is emitted.
38 *
39 * A #GSocketService is a subclass of #GSocketListener and you need
40 * to add the addresses you want to accept connections on with the
41 * #GSocketListener APIs.
42 *
43 * There are two options for implementing a network service based on
44 * #GSocketService. The first is to create the service using
45 * g_socket_service_new() and to connect to the #GSocketService::incoming
46 * signal. The second is to subclass #GSocketService and override the
47 * default signal handler implementation.
48 *
49 * In either case, the handler must immediately return, or else it
50 * will block additional incoming connections from being serviced.
51 * If you are interested in writing connection handlers that contain
52 * blocking code then see #GThreadedSocketService.
53 *
54 * The socket service runs on the main loop of the
55 * [thread-default context][g-main-context-push-thread-default-context]
56 * of the thread it is created in, and is not
57 * threadsafe in general. However, the calls to start and stop the
58 * service are thread-safe so these can be used from threads that
59 * handle incoming clients.
60 *
61 * Since: 2.22
62 */
63public class SocketService : SocketListener
64{
65        /** the main Gtk struct */
66        protected GSocketService* gSocketService;
67
68        /** Get the main Gtk struct */
69        public GSocketService* getSocketServiceStruct()
70        {
71                return gSocketService;
72        }
73
74        /** the main Gtk struct as a void* */
75        protected override void* getStruct()
76        {
77                return cast(void*)gSocketService;
78        }
79
80        protected override void setStruct(GObject* obj)
81        {
82                gSocketService = cast(GSocketService*)obj;
83                super.setStruct(obj);
84        }
85
86        /**
87         * Sets our main struct and passes it to the parent class.
88         */
89        public this (GSocketService* gSocketService, bool ownedRef = false)
90        {
91                this.gSocketService = gSocketService;
92                super(cast(GSocketListener*)gSocketService, ownedRef);
93        }
94
95
96        /** */
97        public static GType getType()
98        {
99                return g_socket_service_get_type();
100        }
101
102        /**
103         * Creates a new #GSocketService with no sockets to listen for.
104         * New listeners can be added with e.g. g_socket_listener_add_address()
105         * or g_socket_listener_add_inet_port().
106         *
107         * Returns: a new #GSocketService.
108         *
109         * Since: 2.22
110         *
111         * Throws: ConstructionException GTK+ fails to create the object.
112         */
113        public this()
114        {
115                auto p = g_socket_service_new();
116               
117                if(p is null)
118                {
119                        throw new ConstructionException("null returned by new");
120                }
121               
122                this(cast(GSocketService*) p, true);
123        }
124
125        /**
126         * Check whether the service is active or not. An active
127         * service will accept new clients that connect, while
128         * a non-active service will let connecting clients queue
129         * up until the service is started.
130         *
131         * Returns: %TRUE if the service is active, %FALSE otherwise
132         *
133         * Since: 2.22
134         */
135        public bool isActive()
136        {
137                return g_socket_service_is_active(gSocketService) != 0;
138        }
139
140        /**
141         * Starts the service, i.e. start accepting connections
142         * from the added sockets when the mainloop runs.
143         *
144         * This call is thread-safe, so it may be called from a thread
145         * handling an incoming client request.
146         *
147         * Since: 2.22
148         */
149        public void start()
150        {
151                g_socket_service_start(gSocketService);
152        }
153
154        /**
155         * Stops the service, i.e. stops accepting connections
156         * from the added sockets when the mainloop runs.
157         *
158         * This call is thread-safe, so it may be called from a thread
159         * handling an incoming client request.
160         *
161         * Note that this only stops accepting new connections; it does not
162         * close the listening sockets, and you can call
163         * g_socket_service_start() again later to begin listening again. To
164         * close the listening sockets, call g_socket_listener_close(). (This
165         * will happen automatically when the #GSocketService is finalized.)
166         *
167         * Since: 2.22
168         */
169        public void stop()
170        {
171                g_socket_service_stop(gSocketService);
172        }
173
174        protected class OnIncomingDelegateWrapper
175        {
176                static OnIncomingDelegateWrapper[] listeners;
177                bool delegate(SocketConnection, ObjectG, SocketService) dlg;
178                gulong handlerId;
179               
180                this(bool delegate(SocketConnection, ObjectG, SocketService) dlg)
181                {
182                        this.dlg = dlg;
183                        this.listeners ~= this;
184                }
185               
186                void remove(OnIncomingDelegateWrapper source)
187                {
188                        foreach(index, wrapper; listeners)
189                        {
190                                if (wrapper.handlerId == source.handlerId)
191                                {
192                                        listeners[index] = null;
193                                        listeners = std.algorithm.remove(listeners, index);
194                                        break;
195                                }
196                        }
197                }
198        }
199
200        /**
201         * The ::incoming signal is emitted when a new incoming connection
202         * to @service needs to be handled. The handler must initiate the
203         * handling of @connection, but may not block; in essence,
204         * asynchronous operations must be used.
205         *
206         * @connection will be unreffed once the signal handler returns,
207         * so you need to ref it yourself if you are planning to use it.
208         *
209         * Params:
210         *     connection = a new #GSocketConnection object
211         *     sourceObject = the source_object passed to
212         *         g_socket_listener_add_address()
213         *
214         * Returns: %TRUE to stop other handlers from being called
215         *
216         * Since: 2.22
217         */
218        gulong addOnIncoming(bool delegate(SocketConnection, ObjectG, SocketService) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
219        {
220                auto wrapper = new OnIncomingDelegateWrapper(dlg);
221                wrapper.handlerId = Signals.connectData(
222                        this,
223                        "incoming",
224                        cast(GCallback)&callBackIncoming,
225                        cast(void*)wrapper,
226                        cast(GClosureNotify)&callBackIncomingDestroy,
227                        connectFlags);
228                return wrapper.handlerId;
229        }
230       
231        extern(C) static int callBackIncoming(GSocketService* socketserviceStruct, GSocketConnection* connection, GObject* sourceObject, OnIncomingDelegateWrapper wrapper)
232        {
233                return wrapper.dlg(ObjectG.getDObject!(SocketConnection)(connection), ObjectG.getDObject!(ObjectG)(sourceObject), wrapper.outer);
234        }
235       
236        extern(C) static void callBackIncomingDestroy(OnIncomingDelegateWrapper wrapper, GClosure* closure)
237        {
238                wrapper.remove(wrapper);
239        }
240}
Note: See TracBrowser for help on using the repository browser.