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