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