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