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.Socket; 26 27 private import gio.Cancellable; 28 private import gio.Credentials; 29 private import gio.DatagramBasedIF; 30 private import gio.DatagramBasedT; 31 private import gio.InetAddress; 32 private import gio.InitableIF; 33 private import gio.InitableT; 34 private import gio.SocketAddress; 35 private import gio.SocketConnection; 36 private import gio.SocketControlMessage; 37 private import gio.c.functions; 38 public import gio.c.types; 39 private import glib.ConstructionException; 40 private import glib.ErrorG; 41 private import glib.GException; 42 private import glib.Source; 43 private import glib.Str; 44 private import gobject.ObjectG; 45 46 47 /** 48 * A #GSocket is a low-level networking primitive. It is a more or less 49 * direct mapping of the BSD socket API in a portable GObject based API. 50 * It supports both the UNIX socket implementations and winsock2 on Windows. 51 * 52 * #GSocket is the platform independent base upon which the higher level 53 * network primitives are based. Applications are not typically meant to 54 * use it directly, but rather through classes like #GSocketClient, 55 * #GSocketService and #GSocketConnection. However there may be cases where 56 * direct use of #GSocket is useful. 57 * 58 * #GSocket implements the #GInitable interface, so if it is manually constructed 59 * by e.g. g_object_new() you must call g_initable_init() and check the 60 * results before using the object. This is done automatically in 61 * g_socket_new() and g_socket_new_from_fd(), so these functions can return 62 * %NULL. 63 * 64 * Sockets operate in two general modes, blocking or non-blocking. When 65 * in blocking mode all operations (which don’t take an explicit blocking 66 * parameter) block until the requested operation 67 * is finished or there is an error. In non-blocking mode all calls that 68 * would block return immediately with a %G_IO_ERROR_WOULD_BLOCK error. 69 * To know when a call would successfully run you can call g_socket_condition_check(), 70 * or g_socket_condition_wait(). You can also use g_socket_create_source() and 71 * attach it to a #GMainContext to get callbacks when I/O is possible. 72 * Note that all sockets are always set to non blocking mode in the system, and 73 * blocking mode is emulated in GSocket. 74 * 75 * When working in non-blocking mode applications should always be able to 76 * handle getting a %G_IO_ERROR_WOULD_BLOCK error even when some other 77 * function said that I/O was possible. This can easily happen in case 78 * of a race condition in the application, but it can also happen for other 79 * reasons. For instance, on Windows a socket is always seen as writable 80 * until a write returns %G_IO_ERROR_WOULD_BLOCK. 81 * 82 * #GSockets can be either connection oriented or datagram based. 83 * For connection oriented types you must first establish a connection by 84 * either connecting to an address or accepting a connection from another 85 * address. For connectionless socket types the target/source address is 86 * specified or received in each I/O operation. 87 * 88 * All socket file descriptors are set to be close-on-exec. 89 * 90 * Note that creating a #GSocket causes the signal %SIGPIPE to be 91 * ignored for the remainder of the program. If you are writing a 92 * command-line utility that uses #GSocket, you may need to take into 93 * account the fact that your program will not automatically be killed 94 * if it tries to write to %stdout after it has been closed. 95 * 96 * Like most other APIs in GLib, #GSocket is not inherently thread safe. To use 97 * a #GSocket concurrently from multiple threads, you must implement your own 98 * locking. 99 * 100 * Since: 2.22 101 */ 102 public class Socket : ObjectG, DatagramBasedIF, InitableIF 103 { 104 /** the main Gtk struct */ 105 protected GSocket* gSocket; 106 107 /** Get the main Gtk struct */ 108 public GSocket* getSocketStruct(bool transferOwnership = false) 109 { 110 if (transferOwnership) 111 ownedRef = false; 112 return gSocket; 113 } 114 115 /** the main Gtk struct as a void* */ 116 protected override void* getStruct() 117 { 118 return cast(void*)gSocket; 119 } 120 121 /** 122 * Sets our main struct and passes it to the parent class. 123 */ 124 public this (GSocket* gSocket, bool ownedRef = false) 125 { 126 this.gSocket = gSocket; 127 super(cast(GObject*)gSocket, ownedRef); 128 } 129 130 // add the DatagramBased capabilities 131 mixin DatagramBasedT!(GSocket); 132 133 // add the Initable capabilities 134 mixin InitableT!(GSocket); 135 136 137 /** */ 138 public static GType getType() 139 { 140 return g_socket_get_type(); 141 } 142 143 /** 144 * Creates a new #GSocket with the defined family, type and protocol. 145 * If @protocol is 0 (%G_SOCKET_PROTOCOL_DEFAULT) the default protocol type 146 * for the family and type is used. 147 * 148 * The @protocol is a family and type specific int that specifies what 149 * kind of protocol to use. #GSocketProtocol lists several common ones. 150 * Many families only support one protocol, and use 0 for this, others 151 * support several and using 0 means to use the default protocol for 152 * the family and type. 153 * 154 * The protocol id is passed directly to the operating 155 * system, so you can use protocols not listed in #GSocketProtocol if you 156 * know the protocol number used for it. 157 * 158 * Params: 159 * family = the socket family to use, e.g. %G_SOCKET_FAMILY_IPV4. 160 * type = the socket type to use. 161 * protocol = the id of the protocol to use, or 0 for default. 162 * 163 * Returns: a #GSocket or %NULL on error. 164 * Free the returned object with g_object_unref(). 165 * 166 * Since: 2.22 167 * 168 * Throws: GException on failure. 169 * Throws: ConstructionException GTK+ fails to create the object. 170 */ 171 public this(GSocketFamily family, GSocketType type, GSocketProtocol protocol) 172 { 173 GError* err = null; 174 175 auto __p = g_socket_new(family, type, protocol, &err); 176 177 if (err !is null) 178 { 179 throw new GException( new ErrorG(err) ); 180 } 181 182 if(__p is null) 183 { 184 throw new ConstructionException("null returned by new"); 185 } 186 187 this(cast(GSocket*) __p, true); 188 } 189 190 /** 191 * Creates a new #GSocket from a native file descriptor 192 * or winsock SOCKET handle. 193 * 194 * This reads all the settings from the file descriptor so that 195 * all properties should work. Note that the file descriptor 196 * will be set to non-blocking mode, independent on the blocking 197 * mode of the #GSocket. 198 * 199 * On success, the returned #GSocket takes ownership of @fd. On failure, the 200 * caller must close @fd themselves. 201 * 202 * Since GLib 2.46, it is no longer a fatal error to call this on a non-socket 203 * descriptor. Instead, a GError will be set with code %G_IO_ERROR_FAILED 204 * 205 * Params: 206 * fd = a native socket file descriptor. 207 * 208 * Returns: a #GSocket or %NULL on error. 209 * Free the returned object with g_object_unref(). 210 * 211 * Since: 2.22 212 * 213 * Throws: GException on failure. 214 * Throws: ConstructionException GTK+ fails to create the object. 215 */ 216 public this(int fd) 217 { 218 GError* err = null; 219 220 auto __p = g_socket_new_from_fd(fd, &err); 221 222 if (err !is null) 223 { 224 throw new GException( new ErrorG(err) ); 225 } 226 227 if(__p is null) 228 { 229 throw new ConstructionException("null returned by new_from_fd"); 230 } 231 232 this(cast(GSocket*) __p, true); 233 } 234 235 /** 236 * Accept incoming connections on a connection-based socket. This removes 237 * the first outstanding connection request from the listening socket and 238 * creates a #GSocket object for it. 239 * 240 * The @socket must be bound to a local address with g_socket_bind() and 241 * must be listening for incoming connections (g_socket_listen()). 242 * 243 * If there are no outstanding connections then the operation will block 244 * or return %G_IO_ERROR_WOULD_BLOCK if non-blocking I/O is enabled. 245 * To be notified of an incoming connection, wait for the %G_IO_IN condition. 246 * 247 * Params: 248 * cancellable = a %GCancellable or %NULL 249 * 250 * Returns: a new #GSocket, or %NULL on error. 251 * Free the returned object with g_object_unref(). 252 * 253 * Since: 2.22 254 * 255 * Throws: GException on failure. 256 */ 257 public Socket accept(Cancellable cancellable) 258 { 259 GError* err = null; 260 261 auto __p = g_socket_accept(gSocket, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 262 263 if (err !is null) 264 { 265 throw new GException( new ErrorG(err) ); 266 } 267 268 if(__p is null) 269 { 270 return null; 271 } 272 273 return ObjectG.getDObject!(Socket)(cast(GSocket*) __p, true); 274 } 275 276 /** 277 * When a socket is created it is attached to an address family, but it 278 * doesn't have an address in this family. g_socket_bind() assigns the 279 * address (sometimes called name) of the socket. 280 * 281 * It is generally required to bind to a local address before you can 282 * receive connections. (See g_socket_listen() and g_socket_accept() ). 283 * In certain situations, you may also want to bind a socket that will be 284 * used to initiate connections, though this is not normally required. 285 * 286 * If @socket is a TCP socket, then @allow_reuse controls the setting 287 * of the `SO_REUSEADDR` socket option; normally it should be %TRUE for 288 * server sockets (sockets that you will eventually call 289 * g_socket_accept() on), and %FALSE for client sockets. (Failing to 290 * set this flag on a server socket may cause g_socket_bind() to return 291 * %G_IO_ERROR_ADDRESS_IN_USE if the server program is stopped and then 292 * immediately restarted.) 293 * 294 * If @socket is a UDP socket, then @allow_reuse determines whether or 295 * not other UDP sockets can be bound to the same address at the same 296 * time. In particular, you can have several UDP sockets bound to the 297 * same address, and they will all receive all of the multicast and 298 * broadcast packets sent to that address. (The behavior of unicast 299 * UDP packets to an address with multiple listeners is not defined.) 300 * 301 * Params: 302 * address = a #GSocketAddress specifying the local address. 303 * allowReuse = whether to allow reusing this address 304 * 305 * Returns: %TRUE on success, %FALSE on error. 306 * 307 * Since: 2.22 308 * 309 * Throws: GException on failure. 310 */ 311 public bool bind(SocketAddress address, bool allowReuse) 312 { 313 GError* err = null; 314 315 auto __p = g_socket_bind(gSocket, (address is null) ? null : address.getSocketAddressStruct(), allowReuse, &err) != 0; 316 317 if (err !is null) 318 { 319 throw new GException( new ErrorG(err) ); 320 } 321 322 return __p; 323 } 324 325 /** 326 * Checks and resets the pending connect error for the socket. 327 * This is used to check for errors when g_socket_connect() is 328 * used in non-blocking mode. 329 * 330 * Returns: %TRUE if no error, %FALSE otherwise, setting @error to the error 331 * 332 * Since: 2.22 333 * 334 * Throws: GException on failure. 335 */ 336 public bool checkConnectResult() 337 { 338 GError* err = null; 339 340 auto __p = g_socket_check_connect_result(gSocket, &err) != 0; 341 342 if (err !is null) 343 { 344 throw new GException( new ErrorG(err) ); 345 } 346 347 return __p; 348 } 349 350 /** 351 * Closes the socket, shutting down any active connection. 352 * 353 * Closing a socket does not wait for all outstanding I/O operations 354 * to finish, so the caller should not rely on them to be guaranteed 355 * to complete even if the close returns with no error. 356 * 357 * Once the socket is closed, all other operations will return 358 * %G_IO_ERROR_CLOSED. Closing a socket multiple times will not 359 * return an error. 360 * 361 * Sockets will be automatically closed when the last reference 362 * is dropped, but you might want to call this function to make sure 363 * resources are released as early as possible. 364 * 365 * Beware that due to the way that TCP works, it is possible for 366 * recently-sent data to be lost if either you close a socket while the 367 * %G_IO_IN condition is set, or else if the remote connection tries to 368 * send something to you after you close the socket but before it has 369 * finished reading all of the data you sent. There is no easy generic 370 * way to avoid this problem; the easiest fix is to design the network 371 * protocol such that the client will never send data "out of turn". 372 * Another solution is for the server to half-close the connection by 373 * calling g_socket_shutdown() with only the @shutdown_write flag set, 374 * and then wait for the client to notice this and close its side of the 375 * connection, after which the server can safely call g_socket_close(). 376 * (This is what #GTcpConnection does if you call 377 * g_tcp_connection_set_graceful_disconnect(). But of course, this 378 * only works if the client will close its connection after the server 379 * does.) 380 * 381 * Returns: %TRUE on success, %FALSE on error 382 * 383 * Since: 2.22 384 * 385 * Throws: GException on failure. 386 */ 387 public bool close() 388 { 389 GError* err = null; 390 391 auto __p = g_socket_close(gSocket, &err) != 0; 392 393 if (err !is null) 394 { 395 throw new GException( new ErrorG(err) ); 396 } 397 398 return __p; 399 } 400 401 /** 402 * Checks on the readiness of @socket to perform operations. 403 * The operations specified in @condition are checked for and masked 404 * against the currently-satisfied conditions on @socket. The result 405 * is returned. 406 * 407 * Note that on Windows, it is possible for an operation to return 408 * %G_IO_ERROR_WOULD_BLOCK even immediately after 409 * g_socket_condition_check() has claimed that the socket is ready for 410 * writing. Rather than calling g_socket_condition_check() and then 411 * writing to the socket if it succeeds, it is generally better to 412 * simply try writing to the socket right away, and try again later if 413 * the initial attempt returns %G_IO_ERROR_WOULD_BLOCK. 414 * 415 * It is meaningless to specify %G_IO_ERR or %G_IO_HUP in condition; 416 * these conditions will always be set in the output if they are true. 417 * 418 * This call never blocks. 419 * 420 * Params: 421 * condition = a #GIOCondition mask to check 422 * 423 * Returns: the @GIOCondition mask of the current state 424 * 425 * Since: 2.22 426 */ 427 public GIOCondition conditionCheck(GIOCondition condition) 428 { 429 return g_socket_condition_check(gSocket, condition); 430 } 431 432 /** 433 * Waits for up to @timeout_us microseconds for @condition to become true 434 * on @socket. If the condition is met, %TRUE is returned. 435 * 436 * If @cancellable is cancelled before the condition is met, or if 437 * @timeout_us (or the socket's #GSocket:timeout) is reached before the 438 * condition is met, then %FALSE is returned and @error, if non-%NULL, 439 * is set to the appropriate value (%G_IO_ERROR_CANCELLED or 440 * %G_IO_ERROR_TIMED_OUT). 441 * 442 * If you don't want a timeout, use g_socket_condition_wait(). 443 * (Alternatively, you can pass -1 for @timeout_us.) 444 * 445 * Note that although @timeout_us is in microseconds for consistency with 446 * other GLib APIs, this function actually only has millisecond 447 * resolution, and the behavior is undefined if @timeout_us is not an 448 * exact number of milliseconds. 449 * 450 * Params: 451 * condition = a #GIOCondition mask to wait for 452 * timeoutUs = the maximum time (in microseconds) to wait, or -1 453 * cancellable = a #GCancellable, or %NULL 454 * 455 * Returns: %TRUE if the condition was met, %FALSE otherwise 456 * 457 * Since: 2.32 458 * 459 * Throws: GException on failure. 460 */ 461 public bool conditionTimedWait(GIOCondition condition, long timeoutUs, Cancellable cancellable) 462 { 463 GError* err = null; 464 465 auto __p = g_socket_condition_timed_wait(gSocket, condition, timeoutUs, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 466 467 if (err !is null) 468 { 469 throw new GException( new ErrorG(err) ); 470 } 471 472 return __p; 473 } 474 475 /** 476 * Waits for @condition to become true on @socket. When the condition 477 * is met, %TRUE is returned. 478 * 479 * If @cancellable is cancelled before the condition is met, or if the 480 * socket has a timeout set and it is reached before the condition is 481 * met, then %FALSE is returned and @error, if non-%NULL, is set to 482 * the appropriate value (%G_IO_ERROR_CANCELLED or 483 * %G_IO_ERROR_TIMED_OUT). 484 * 485 * See also g_socket_condition_timed_wait(). 486 * 487 * Params: 488 * condition = a #GIOCondition mask to wait for 489 * cancellable = a #GCancellable, or %NULL 490 * 491 * Returns: %TRUE if the condition was met, %FALSE otherwise 492 * 493 * Since: 2.22 494 * 495 * Throws: GException on failure. 496 */ 497 public bool conditionWait(GIOCondition condition, Cancellable cancellable) 498 { 499 GError* err = null; 500 501 auto __p = g_socket_condition_wait(gSocket, condition, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 502 503 if (err !is null) 504 { 505 throw new GException( new ErrorG(err) ); 506 } 507 508 return __p; 509 } 510 511 /** 512 * Connect the socket to the specified remote address. 513 * 514 * For connection oriented socket this generally means we attempt to make 515 * a connection to the @address. For a connection-less socket it sets 516 * the default address for g_socket_send() and discards all incoming datagrams 517 * from other sources. 518 * 519 * Generally connection oriented sockets can only connect once, but 520 * connection-less sockets can connect multiple times to change the 521 * default address. 522 * 523 * If the connect call needs to do network I/O it will block, unless 524 * non-blocking I/O is enabled. Then %G_IO_ERROR_PENDING is returned 525 * and the user can be notified of the connection finishing by waiting 526 * for the G_IO_OUT condition. The result of the connection must then be 527 * checked with g_socket_check_connect_result(). 528 * 529 * Params: 530 * address = a #GSocketAddress specifying the remote address. 531 * cancellable = a %GCancellable or %NULL 532 * 533 * Returns: %TRUE if connected, %FALSE on error. 534 * 535 * Since: 2.22 536 * 537 * Throws: GException on failure. 538 */ 539 public bool connect(SocketAddress address, Cancellable cancellable) 540 { 541 GError* err = null; 542 543 auto __p = g_socket_connect(gSocket, (address is null) ? null : address.getSocketAddressStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 544 545 if (err !is null) 546 { 547 throw new GException( new ErrorG(err) ); 548 } 549 550 return __p; 551 } 552 553 /** 554 * Creates a #GSocketConnection subclass of the right type for 555 * @socket. 556 * 557 * Returns: a #GSocketConnection 558 * 559 * Since: 2.22 560 */ 561 public SocketConnection connectionFactoryCreateConnection() 562 { 563 auto __p = g_socket_connection_factory_create_connection(gSocket); 564 565 if(__p is null) 566 { 567 return null; 568 } 569 570 return ObjectG.getDObject!(SocketConnection)(cast(GSocketConnection*) __p, true); 571 } 572 573 /** 574 * Creates a #GSource that can be attached to a %GMainContext to monitor 575 * for the availability of the specified @condition on the socket. The #GSource 576 * keeps a reference to the @socket. 577 * 578 * The callback on the source is of the #GSocketSourceFunc type. 579 * 580 * It is meaningless to specify %G_IO_ERR or %G_IO_HUP in @condition; 581 * these conditions will always be reported output if they are true. 582 * 583 * @cancellable if not %NULL can be used to cancel the source, which will 584 * cause the source to trigger, reporting the current condition (which 585 * is likely 0 unless cancellation happened at the same time as a 586 * condition change). You can check for this in the callback using 587 * g_cancellable_is_cancelled(). 588 * 589 * If @socket has a timeout set, and it is reached before @condition 590 * occurs, the source will then trigger anyway, reporting %G_IO_IN or 591 * %G_IO_OUT depending on @condition. However, @socket will have been 592 * marked as having had a timeout, and so the next #GSocket I/O method 593 * you call will then fail with a %G_IO_ERROR_TIMED_OUT. 594 * 595 * Params: 596 * condition = a #GIOCondition mask to monitor 597 * cancellable = a %GCancellable or %NULL 598 * 599 * Returns: a newly allocated %GSource, free with g_source_unref(). 600 * 601 * Since: 2.22 602 */ 603 public Source createSource(GIOCondition condition, Cancellable cancellable) 604 { 605 auto __p = g_socket_create_source(gSocket, condition, (cancellable is null) ? null : cancellable.getCancellableStruct()); 606 607 if(__p is null) 608 { 609 return null; 610 } 611 612 return new Source(cast(GSource*) __p, true); 613 } 614 615 /** 616 * Get the amount of data pending in the OS input buffer, without blocking. 617 * 618 * If @socket is a UDP or SCTP socket, this will return the size of 619 * just the next packet, even if additional packets are buffered after 620 * that one. 621 * 622 * Note that on Windows, this function is rather inefficient in the 623 * UDP case, and so if you know any plausible upper bound on the size 624 * of the incoming packet, it is better to just do a 625 * g_socket_receive() with a buffer of that size, rather than calling 626 * g_socket_get_available_bytes() first and then doing a receive of 627 * exactly the right size. 628 * 629 * Returns: the number of bytes that can be read from the socket 630 * without blocking or truncating, or -1 on error. 631 * 632 * Since: 2.32 633 */ 634 public ptrdiff_t getAvailableBytes() 635 { 636 return g_socket_get_available_bytes(gSocket); 637 } 638 639 /** 640 * Gets the blocking mode of the socket. For details on blocking I/O, 641 * see g_socket_set_blocking(). 642 * 643 * Returns: %TRUE if blocking I/O is used, %FALSE otherwise. 644 * 645 * Since: 2.22 646 */ 647 public bool getBlocking() 648 { 649 return g_socket_get_blocking(gSocket) != 0; 650 } 651 652 /** 653 * Gets the broadcast setting on @socket; if %TRUE, 654 * it is possible to send packets to broadcast 655 * addresses. 656 * 657 * Returns: the broadcast setting on @socket 658 * 659 * Since: 2.32 660 */ 661 public bool getBroadcast() 662 { 663 return g_socket_get_broadcast(gSocket) != 0; 664 } 665 666 /** 667 * Returns the credentials of the foreign process connected to this 668 * socket, if any (e.g. it is only supported for %G_SOCKET_FAMILY_UNIX 669 * sockets). 670 * 671 * If this operation isn't supported on the OS, the method fails with 672 * the %G_IO_ERROR_NOT_SUPPORTED error. On Linux this is implemented 673 * by reading the %SO_PEERCRED option on the underlying socket. 674 * 675 * This method can be expected to be available on the following platforms: 676 * 677 * - Linux since GLib 2.26 678 * - OpenBSD since GLib 2.30 679 * - Solaris, Illumos and OpenSolaris since GLib 2.40 680 * - NetBSD since GLib 2.42 681 * - macOS, tvOS, iOS since GLib 2.66 682 * 683 * Other ways to obtain credentials from a foreign peer includes the 684 * #GUnixCredentialsMessage type and 685 * g_unix_connection_send_credentials() / 686 * g_unix_connection_receive_credentials() functions. 687 * 688 * Returns: %NULL if @error is set, otherwise a #GCredentials object 689 * that must be freed with g_object_unref(). 690 * 691 * Since: 2.26 692 * 693 * Throws: GException on failure. 694 */ 695 public Credentials getCredentials() 696 { 697 GError* err = null; 698 699 auto __p = g_socket_get_credentials(gSocket, &err); 700 701 if (err !is null) 702 { 703 throw new GException( new ErrorG(err) ); 704 } 705 706 if(__p is null) 707 { 708 return null; 709 } 710 711 return ObjectG.getDObject!(Credentials)(cast(GCredentials*) __p, true); 712 } 713 714 /** 715 * Gets the socket family of the socket. 716 * 717 * Returns: a #GSocketFamily 718 * 719 * Since: 2.22 720 */ 721 public GSocketFamily getFamily() 722 { 723 return g_socket_get_family(gSocket); 724 } 725 726 /** 727 * Returns the underlying OS socket object. On unix this 728 * is a socket file descriptor, and on Windows this is 729 * a Winsock2 SOCKET handle. This may be useful for 730 * doing platform specific or otherwise unusual operations 731 * on the socket. 732 * 733 * Returns: the file descriptor of the socket. 734 * 735 * Since: 2.22 736 */ 737 public int getFd() 738 { 739 return g_socket_get_fd(gSocket); 740 } 741 742 /** 743 * Gets the keepalive mode of the socket. For details on this, 744 * see g_socket_set_keepalive(). 745 * 746 * Returns: %TRUE if keepalive is active, %FALSE otherwise. 747 * 748 * Since: 2.22 749 */ 750 public bool getKeepalive() 751 { 752 return g_socket_get_keepalive(gSocket) != 0; 753 } 754 755 /** 756 * Gets the listen backlog setting of the socket. For details on this, 757 * see g_socket_set_listen_backlog(). 758 * 759 * Returns: the maximum number of pending connections. 760 * 761 * Since: 2.22 762 */ 763 public int getListenBacklog() 764 { 765 return g_socket_get_listen_backlog(gSocket); 766 } 767 768 /** 769 * Try to get the local address of a bound socket. This is only 770 * useful if the socket has been bound to a local address, 771 * either explicitly or implicitly when connecting. 772 * 773 * Returns: a #GSocketAddress or %NULL on error. 774 * Free the returned object with g_object_unref(). 775 * 776 * Since: 2.22 777 * 778 * Throws: GException on failure. 779 */ 780 public SocketAddress getLocalAddress() 781 { 782 GError* err = null; 783 784 auto __p = g_socket_get_local_address(gSocket, &err); 785 786 if (err !is null) 787 { 788 throw new GException( new ErrorG(err) ); 789 } 790 791 if(__p is null) 792 { 793 return null; 794 } 795 796 return ObjectG.getDObject!(SocketAddress)(cast(GSocketAddress*) __p, true); 797 } 798 799 /** 800 * Gets the multicast loopback setting on @socket; if %TRUE (the 801 * default), outgoing multicast packets will be looped back to 802 * multicast listeners on the same host. 803 * 804 * Returns: the multicast loopback setting on @socket 805 * 806 * Since: 2.32 807 */ 808 public bool getMulticastLoopback() 809 { 810 return g_socket_get_multicast_loopback(gSocket) != 0; 811 } 812 813 /** 814 * Gets the multicast time-to-live setting on @socket; see 815 * g_socket_set_multicast_ttl() for more details. 816 * 817 * Returns: the multicast time-to-live setting on @socket 818 * 819 * Since: 2.32 820 */ 821 public uint getMulticastTtl() 822 { 823 return g_socket_get_multicast_ttl(gSocket); 824 } 825 826 /** 827 * Gets the value of an integer-valued option on @socket, as with 828 * getsockopt(). (If you need to fetch a non-integer-valued option, 829 * you will need to call getsockopt() directly.) 830 * 831 * The [<gio/gnetworking.h>][gio-gnetworking.h] 832 * header pulls in system headers that will define most of the 833 * standard/portable socket options. For unusual socket protocols or 834 * platform-dependent options, you may need to include additional 835 * headers. 836 * 837 * Note that even for socket options that are a single byte in size, 838 * @value is still a pointer to a #gint variable, not a #guchar; 839 * g_socket_get_option() will handle the conversion internally. 840 * 841 * Params: 842 * level = the "API level" of the option (eg, `SOL_SOCKET`) 843 * optname = the "name" of the option (eg, `SO_BROADCAST`) 844 * value = return location for the option value 845 * 846 * Returns: success or failure. On failure, @error will be set, and 847 * the system error value (`errno` or WSAGetLastError()) will still 848 * be set to the result of the getsockopt() call. 849 * 850 * Since: 2.36 851 * 852 * Throws: GException on failure. 853 */ 854 public bool getOption(int level, int optname, out int value) 855 { 856 GError* err = null; 857 858 auto __p = g_socket_get_option(gSocket, level, optname, &value, &err) != 0; 859 860 if (err !is null) 861 { 862 throw new GException( new ErrorG(err) ); 863 } 864 865 return __p; 866 } 867 868 /** 869 * Gets the socket protocol id the socket was created with. 870 * In case the protocol is unknown, -1 is returned. 871 * 872 * Returns: a protocol id, or -1 if unknown 873 * 874 * Since: 2.22 875 */ 876 public GSocketProtocol getProtocol() 877 { 878 return g_socket_get_protocol(gSocket); 879 } 880 881 /** 882 * Try to get the remote address of a connected socket. This is only 883 * useful for connection oriented sockets that have been connected. 884 * 885 * Returns: a #GSocketAddress or %NULL on error. 886 * Free the returned object with g_object_unref(). 887 * 888 * Since: 2.22 889 * 890 * Throws: GException on failure. 891 */ 892 public SocketAddress getRemoteAddress() 893 { 894 GError* err = null; 895 896 auto __p = g_socket_get_remote_address(gSocket, &err); 897 898 if (err !is null) 899 { 900 throw new GException( new ErrorG(err) ); 901 } 902 903 if(__p is null) 904 { 905 return null; 906 } 907 908 return ObjectG.getDObject!(SocketAddress)(cast(GSocketAddress*) __p, true); 909 } 910 911 /** 912 * Gets the socket type of the socket. 913 * 914 * Returns: a #GSocketType 915 * 916 * Since: 2.22 917 */ 918 public GSocketType getSocketType() 919 { 920 return g_socket_get_socket_type(gSocket); 921 } 922 923 /** 924 * Gets the timeout setting of the socket. For details on this, see 925 * g_socket_set_timeout(). 926 * 927 * Returns: the timeout in seconds 928 * 929 * Since: 2.26 930 */ 931 public uint getTimeout() 932 { 933 return g_socket_get_timeout(gSocket); 934 } 935 936 /** 937 * Gets the unicast time-to-live setting on @socket; see 938 * g_socket_set_ttl() for more details. 939 * 940 * Returns: the time-to-live setting on @socket 941 * 942 * Since: 2.32 943 */ 944 public uint getTtl() 945 { 946 return g_socket_get_ttl(gSocket); 947 } 948 949 /** 950 * Checks whether a socket is closed. 951 * 952 * Returns: %TRUE if socket is closed, %FALSE otherwise 953 * 954 * Since: 2.22 955 */ 956 public bool isClosed() 957 { 958 return g_socket_is_closed(gSocket) != 0; 959 } 960 961 /** 962 * Check whether the socket is connected. This is only useful for 963 * connection-oriented sockets. 964 * 965 * If using g_socket_shutdown(), this function will return %TRUE until the 966 * socket has been shut down for reading and writing. If you do a non-blocking 967 * connect, this function will not return %TRUE until after you call 968 * g_socket_check_connect_result(). 969 * 970 * Returns: %TRUE if socket is connected, %FALSE otherwise. 971 * 972 * Since: 2.22 973 */ 974 public bool isConnected() 975 { 976 return g_socket_is_connected(gSocket) != 0; 977 } 978 979 /** 980 * Registers @socket to receive multicast messages sent to @group. 981 * @socket must be a %G_SOCKET_TYPE_DATAGRAM socket, and must have 982 * been bound to an appropriate interface and port with 983 * g_socket_bind(). 984 * 985 * If @iface is %NULL, the system will automatically pick an interface 986 * to bind to based on @group. 987 * 988 * If @source_specific is %TRUE, source-specific multicast as defined 989 * in RFC 4604 is used. Note that on older platforms this may fail 990 * with a %G_IO_ERROR_NOT_SUPPORTED error. 991 * 992 * To bind to a given source-specific multicast address, use 993 * g_socket_join_multicast_group_ssm() instead. 994 * 995 * Params: 996 * group = a #GInetAddress specifying the group address to join. 997 * sourceSpecific = %TRUE if source-specific multicast should be used 998 * iface = Name of the interface to use, or %NULL 999 * 1000 * Returns: %TRUE on success, %FALSE on error. 1001 * 1002 * Since: 2.32 1003 * 1004 * Throws: GException on failure. 1005 */ 1006 public bool joinMulticastGroup(InetAddress group, bool sourceSpecific, string iface) 1007 { 1008 GError* err = null; 1009 1010 auto __p = g_socket_join_multicast_group(gSocket, (group is null) ? null : group.getInetAddressStruct(), sourceSpecific, Str.toStringz(iface), &err) != 0; 1011 1012 if (err !is null) 1013 { 1014 throw new GException( new ErrorG(err) ); 1015 } 1016 1017 return __p; 1018 } 1019 1020 /** 1021 * Registers @socket to receive multicast messages sent to @group. 1022 * @socket must be a %G_SOCKET_TYPE_DATAGRAM socket, and must have 1023 * been bound to an appropriate interface and port with 1024 * g_socket_bind(). 1025 * 1026 * If @iface is %NULL, the system will automatically pick an interface 1027 * to bind to based on @group. 1028 * 1029 * If @source_specific is not %NULL, use source-specific multicast as 1030 * defined in RFC 4604. Note that on older platforms this may fail 1031 * with a %G_IO_ERROR_NOT_SUPPORTED error. 1032 * 1033 * Note that this function can be called multiple times for the same 1034 * @group with different @source_specific in order to receive multicast 1035 * packets from more than one source. 1036 * 1037 * Params: 1038 * group = a #GInetAddress specifying the group address to join. 1039 * sourceSpecific = a #GInetAddress specifying the 1040 * source-specific multicast address or %NULL to ignore. 1041 * iface = Name of the interface to use, or %NULL 1042 * 1043 * Returns: %TRUE on success, %FALSE on error. 1044 * 1045 * Since: 2.56 1046 * 1047 * Throws: GException on failure. 1048 */ 1049 public bool joinMulticastGroupSsm(InetAddress group, InetAddress sourceSpecific, string iface) 1050 { 1051 GError* err = null; 1052 1053 auto __p = g_socket_join_multicast_group_ssm(gSocket, (group is null) ? null : group.getInetAddressStruct(), (sourceSpecific is null) ? null : sourceSpecific.getInetAddressStruct(), Str.toStringz(iface), &err) != 0; 1054 1055 if (err !is null) 1056 { 1057 throw new GException( new ErrorG(err) ); 1058 } 1059 1060 return __p; 1061 } 1062 1063 /** 1064 * Removes @socket from the multicast group defined by @group, @iface, 1065 * and @source_specific (which must all have the same values they had 1066 * when you joined the group). 1067 * 1068 * @socket remains bound to its address and port, and can still receive 1069 * unicast messages after calling this. 1070 * 1071 * To unbind to a given source-specific multicast address, use 1072 * g_socket_leave_multicast_group_ssm() instead. 1073 * 1074 * Params: 1075 * group = a #GInetAddress specifying the group address to leave. 1076 * sourceSpecific = %TRUE if source-specific multicast was used 1077 * iface = Interface used 1078 * 1079 * Returns: %TRUE on success, %FALSE on error. 1080 * 1081 * Since: 2.32 1082 * 1083 * Throws: GException on failure. 1084 */ 1085 public bool leaveMulticastGroup(InetAddress group, bool sourceSpecific, string iface) 1086 { 1087 GError* err = null; 1088 1089 auto __p = g_socket_leave_multicast_group(gSocket, (group is null) ? null : group.getInetAddressStruct(), sourceSpecific, Str.toStringz(iface), &err) != 0; 1090 1091 if (err !is null) 1092 { 1093 throw new GException( new ErrorG(err) ); 1094 } 1095 1096 return __p; 1097 } 1098 1099 /** 1100 * Removes @socket from the multicast group defined by @group, @iface, 1101 * and @source_specific (which must all have the same values they had 1102 * when you joined the group). 1103 * 1104 * @socket remains bound to its address and port, and can still receive 1105 * unicast messages after calling this. 1106 * 1107 * Params: 1108 * group = a #GInetAddress specifying the group address to leave. 1109 * sourceSpecific = a #GInetAddress specifying the 1110 * source-specific multicast address or %NULL to ignore. 1111 * iface = Name of the interface to use, or %NULL 1112 * 1113 * Returns: %TRUE on success, %FALSE on error. 1114 * 1115 * Since: 2.56 1116 * 1117 * Throws: GException on failure. 1118 */ 1119 public bool leaveMulticastGroupSsm(InetAddress group, InetAddress sourceSpecific, string iface) 1120 { 1121 GError* err = null; 1122 1123 auto __p = g_socket_leave_multicast_group_ssm(gSocket, (group is null) ? null : group.getInetAddressStruct(), (sourceSpecific is null) ? null : sourceSpecific.getInetAddressStruct(), Str.toStringz(iface), &err) != 0; 1124 1125 if (err !is null) 1126 { 1127 throw new GException( new ErrorG(err) ); 1128 } 1129 1130 return __p; 1131 } 1132 1133 /** 1134 * Marks the socket as a server socket, i.e. a socket that is used 1135 * to accept incoming requests using g_socket_accept(). 1136 * 1137 * Before calling this the socket must be bound to a local address using 1138 * g_socket_bind(). 1139 * 1140 * To set the maximum amount of outstanding clients, use 1141 * g_socket_set_listen_backlog(). 1142 * 1143 * Returns: %TRUE on success, %FALSE on error. 1144 * 1145 * Since: 2.22 1146 * 1147 * Throws: GException on failure. 1148 */ 1149 public bool listen() 1150 { 1151 GError* err = null; 1152 1153 auto __p = g_socket_listen(gSocket, &err) != 0; 1154 1155 if (err !is null) 1156 { 1157 throw new GException( new ErrorG(err) ); 1158 } 1159 1160 return __p; 1161 } 1162 1163 /** 1164 * Receive data (up to @size bytes) from a socket. This is mainly used by 1165 * connection-oriented sockets; it is identical to g_socket_receive_from() 1166 * with @address set to %NULL. 1167 * 1168 * For %G_SOCKET_TYPE_DATAGRAM and %G_SOCKET_TYPE_SEQPACKET sockets, 1169 * g_socket_receive() will always read either 0 or 1 complete messages from 1170 * the socket. If the received message is too large to fit in @buffer, then 1171 * the data beyond @size bytes will be discarded, without any explicit 1172 * indication that this has occurred. 1173 * 1174 * For %G_SOCKET_TYPE_STREAM sockets, g_socket_receive() can return any 1175 * number of bytes, up to @size. If more than @size bytes have been 1176 * received, the additional data will be returned in future calls to 1177 * g_socket_receive(). 1178 * 1179 * If the socket is in blocking mode the call will block until there 1180 * is some data to receive, the connection is closed, or there is an 1181 * error. If there is no data available and the socket is in 1182 * non-blocking mode, a %G_IO_ERROR_WOULD_BLOCK error will be 1183 * returned. To be notified when data is available, wait for the 1184 * %G_IO_IN condition. 1185 * 1186 * On error -1 is returned and @error is set accordingly. 1187 * 1188 * Params: 1189 * buffer = a buffer to read data into (which should be at least @size bytes long). 1190 * cancellable = a %GCancellable or %NULL 1191 * 1192 * Returns: Number of bytes read, or 0 if the connection was closed by 1193 * the peer, or -1 on error 1194 * 1195 * Since: 2.22 1196 * 1197 * Throws: GException on failure. 1198 */ 1199 public ptrdiff_t receive(ref char[] buffer, Cancellable cancellable) 1200 { 1201 GError* err = null; 1202 1203 auto __p = g_socket_receive(gSocket, buffer.ptr, cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1204 1205 if (err !is null) 1206 { 1207 throw new GException( new ErrorG(err) ); 1208 } 1209 1210 return __p; 1211 } 1212 1213 /** 1214 * Receive data (up to @size bytes) from a socket. 1215 * 1216 * If @address is non-%NULL then @address will be set equal to the 1217 * source address of the received packet. 1218 * @address is owned by the caller. 1219 * 1220 * See g_socket_receive() for additional information. 1221 * 1222 * Params: 1223 * address = a pointer to a #GSocketAddress 1224 * pointer, or %NULL 1225 * buffer = a buffer to read data into (which should be at least @size bytes long). 1226 * cancellable = a %GCancellable or %NULL 1227 * 1228 * Returns: Number of bytes read, or 0 if the connection was closed by 1229 * the peer, or -1 on error 1230 * 1231 * Since: 2.22 1232 * 1233 * Throws: GException on failure. 1234 */ 1235 public ptrdiff_t receiveFrom(out SocketAddress address, ref char[] buffer, Cancellable cancellable) 1236 { 1237 GSocketAddress* outaddress = null; 1238 GError* err = null; 1239 1240 auto __p = g_socket_receive_from(gSocket, &outaddress, buffer.ptr, cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1241 1242 if (err !is null) 1243 { 1244 throw new GException( new ErrorG(err) ); 1245 } 1246 1247 address = ObjectG.getDObject!(SocketAddress)(outaddress); 1248 1249 return __p; 1250 } 1251 1252 /** 1253 * Receive data from a socket. For receiving multiple messages, see 1254 * g_socket_receive_messages(); for easier use, see 1255 * g_socket_receive() and g_socket_receive_from(). 1256 * 1257 * If @address is non-%NULL then @address will be set equal to the 1258 * source address of the received packet. 1259 * @address is owned by the caller. 1260 * 1261 * @vector must point to an array of #GInputVector structs and 1262 * @num_vectors must be the length of this array. These structs 1263 * describe the buffers that received data will be scattered into. 1264 * If @num_vectors is -1, then @vectors is assumed to be terminated 1265 * by a #GInputVector with a %NULL buffer pointer. 1266 * 1267 * As a special case, if @num_vectors is 0 (in which case, @vectors 1268 * may of course be %NULL), then a single byte is received and 1269 * discarded. This is to facilitate the common practice of sending a 1270 * single '\0' byte for the purposes of transferring ancillary data. 1271 * 1272 * @messages, if non-%NULL, will be set to point to a newly-allocated 1273 * array of #GSocketControlMessage instances or %NULL if no such 1274 * messages was received. These correspond to the control messages 1275 * received from the kernel, one #GSocketControlMessage per message 1276 * from the kernel. This array is %NULL-terminated and must be freed 1277 * by the caller using g_free() after calling g_object_unref() on each 1278 * element. If @messages is %NULL, any control messages received will 1279 * be discarded. 1280 * 1281 * @num_messages, if non-%NULL, will be set to the number of control 1282 * messages received. 1283 * 1284 * If both @messages and @num_messages are non-%NULL, then 1285 * @num_messages gives the number of #GSocketControlMessage instances 1286 * in @messages (ie: not including the %NULL terminator). 1287 * 1288 * @flags is an in/out parameter. The commonly available arguments 1289 * for this are available in the #GSocketMsgFlags enum, but the 1290 * values there are the same as the system values, and the flags 1291 * are passed in as-is, so you can pass in system-specific flags too 1292 * (and g_socket_receive_message() may pass system-specific flags out). 1293 * Flags passed in to the parameter affect the receive operation; flags returned 1294 * out of it are relevant to the specific returned message. 1295 * 1296 * As with g_socket_receive(), data may be discarded if @socket is 1297 * %G_SOCKET_TYPE_DATAGRAM or %G_SOCKET_TYPE_SEQPACKET and you do not 1298 * provide enough buffer space to read a complete message. You can pass 1299 * %G_SOCKET_MSG_PEEK in @flags to peek at the current message without 1300 * removing it from the receive queue, but there is no portable way to find 1301 * out the length of the message other than by reading it into a 1302 * sufficiently-large buffer. 1303 * 1304 * If the socket is in blocking mode the call will block until there 1305 * is some data to receive, the connection is closed, or there is an 1306 * error. If there is no data available and the socket is in 1307 * non-blocking mode, a %G_IO_ERROR_WOULD_BLOCK error will be 1308 * returned. To be notified when data is available, wait for the 1309 * %G_IO_IN condition. 1310 * 1311 * On error -1 is returned and @error is set accordingly. 1312 * 1313 * Params: 1314 * address = a pointer to a #GSocketAddress 1315 * pointer, or %NULL 1316 * vectors = an array of #GInputVector structs 1317 * messages = a pointer 1318 * which may be filled with an array of #GSocketControlMessages, or %NULL 1319 * flags = a pointer to an int containing #GSocketMsgFlags flags, 1320 * which may additionally contain 1321 * [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html) 1322 * cancellable = a %GCancellable or %NULL 1323 * 1324 * Returns: Number of bytes read, or 0 if the connection was closed by 1325 * the peer, or -1 on error 1326 * 1327 * Since: 2.22 1328 * 1329 * Throws: GException on failure. 1330 */ 1331 public ptrdiff_t receiveMessage(out SocketAddress address, GInputVector[] vectors, out SocketControlMessage[] messages, ref int flags, Cancellable cancellable) 1332 { 1333 GSocketAddress* outaddress = null; 1334 GSocketControlMessage** outmessages = null; 1335 int numMessages; 1336 GError* err = null; 1337 1338 auto __p = g_socket_receive_message(gSocket, &outaddress, vectors.ptr, cast(int)vectors.length, &outmessages, &numMessages, &flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1339 1340 if (err !is null) 1341 { 1342 throw new GException( new ErrorG(err) ); 1343 } 1344 1345 address = ObjectG.getDObject!(SocketAddress)(outaddress); 1346 1347 messages = new SocketControlMessage[numMessages]; 1348 for(size_t i = 0; i < numMessages; i++) 1349 { 1350 messages[i] = ObjectG.getDObject!(SocketControlMessage)(cast(GSocketControlMessage*) outmessages[i]); 1351 } 1352 1353 return __p; 1354 } 1355 1356 /** 1357 * Receive multiple data messages from @socket in one go. This is the most 1358 * complicated and fully-featured version of this call. For easier use, see 1359 * g_socket_receive(), g_socket_receive_from(), and g_socket_receive_message(). 1360 * 1361 * @messages must point to an array of #GInputMessage structs and 1362 * @num_messages must be the length of this array. Each #GInputMessage 1363 * contains a pointer to an array of #GInputVector structs describing the 1364 * buffers that the data received in each message will be written to. Using 1365 * multiple #GInputVectors is more memory-efficient than manually copying data 1366 * out of a single buffer to multiple sources, and more system-call-efficient 1367 * than making multiple calls to g_socket_receive(), such as in scenarios where 1368 * a lot of data packets need to be received (e.g. high-bandwidth video 1369 * streaming over RTP/UDP). 1370 * 1371 * @flags modify how all messages are received. The commonly available 1372 * arguments for this are available in the #GSocketMsgFlags enum, but the 1373 * values there are the same as the system values, and the flags 1374 * are passed in as-is, so you can pass in system-specific flags too. These 1375 * flags affect the overall receive operation. Flags affecting individual 1376 * messages are returned in #GInputMessage.flags. 1377 * 1378 * The other members of #GInputMessage are treated as described in its 1379 * documentation. 1380 * 1381 * If #GSocket:blocking is %TRUE the call will block until @num_messages have 1382 * been received, or the end of the stream is reached. 1383 * 1384 * If #GSocket:blocking is %FALSE the call will return up to @num_messages 1385 * without blocking, or %G_IO_ERROR_WOULD_BLOCK if no messages are queued in the 1386 * operating system to be received. 1387 * 1388 * In blocking mode, if #GSocket:timeout is positive and is reached before any 1389 * messages are received, %G_IO_ERROR_TIMED_OUT is returned, otherwise up to 1390 * @num_messages are returned. (Note: This is effectively the 1391 * behaviour of `MSG_WAITFORONE` with recvmmsg().) 1392 * 1393 * To be notified when messages are available, wait for the 1394 * %G_IO_IN condition. Note though that you may still receive 1395 * %G_IO_ERROR_WOULD_BLOCK from g_socket_receive_messages() even if you were 1396 * previously notified of a %G_IO_IN condition. 1397 * 1398 * If the remote peer closes the connection, any messages queued in the 1399 * operating system will be returned, and subsequent calls to 1400 * g_socket_receive_messages() will return 0 (with no error set). 1401 * 1402 * On error -1 is returned and @error is set accordingly. An error will only 1403 * be returned if zero messages could be received; otherwise the number of 1404 * messages successfully received before the error will be returned. 1405 * 1406 * Params: 1407 * messages = an array of #GInputMessage structs 1408 * flags = an int containing #GSocketMsgFlags flags for the overall operation, 1409 * which may additionally contain 1410 * [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html) 1411 * cancellable = a %GCancellable or %NULL 1412 * 1413 * Returns: number of messages received, or -1 on error. Note that the number 1414 * of messages received may be smaller than @num_messages if in non-blocking 1415 * mode, if the peer closed the connection, or if @num_messages 1416 * was larger than `UIO_MAXIOV` (1024), in which case the caller may re-try 1417 * to receive the remaining messages. 1418 * 1419 * Since: 2.48 1420 * 1421 * Throws: GException on failure. 1422 */ 1423 public int receiveMessages(GInputMessage[] messages, int flags, Cancellable cancellable) 1424 { 1425 GError* err = null; 1426 1427 auto __p = g_socket_receive_messages(gSocket, messages.ptr, cast(uint)messages.length, flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1428 1429 if (err !is null) 1430 { 1431 throw new GException( new ErrorG(err) ); 1432 } 1433 1434 return __p; 1435 } 1436 1437 /** 1438 * This behaves exactly the same as g_socket_receive(), except that 1439 * the choice of blocking or non-blocking behavior is determined by 1440 * the @blocking argument rather than by @socket's properties. 1441 * 1442 * Params: 1443 * buffer = a buffer to read data into (which should be at least @size bytes long). 1444 * blocking = whether to do blocking or non-blocking I/O 1445 * cancellable = a %GCancellable or %NULL 1446 * 1447 * Returns: Number of bytes read, or 0 if the connection was closed by 1448 * the peer, or -1 on error 1449 * 1450 * Since: 2.26 1451 * 1452 * Throws: GException on failure. 1453 */ 1454 public ptrdiff_t receiveWithBlocking(out char[] buffer, bool blocking, Cancellable cancellable) 1455 { 1456 GError* err = null; 1457 1458 auto __p = g_socket_receive_with_blocking(gSocket, buffer.ptr, cast(size_t)buffer.length, blocking, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1459 1460 if (err !is null) 1461 { 1462 throw new GException( new ErrorG(err) ); 1463 } 1464 1465 return __p; 1466 } 1467 1468 /** 1469 * Tries to send @size bytes from @buffer on the socket. This is 1470 * mainly used by connection-oriented sockets; it is identical to 1471 * g_socket_send_to() with @address set to %NULL. 1472 * 1473 * If the socket is in blocking mode the call will block until there is 1474 * space for the data in the socket queue. If there is no space available 1475 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1476 * will be returned. To be notified when space is available, wait for the 1477 * %G_IO_OUT condition. Note though that you may still receive 1478 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1479 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1480 * very common due to the way the underlying APIs work.) 1481 * 1482 * On error -1 is returned and @error is set accordingly. 1483 * 1484 * Params: 1485 * buffer = the buffer 1486 * containing the data to send. 1487 * cancellable = a %GCancellable or %NULL 1488 * 1489 * Returns: Number of bytes written (which may be less than @size), or -1 1490 * on error 1491 * 1492 * Since: 2.22 1493 * 1494 * Throws: GException on failure. 1495 */ 1496 public ptrdiff_t send(string buffer, Cancellable cancellable) 1497 { 1498 GError* err = null; 1499 1500 auto __p = g_socket_send(gSocket, Str.toStringz(buffer), cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1501 1502 if (err !is null) 1503 { 1504 throw new GException( new ErrorG(err) ); 1505 } 1506 1507 return __p; 1508 } 1509 1510 /** 1511 * Send data to @address on @socket. For sending multiple messages see 1512 * g_socket_send_messages(); for easier use, see 1513 * g_socket_send() and g_socket_send_to(). 1514 * 1515 * If @address is %NULL then the message is sent to the default receiver 1516 * (set by g_socket_connect()). 1517 * 1518 * @vectors must point to an array of #GOutputVector structs and 1519 * @num_vectors must be the length of this array. (If @num_vectors is -1, 1520 * then @vectors is assumed to be terminated by a #GOutputVector with a 1521 * %NULL buffer pointer.) The #GOutputVector structs describe the buffers 1522 * that the sent data will be gathered from. Using multiple 1523 * #GOutputVectors is more memory-efficient than manually copying 1524 * data from multiple sources into a single buffer, and more 1525 * network-efficient than making multiple calls to g_socket_send(). 1526 * 1527 * @messages, if non-%NULL, is taken to point to an array of @num_messages 1528 * #GSocketControlMessage instances. These correspond to the control 1529 * messages to be sent on the socket. 1530 * If @num_messages is -1 then @messages is treated as a %NULL-terminated 1531 * array. 1532 * 1533 * @flags modify how the message is sent. The commonly available arguments 1534 * for this are available in the #GSocketMsgFlags enum, but the 1535 * values there are the same as the system values, and the flags 1536 * are passed in as-is, so you can pass in system-specific flags too. 1537 * 1538 * If the socket is in blocking mode the call will block until there is 1539 * space for the data in the socket queue. If there is no space available 1540 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1541 * will be returned. To be notified when space is available, wait for the 1542 * %G_IO_OUT condition. Note though that you may still receive 1543 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1544 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1545 * very common due to the way the underlying APIs work.) 1546 * 1547 * The sum of the sizes of each #GOutputVector in vectors must not be 1548 * greater than %G_MAXSSIZE. If the message can be larger than this, 1549 * then it is mandatory to use the g_socket_send_message_with_timeout() 1550 * function. 1551 * 1552 * On error -1 is returned and @error is set accordingly. 1553 * 1554 * Params: 1555 * address = a #GSocketAddress, or %NULL 1556 * vectors = an array of #GOutputVector structs 1557 * messages = a pointer to an 1558 * array of #GSocketControlMessages, or %NULL. 1559 * flags = an int containing #GSocketMsgFlags flags, which may additionally 1560 * contain [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html) 1561 * cancellable = a %GCancellable or %NULL 1562 * 1563 * Returns: Number of bytes written (which may be less than @size), or -1 1564 * on error 1565 * 1566 * Since: 2.22 1567 * 1568 * Throws: GException on failure. 1569 */ 1570 public ptrdiff_t sendMessage(SocketAddress address, GOutputVector[] vectors, SocketControlMessage[] messages, int flags, Cancellable cancellable) 1571 { 1572 GSocketControlMessage*[] messagesArray = new GSocketControlMessage*[messages.length]; 1573 for ( int i = 0; i < messages.length; i++ ) 1574 { 1575 messagesArray[i] = messages[i].getSocketControlMessageStruct(); 1576 } 1577 1578 GError* err = null; 1579 1580 auto __p = g_socket_send_message(gSocket, (address is null) ? null : address.getSocketAddressStruct(), vectors.ptr, cast(int)vectors.length, messagesArray.ptr, cast(int)messages.length, flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1581 1582 if (err !is null) 1583 { 1584 throw new GException( new ErrorG(err) ); 1585 } 1586 1587 return __p; 1588 } 1589 1590 /** 1591 * This behaves exactly the same as g_socket_send_message(), except that 1592 * the choice of timeout behavior is determined by the @timeout_us argument 1593 * rather than by @socket's properties. 1594 * 1595 * On error %G_POLLABLE_RETURN_FAILED is returned and @error is set accordingly, or 1596 * if the socket is currently not writable %G_POLLABLE_RETURN_WOULD_BLOCK is 1597 * returned. @bytes_written will contain 0 in both cases. 1598 * 1599 * Params: 1600 * address = a #GSocketAddress, or %NULL 1601 * vectors = an array of #GOutputVector structs 1602 * messages = a pointer to an 1603 * array of #GSocketControlMessages, or %NULL. 1604 * flags = an int containing #GSocketMsgFlags flags, which may additionally 1605 * contain [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html) 1606 * timeoutUs = the maximum time (in microseconds) to wait, or -1 1607 * bytesWritten = location to store the number of bytes that were written to the socket 1608 * cancellable = a %GCancellable or %NULL 1609 * 1610 * Returns: %G_POLLABLE_RETURN_OK if all data was successfully written, 1611 * %G_POLLABLE_RETURN_WOULD_BLOCK if the socket is currently not writable, or 1612 * %G_POLLABLE_RETURN_FAILED if an error happened and @error is set. 1613 * 1614 * Since: 2.60 1615 * 1616 * Throws: GException on failure. 1617 */ 1618 public GPollableReturn sendMessageWithTimeout(SocketAddress address, GOutputVector[] vectors, SocketControlMessage[] messages, int flags, long timeoutUs, out size_t bytesWritten, Cancellable cancellable) 1619 { 1620 GSocketControlMessage*[] messagesArray = new GSocketControlMessage*[messages.length]; 1621 for ( int i = 0; i < messages.length; i++ ) 1622 { 1623 messagesArray[i] = messages[i].getSocketControlMessageStruct(); 1624 } 1625 1626 GError* err = null; 1627 1628 auto __p = g_socket_send_message_with_timeout(gSocket, (address is null) ? null : address.getSocketAddressStruct(), vectors.ptr, cast(int)vectors.length, messagesArray.ptr, cast(int)messages.length, flags, timeoutUs, &bytesWritten, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1629 1630 if (err !is null) 1631 { 1632 throw new GException( new ErrorG(err) ); 1633 } 1634 1635 return __p; 1636 } 1637 1638 /** 1639 * Send multiple data messages from @socket in one go. This is the most 1640 * complicated and fully-featured version of this call. For easier use, see 1641 * g_socket_send(), g_socket_send_to(), and g_socket_send_message(). 1642 * 1643 * @messages must point to an array of #GOutputMessage structs and 1644 * @num_messages must be the length of this array. Each #GOutputMessage 1645 * contains an address to send the data to, and a pointer to an array of 1646 * #GOutputVector structs to describe the buffers that the data to be sent 1647 * for each message will be gathered from. Using multiple #GOutputVectors is 1648 * more memory-efficient than manually copying data from multiple sources 1649 * into a single buffer, and more network-efficient than making multiple 1650 * calls to g_socket_send(). Sending multiple messages in one go avoids the 1651 * overhead of making a lot of syscalls in scenarios where a lot of data 1652 * packets need to be sent (e.g. high-bandwidth video streaming over RTP/UDP), 1653 * or where the same data needs to be sent to multiple recipients. 1654 * 1655 * @flags modify how the message is sent. The commonly available arguments 1656 * for this are available in the #GSocketMsgFlags enum, but the 1657 * values there are the same as the system values, and the flags 1658 * are passed in as-is, so you can pass in system-specific flags too. 1659 * 1660 * If the socket is in blocking mode the call will block until there is 1661 * space for all the data in the socket queue. If there is no space available 1662 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1663 * will be returned if no data was written at all, otherwise the number of 1664 * messages sent will be returned. To be notified when space is available, 1665 * wait for the %G_IO_OUT condition. Note though that you may still receive 1666 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1667 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1668 * very common due to the way the underlying APIs work.) 1669 * 1670 * On error -1 is returned and @error is set accordingly. An error will only 1671 * be returned if zero messages could be sent; otherwise the number of messages 1672 * successfully sent before the error will be returned. 1673 * 1674 * Params: 1675 * messages = an array of #GOutputMessage structs 1676 * flags = an int containing #GSocketMsgFlags flags, which may additionally 1677 * contain [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html) 1678 * cancellable = a %GCancellable or %NULL 1679 * 1680 * Returns: number of messages sent, or -1 on error. Note that the number of 1681 * messages sent may be smaller than @num_messages if the socket is 1682 * non-blocking or if @num_messages was larger than UIO_MAXIOV (1024), 1683 * in which case the caller may re-try to send the remaining messages. 1684 * 1685 * Since: 2.44 1686 * 1687 * Throws: GException on failure. 1688 */ 1689 public int sendMessages(GOutputMessage[] messages, int flags, Cancellable cancellable) 1690 { 1691 GError* err = null; 1692 1693 auto __p = g_socket_send_messages(gSocket, messages.ptr, cast(uint)messages.length, flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1694 1695 if (err !is null) 1696 { 1697 throw new GException( new ErrorG(err) ); 1698 } 1699 1700 return __p; 1701 } 1702 1703 /** 1704 * Tries to send @size bytes from @buffer to @address. If @address is 1705 * %NULL then the message is sent to the default receiver (set by 1706 * g_socket_connect()). 1707 * 1708 * See g_socket_send() for additional information. 1709 * 1710 * Params: 1711 * address = a #GSocketAddress, or %NULL 1712 * buffer = the buffer 1713 * containing the data to send. 1714 * cancellable = a %GCancellable or %NULL 1715 * 1716 * Returns: Number of bytes written (which may be less than @size), or -1 1717 * on error 1718 * 1719 * Since: 2.22 1720 * 1721 * Throws: GException on failure. 1722 */ 1723 public ptrdiff_t sendTo(SocketAddress address, string buffer, Cancellable cancellable) 1724 { 1725 GError* err = null; 1726 1727 auto __p = g_socket_send_to(gSocket, (address is null) ? null : address.getSocketAddressStruct(), Str.toStringz(buffer), cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1728 1729 if (err !is null) 1730 { 1731 throw new GException( new ErrorG(err) ); 1732 } 1733 1734 return __p; 1735 } 1736 1737 /** 1738 * This behaves exactly the same as g_socket_send(), except that 1739 * the choice of blocking or non-blocking behavior is determined by 1740 * the @blocking argument rather than by @socket's properties. 1741 * 1742 * Params: 1743 * buffer = the buffer 1744 * containing the data to send. 1745 * blocking = whether to do blocking or non-blocking I/O 1746 * cancellable = a %GCancellable or %NULL 1747 * 1748 * Returns: Number of bytes written (which may be less than @size), or -1 1749 * on error 1750 * 1751 * Since: 2.26 1752 * 1753 * Throws: GException on failure. 1754 */ 1755 public ptrdiff_t sendWithBlocking(string buffer, bool blocking, Cancellable cancellable) 1756 { 1757 GError* err = null; 1758 1759 auto __p = g_socket_send_with_blocking(gSocket, Str.toStringz(buffer), cast(size_t)buffer.length, blocking, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1760 1761 if (err !is null) 1762 { 1763 throw new GException( new ErrorG(err) ); 1764 } 1765 1766 return __p; 1767 } 1768 1769 /** 1770 * Sets the blocking mode of the socket. In blocking mode 1771 * all operations (which don’t take an explicit blocking parameter) block until 1772 * they succeed or there is an error. In 1773 * non-blocking mode all functions return results immediately or 1774 * with a %G_IO_ERROR_WOULD_BLOCK error. 1775 * 1776 * All sockets are created in blocking mode. However, note that the 1777 * platform level socket is always non-blocking, and blocking mode 1778 * is a GSocket level feature. 1779 * 1780 * Params: 1781 * blocking = Whether to use blocking I/O or not. 1782 * 1783 * Since: 2.22 1784 */ 1785 public void setBlocking(bool blocking) 1786 { 1787 g_socket_set_blocking(gSocket, blocking); 1788 } 1789 1790 /** 1791 * Sets whether @socket should allow sending to broadcast addresses. 1792 * This is %FALSE by default. 1793 * 1794 * Params: 1795 * broadcast = whether @socket should allow sending to broadcast 1796 * addresses 1797 * 1798 * Since: 2.32 1799 */ 1800 public void setBroadcast(bool broadcast) 1801 { 1802 g_socket_set_broadcast(gSocket, broadcast); 1803 } 1804 1805 /** 1806 * Sets or unsets the %SO_KEEPALIVE flag on the underlying socket. When 1807 * this flag is set on a socket, the system will attempt to verify that the 1808 * remote socket endpoint is still present if a sufficiently long period of 1809 * time passes with no data being exchanged. If the system is unable to 1810 * verify the presence of the remote endpoint, it will automatically close 1811 * the connection. 1812 * 1813 * This option is only functional on certain kinds of sockets. (Notably, 1814 * %G_SOCKET_PROTOCOL_TCP sockets.) 1815 * 1816 * The exact time between pings is system- and protocol-dependent, but will 1817 * normally be at least two hours. Most commonly, you would set this flag 1818 * on a server socket if you want to allow clients to remain idle for long 1819 * periods of time, but also want to ensure that connections are eventually 1820 * garbage-collected if clients crash or become unreachable. 1821 * 1822 * Params: 1823 * keepalive = Value for the keepalive flag 1824 * 1825 * Since: 2.22 1826 */ 1827 public void setKeepalive(bool keepalive) 1828 { 1829 g_socket_set_keepalive(gSocket, keepalive); 1830 } 1831 1832 /** 1833 * Sets the maximum number of outstanding connections allowed 1834 * when listening on this socket. If more clients than this are 1835 * connecting to the socket and the application is not handling them 1836 * on time then the new connections will be refused. 1837 * 1838 * Note that this must be called before g_socket_listen() and has no 1839 * effect if called after that. 1840 * 1841 * Params: 1842 * backlog = the maximum number of pending connections. 1843 * 1844 * Since: 2.22 1845 */ 1846 public void setListenBacklog(int backlog) 1847 { 1848 g_socket_set_listen_backlog(gSocket, backlog); 1849 } 1850 1851 /** 1852 * Sets whether outgoing multicast packets will be received by sockets 1853 * listening on that multicast address on the same host. This is %TRUE 1854 * by default. 1855 * 1856 * Params: 1857 * loopback = whether @socket should receive messages sent to its 1858 * multicast groups from the local host 1859 * 1860 * Since: 2.32 1861 */ 1862 public void setMulticastLoopback(bool loopback) 1863 { 1864 g_socket_set_multicast_loopback(gSocket, loopback); 1865 } 1866 1867 /** 1868 * Sets the time-to-live for outgoing multicast datagrams on @socket. 1869 * By default, this is 1, meaning that multicast packets will not leave 1870 * the local network. 1871 * 1872 * Params: 1873 * ttl = the time-to-live value for all multicast datagrams on @socket 1874 * 1875 * Since: 2.32 1876 */ 1877 public void setMulticastTtl(uint ttl) 1878 { 1879 g_socket_set_multicast_ttl(gSocket, ttl); 1880 } 1881 1882 /** 1883 * Sets the value of an integer-valued option on @socket, as with 1884 * setsockopt(). (If you need to set a non-integer-valued option, 1885 * you will need to call setsockopt() directly.) 1886 * 1887 * The [<gio/gnetworking.h>][gio-gnetworking.h] 1888 * header pulls in system headers that will define most of the 1889 * standard/portable socket options. For unusual socket protocols or 1890 * platform-dependent options, you may need to include additional 1891 * headers. 1892 * 1893 * Params: 1894 * level = the "API level" of the option (eg, `SOL_SOCKET`) 1895 * optname = the "name" of the option (eg, `SO_BROADCAST`) 1896 * value = the value to set the option to 1897 * 1898 * Returns: success or failure. On failure, @error will be set, and 1899 * the system error value (`errno` or WSAGetLastError()) will still 1900 * be set to the result of the setsockopt() call. 1901 * 1902 * Since: 2.36 1903 * 1904 * Throws: GException on failure. 1905 */ 1906 public bool setOption(int level, int optname, int value) 1907 { 1908 GError* err = null; 1909 1910 auto __p = g_socket_set_option(gSocket, level, optname, value, &err) != 0; 1911 1912 if (err !is null) 1913 { 1914 throw new GException( new ErrorG(err) ); 1915 } 1916 1917 return __p; 1918 } 1919 1920 /** 1921 * Sets the time in seconds after which I/O operations on @socket will 1922 * time out if they have not yet completed. 1923 * 1924 * On a blocking socket, this means that any blocking #GSocket 1925 * operation will time out after @timeout seconds of inactivity, 1926 * returning %G_IO_ERROR_TIMED_OUT. 1927 * 1928 * On a non-blocking socket, calls to g_socket_condition_wait() will 1929 * also fail with %G_IO_ERROR_TIMED_OUT after the given time. Sources 1930 * created with g_socket_create_source() will trigger after 1931 * @timeout seconds of inactivity, with the requested condition 1932 * set, at which point calling g_socket_receive(), g_socket_send(), 1933 * g_socket_check_connect_result(), etc, will fail with 1934 * %G_IO_ERROR_TIMED_OUT. 1935 * 1936 * If @timeout is 0 (the default), operations will never time out 1937 * on their own. 1938 * 1939 * Note that if an I/O operation is interrupted by a signal, this may 1940 * cause the timeout to be reset. 1941 * 1942 * Params: 1943 * timeout = the timeout for @socket, in seconds, or 0 for none 1944 * 1945 * Since: 2.26 1946 */ 1947 public void setTimeout(uint timeout) 1948 { 1949 g_socket_set_timeout(gSocket, timeout); 1950 } 1951 1952 /** 1953 * Sets the time-to-live for outgoing unicast packets on @socket. 1954 * By default the platform-specific default value is used. 1955 * 1956 * Params: 1957 * ttl = the time-to-live value for all unicast packets on @socket 1958 * 1959 * Since: 2.32 1960 */ 1961 public void setTtl(uint ttl) 1962 { 1963 g_socket_set_ttl(gSocket, ttl); 1964 } 1965 1966 /** 1967 * Shut down part or all of a full-duplex connection. 1968 * 1969 * If @shutdown_read is %TRUE then the receiving side of the connection 1970 * is shut down, and further reading is disallowed. 1971 * 1972 * If @shutdown_write is %TRUE then the sending side of the connection 1973 * is shut down, and further writing is disallowed. 1974 * 1975 * It is allowed for both @shutdown_read and @shutdown_write to be %TRUE. 1976 * 1977 * One example where it is useful to shut down only one side of a connection is 1978 * graceful disconnect for TCP connections where you close the sending side, 1979 * then wait for the other side to close the connection, thus ensuring that the 1980 * other side saw all sent data. 1981 * 1982 * Params: 1983 * shutdownRead = whether to shut down the read side 1984 * shutdownWrite = whether to shut down the write side 1985 * 1986 * Returns: %TRUE on success, %FALSE on error 1987 * 1988 * Since: 2.22 1989 * 1990 * Throws: GException on failure. 1991 */ 1992 public bool shutdown(bool shutdownRead, bool shutdownWrite) 1993 { 1994 GError* err = null; 1995 1996 auto __p = g_socket_shutdown(gSocket, shutdownRead, shutdownWrite, &err) != 0; 1997 1998 if (err !is null) 1999 { 2000 throw new GException( new ErrorG(err) ); 2001 } 2002 2003 return __p; 2004 } 2005 2006 /** 2007 * Checks if a socket is capable of speaking IPv4. 2008 * 2009 * IPv4 sockets are capable of speaking IPv4. On some operating systems 2010 * and under some combinations of circumstances IPv6 sockets are also 2011 * capable of speaking IPv4. See RFC 3493 section 3.7 for more 2012 * information. 2013 * 2014 * No other types of sockets are currently considered as being capable 2015 * of speaking IPv4. 2016 * 2017 * Returns: %TRUE if this socket can be used with IPv4. 2018 * 2019 * Since: 2.22 2020 */ 2021 public bool speaksIpv4() 2022 { 2023 return g_socket_speaks_ipv4(gSocket) != 0; 2024 } 2025 }