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.SocketListener; 26 27 private import gio.AsyncResultIF; 28 private import gio.Cancellable; 29 private import gio.Socket; 30 private import gio.SocketAddress; 31 private import gio.SocketConnection; 32 private import gio.c.functions; 33 public import gio.c.types; 34 private import glib.ConstructionException; 35 private import glib.ErrorG; 36 private import glib.GException; 37 private import gobject.ObjectG; 38 private import gobject.Signals; 39 private import std.algorithm; 40 41 42 /** 43 * A #GSocketListener is an object that keeps track of a set 44 * of server sockets and helps you accept sockets from any of the 45 * socket, either sync or async. 46 * 47 * Add addresses and ports to listen on using g_socket_listener_add_address() 48 * and g_socket_listener_add_inet_port(). These will be listened on until 49 * g_socket_listener_close() is called. Dropping your final reference to the 50 * #GSocketListener will not cause g_socket_listener_close() to be called 51 * implicitly, as some references to the #GSocketListener may be held 52 * internally. 53 * 54 * If you want to implement a network server, also look at #GSocketService 55 * and #GThreadedSocketService which are subclasses of #GSocketListener 56 * that make this even easier. 57 * 58 * Since: 2.22 59 */ 60 public class SocketListener : ObjectG 61 { 62 /** the main Gtk struct */ 63 protected GSocketListener* gSocketListener; 64 65 /** Get the main Gtk struct */ 66 public GSocketListener* getSocketListenerStruct(bool transferOwnership = false) 67 { 68 if (transferOwnership) 69 ownedRef = false; 70 return gSocketListener; 71 } 72 73 /** the main Gtk struct as a void* */ 74 protected override void* getStruct() 75 { 76 return cast(void*)gSocketListener; 77 } 78 79 /** 80 * Sets our main struct and passes it to the parent class. 81 */ 82 public this (GSocketListener* gSocketListener, bool ownedRef = false) 83 { 84 this.gSocketListener = gSocketListener; 85 super(cast(GObject*)gSocketListener, ownedRef); 86 } 87 88 89 /** */ 90 public static GType getType() 91 { 92 return g_socket_listener_get_type(); 93 } 94 95 /** 96 * Creates a new #GSocketListener with no sockets to listen for. 97 * New listeners can be added with e.g. g_socket_listener_add_address() 98 * or g_socket_listener_add_inet_port(). 99 * 100 * Returns: a new #GSocketListener. 101 * 102 * Since: 2.22 103 * 104 * Throws: ConstructionException GTK+ fails to create the object. 105 */ 106 public this() 107 { 108 auto __p = g_socket_listener_new(); 109 110 if(__p is null) 111 { 112 throw new ConstructionException("null returned by new"); 113 } 114 115 this(cast(GSocketListener*) __p, true); 116 } 117 118 /** 119 * Blocks waiting for a client to connect to any of the sockets added 120 * to the listener. Returns a #GSocketConnection for the socket that was 121 * accepted. 122 * 123 * If @source_object is not %NULL it will be filled out with the source 124 * object specified when the corresponding socket or address was added 125 * to the listener. 126 * 127 * If @cancellable is not %NULL, then the operation can be cancelled by 128 * triggering the cancellable object from another thread. If the operation 129 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 130 * 131 * Params: 132 * sourceObject = location where #GObject pointer will be stored, or %NULL 133 * cancellable = optional #GCancellable object, %NULL to ignore. 134 * 135 * Returns: a #GSocketConnection on success, %NULL on error. 136 * 137 * Since: 2.22 138 * 139 * Throws: GException on failure. 140 */ 141 public SocketConnection accept(out ObjectG sourceObject, Cancellable cancellable) 142 { 143 GObject* outsourceObject = null; 144 GError* err = null; 145 146 auto __p = g_socket_listener_accept(gSocketListener, &outsourceObject, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 147 148 if (err !is null) 149 { 150 throw new GException( new ErrorG(err) ); 151 } 152 153 sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject); 154 155 if(__p is null) 156 { 157 return null; 158 } 159 160 return ObjectG.getDObject!(SocketConnection)(cast(GSocketConnection*) __p, true); 161 } 162 163 /** 164 * This is the asynchronous version of g_socket_listener_accept(). 165 * 166 * When the operation is finished @callback will be 167 * called. You can then call g_socket_listener_accept_socket() 168 * to get the result of the operation. 169 * 170 * Params: 171 * cancellable = a #GCancellable, or %NULL 172 * callback = a #GAsyncReadyCallback 173 * userData = user data for the callback 174 * 175 * Since: 2.22 176 */ 177 public void acceptAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 178 { 179 g_socket_listener_accept_async(gSocketListener, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 180 } 181 182 /** 183 * Finishes an async accept operation. See g_socket_listener_accept_async() 184 * 185 * Params: 186 * result = a #GAsyncResult. 187 * sourceObject = Optional #GObject identifying this source 188 * 189 * Returns: a #GSocketConnection on success, %NULL on error. 190 * 191 * Since: 2.22 192 * 193 * Throws: GException on failure. 194 */ 195 public SocketConnection acceptFinish(AsyncResultIF result, out ObjectG sourceObject) 196 { 197 GObject* outsourceObject = null; 198 GError* err = null; 199 200 auto __p = g_socket_listener_accept_finish(gSocketListener, (result is null) ? null : result.getAsyncResultStruct(), &outsourceObject, &err); 201 202 if (err !is null) 203 { 204 throw new GException( new ErrorG(err) ); 205 } 206 207 sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject); 208 209 if(__p is null) 210 { 211 return null; 212 } 213 214 return ObjectG.getDObject!(SocketConnection)(cast(GSocketConnection*) __p, true); 215 } 216 217 /** 218 * Blocks waiting for a client to connect to any of the sockets added 219 * to the listener. Returns the #GSocket that was accepted. 220 * 221 * If you want to accept the high-level #GSocketConnection, not a #GSocket, 222 * which is often the case, then you should use g_socket_listener_accept() 223 * instead. 224 * 225 * If @source_object is not %NULL it will be filled out with the source 226 * object specified when the corresponding socket or address was added 227 * to the listener. 228 * 229 * If @cancellable is not %NULL, then the operation can be cancelled by 230 * triggering the cancellable object from another thread. If the operation 231 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 232 * 233 * Params: 234 * sourceObject = location where #GObject pointer will be stored, or %NULL. 235 * cancellable = optional #GCancellable object, %NULL to ignore. 236 * 237 * Returns: a #GSocket on success, %NULL on error. 238 * 239 * Since: 2.22 240 * 241 * Throws: GException on failure. 242 */ 243 public Socket acceptSocket(out ObjectG sourceObject, Cancellable cancellable) 244 { 245 GObject* outsourceObject = null; 246 GError* err = null; 247 248 auto __p = g_socket_listener_accept_socket(gSocketListener, &outsourceObject, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 249 250 if (err !is null) 251 { 252 throw new GException( new ErrorG(err) ); 253 } 254 255 sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject); 256 257 if(__p is null) 258 { 259 return null; 260 } 261 262 return ObjectG.getDObject!(Socket)(cast(GSocket*) __p, true); 263 } 264 265 /** 266 * This is the asynchronous version of g_socket_listener_accept_socket(). 267 * 268 * When the operation is finished @callback will be 269 * called. You can then call g_socket_listener_accept_socket_finish() 270 * to get the result of the operation. 271 * 272 * Params: 273 * cancellable = a #GCancellable, or %NULL 274 * callback = a #GAsyncReadyCallback 275 * userData = user data for the callback 276 * 277 * Since: 2.22 278 */ 279 public void acceptSocketAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 280 { 281 g_socket_listener_accept_socket_async(gSocketListener, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 282 } 283 284 /** 285 * Finishes an async accept operation. See g_socket_listener_accept_socket_async() 286 * 287 * Params: 288 * result = a #GAsyncResult. 289 * sourceObject = Optional #GObject identifying this source 290 * 291 * Returns: a #GSocket on success, %NULL on error. 292 * 293 * Since: 2.22 294 * 295 * Throws: GException on failure. 296 */ 297 public Socket acceptSocketFinish(AsyncResultIF result, out ObjectG sourceObject) 298 { 299 GObject* outsourceObject = null; 300 GError* err = null; 301 302 auto __p = g_socket_listener_accept_socket_finish(gSocketListener, (result is null) ? null : result.getAsyncResultStruct(), &outsourceObject, &err); 303 304 if (err !is null) 305 { 306 throw new GException( new ErrorG(err) ); 307 } 308 309 sourceObject = ObjectG.getDObject!(ObjectG)(outsourceObject); 310 311 if(__p is null) 312 { 313 return null; 314 } 315 316 return ObjectG.getDObject!(Socket)(cast(GSocket*) __p, true); 317 } 318 319 /** 320 * Creates a socket of type @type and protocol @protocol, binds 321 * it to @address and adds it to the set of sockets we're accepting 322 * sockets from. 323 * 324 * Note that adding an IPv6 address, depending on the platform, 325 * may or may not result in a listener that also accepts IPv4 326 * connections. For more deterministic behavior, see 327 * g_socket_listener_add_inet_port(). 328 * 329 * @source_object will be passed out in the various calls 330 * to accept to identify this particular source, which is 331 * useful if you're listening on multiple addresses and do 332 * different things depending on what address is connected to. 333 * 334 * If successful and @effective_address is non-%NULL then it will 335 * be set to the address that the binding actually occurred at. This 336 * is helpful for determining the port number that was used for when 337 * requesting a binding to port 0 (ie: "any port"). This address, if 338 * requested, belongs to the caller and must be freed. 339 * 340 * Call g_socket_listener_close() to stop listening on @address; this will not 341 * be done automatically when you drop your final reference to @listener, as 342 * references may be held internally. 343 * 344 * Params: 345 * address = a #GSocketAddress 346 * type = a #GSocketType 347 * protocol = a #GSocketProtocol 348 * sourceObject = Optional #GObject identifying this source 349 * effectiveAddress = location to store the address that was bound to, or %NULL. 350 * 351 * Returns: %TRUE on success, %FALSE on error. 352 * 353 * Since: 2.22 354 * 355 * Throws: GException on failure. 356 */ 357 public bool addAddress(SocketAddress address, GSocketType type, GSocketProtocol protocol, ObjectG sourceObject, out SocketAddress effectiveAddress) 358 { 359 GSocketAddress* outeffectiveAddress = null; 360 GError* err = null; 361 362 auto __p = g_socket_listener_add_address(gSocketListener, (address is null) ? null : address.getSocketAddressStruct(), type, protocol, (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &outeffectiveAddress, &err) != 0; 363 364 if (err !is null) 365 { 366 throw new GException( new ErrorG(err) ); 367 } 368 369 effectiveAddress = ObjectG.getDObject!(SocketAddress)(outeffectiveAddress); 370 371 return __p; 372 } 373 374 /** 375 * Listens for TCP connections on any available port number for both 376 * IPv6 and IPv4 (if each is available). 377 * 378 * This is useful if you need to have a socket for incoming connections 379 * but don't care about the specific port number. 380 * 381 * @source_object will be passed out in the various calls 382 * to accept to identify this particular source, which is 383 * useful if you're listening on multiple addresses and do 384 * different things depending on what address is connected to. 385 * 386 * Params: 387 * sourceObject = Optional #GObject identifying this source 388 * 389 * Returns: the port number, or 0 in case of failure. 390 * 391 * Since: 2.24 392 * 393 * Throws: GException on failure. 394 */ 395 public ushort addAnyInetPort(ObjectG sourceObject) 396 { 397 GError* err = null; 398 399 auto __p = g_socket_listener_add_any_inet_port(gSocketListener, (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err); 400 401 if (err !is null) 402 { 403 throw new GException( new ErrorG(err) ); 404 } 405 406 return __p; 407 } 408 409 /** 410 * Helper function for g_socket_listener_add_address() that 411 * creates a TCP/IP socket listening on IPv4 and IPv6 (if 412 * supported) on the specified port on all interfaces. 413 * 414 * @source_object will be passed out in the various calls 415 * to accept to identify this particular source, which is 416 * useful if you're listening on multiple addresses and do 417 * different things depending on what address is connected to. 418 * 419 * Call g_socket_listener_close() to stop listening on @port; this will not 420 * be done automatically when you drop your final reference to @listener, as 421 * references may be held internally. 422 * 423 * Params: 424 * port = an IP port number (non-zero) 425 * sourceObject = Optional #GObject identifying this source 426 * 427 * Returns: %TRUE on success, %FALSE on error. 428 * 429 * Since: 2.22 430 * 431 * Throws: GException on failure. 432 */ 433 public bool addInetPort(ushort port, ObjectG sourceObject) 434 { 435 GError* err = null; 436 437 auto __p = g_socket_listener_add_inet_port(gSocketListener, port, (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err) != 0; 438 439 if (err !is null) 440 { 441 throw new GException( new ErrorG(err) ); 442 } 443 444 return __p; 445 } 446 447 /** 448 * Adds @socket to the set of sockets that we try to accept 449 * new clients from. The socket must be bound to a local 450 * address and listened to. 451 * 452 * @source_object will be passed out in the various calls 453 * to accept to identify this particular source, which is 454 * useful if you're listening on multiple addresses and do 455 * different things depending on what address is connected to. 456 * 457 * The @socket will not be automatically closed when the @listener is finalized 458 * unless the listener held the final reference to the socket. Before GLib 2.42, 459 * the @socket was automatically closed on finalization of the @listener, even 460 * if references to it were held elsewhere. 461 * 462 * Params: 463 * socket = a listening #GSocket 464 * sourceObject = Optional #GObject identifying this source 465 * 466 * Returns: %TRUE on success, %FALSE on error. 467 * 468 * Since: 2.22 469 * 470 * Throws: GException on failure. 471 */ 472 public bool addSocket(Socket socket, ObjectG sourceObject) 473 { 474 GError* err = null; 475 476 auto __p = g_socket_listener_add_socket(gSocketListener, (socket is null) ? null : socket.getSocketStruct(), (sourceObject is null) ? null : sourceObject.getObjectGStruct(), &err) != 0; 477 478 if (err !is null) 479 { 480 throw new GException( new ErrorG(err) ); 481 } 482 483 return __p; 484 } 485 486 /** 487 * Closes all the sockets in the listener. 488 * 489 * Since: 2.22 490 */ 491 public void close() 492 { 493 g_socket_listener_close(gSocketListener); 494 } 495 496 /** 497 * Sets the listen backlog on the sockets in the listener. This must be called 498 * before adding any sockets, addresses or ports to the #GSocketListener (for 499 * example, by calling g_socket_listener_add_inet_port()) to be effective. 500 * 501 * See g_socket_set_listen_backlog() for details 502 * 503 * Params: 504 * listenBacklog = an integer 505 * 506 * Since: 2.22 507 */ 508 public void setBacklog(int listenBacklog) 509 { 510 g_socket_listener_set_backlog(gSocketListener, listenBacklog); 511 } 512 513 /** 514 * Emitted when @listener's activity on @socket changes state. 515 * Note that when @listener is used to listen on both IPv4 and 516 * IPv6, a separate set of signals will be emitted for each, and 517 * the order they happen in is undefined. 518 * 519 * Params: 520 * event = the event that is occurring 521 * socket = the #GSocket the event is occurring on 522 * 523 * Since: 2.46 524 */ 525 gulong addOnEvent(void delegate(GSocketListenerEvent, Socket, SocketListener) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 526 { 527 return Signals.connect(this, "event", dlg, connectFlags ^ ConnectFlags.SWAPPED); 528 } 529 }