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.SocketService;
26 
27 private import gio.SocketConnection;
28 private import gio.SocketListener;
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 private import std.algorithm;
35 
36 
37 /**
38  * A #GSocketService is an object that represents a service that
39  * is provided to the network or over local sockets.  When a new
40  * connection is made to the service the #GSocketService::incoming
41  * signal is emitted.
42  * 
43  * A #GSocketService is a subclass of #GSocketListener and you need
44  * to add the addresses you want to accept connections on with the
45  * #GSocketListener APIs.
46  * 
47  * There are two options for implementing a network service based on
48  * #GSocketService. The first is to create the service using
49  * g_socket_service_new() and to connect to the #GSocketService::incoming
50  * signal. The second is to subclass #GSocketService and override the
51  * default signal handler implementation.
52  * 
53  * In either case, the handler must immediately return, or else it
54  * will block additional incoming connections from being serviced.
55  * If you are interested in writing connection handlers that contain
56  * blocking code then see #GThreadedSocketService.
57  * 
58  * The socket service runs on the main loop of the
59  * [thread-default context][g-main-context-push-thread-default-context]
60  * of the thread it is created in, and is not
61  * threadsafe in general. However, the calls to start and stop the
62  * service are thread-safe so these can be used from threads that
63  * handle incoming clients.
64  *
65  * Since: 2.22
66  */
67 public class SocketService : SocketListener
68 {
69 	/** the main Gtk struct */
70 	protected GSocketService* gSocketService;
71 
72 	/** Get the main Gtk struct */
73 	public GSocketService* getSocketServiceStruct(bool transferOwnership = false)
74 	{
75 		if (transferOwnership)
76 			ownedRef = false;
77 		return gSocketService;
78 	}
79 
80 	/** the main Gtk struct as a void* */
81 	protected override void* getStruct()
82 	{
83 		return cast(void*)gSocketService;
84 	}
85 
86 	/**
87 	 * Sets our main struct and passes it to the parent class.
88 	 */
89 	public this (GSocketService* gSocketService, bool ownedRef = false)
90 	{
91 		this.gSocketService = gSocketService;
92 		super(cast(GSocketListener*)gSocketService, ownedRef);
93 	}
94 
95 
96 	/** */
97 	public static GType getType()
98 	{
99 		return g_socket_service_get_type();
100 	}
101 
102 	/**
103 	 * Creates a new #GSocketService with no sockets to listen for.
104 	 * New listeners can be added with e.g. g_socket_listener_add_address()
105 	 * or g_socket_listener_add_inet_port().
106 	 *
107 	 * New services are created active, there is no need to call
108 	 * g_socket_service_start(), unless g_socket_service_stop() has been
109 	 * called before.
110 	 *
111 	 * Returns: a new #GSocketService.
112 	 *
113 	 * Since: 2.22
114 	 *
115 	 * Throws: ConstructionException GTK+ fails to create the object.
116 	 */
117 	public this()
118 	{
119 		auto __p = g_socket_service_new();
120 
121 		if(__p is null)
122 		{
123 			throw new ConstructionException("null returned by new");
124 		}
125 
126 		this(cast(GSocketService*) __p, true);
127 	}
128 
129 	/**
130 	 * Check whether the service is active or not. An active
131 	 * service will accept new clients that connect, while
132 	 * a non-active service will let connecting clients queue
133 	 * up until the service is started.
134 	 *
135 	 * Returns: %TRUE if the service is active, %FALSE otherwise
136 	 *
137 	 * Since: 2.22
138 	 */
139 	public bool isActive()
140 	{
141 		return g_socket_service_is_active(gSocketService) != 0;
142 	}
143 
144 	/**
145 	 * Restarts the service, i.e. start accepting connections
146 	 * from the added sockets when the mainloop runs. This only needs
147 	 * to be called after the service has been stopped from
148 	 * g_socket_service_stop().
149 	 *
150 	 * This call is thread-safe, so it may be called from a thread
151 	 * handling an incoming client request.
152 	 *
153 	 * Since: 2.22
154 	 */
155 	public void start()
156 	{
157 		g_socket_service_start(gSocketService);
158 	}
159 
160 	/**
161 	 * Stops the service, i.e. stops accepting connections
162 	 * from the added sockets when the mainloop runs.
163 	 *
164 	 * This call is thread-safe, so it may be called from a thread
165 	 * handling an incoming client request.
166 	 *
167 	 * Note that this only stops accepting new connections; it does not
168 	 * close the listening sockets, and you can call
169 	 * g_socket_service_start() again later to begin listening again. To
170 	 * close the listening sockets, call g_socket_listener_close(). (This
171 	 * will happen automatically when the #GSocketService is finalized.)
172 	 *
173 	 * This must be called before calling g_socket_listener_close() as
174 	 * the socket service will start accepting connections immediately
175 	 * when a new socket is added.
176 	 *
177 	 * Since: 2.22
178 	 */
179 	public void stop()
180 	{
181 		g_socket_service_stop(gSocketService);
182 	}
183 
184 	/**
185 	 * The ::incoming signal is emitted when a new incoming connection
186 	 * to @service needs to be handled. The handler must initiate the
187 	 * handling of @connection, but may not block; in essence,
188 	 * asynchronous operations must be used.
189 	 *
190 	 * @connection will be unreffed once the signal handler returns,
191 	 * so you need to ref it yourself if you are planning to use it.
192 	 *
193 	 * Params:
194 	 *     connection = a new #GSocketConnection object
195 	 *     sourceObject = the source_object passed to
196 	 *         g_socket_listener_add_address()
197 	 *
198 	 * Returns: %TRUE to stop other handlers from being called
199 	 *
200 	 * Since: 2.22
201 	 */
202 	gulong addOnIncoming(bool delegate(SocketConnection, ObjectG, SocketService) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
203 	{
204 		return Signals.connect(this, "incoming", dlg, connectFlags ^ ConnectFlags.SWAPPED);
205 	}
206 }