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.ThreadedSocketService;
26 
27 private import gio.SocketConnection;
28 private import gio.SocketService;
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 #GThreadedSocketService is a simple subclass of #GSocketService
40  * that handles incoming connections by creating a worker thread and
41  * dispatching the connection to it by emitting the
42  * #GThreadedSocketService::run signal in the new thread.
43  * 
44  * The signal handler may perform blocking IO and need not return
45  * until the connection is closed.
46  * 
47  * The service is implemented using a thread pool, so there is a
48  * limited amount of threads available to serve incoming requests.
49  * The service automatically stops the #GSocketService from accepting
50  * new connections when all threads are busy.
51  * 
52  * As with #GSocketService, you may connect to #GThreadedSocketService::run,
53  * or subclass and override the default handler.
54  *
55  * Since: 2.22
56  */
57 public class ThreadedSocketService : SocketService
58 {
59 	/** the main Gtk struct */
60 	protected GThreadedSocketService* gThreadedSocketService;
61 
62 	/** Get the main Gtk struct */
63 	public GThreadedSocketService* getThreadedSocketServiceStruct(bool transferOwnership = false)
64 	{
65 		if (transferOwnership)
66 			ownedRef = false;
67 		return gThreadedSocketService;
68 	}
69 
70 	/** the main Gtk struct as a void* */
71 	protected override void* getStruct()
72 	{
73 		return cast(void*)gThreadedSocketService;
74 	}
75 
76 	/**
77 	 * Sets our main struct and passes it to the parent class.
78 	 */
79 	public this (GThreadedSocketService* gThreadedSocketService, bool ownedRef = false)
80 	{
81 		this.gThreadedSocketService = gThreadedSocketService;
82 		super(cast(GSocketService*)gThreadedSocketService, ownedRef);
83 	}
84 
85 
86 	/** */
87 	public static GType getType()
88 	{
89 		return g_threaded_socket_service_get_type();
90 	}
91 
92 	/**
93 	 * Creates a new #GThreadedSocketService with no listeners. Listeners
94 	 * must be added with one of the #GSocketListener "add" methods.
95 	 *
96 	 * Params:
97 	 *     maxThreads = the maximal number of threads to execute concurrently
98 	 *         handling incoming clients, -1 means no limit
99 	 *
100 	 * Returns: a new #GSocketService.
101 	 *
102 	 * Since: 2.22
103 	 *
104 	 * Throws: ConstructionException GTK+ fails to create the object.
105 	 */
106 	public this(int maxThreads)
107 	{
108 		auto p = g_threaded_socket_service_new(maxThreads);
109 
110 		if(p is null)
111 		{
112 			throw new ConstructionException("null returned by new");
113 		}
114 
115 		this(cast(GThreadedSocketService*) p, true);
116 	}
117 
118 	protected class OnRunDelegateWrapper
119 	{
120 		bool delegate(SocketConnection, ObjectG, ThreadedSocketService) dlg;
121 		gulong handlerId;
122 
123 		this(bool delegate(SocketConnection, ObjectG, ThreadedSocketService) dlg)
124 		{
125 			this.dlg = dlg;
126 			onRunListeners ~= this;
127 		}
128 
129 		void remove(OnRunDelegateWrapper source)
130 		{
131 			foreach(index, wrapper; onRunListeners)
132 			{
133 				if (wrapper.handlerId == source.handlerId)
134 				{
135 					onRunListeners[index] = null;
136 					onRunListeners = std.algorithm.remove(onRunListeners, index);
137 					break;
138 				}
139 			}
140 		}
141 	}
142 	OnRunDelegateWrapper[] onRunListeners;
143 
144 	/**
145 	 * The ::run signal is emitted in a worker thread in response to an
146 	 * incoming connection. This thread is dedicated to handling
147 	 * @connection and may perform blocking IO. The signal handler need
148 	 * not return until the connection is closed.
149 	 *
150 	 * Params:
151 	 *     connection = a new #GSocketConnection object.
152 	 *     sourceObject = the source_object passed to g_socket_listener_add_address().
153 	 *
154 	 * Returns: %TRUE to stop further signal handlers from being called
155 	 */
156 	gulong addOnRun(bool delegate(SocketConnection, ObjectG, ThreadedSocketService) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
157 	{
158 		auto wrapper = new OnRunDelegateWrapper(dlg);
159 		wrapper.handlerId = Signals.connectData(
160 			this,
161 			"run",
162 			cast(GCallback)&callBackRun,
163 			cast(void*)wrapper,
164 			cast(GClosureNotify)&callBackRunDestroy,
165 			connectFlags);
166 		return wrapper.handlerId;
167 	}
168 
169 	extern(C) static int callBackRun(GThreadedSocketService* threadedsocketserviceStruct, GSocketConnection* connection, GObject* sourceObject, OnRunDelegateWrapper wrapper)
170 	{
171 		return wrapper.dlg(ObjectG.getDObject!(SocketConnection)(connection), ObjectG.getDObject!(ObjectG)(sourceObject), wrapper.outer);
172 	}
173 
174 	extern(C) static void callBackRunDestroy(OnRunDelegateWrapper wrapper, GClosure* closure)
175 	{
176 		wrapper.remove(wrapper);
177 	}
178 }