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 glib.ConstructionException;
30 private import gobject.ObjectG;
31 private import gobject.Signals;
32 public  import gtkc.gdktypes;
33 private import gtkc.gio;
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()
64 	{
65 		return gThreadedSocketService;
66 	}
67 
68 	/** the main Gtk struct as a void* */
69 	protected override void* getStruct()
70 	{
71 		return cast(void*)gThreadedSocketService;
72 	}
73 
74 	protected override void setStruct(GObject* obj)
75 	{
76 		gThreadedSocketService = cast(GThreadedSocketService*)obj;
77 		super.setStruct(obj);
78 	}
79 
80 	/**
81 	 * Sets our main struct and passes it to the parent class.
82 	 */
83 	public this (GThreadedSocketService* gThreadedSocketService, bool ownedRef = false)
84 	{
85 		this.gThreadedSocketService = gThreadedSocketService;
86 		super(cast(GSocketService*)gThreadedSocketService, ownedRef);
87 	}
88 
89 
90 	/** */
91 	public static GType getType()
92 	{
93 		return g_threaded_socket_service_get_type();
94 	}
95 
96 	/**
97 	 * Creates a new #GThreadedSocketService with no listeners. Listeners
98 	 * must be added with one of the #GSocketListener "add" methods.
99 	 *
100 	 * Params:
101 	 *     maxThreads = the maximal number of threads to execute concurrently
102 	 *         handling incoming clients, -1 means no limit
103 	 *
104 	 * Return: a new #GSocketService.
105 	 *
106 	 * Since: 2.22
107 	 *
108 	 * Throws: ConstructionException GTK+ fails to create the object.
109 	 */
110 	public this(int maxThreads)
111 	{
112 		auto p = g_threaded_socket_service_new(maxThreads);
113 		
114 		if(p is null)
115 		{
116 			throw new ConstructionException("null returned by new");
117 		}
118 		
119 		this(cast(GThreadedSocketService*) p, true);
120 	}
121 
122 	protected class OnRunDelegateWrapper
123 	{
124 		bool delegate(SocketConnection, ObjectG, ThreadedSocketService) dlg;
125 		gulong handlerId;
126 		ConnectFlags flags;
127 		this(bool delegate(SocketConnection, ObjectG, ThreadedSocketService) dlg, gulong handlerId, ConnectFlags flags)
128 		{
129 			this.dlg = dlg;
130 			this.handlerId = handlerId;
131 			this.flags = flags;
132 		}
133 	}
134 	protected OnRunDelegateWrapper[] onRunListeners;
135 
136 	/**
137 	 * The ::run signal is emitted in a worker thread in response to an
138 	 * incoming connection. This thread is dedicated to handling
139 	 * @connection and may perform blocking IO. The signal handler need
140 	 * not return until the connection is closed.
141 	 *
142 	 * Params:
143 	 *     connection = a new #GSocketConnection object.
144 	 *     sourceObject = the source_object passed to g_socket_listener_add_address().
145 	 *
146 	 * Return: %TRUE to stop further signal handlers from being called
147 	 */
148 	gulong addOnRun(bool delegate(SocketConnection, ObjectG, ThreadedSocketService) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
149 	{
150 		onRunListeners ~= new OnRunDelegateWrapper(dlg, 0, connectFlags);
151 		onRunListeners[onRunListeners.length - 1].handlerId = Signals.connectData(
152 			this,
153 			"run",
154 			cast(GCallback)&callBackRun,
155 			cast(void*)onRunListeners[onRunListeners.length - 1],
156 			cast(GClosureNotify)&callBackRunDestroy,
157 			connectFlags);
158 		return onRunListeners[onRunListeners.length - 1].handlerId;
159 	}
160 	
161 	extern(C) static int callBackRun(GThreadedSocketService* threadedsocketserviceStruct, GSocketConnection* connection, GObject* sourceObject,OnRunDelegateWrapper wrapper)
162 	{
163 		return wrapper.dlg(ObjectG.getDObject!(SocketConnection)(connection), ObjectG.getDObject!(ObjectG)(sourceObject), wrapper.outer);
164 	}
165 	
166 	extern(C) static void callBackRunDestroy(OnRunDelegateWrapper wrapper, GClosure* closure)
167 	{
168 		wrapper.outer.internalRemoveOnRun(wrapper);
169 	}
170 
171 	protected void internalRemoveOnRun(OnRunDelegateWrapper source)
172 	{
173 		foreach(index, wrapper; onRunListeners)
174 		{
175 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
176 			{
177 				onRunListeners[index] = null;
178 				onRunListeners = std.algorithm.remove(onRunListeners, index);
179 				break;
180 			}
181 		}
182 	}
183 	
184 }