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