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