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 public import gtkc.giotypes; 46 47 48 /** 49 * A #GSocket is a low-level networking primitive. It is a more or less 50 * direct mapping of the BSD socket API in a portable GObject based API. 51 * It supports both the UNIX socket implementations and winsock2 on Windows. 52 * 53 * #GSocket is the platform independent base upon which the higher level 54 * network primitives are based. Applications are not typically meant to 55 * use it directly, but rather through classes like #GSocketClient, 56 * #GSocketService and #GSocketConnection. However there may be cases where 57 * direct use of #GSocket is useful. 58 * 59 * #GSocket implements the #GInitable interface, so if it is manually constructed 60 * by e.g. g_object_new() you must call g_initable_init() and check the 61 * results before using the object. This is done automatically in 62 * g_socket_new() and g_socket_new_from_fd(), so these functions can return 63 * %NULL. 64 * 65 * Sockets operate in two general modes, blocking or non-blocking. When 66 * in blocking mode all operations (which don’t take an explicit blocking 67 * parameter) block until the requested operation 68 * is finished or there is an error. In non-blocking mode all calls that 69 * would block return immediately with a %G_IO_ERROR_WOULD_BLOCK error. 70 * To know when a call would successfully run you can call g_socket_condition_check(), 71 * or g_socket_condition_wait(). You can also use g_socket_create_source() and 72 * attach it to a #GMainContext to get callbacks when I/O is possible. 73 * Note that all sockets are always set to non blocking mode in the system, and 74 * blocking mode is emulated in GSocket. 75 * 76 * When working in non-blocking mode applications should always be able to 77 * handle getting a %G_IO_ERROR_WOULD_BLOCK error even when some other 78 * function said that I/O was possible. This can easily happen in case 79 * of a race condition in the application, but it can also happen for other 80 * reasons. For instance, on Windows a socket is always seen as writable 81 * until a write returns %G_IO_ERROR_WOULD_BLOCK. 82 * 83 * #GSockets can be either connection oriented or datagram based. 84 * For connection oriented types you must first establish a connection by 85 * either connecting to an address or accepting a connection from another 86 * address. For connectionless socket types the target/source address is 87 * specified or received in each I/O operation. 88 * 89 * All socket file descriptors are set to be close-on-exec. 90 * 91 * Note that creating a #GSocket causes the signal %SIGPIPE to be 92 * ignored for the remainder of the program. If you are writing a 93 * command-line utility that uses #GSocket, you may need to take into 94 * account the fact that your program will not automatically be killed 95 * if it tries to write to %stdout after it has been closed. 96 * 97 * Like most other APIs in GLib, #GSocket is not inherently thread safe. To use 98 * a #GSocket concurrently from multiple threads, you must implement your own 99 * locking. 100 * 101 * Since: 2.22 102 */ 103 public class Socket : ObjectG, DatagramBasedIF, InitableIF 104 { 105 /** the main Gtk struct */ 106 protected GSocket* gSocket; 107 108 /** Get the main Gtk struct */ 109 public GSocket* getSocketStruct(bool transferOwnership = false) 110 { 111 if (transferOwnership) 112 ownedRef = false; 113 return gSocket; 114 } 115 116 /** the main Gtk struct as a void* */ 117 protected override void* getStruct() 118 { 119 return cast(void*)gSocket; 120 } 121 122 /** 123 * Sets our main struct and passes it to the parent class. 124 */ 125 public this (GSocket* gSocket, bool ownedRef = false) 126 { 127 this.gSocket = gSocket; 128 super(cast(GObject*)gSocket, ownedRef); 129 } 130 131 // add the DatagramBased capabilities 132 mixin DatagramBasedT!(GSocket); 133 134 // add the Initable capabilities 135 mixin InitableT!(GSocket); 136 137 138 /** */ 139 public static GType getType() 140 { 141 return g_socket_get_type(); 142 } 143 144 /** 145 * Creates a new #GSocket with the defined family, type and protocol. 146 * If @protocol is 0 (%G_SOCKET_PROTOCOL_DEFAULT) the default protocol type 147 * for the family and type is used. 148 * 149 * The @protocol is a family and type specific int that specifies what 150 * kind of protocol to use. #GSocketProtocol lists several common ones. 151 * Many families only support one protocol, and use 0 for this, others 152 * support several and using 0 means to use the default protocol for 153 * the family and type. 154 * 155 * The protocol id is passed directly to the operating 156 * system, so you can use protocols not listed in #GSocketProtocol if you 157 * know the protocol number used for it. 158 * 159 * Params: 160 * family = the socket family to use, e.g. %G_SOCKET_FAMILY_IPV4. 161 * type = the socket type to use. 162 * protocol = the id of the protocol to use, or 0 for default. 163 * 164 * Returns: a #GSocket or %NULL on error. 165 * Free the returned object with g_object_unref(). 166 * 167 * Since: 2.22 168 * 169 * Throws: GException on failure. 170 * Throws: ConstructionException GTK+ fails to create the object. 171 */ 172 public this(GSocketFamily family, GSocketType type, GSocketProtocol protocol) 173 { 174 GError* err = null; 175 176 auto __p = g_socket_new(family, type, protocol, &err); 177 178 if (err !is null) 179 { 180 throw new GException( new ErrorG(err) ); 181 } 182 183 if(__p is null) 184 { 185 throw new ConstructionException("null returned by new"); 186 } 187 188 this(cast(GSocket*) __p, true); 189 } 190 191 /** 192 * Creates a new #GSocket from a native file descriptor 193 * or winsock SOCKET handle. 194 * 195 * This reads all the settings from the file descriptor so that 196 * all properties should work. Note that the file descriptor 197 * will be set to non-blocking mode, independent on the blocking 198 * mode of the #GSocket. 199 * 200 * On success, the returned #GSocket takes ownership of @fd. On failure, the 201 * caller must close @fd themselves. 202 * 203 * Since GLib 2.46, it is no longer a fatal error to call this on a non-socket 204 * descriptor. Instead, a GError will be set with code %G_IO_ERROR_FAILED 205 * 206 * Params: 207 * fd = a native socket file descriptor. 208 * 209 * Returns: a #GSocket or %NULL on error. 210 * Free the returned object with g_object_unref(). 211 * 212 * Since: 2.22 213 * 214 * Throws: GException on failure. 215 * Throws: ConstructionException GTK+ fails to create the object. 216 */ 217 public this(int fd) 218 { 219 GError* err = null; 220 221 auto __p = g_socket_new_from_fd(fd, &err); 222 223 if (err !is null) 224 { 225 throw new GException( new ErrorG(err) ); 226 } 227 228 if(__p is null) 229 { 230 throw new ConstructionException("null returned by new_from_fd"); 231 } 232 233 this(cast(GSocket*) __p, true); 234 } 235 236 /** 237 * Accept incoming connections on a connection-based socket. This removes 238 * the first outstanding connection request from the listening socket and 239 * creates a #GSocket object for it. 240 * 241 * The @socket must be bound to a local address with g_socket_bind() and 242 * must be listening for incoming connections (g_socket_listen()). 243 * 244 * If there are no outstanding connections then the operation will block 245 * or return %G_IO_ERROR_WOULD_BLOCK if non-blocking I/O is enabled. 246 * To be notified of an incoming connection, wait for the %G_IO_IN condition. 247 * 248 * Params: 249 * cancellable = a %GCancellable or %NULL 250 * 251 * Returns: a new #GSocket, or %NULL on error. 252 * Free the returned object with g_object_unref(). 253 * 254 * Since: 2.22 255 * 256 * Throws: GException on failure. 257 */ 258 public Socket accept(Cancellable cancellable) 259 { 260 GError* err = null; 261 262 auto __p = g_socket_accept(gSocket, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 263 264 if (err !is null) 265 { 266 throw new GException( new ErrorG(err) ); 267 } 268 269 if(__p is null) 270 { 271 return null; 272 } 273 274 return ObjectG.getDObject!(Socket)(cast(GSocket*) __p, true); 275 } 276 277 /** 278 * When a socket is created it is attached to an address family, but it 279 * doesn't have an address in this family. g_socket_bind() assigns the 280 * address (sometimes called name) of the socket. 281 * 282 * It is generally required to bind to a local address before you can 283 * receive connections. (See g_socket_listen() and g_socket_accept() ). 284 * In certain situations, you may also want to bind a socket that will be 285 * used to initiate connections, though this is not normally required. 286 * 287 * If @socket is a TCP socket, then @allow_reuse controls the setting 288 * of the `SO_REUSEADDR` socket option; normally it should be %TRUE for 289 * server sockets (sockets that you will eventually call 290 * g_socket_accept() on), and %FALSE for client sockets. (Failing to 291 * set this flag on a server socket may cause g_socket_bind() to return 292 * %G_IO_ERROR_ADDRESS_IN_USE if the server program is stopped and then 293 * immediately restarted.) 294 * 295 * If @socket is a UDP socket, then @allow_reuse determines whether or 296 * not other UDP sockets can be bound to the same address at the same 297 * time. In particular, you can have several UDP sockets bound to the 298 * same address, and they will all receive all of the multicast and 299 * broadcast packets sent to that address. (The behavior of unicast 300 * UDP packets to an address with multiple listeners is not defined.) 301 * 302 * Params: 303 * address = a #GSocketAddress specifying the local address. 304 * allowReuse = whether to allow reusing this address 305 * 306 * Returns: %TRUE on success, %FALSE on error. 307 * 308 * Since: 2.22 309 * 310 * Throws: GException on failure. 311 */ 312 public bool bind(SocketAddress address, bool allowReuse) 313 { 314 GError* err = null; 315 316 auto __p = g_socket_bind(gSocket, (address is null) ? null : address.getSocketAddressStruct(), allowReuse, &err) != 0; 317 318 if (err !is null) 319 { 320 throw new GException( new ErrorG(err) ); 321 } 322 323 return __p; 324 } 325 326 /** 327 * Checks and resets the pending connect error for the socket. 328 * This is used to check for errors when g_socket_connect() is 329 * used in non-blocking mode. 330 * 331 * Returns: %TRUE if no error, %FALSE otherwise, setting @error to the error 332 * 333 * Since: 2.22 334 * 335 * Throws: GException on failure. 336 */ 337 public bool checkConnectResult() 338 { 339 GError* err = null; 340 341 auto __p = g_socket_check_connect_result(gSocket, &err) != 0; 342 343 if (err !is null) 344 { 345 throw new GException( new ErrorG(err) ); 346 } 347 348 return __p; 349 } 350 351 /** 352 * Closes the socket, shutting down any active connection. 353 * 354 * Closing a socket does not wait for all outstanding I/O operations 355 * to finish, so the caller should not rely on them to be guaranteed 356 * to complete even if the close returns with no error. 357 * 358 * Once the socket is closed, all other operations will return 359 * %G_IO_ERROR_CLOSED. Closing a socket multiple times will not 360 * return an error. 361 * 362 * Sockets will be automatically closed when the last reference 363 * is dropped, but you might want to call this function to make sure 364 * resources are released as early as possible. 365 * 366 * Beware that due to the way that TCP works, it is possible for 367 * recently-sent data to be lost if either you close a socket while the 368 * %G_IO_IN condition is set, or else if the remote connection tries to 369 * send something to you after you close the socket but before it has 370 * finished reading all of the data you sent. There is no easy generic 371 * way to avoid this problem; the easiest fix is to design the network 372 * protocol such that the client will never send data "out of turn". 373 * Another solution is for the server to half-close the connection by 374 * calling g_socket_shutdown() with only the @shutdown_write flag set, 375 * and then wait for the client to notice this and close its side of the 376 * connection, after which the server can safely call g_socket_close(). 377 * (This is what #GTcpConnection does if you call 378 * g_tcp_connection_set_graceful_disconnect(). But of course, this 379 * only works if the client will close its connection after the server 380 * does.) 381 * 382 * Returns: %TRUE on success, %FALSE on error 383 * 384 * Since: 2.22 385 * 386 * Throws: GException on failure. 387 */ 388 public bool close() 389 { 390 GError* err = null; 391 392 auto __p = g_socket_close(gSocket, &err) != 0; 393 394 if (err !is null) 395 { 396 throw new GException( new ErrorG(err) ); 397 } 398 399 return __p; 400 } 401 402 /** 403 * Checks on the readiness of @socket to perform operations. 404 * The operations specified in @condition are checked for and masked 405 * against the currently-satisfied conditions on @socket. The result 406 * is returned. 407 * 408 * Note that on Windows, it is possible for an operation to return 409 * %G_IO_ERROR_WOULD_BLOCK even immediately after 410 * g_socket_condition_check() has claimed that the socket is ready for 411 * writing. Rather than calling g_socket_condition_check() and then 412 * writing to the socket if it succeeds, it is generally better to 413 * simply try writing to the socket right away, and try again later if 414 * the initial attempt returns %G_IO_ERROR_WOULD_BLOCK. 415 * 416 * It is meaningless to specify %G_IO_ERR or %G_IO_HUP in condition; 417 * these conditions will always be set in the output if they are true. 418 * 419 * This call never blocks. 420 * 421 * Params: 422 * condition = a #GIOCondition mask to check 423 * 424 * Returns: the @GIOCondition mask of the current state 425 * 426 * Since: 2.22 427 */ 428 public GIOCondition conditionCheck(GIOCondition condition) 429 { 430 return g_socket_condition_check(gSocket, condition); 431 } 432 433 /** 434 * Waits for up to @timeout_us microseconds for @condition to become true 435 * on @socket. If the condition is met, %TRUE is returned. 436 * 437 * If @cancellable is cancelled before the condition is met, or if 438 * @timeout_us (or the socket's #GSocket:timeout) is reached before the 439 * condition is met, then %FALSE is returned and @error, if non-%NULL, 440 * is set to the appropriate value (%G_IO_ERROR_CANCELLED or 441 * %G_IO_ERROR_TIMED_OUT). 442 * 443 * If you don't want a timeout, use g_socket_condition_wait(). 444 * (Alternatively, you can pass -1 for @timeout_us.) 445 * 446 * Note that although @timeout_us is in microseconds for consistency with 447 * other GLib APIs, this function actually only has millisecond 448 * resolution, and the behavior is undefined if @timeout_us is not an 449 * exact number of milliseconds. 450 * 451 * Params: 452 * condition = a #GIOCondition mask to wait for 453 * timeoutUs = the maximum time (in microseconds) to wait, or -1 454 * cancellable = a #GCancellable, or %NULL 455 * 456 * Returns: %TRUE if the condition was met, %FALSE otherwise 457 * 458 * Since: 2.32 459 * 460 * Throws: GException on failure. 461 */ 462 public bool conditionTimedWait(GIOCondition condition, long timeoutUs, Cancellable cancellable) 463 { 464 GError* err = null; 465 466 auto __p = g_socket_condition_timed_wait(gSocket, condition, timeoutUs, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 467 468 if (err !is null) 469 { 470 throw new GException( new ErrorG(err) ); 471 } 472 473 return __p; 474 } 475 476 /** 477 * Waits for @condition to become true on @socket. When the condition 478 * is met, %TRUE is returned. 479 * 480 * If @cancellable is cancelled before the condition is met, or if the 481 * socket has a timeout set and it is reached before the condition is 482 * met, then %FALSE is returned and @error, if non-%NULL, is set to 483 * the appropriate value (%G_IO_ERROR_CANCELLED or 484 * %G_IO_ERROR_TIMED_OUT). 485 * 486 * See also g_socket_condition_timed_wait(). 487 * 488 * Params: 489 * condition = a #GIOCondition mask to wait for 490 * cancellable = a #GCancellable, or %NULL 491 * 492 * Returns: %TRUE if the condition was met, %FALSE otherwise 493 * 494 * Since: 2.22 495 * 496 * Throws: GException on failure. 497 */ 498 public bool conditionWait(GIOCondition condition, Cancellable cancellable) 499 { 500 GError* err = null; 501 502 auto __p = g_socket_condition_wait(gSocket, condition, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 503 504 if (err !is null) 505 { 506 throw new GException( new ErrorG(err) ); 507 } 508 509 return __p; 510 } 511 512 /** 513 * Connect the socket to the specified remote address. 514 * 515 * For connection oriented socket this generally means we attempt to make 516 * a connection to the @address. For a connection-less socket it sets 517 * the default address for g_socket_send() and discards all incoming datagrams 518 * from other sources. 519 * 520 * Generally connection oriented sockets can only connect once, but 521 * connection-less sockets can connect multiple times to change the 522 * default address. 523 * 524 * If the connect call needs to do network I/O it will block, unless 525 * non-blocking I/O is enabled. Then %G_IO_ERROR_PENDING is returned 526 * and the user can be notified of the connection finishing by waiting 527 * for the G_IO_OUT condition. The result of the connection must then be 528 * checked with g_socket_check_connect_result(). 529 * 530 * Params: 531 * address = a #GSocketAddress specifying the remote address. 532 * cancellable = a %GCancellable or %NULL 533 * 534 * Returns: %TRUE if connected, %FALSE on error. 535 * 536 * Since: 2.22 537 * 538 * Throws: GException on failure. 539 */ 540 public bool connect(SocketAddress address, Cancellable cancellable) 541 { 542 GError* err = null; 543 544 auto __p = g_socket_connect(gSocket, (address is null) ? null : address.getSocketAddressStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 545 546 if (err !is null) 547 { 548 throw new GException( new ErrorG(err) ); 549 } 550 551 return __p; 552 } 553 554 /** 555 * Creates a #GSocketConnection subclass of the right type for 556 * @socket. 557 * 558 * Returns: a #GSocketConnection 559 * 560 * Since: 2.22 561 */ 562 public SocketConnection connectionFactoryCreateConnection() 563 { 564 auto __p = g_socket_connection_factory_create_connection(gSocket); 565 566 if(__p is null) 567 { 568 return null; 569 } 570 571 return ObjectG.getDObject!(SocketConnection)(cast(GSocketConnection*) __p, true); 572 } 573 574 /** 575 * Creates a #GSource that can be attached to a %GMainContext to monitor 576 * for the availability of the specified @condition on the socket. The #GSource 577 * keeps a reference to the @socket. 578 * 579 * The callback on the source is of the #GSocketSourceFunc type. 580 * 581 * It is meaningless to specify %G_IO_ERR or %G_IO_HUP in @condition; 582 * these conditions will always be reported output if they are true. 583 * 584 * @cancellable if not %NULL can be used to cancel the source, which will 585 * cause the source to trigger, reporting the current condition (which 586 * is likely 0 unless cancellation happened at the same time as a 587 * condition change). You can check for this in the callback using 588 * g_cancellable_is_cancelled(). 589 * 590 * If @socket has a timeout set, and it is reached before @condition 591 * occurs, the source will then trigger anyway, reporting %G_IO_IN or 592 * %G_IO_OUT depending on @condition. However, @socket will have been 593 * marked as having had a timeout, and so the next #GSocket I/O method 594 * you call will then fail with a %G_IO_ERROR_TIMED_OUT. 595 * 596 * Params: 597 * condition = a #GIOCondition mask to monitor 598 * cancellable = a %GCancellable or %NULL 599 * 600 * Returns: a newly allocated %GSource, free with g_source_unref(). 601 * 602 * Since: 2.22 603 */ 604 public Source createSource(GIOCondition condition, Cancellable cancellable) 605 { 606 auto __p = g_socket_create_source(gSocket, condition, (cancellable is null) ? null : cancellable.getCancellableStruct()); 607 608 if(__p is null) 609 { 610 return null; 611 } 612 613 return new Source(cast(GSource*) __p, true); 614 } 615 616 /** 617 * Get the amount of data pending in the OS input buffer, without blocking. 618 * 619 * If @socket is a UDP or SCTP socket, this will return the size of 620 * just the next packet, even if additional packets are buffered after 621 * that one. 622 * 623 * Note that on Windows, this function is rather inefficient in the 624 * UDP case, and so if you know any plausible upper bound on the size 625 * of the incoming packet, it is better to just do a 626 * g_socket_receive() with a buffer of that size, rather than calling 627 * g_socket_get_available_bytes() first and then doing a receive of 628 * exactly the right size. 629 * 630 * Returns: the number of bytes that can be read from the socket 631 * without blocking or truncating, or -1 on error. 632 * 633 * Since: 2.32 634 */ 635 public ptrdiff_t getAvailableBytes() 636 { 637 return g_socket_get_available_bytes(gSocket); 638 } 639 640 /** 641 * Gets the blocking mode of the socket. For details on blocking I/O, 642 * see g_socket_set_blocking(). 643 * 644 * Returns: %TRUE if blocking I/O is used, %FALSE otherwise. 645 * 646 * Since: 2.22 647 */ 648 public bool getBlocking() 649 { 650 return g_socket_get_blocking(gSocket) != 0; 651 } 652 653 /** 654 * Gets the broadcast setting on @socket; if %TRUE, 655 * it is possible to send packets to broadcast 656 * addresses. 657 * 658 * Returns: the broadcast setting on @socket 659 * 660 * Since: 2.32 661 */ 662 public bool getBroadcast() 663 { 664 return g_socket_get_broadcast(gSocket) != 0; 665 } 666 667 /** 668 * Returns the credentials of the foreign process connected to this 669 * socket, if any (e.g. it is only supported for %G_SOCKET_FAMILY_UNIX 670 * sockets). 671 * 672 * If this operation isn't supported on the OS, the method fails with 673 * the %G_IO_ERROR_NOT_SUPPORTED error. On Linux this is implemented 674 * by reading the %SO_PEERCRED option on the underlying socket. 675 * 676 * This method can be expected to be available on the following platforms: 677 * 678 * - Linux since GLib 2.26 679 * - OpenBSD since GLib 2.30 680 * - Solaris, Illumos and OpenSolaris since GLib 2.40 681 * - NetBSD since GLib 2.42 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 1190 * read data into (which should be at least @size bytes long). 1191 * cancellable = a %GCancellable or %NULL 1192 * 1193 * Returns: Number of bytes read, or 0 if the connection was closed by 1194 * the peer, or -1 on error 1195 * 1196 * Since: 2.22 1197 * 1198 * Throws: GException on failure. 1199 */ 1200 public ptrdiff_t receive(ref char[] buffer, Cancellable cancellable) 1201 { 1202 GError* err = null; 1203 1204 auto __p = g_socket_receive(gSocket, buffer.ptr, cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1205 1206 if (err !is null) 1207 { 1208 throw new GException( new ErrorG(err) ); 1209 } 1210 1211 return __p; 1212 } 1213 1214 /** 1215 * Receive data (up to @size bytes) from a socket. 1216 * 1217 * If @address is non-%NULL then @address will be set equal to the 1218 * source address of the received packet. 1219 * @address is owned by the caller. 1220 * 1221 * See g_socket_receive() for additional information. 1222 * 1223 * Params: 1224 * address = a pointer to a #GSocketAddress 1225 * pointer, or %NULL 1226 * buffer = a buffer to 1227 * read data into (which should be at least @size bytes long). 1228 * cancellable = a %GCancellable or %NULL 1229 * 1230 * Returns: Number of bytes read, or 0 if the connection was closed by 1231 * the peer, or -1 on error 1232 * 1233 * Since: 2.22 1234 * 1235 * Throws: GException on failure. 1236 */ 1237 public ptrdiff_t receiveFrom(out SocketAddress address, ref char[] buffer, Cancellable cancellable) 1238 { 1239 GSocketAddress* outaddress = null; 1240 GError* err = null; 1241 1242 auto __p = g_socket_receive_from(gSocket, &outaddress, buffer.ptr, cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1243 1244 if (err !is null) 1245 { 1246 throw new GException( new ErrorG(err) ); 1247 } 1248 1249 address = ObjectG.getDObject!(SocketAddress)(outaddress); 1250 1251 return __p; 1252 } 1253 1254 /** 1255 * Receive data from a socket. For receiving multiple messages, see 1256 * g_socket_receive_messages(); for easier use, see 1257 * g_socket_receive() and g_socket_receive_from(). 1258 * 1259 * If @address is non-%NULL then @address will be set equal to the 1260 * source address of the received packet. 1261 * @address is owned by the caller. 1262 * 1263 * @vector must point to an array of #GInputVector structs and 1264 * @num_vectors must be the length of this array. These structs 1265 * describe the buffers that received data will be scattered into. 1266 * If @num_vectors is -1, then @vectors is assumed to be terminated 1267 * by a #GInputVector with a %NULL buffer pointer. 1268 * 1269 * As a special case, if @num_vectors is 0 (in which case, @vectors 1270 * may of course be %NULL), then a single byte is received and 1271 * discarded. This is to facilitate the common practice of sending a 1272 * single '\0' byte for the purposes of transferring ancillary data. 1273 * 1274 * @messages, if non-%NULL, will be set to point to a newly-allocated 1275 * array of #GSocketControlMessage instances or %NULL if no such 1276 * messages was received. These correspond to the control messages 1277 * received from the kernel, one #GSocketControlMessage per message 1278 * from the kernel. This array is %NULL-terminated and must be freed 1279 * by the caller using g_free() after calling g_object_unref() on each 1280 * element. If @messages is %NULL, any control messages received will 1281 * be discarded. 1282 * 1283 * @num_messages, if non-%NULL, will be set to the number of control 1284 * messages received. 1285 * 1286 * If both @messages and @num_messages are non-%NULL, then 1287 * @num_messages gives the number of #GSocketControlMessage instances 1288 * in @messages (ie: not including the %NULL terminator). 1289 * 1290 * @flags is an in/out parameter. The commonly available arguments 1291 * for this are available in the #GSocketMsgFlags enum, but the 1292 * values there are the same as the system values, and the flags 1293 * are passed in as-is, so you can pass in system-specific flags too 1294 * (and g_socket_receive_message() may pass system-specific flags out). 1295 * Flags passed in to the parameter affect the receive operation; flags returned 1296 * out of it are relevant to the specific returned message. 1297 * 1298 * As with g_socket_receive(), data may be discarded if @socket is 1299 * %G_SOCKET_TYPE_DATAGRAM or %G_SOCKET_TYPE_SEQPACKET and you do not 1300 * provide enough buffer space to read a complete message. You can pass 1301 * %G_SOCKET_MSG_PEEK in @flags to peek at the current message without 1302 * removing it from the receive queue, but there is no portable way to find 1303 * out the length of the message other than by reading it into a 1304 * sufficiently-large buffer. 1305 * 1306 * If the socket is in blocking mode the call will block until there 1307 * is some data to receive, the connection is closed, or there is an 1308 * error. If there is no data available and the socket is in 1309 * non-blocking mode, a %G_IO_ERROR_WOULD_BLOCK error will be 1310 * returned. To be notified when data is available, wait for the 1311 * %G_IO_IN condition. 1312 * 1313 * On error -1 is returned and @error is set accordingly. 1314 * 1315 * Params: 1316 * address = a pointer to a #GSocketAddress 1317 * pointer, or %NULL 1318 * vectors = an array of #GInputVector structs 1319 * messages = a pointer 1320 * which may be filled with an array of #GSocketControlMessages, or %NULL 1321 * flags = a pointer to an int containing #GSocketMsgFlags flags, 1322 * which may additionally contain 1323 * [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html) 1324 * cancellable = a %GCancellable or %NULL 1325 * 1326 * Returns: Number of bytes read, or 0 if the connection was closed by 1327 * the peer, or -1 on error 1328 * 1329 * Since: 2.22 1330 * 1331 * Throws: GException on failure. 1332 */ 1333 public ptrdiff_t receiveMessage(out SocketAddress address, GInputVector[] vectors, out SocketControlMessage[] messages, ref int flags, Cancellable cancellable) 1334 { 1335 GSocketAddress* outaddress = null; 1336 GSocketControlMessage** outmessages = null; 1337 int numMessages; 1338 GError* err = null; 1339 1340 auto __p = g_socket_receive_message(gSocket, &outaddress, vectors.ptr, cast(int)vectors.length, &outmessages, &numMessages, &flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1341 1342 if (err !is null) 1343 { 1344 throw new GException( new ErrorG(err) ); 1345 } 1346 1347 address = ObjectG.getDObject!(SocketAddress)(outaddress); 1348 1349 messages = new SocketControlMessage[numMessages]; 1350 for(size_t i = 0; i < numMessages; i++) 1351 { 1352 messages[i] = ObjectG.getDObject!(SocketControlMessage)(cast(GSocketControlMessage*) outmessages[i]); 1353 } 1354 1355 return __p; 1356 } 1357 1358 /** 1359 * Receive multiple data messages from @socket in one go. This is the most 1360 * complicated and fully-featured version of this call. For easier use, see 1361 * g_socket_receive(), g_socket_receive_from(), and g_socket_receive_message(). 1362 * 1363 * @messages must point to an array of #GInputMessage structs and 1364 * @num_messages must be the length of this array. Each #GInputMessage 1365 * contains a pointer to an array of #GInputVector structs describing the 1366 * buffers that the data received in each message will be written to. Using 1367 * multiple #GInputVectors is more memory-efficient than manually copying data 1368 * out of a single buffer to multiple sources, and more system-call-efficient 1369 * than making multiple calls to g_socket_receive(), such as in scenarios where 1370 * a lot of data packets need to be received (e.g. high-bandwidth video 1371 * streaming over RTP/UDP). 1372 * 1373 * @flags modify how all messages are received. The commonly available 1374 * arguments for this are available in the #GSocketMsgFlags enum, but the 1375 * values there are the same as the system values, and the flags 1376 * are passed in as-is, so you can pass in system-specific flags too. These 1377 * flags affect the overall receive operation. Flags affecting individual 1378 * messages are returned in #GInputMessage.flags. 1379 * 1380 * The other members of #GInputMessage are treated as described in its 1381 * documentation. 1382 * 1383 * If #GSocket:blocking is %TRUE the call will block until @num_messages have 1384 * been received, or the end of the stream is reached. 1385 * 1386 * If #GSocket:blocking is %FALSE the call will return up to @num_messages 1387 * without blocking, or %G_IO_ERROR_WOULD_BLOCK if no messages are queued in the 1388 * operating system to be received. 1389 * 1390 * In blocking mode, if #GSocket:timeout is positive and is reached before any 1391 * messages are received, %G_IO_ERROR_TIMED_OUT is returned, otherwise up to 1392 * @num_messages are returned. (Note: This is effectively the 1393 * behaviour of `MSG_WAITFORONE` with recvmmsg().) 1394 * 1395 * To be notified when messages are available, wait for the 1396 * %G_IO_IN condition. Note though that you may still receive 1397 * %G_IO_ERROR_WOULD_BLOCK from g_socket_receive_messages() even if you were 1398 * previously notified of a %G_IO_IN condition. 1399 * 1400 * If the remote peer closes the connection, any messages queued in the 1401 * operating system will be returned, and subsequent calls to 1402 * g_socket_receive_messages() will return 0 (with no error set). 1403 * 1404 * On error -1 is returned and @error is set accordingly. An error will only 1405 * be returned if zero messages could be received; otherwise the number of 1406 * messages successfully received before the error will be returned. 1407 * 1408 * Params: 1409 * messages = an array of #GInputMessage structs 1410 * flags = an int containing #GSocketMsgFlags flags for the overall operation, 1411 * which may additionally contain 1412 * [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html) 1413 * cancellable = a %GCancellable or %NULL 1414 * 1415 * Returns: number of messages received, or -1 on error. Note that the number 1416 * of messages received may be smaller than @num_messages if in non-blocking 1417 * mode, if the peer closed the connection, or if @num_messages 1418 * was larger than `UIO_MAXIOV` (1024), in which case the caller may re-try 1419 * to receive the remaining messages. 1420 * 1421 * Since: 2.48 1422 * 1423 * Throws: GException on failure. 1424 */ 1425 public int receiveMessages(GInputMessage[] messages, int flags, Cancellable cancellable) 1426 { 1427 GError* err = null; 1428 1429 auto __p = g_socket_receive_messages(gSocket, messages.ptr, cast(uint)messages.length, flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1430 1431 if (err !is null) 1432 { 1433 throw new GException( new ErrorG(err) ); 1434 } 1435 1436 return __p; 1437 } 1438 1439 /** 1440 * This behaves exactly the same as g_socket_receive(), except that 1441 * the choice of blocking or non-blocking behavior is determined by 1442 * the @blocking argument rather than by @socket's properties. 1443 * 1444 * Params: 1445 * buffer = a buffer to 1446 * read data into (which should be at least @size bytes long). 1447 * blocking = whether to do blocking or non-blocking I/O 1448 * cancellable = a %GCancellable or %NULL 1449 * 1450 * Returns: Number of bytes read, or 0 if the connection was closed by 1451 * the peer, or -1 on error 1452 * 1453 * Since: 2.26 1454 * 1455 * Throws: GException on failure. 1456 */ 1457 public ptrdiff_t receiveWithBlocking(string buffer, bool blocking, Cancellable cancellable) 1458 { 1459 GError* err = null; 1460 1461 auto __p = g_socket_receive_with_blocking(gSocket, Str.toStringz(buffer), cast(size_t)buffer.length, blocking, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1462 1463 if (err !is null) 1464 { 1465 throw new GException( new ErrorG(err) ); 1466 } 1467 1468 return __p; 1469 } 1470 1471 /** 1472 * Tries to send @size bytes from @buffer on the socket. This is 1473 * mainly used by connection-oriented sockets; it is identical to 1474 * g_socket_send_to() with @address set to %NULL. 1475 * 1476 * If the socket is in blocking mode the call will block until there is 1477 * space for the data in the socket queue. If there is no space available 1478 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1479 * will be returned. To be notified when space is available, wait for the 1480 * %G_IO_OUT condition. Note though that you may still receive 1481 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1482 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1483 * very common due to the way the underlying APIs work.) 1484 * 1485 * On error -1 is returned and @error is set accordingly. 1486 * 1487 * Params: 1488 * buffer = the buffer 1489 * containing the data to send. 1490 * cancellable = a %GCancellable or %NULL 1491 * 1492 * Returns: Number of bytes written (which may be less than @size), or -1 1493 * on error 1494 * 1495 * Since: 2.22 1496 * 1497 * Throws: GException on failure. 1498 */ 1499 public ptrdiff_t send(string buffer, Cancellable cancellable) 1500 { 1501 GError* err = null; 1502 1503 auto __p = g_socket_send(gSocket, Str.toStringz(buffer), cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1504 1505 if (err !is null) 1506 { 1507 throw new GException( new ErrorG(err) ); 1508 } 1509 1510 return __p; 1511 } 1512 1513 /** 1514 * Send data to @address on @socket. For sending multiple messages see 1515 * g_socket_send_messages(); for easier use, see 1516 * g_socket_send() and g_socket_send_to(). 1517 * 1518 * If @address is %NULL then the message is sent to the default receiver 1519 * (set by g_socket_connect()). 1520 * 1521 * @vectors must point to an array of #GOutputVector structs and 1522 * @num_vectors must be the length of this array. (If @num_vectors is -1, 1523 * then @vectors is assumed to be terminated by a #GOutputVector with a 1524 * %NULL buffer pointer.) The #GOutputVector structs describe the buffers 1525 * that the sent data will be gathered from. Using multiple 1526 * #GOutputVectors is more memory-efficient than manually copying 1527 * data from multiple sources into a single buffer, and more 1528 * network-efficient than making multiple calls to g_socket_send(). 1529 * 1530 * @messages, if non-%NULL, is taken to point to an array of @num_messages 1531 * #GSocketControlMessage instances. These correspond to the control 1532 * messages to be sent on the socket. 1533 * If @num_messages is -1 then @messages is treated as a %NULL-terminated 1534 * array. 1535 * 1536 * @flags modify how the message is sent. The commonly available arguments 1537 * for this are available in the #GSocketMsgFlags enum, but the 1538 * values there are the same as the system values, and the flags 1539 * are passed in as-is, so you can pass in system-specific flags too. 1540 * 1541 * If the socket is in blocking mode the call will block until there is 1542 * space for the data in the socket queue. If there is no space available 1543 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1544 * will be returned. To be notified when space is available, wait for the 1545 * %G_IO_OUT condition. Note though that you may still receive 1546 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1547 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1548 * very common due to the way the underlying APIs work.) 1549 * 1550 * On error -1 is returned and @error is set accordingly. 1551 * 1552 * Params: 1553 * address = a #GSocketAddress, or %NULL 1554 * vectors = an array of #GOutputVector structs 1555 * messages = a pointer to an 1556 * array of #GSocketControlMessages, or %NULL. 1557 * flags = an int containing #GSocketMsgFlags flags, which may additionally 1558 * contain [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html) 1559 * cancellable = a %GCancellable or %NULL 1560 * 1561 * Returns: Number of bytes written (which may be less than @size), or -1 1562 * on error 1563 * 1564 * Since: 2.22 1565 * 1566 * Throws: GException on failure. 1567 */ 1568 public ptrdiff_t sendMessage(SocketAddress address, GOutputVector[] vectors, SocketControlMessage[] messages, int flags, Cancellable cancellable) 1569 { 1570 GSocketControlMessage*[] messagesArray = new GSocketControlMessage*[messages.length]; 1571 for ( int i = 0; i < messages.length; i++ ) 1572 { 1573 messagesArray[i] = messages[i].getSocketControlMessageStruct(); 1574 } 1575 1576 GError* err = null; 1577 1578 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); 1579 1580 if (err !is null) 1581 { 1582 throw new GException( new ErrorG(err) ); 1583 } 1584 1585 return __p; 1586 } 1587 1588 /** 1589 * This behaves exactly the same as g_socket_send_message(), except that 1590 * the choice of timeout behavior is determined by the @timeout_us argument 1591 * rather than by @socket's properties. 1592 * 1593 * On error %G_POLLABLE_RETURN_FAILED is returned and @error is set accordingly, or 1594 * if the socket is currently not writable %G_POLLABLE_RETURN_WOULD_BLOCK is 1595 * returned. @bytes_written will contain 0 in both cases. 1596 * 1597 * Params: 1598 * address = a #GSocketAddress, or %NULL 1599 * vectors = an array of #GOutputVector structs 1600 * messages = a pointer to an 1601 * array of #GSocketControlMessages, or %NULL. 1602 * flags = an int containing #GSocketMsgFlags flags, which may additionally 1603 * contain [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html) 1604 * timeoutUs = the maximum time (in microseconds) to wait, or -1 1605 * bytesWritten = location to store the number of bytes that were written to the socket 1606 * cancellable = a %GCancellable or %NULL 1607 * 1608 * Returns: %G_POLLABLE_RETURN_OK if all data was successfully written, 1609 * %G_POLLABLE_RETURN_WOULD_BLOCK if the socket is currently not writable, or 1610 * %G_POLLABLE_RETURN_FAILED if an error happened and @error is set. 1611 * 1612 * Since: 2.60 1613 * 1614 * Throws: GException on failure. 1615 */ 1616 public GPollableReturn sendMessageWithTimeout(SocketAddress address, GOutputVector[] vectors, SocketControlMessage[] messages, int flags, long timeoutUs, out size_t bytesWritten, Cancellable cancellable) 1617 { 1618 GSocketControlMessage*[] messagesArray = new GSocketControlMessage*[messages.length]; 1619 for ( int i = 0; i < messages.length; i++ ) 1620 { 1621 messagesArray[i] = messages[i].getSocketControlMessageStruct(); 1622 } 1623 1624 GError* err = null; 1625 1626 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); 1627 1628 if (err !is null) 1629 { 1630 throw new GException( new ErrorG(err) ); 1631 } 1632 1633 return __p; 1634 } 1635 1636 /** 1637 * Send multiple data messages from @socket in one go. This is the most 1638 * complicated and fully-featured version of this call. For easier use, see 1639 * g_socket_send(), g_socket_send_to(), and g_socket_send_message(). 1640 * 1641 * @messages must point to an array of #GOutputMessage structs and 1642 * @num_messages must be the length of this array. Each #GOutputMessage 1643 * contains an address to send the data to, and a pointer to an array of 1644 * #GOutputVector structs to describe the buffers that the data to be sent 1645 * for each message will be gathered from. Using multiple #GOutputVectors is 1646 * more memory-efficient than manually copying data from multiple sources 1647 * into a single buffer, and more network-efficient than making multiple 1648 * calls to g_socket_send(). Sending multiple messages in one go avoids the 1649 * overhead of making a lot of syscalls in scenarios where a lot of data 1650 * packets need to be sent (e.g. high-bandwidth video streaming over RTP/UDP), 1651 * or where the same data needs to be sent to multiple recipients. 1652 * 1653 * @flags modify how the message is sent. The commonly available arguments 1654 * for this are available in the #GSocketMsgFlags enum, but the 1655 * values there are the same as the system values, and the flags 1656 * are passed in as-is, so you can pass in system-specific flags too. 1657 * 1658 * If the socket is in blocking mode the call will block until there is 1659 * space for all the data in the socket queue. If there is no space available 1660 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1661 * will be returned if no data was written at all, otherwise the number of 1662 * messages sent will be returned. To be notified when space is available, 1663 * wait for the %G_IO_OUT condition. Note though that you may still receive 1664 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1665 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1666 * very common due to the way the underlying APIs work.) 1667 * 1668 * On error -1 is returned and @error is set accordingly. An error will only 1669 * be returned if zero messages could be sent; otherwise the number of messages 1670 * successfully sent before the error will be returned. 1671 * 1672 * Params: 1673 * messages = an array of #GOutputMessage structs 1674 * flags = an int containing #GSocketMsgFlags flags, which may additionally 1675 * contain [other platform specific flags](http://man7.org/linux/man-pages/man2/recv.2.html) 1676 * cancellable = a %GCancellable or %NULL 1677 * 1678 * Returns: number of messages sent, or -1 on error. Note that the number of 1679 * messages sent may be smaller than @num_messages if the socket is 1680 * non-blocking or if @num_messages was larger than UIO_MAXIOV (1024), 1681 * in which case the caller may re-try to send the remaining messages. 1682 * 1683 * Since: 2.44 1684 * 1685 * Throws: GException on failure. 1686 */ 1687 public int sendMessages(GOutputMessage[] messages, int flags, Cancellable cancellable) 1688 { 1689 GError* err = null; 1690 1691 auto __p = g_socket_send_messages(gSocket, messages.ptr, cast(uint)messages.length, flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1692 1693 if (err !is null) 1694 { 1695 throw new GException( new ErrorG(err) ); 1696 } 1697 1698 return __p; 1699 } 1700 1701 /** 1702 * Tries to send @size bytes from @buffer to @address. If @address is 1703 * %NULL then the message is sent to the default receiver (set by 1704 * g_socket_connect()). 1705 * 1706 * See g_socket_send() for additional information. 1707 * 1708 * Params: 1709 * address = a #GSocketAddress, or %NULL 1710 * buffer = the buffer 1711 * containing the data to send. 1712 * cancellable = a %GCancellable or %NULL 1713 * 1714 * Returns: Number of bytes written (which may be less than @size), or -1 1715 * on error 1716 * 1717 * Since: 2.22 1718 * 1719 * Throws: GException on failure. 1720 */ 1721 public ptrdiff_t sendTo(SocketAddress address, string buffer, Cancellable cancellable) 1722 { 1723 GError* err = null; 1724 1725 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); 1726 1727 if (err !is null) 1728 { 1729 throw new GException( new ErrorG(err) ); 1730 } 1731 1732 return __p; 1733 } 1734 1735 /** 1736 * This behaves exactly the same as g_socket_send(), except that 1737 * the choice of blocking or non-blocking behavior is determined by 1738 * the @blocking argument rather than by @socket's properties. 1739 * 1740 * Params: 1741 * buffer = the buffer 1742 * containing the data to send. 1743 * blocking = whether to do blocking or non-blocking I/O 1744 * cancellable = a %GCancellable or %NULL 1745 * 1746 * Returns: Number of bytes written (which may be less than @size), or -1 1747 * on error 1748 * 1749 * Since: 2.26 1750 * 1751 * Throws: GException on failure. 1752 */ 1753 public ptrdiff_t sendWithBlocking(string buffer, bool blocking, Cancellable cancellable) 1754 { 1755 GError* err = null; 1756 1757 auto __p = g_socket_send_with_blocking(gSocket, Str.toStringz(buffer), cast(size_t)buffer.length, blocking, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1758 1759 if (err !is null) 1760 { 1761 throw new GException( new ErrorG(err) ); 1762 } 1763 1764 return __p; 1765 } 1766 1767 /** 1768 * Sets the blocking mode of the socket. In blocking mode 1769 * all operations (which don’t take an explicit blocking parameter) block until 1770 * they succeed or there is an error. In 1771 * non-blocking mode all functions return results immediately or 1772 * with a %G_IO_ERROR_WOULD_BLOCK error. 1773 * 1774 * All sockets are created in blocking mode. However, note that the 1775 * platform level socket is always non-blocking, and blocking mode 1776 * is a GSocket level feature. 1777 * 1778 * Params: 1779 * blocking = Whether to use blocking I/O or not. 1780 * 1781 * Since: 2.22 1782 */ 1783 public void setBlocking(bool blocking) 1784 { 1785 g_socket_set_blocking(gSocket, blocking); 1786 } 1787 1788 /** 1789 * Sets whether @socket should allow sending to broadcast addresses. 1790 * This is %FALSE by default. 1791 * 1792 * Params: 1793 * broadcast = whether @socket should allow sending to broadcast 1794 * addresses 1795 * 1796 * Since: 2.32 1797 */ 1798 public void setBroadcast(bool broadcast) 1799 { 1800 g_socket_set_broadcast(gSocket, broadcast); 1801 } 1802 1803 /** 1804 * Sets or unsets the %SO_KEEPALIVE flag on the underlying socket. When 1805 * this flag is set on a socket, the system will attempt to verify that the 1806 * remote socket endpoint is still present if a sufficiently long period of 1807 * time passes with no data being exchanged. If the system is unable to 1808 * verify the presence of the remote endpoint, it will automatically close 1809 * the connection. 1810 * 1811 * This option is only functional on certain kinds of sockets. (Notably, 1812 * %G_SOCKET_PROTOCOL_TCP sockets.) 1813 * 1814 * The exact time between pings is system- and protocol-dependent, but will 1815 * normally be at least two hours. Most commonly, you would set this flag 1816 * on a server socket if you want to allow clients to remain idle for long 1817 * periods of time, but also want to ensure that connections are eventually 1818 * garbage-collected if clients crash or become unreachable. 1819 * 1820 * Params: 1821 * keepalive = Value for the keepalive flag 1822 * 1823 * Since: 2.22 1824 */ 1825 public void setKeepalive(bool keepalive) 1826 { 1827 g_socket_set_keepalive(gSocket, keepalive); 1828 } 1829 1830 /** 1831 * Sets the maximum number of outstanding connections allowed 1832 * when listening on this socket. If more clients than this are 1833 * connecting to the socket and the application is not handling them 1834 * on time then the new connections will be refused. 1835 * 1836 * Note that this must be called before g_socket_listen() and has no 1837 * effect if called after that. 1838 * 1839 * Params: 1840 * backlog = the maximum number of pending connections. 1841 * 1842 * Since: 2.22 1843 */ 1844 public void setListenBacklog(int backlog) 1845 { 1846 g_socket_set_listen_backlog(gSocket, backlog); 1847 } 1848 1849 /** 1850 * Sets whether outgoing multicast packets will be received by sockets 1851 * listening on that multicast address on the same host. This is %TRUE 1852 * by default. 1853 * 1854 * Params: 1855 * loopback = whether @socket should receive messages sent to its 1856 * multicast groups from the local host 1857 * 1858 * Since: 2.32 1859 */ 1860 public void setMulticastLoopback(bool loopback) 1861 { 1862 g_socket_set_multicast_loopback(gSocket, loopback); 1863 } 1864 1865 /** 1866 * Sets the time-to-live for outgoing multicast datagrams on @socket. 1867 * By default, this is 1, meaning that multicast packets will not leave 1868 * the local network. 1869 * 1870 * Params: 1871 * ttl = the time-to-live value for all multicast datagrams on @socket 1872 * 1873 * Since: 2.32 1874 */ 1875 public void setMulticastTtl(uint ttl) 1876 { 1877 g_socket_set_multicast_ttl(gSocket, ttl); 1878 } 1879 1880 /** 1881 * Sets the value of an integer-valued option on @socket, as with 1882 * setsockopt(). (If you need to set a non-integer-valued option, 1883 * you will need to call setsockopt() directly.) 1884 * 1885 * The [<gio/gnetworking.h>][gio-gnetworking.h] 1886 * header pulls in system headers that will define most of the 1887 * standard/portable socket options. For unusual socket protocols or 1888 * platform-dependent options, you may need to include additional 1889 * headers. 1890 * 1891 * Params: 1892 * level = the "API level" of the option (eg, `SOL_SOCKET`) 1893 * optname = the "name" of the option (eg, `SO_BROADCAST`) 1894 * value = the value to set the option to 1895 * 1896 * Returns: success or failure. On failure, @error will be set, and 1897 * the system error value (`errno` or WSAGetLastError()) will still 1898 * be set to the result of the setsockopt() call. 1899 * 1900 * Since: 2.36 1901 * 1902 * Throws: GException on failure. 1903 */ 1904 public bool setOption(int level, int optname, int value) 1905 { 1906 GError* err = null; 1907 1908 auto __p = g_socket_set_option(gSocket, level, optname, value, &err) != 0; 1909 1910 if (err !is null) 1911 { 1912 throw new GException( new ErrorG(err) ); 1913 } 1914 1915 return __p; 1916 } 1917 1918 /** 1919 * Sets the time in seconds after which I/O operations on @socket will 1920 * time out if they have not yet completed. 1921 * 1922 * On a blocking socket, this means that any blocking #GSocket 1923 * operation will time out after @timeout seconds of inactivity, 1924 * returning %G_IO_ERROR_TIMED_OUT. 1925 * 1926 * On a non-blocking socket, calls to g_socket_condition_wait() will 1927 * also fail with %G_IO_ERROR_TIMED_OUT after the given time. Sources 1928 * created with g_socket_create_source() will trigger after 1929 * @timeout seconds of inactivity, with the requested condition 1930 * set, at which point calling g_socket_receive(), g_socket_send(), 1931 * g_socket_check_connect_result(), etc, will fail with 1932 * %G_IO_ERROR_TIMED_OUT. 1933 * 1934 * If @timeout is 0 (the default), operations will never time out 1935 * on their own. 1936 * 1937 * Note that if an I/O operation is interrupted by a signal, this may 1938 * cause the timeout to be reset. 1939 * 1940 * Params: 1941 * timeout = the timeout for @socket, in seconds, or 0 for none 1942 * 1943 * Since: 2.26 1944 */ 1945 public void setTimeout(uint timeout) 1946 { 1947 g_socket_set_timeout(gSocket, timeout); 1948 } 1949 1950 /** 1951 * Sets the time-to-live for outgoing unicast packets on @socket. 1952 * By default the platform-specific default value is used. 1953 * 1954 * Params: 1955 * ttl = the time-to-live value for all unicast packets on @socket 1956 * 1957 * Since: 2.32 1958 */ 1959 public void setTtl(uint ttl) 1960 { 1961 g_socket_set_ttl(gSocket, ttl); 1962 } 1963 1964 /** 1965 * Shut down part or all of a full-duplex connection. 1966 * 1967 * If @shutdown_read is %TRUE then the receiving side of the connection 1968 * is shut down, and further reading is disallowed. 1969 * 1970 * If @shutdown_write is %TRUE then the sending side of the connection 1971 * is shut down, and further writing is disallowed. 1972 * 1973 * It is allowed for both @shutdown_read and @shutdown_write to be %TRUE. 1974 * 1975 * One example where it is useful to shut down only one side of a connection is 1976 * graceful disconnect for TCP connections where you close the sending side, 1977 * then wait for the other side to close the connection, thus ensuring that the 1978 * other side saw all sent data. 1979 * 1980 * Params: 1981 * shutdownRead = whether to shut down the read side 1982 * shutdownWrite = whether to shut down the write side 1983 * 1984 * Returns: %TRUE on success, %FALSE on error 1985 * 1986 * Since: 2.22 1987 * 1988 * Throws: GException on failure. 1989 */ 1990 public bool shutdown(bool shutdownRead, bool shutdownWrite) 1991 { 1992 GError* err = null; 1993 1994 auto __p = g_socket_shutdown(gSocket, shutdownRead, shutdownWrite, &err) != 0; 1995 1996 if (err !is null) 1997 { 1998 throw new GException( new ErrorG(err) ); 1999 } 2000 2001 return __p; 2002 } 2003 2004 /** 2005 * Checks if a socket is capable of speaking IPv4. 2006 * 2007 * IPv4 sockets are capable of speaking IPv4. On some operating systems 2008 * and under some combinations of circumstances IPv6 sockets are also 2009 * capable of speaking IPv4. See RFC 3493 section 3.7 for more 2010 * information. 2011 * 2012 * No other types of sockets are currently considered as being capable 2013 * of speaking IPv4. 2014 * 2015 * Returns: %TRUE if this socket can be used with IPv4. 2016 * 2017 * Since: 2.22 2018 */ 2019 public bool speaksIpv4() 2020 { 2021 return g_socket_speaks_ipv4(gSocket) != 0; 2022 } 2023 }