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