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 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 (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.) 445 * 446 * Note that although @timeout 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 is not an 449 * exact number of milliseconds. 450 * 451 * Params: 452 * condition = a #GIOCondition mask to wait for 453 * timeout = 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 timeout, Cancellable cancellable) 463 { 464 GError* err = null; 465 466 auto p = g_socket_condition_timed_wait(gSocket, condition, timeout, (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 * Other ways to obtain credentials from a foreign peer includes the 677 * #GUnixCredentialsMessage type and 678 * g_unix_connection_send_credentials() / 679 * g_unix_connection_receive_credentials() functions. 680 * 681 * Returns: %NULL if @error is set, otherwise a #GCredentials object 682 * that must be freed with g_object_unref(). 683 * 684 * Since: 2.26 685 * 686 * Throws: GException on failure. 687 */ 688 public Credentials getCredentials() 689 { 690 GError* err = null; 691 692 auto p = g_socket_get_credentials(gSocket, &err); 693 694 if (err !is null) 695 { 696 throw new GException( new ErrorG(err) ); 697 } 698 699 if(p is null) 700 { 701 return null; 702 } 703 704 return ObjectG.getDObject!(Credentials)(cast(GCredentials*) p, true); 705 } 706 707 /** 708 * Gets the socket family of the socket. 709 * 710 * Returns: a #GSocketFamily 711 * 712 * Since: 2.22 713 */ 714 public GSocketFamily getFamily() 715 { 716 return g_socket_get_family(gSocket); 717 } 718 719 /** 720 * Returns the underlying OS socket object. On unix this 721 * is a socket file descriptor, and on Windows this is 722 * a Winsock2 SOCKET handle. This may be useful for 723 * doing platform specific or otherwise unusual operations 724 * on the socket. 725 * 726 * Returns: the file descriptor of the socket. 727 * 728 * Since: 2.22 729 */ 730 public int getFd() 731 { 732 return g_socket_get_fd(gSocket); 733 } 734 735 /** 736 * Gets the keepalive mode of the socket. For details on this, 737 * see g_socket_set_keepalive(). 738 * 739 * Returns: %TRUE if keepalive is active, %FALSE otherwise. 740 * 741 * Since: 2.22 742 */ 743 public bool getKeepalive() 744 { 745 return g_socket_get_keepalive(gSocket) != 0; 746 } 747 748 /** 749 * Gets the listen backlog setting of the socket. For details on this, 750 * see g_socket_set_listen_backlog(). 751 * 752 * Returns: the maximum number of pending connections. 753 * 754 * Since: 2.22 755 */ 756 public int getListenBacklog() 757 { 758 return g_socket_get_listen_backlog(gSocket); 759 } 760 761 /** 762 * Try to get the local address of a bound socket. This is only 763 * useful if the socket has been bound to a local address, 764 * either explicitly or implicitly when connecting. 765 * 766 * Returns: a #GSocketAddress or %NULL on error. 767 * Free the returned object with g_object_unref(). 768 * 769 * Since: 2.22 770 * 771 * Throws: GException on failure. 772 */ 773 public SocketAddress getLocalAddress() 774 { 775 GError* err = null; 776 777 auto p = g_socket_get_local_address(gSocket, &err); 778 779 if (err !is null) 780 { 781 throw new GException( new ErrorG(err) ); 782 } 783 784 if(p is null) 785 { 786 return null; 787 } 788 789 return ObjectG.getDObject!(SocketAddress)(cast(GSocketAddress*) p, true); 790 } 791 792 /** 793 * Gets the multicast loopback setting on @socket; if %TRUE (the 794 * default), outgoing multicast packets will be looped back to 795 * multicast listeners on the same host. 796 * 797 * Returns: the multicast loopback setting on @socket 798 * 799 * Since: 2.32 800 */ 801 public bool getMulticastLoopback() 802 { 803 return g_socket_get_multicast_loopback(gSocket) != 0; 804 } 805 806 /** 807 * Gets the multicast time-to-live setting on @socket; see 808 * g_socket_set_multicast_ttl() for more details. 809 * 810 * Returns: the multicast time-to-live setting on @socket 811 * 812 * Since: 2.32 813 */ 814 public uint getMulticastTtl() 815 { 816 return g_socket_get_multicast_ttl(gSocket); 817 } 818 819 /** 820 * Gets the value of an integer-valued option on @socket, as with 821 * getsockopt(). (If you need to fetch a non-integer-valued option, 822 * you will need to call getsockopt() directly.) 823 * 824 * The [<gio/gnetworking.h>][gio-gnetworking.h] 825 * header pulls in system headers that will define most of the 826 * standard/portable socket options. For unusual socket protocols or 827 * platform-dependent options, you may need to include additional 828 * headers. 829 * 830 * Note that even for socket options that are a single byte in size, 831 * @value is still a pointer to a #gint variable, not a #guchar; 832 * g_socket_get_option() will handle the conversion internally. 833 * 834 * Params: 835 * level = the "API level" of the option (eg, `SOL_SOCKET`) 836 * optname = the "name" of the option (eg, `SO_BROADCAST`) 837 * value = return location for the option value 838 * 839 * Returns: success or failure. On failure, @error will be set, and 840 * the system error value (`errno` or WSAGetLastError()) will still 841 * be set to the result of the getsockopt() call. 842 * 843 * Since: 2.36 844 * 845 * Throws: GException on failure. 846 */ 847 public bool getOption(int level, int optname, out int value) 848 { 849 GError* err = null; 850 851 auto p = g_socket_get_option(gSocket, level, optname, &value, &err) != 0; 852 853 if (err !is null) 854 { 855 throw new GException( new ErrorG(err) ); 856 } 857 858 return p; 859 } 860 861 /** 862 * Gets the socket protocol id the socket was created with. 863 * In case the protocol is unknown, -1 is returned. 864 * 865 * Returns: a protocol id, or -1 if unknown 866 * 867 * Since: 2.22 868 */ 869 public GSocketProtocol getProtocol() 870 { 871 return g_socket_get_protocol(gSocket); 872 } 873 874 /** 875 * Try to get the remove address of a connected socket. This is only 876 * useful for connection oriented sockets that have been connected. 877 * 878 * Returns: a #GSocketAddress or %NULL on error. 879 * Free the returned object with g_object_unref(). 880 * 881 * Since: 2.22 882 * 883 * Throws: GException on failure. 884 */ 885 public SocketAddress getRemoteAddress() 886 { 887 GError* err = null; 888 889 auto p = g_socket_get_remote_address(gSocket, &err); 890 891 if (err !is null) 892 { 893 throw new GException( new ErrorG(err) ); 894 } 895 896 if(p is null) 897 { 898 return null; 899 } 900 901 return ObjectG.getDObject!(SocketAddress)(cast(GSocketAddress*) p, true); 902 } 903 904 /** 905 * Gets the socket type of the socket. 906 * 907 * Returns: a #GSocketType 908 * 909 * Since: 2.22 910 */ 911 public GSocketType getSocketType() 912 { 913 return g_socket_get_socket_type(gSocket); 914 } 915 916 /** 917 * Gets the timeout setting of the socket. For details on this, see 918 * g_socket_set_timeout(). 919 * 920 * Returns: the timeout in seconds 921 * 922 * Since: 2.26 923 */ 924 public uint getTimeout() 925 { 926 return g_socket_get_timeout(gSocket); 927 } 928 929 /** 930 * Gets the unicast time-to-live setting on @socket; see 931 * g_socket_set_ttl() for more details. 932 * 933 * Returns: the time-to-live setting on @socket 934 * 935 * Since: 2.32 936 */ 937 public uint getTtl() 938 { 939 return g_socket_get_ttl(gSocket); 940 } 941 942 /** 943 * Checks whether a socket is closed. 944 * 945 * Returns: %TRUE if socket is closed, %FALSE otherwise 946 * 947 * Since: 2.22 948 */ 949 public bool isClosed() 950 { 951 return g_socket_is_closed(gSocket) != 0; 952 } 953 954 /** 955 * Check whether the socket is connected. This is only useful for 956 * connection-oriented sockets. 957 * 958 * If using g_socket_shutdown(), this function will return %TRUE until the 959 * socket has been shut down for reading and writing. If you do a non-blocking 960 * connect, this function will not return %TRUE until after you call 961 * g_socket_check_connect_result(). 962 * 963 * Returns: %TRUE if socket is connected, %FALSE otherwise. 964 * 965 * Since: 2.22 966 */ 967 public bool isConnected() 968 { 969 return g_socket_is_connected(gSocket) != 0; 970 } 971 972 /** 973 * Registers @socket to receive multicast messages sent to @group. 974 * @socket must be a %G_SOCKET_TYPE_DATAGRAM socket, and must have 975 * been bound to an appropriate interface and port with 976 * g_socket_bind(). 977 * 978 * If @iface is %NULL, the system will automatically pick an interface 979 * to bind to based on @group. 980 * 981 * If @source_specific is %TRUE, source-specific multicast as defined 982 * in RFC 4604 is used. Note that on older platforms this may fail 983 * with a %G_IO_ERROR_NOT_SUPPORTED error. 984 * 985 * Params: 986 * group = a #GInetAddress specifying the group address to join. 987 * sourceSpecific = %TRUE if source-specific multicast should be used 988 * iface = Name of the interface to use, or %NULL 989 * 990 * Returns: %TRUE on success, %FALSE on error. 991 * 992 * Since: 2.32 993 * 994 * Throws: GException on failure. 995 */ 996 public bool joinMulticastGroup(InetAddress group, bool sourceSpecific, string iface) 997 { 998 GError* err = null; 999 1000 auto p = g_socket_join_multicast_group(gSocket, (group is null) ? null : group.getInetAddressStruct(), sourceSpecific, Str.toStringz(iface), &err) != 0; 1001 1002 if (err !is null) 1003 { 1004 throw new GException( new ErrorG(err) ); 1005 } 1006 1007 return p; 1008 } 1009 1010 /** 1011 * Removes @socket from the multicast group defined by @group, @iface, 1012 * and @source_specific (which must all have the same values they had 1013 * when you joined the group). 1014 * 1015 * @socket remains bound to its address and port, and can still receive 1016 * unicast messages after calling this. 1017 * 1018 * Params: 1019 * group = a #GInetAddress specifying the group address to leave. 1020 * sourceSpecific = %TRUE if source-specific multicast was used 1021 * iface = Interface used 1022 * 1023 * Returns: %TRUE on success, %FALSE on error. 1024 * 1025 * Since: 2.32 1026 * 1027 * Throws: GException on failure. 1028 */ 1029 public bool leaveMulticastGroup(InetAddress group, bool sourceSpecific, string iface) 1030 { 1031 GError* err = null; 1032 1033 auto p = g_socket_leave_multicast_group(gSocket, (group is null) ? null : group.getInetAddressStruct(), sourceSpecific, Str.toStringz(iface), &err) != 0; 1034 1035 if (err !is null) 1036 { 1037 throw new GException( new ErrorG(err) ); 1038 } 1039 1040 return p; 1041 } 1042 1043 /** 1044 * Marks the socket as a server socket, i.e. a socket that is used 1045 * to accept incoming requests using g_socket_accept(). 1046 * 1047 * Before calling this the socket must be bound to a local address using 1048 * g_socket_bind(). 1049 * 1050 * To set the maximum amount of outstanding clients, use 1051 * g_socket_set_listen_backlog(). 1052 * 1053 * Returns: %TRUE on success, %FALSE on error. 1054 * 1055 * Since: 2.22 1056 * 1057 * Throws: GException on failure. 1058 */ 1059 public bool listen() 1060 { 1061 GError* err = null; 1062 1063 auto p = g_socket_listen(gSocket, &err) != 0; 1064 1065 if (err !is null) 1066 { 1067 throw new GException( new ErrorG(err) ); 1068 } 1069 1070 return p; 1071 } 1072 1073 /** 1074 * Receive data (up to @size bytes) from a socket. This is mainly used by 1075 * connection-oriented sockets; it is identical to g_socket_receive_from() 1076 * with @address set to %NULL. 1077 * 1078 * For %G_SOCKET_TYPE_DATAGRAM and %G_SOCKET_TYPE_SEQPACKET sockets, 1079 * g_socket_receive() will always read either 0 or 1 complete messages from 1080 * the socket. If the received message is too large to fit in @buffer, then 1081 * the data beyond @size bytes will be discarded, without any explicit 1082 * indication that this has occurred. 1083 * 1084 * For %G_SOCKET_TYPE_STREAM sockets, g_socket_receive() can return any 1085 * number of bytes, up to @size. If more than @size bytes have been 1086 * received, the additional data will be returned in future calls to 1087 * g_socket_receive(). 1088 * 1089 * If the socket is in blocking mode the call will block until there 1090 * is some data to receive, the connection is closed, or there is an 1091 * error. If there is no data available and the socket is in 1092 * non-blocking mode, a %G_IO_ERROR_WOULD_BLOCK error will be 1093 * returned. To be notified when data is available, wait for the 1094 * %G_IO_IN condition. 1095 * 1096 * On error -1 is returned and @error is set accordingly. 1097 * 1098 * Params: 1099 * buffer = a buffer to 1100 * read data into (which should be at least @size bytes long). 1101 * cancellable = a %GCancellable or %NULL 1102 * 1103 * Returns: Number of bytes read, or 0 if the connection was closed by 1104 * the peer, or -1 on error 1105 * 1106 * Since: 2.22 1107 * 1108 * Throws: GException on failure. 1109 */ 1110 public ptrdiff_t receive(ref char[] buffer, Cancellable cancellable) 1111 { 1112 GError* err = null; 1113 1114 auto p = g_socket_receive(gSocket, buffer.ptr, cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1115 1116 if (err !is null) 1117 { 1118 throw new GException( new ErrorG(err) ); 1119 } 1120 1121 return p; 1122 } 1123 1124 /** 1125 * Receive data (up to @size bytes) from a socket. 1126 * 1127 * If @address is non-%NULL then @address will be set equal to the 1128 * source address of the received packet. 1129 * @address is owned by the caller. 1130 * 1131 * See g_socket_receive() for additional information. 1132 * 1133 * Params: 1134 * address = a pointer to a #GSocketAddress 1135 * pointer, or %NULL 1136 * buffer = a buffer to 1137 * read data into (which should be at least @size bytes long). 1138 * cancellable = a %GCancellable or %NULL 1139 * 1140 * Returns: Number of bytes read, or 0 if the connection was closed by 1141 * the peer, or -1 on error 1142 * 1143 * Since: 2.22 1144 * 1145 * Throws: GException on failure. 1146 */ 1147 public ptrdiff_t receiveFrom(out SocketAddress address, ref char[] buffer, Cancellable cancellable) 1148 { 1149 GSocketAddress* outaddress = null; 1150 GError* err = null; 1151 1152 auto p = g_socket_receive_from(gSocket, &outaddress, buffer.ptr, cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1153 1154 if (err !is null) 1155 { 1156 throw new GException( new ErrorG(err) ); 1157 } 1158 1159 address = ObjectG.getDObject!(SocketAddress)(outaddress); 1160 1161 return p; 1162 } 1163 1164 /** 1165 * Receive data from a socket. For receiving multiple messages, see 1166 * g_socket_receive_messages(); for easier use, see 1167 * g_socket_receive() and g_socket_receive_from(). 1168 * 1169 * If @address is non-%NULL then @address will be set equal to the 1170 * source address of the received packet. 1171 * @address is owned by the caller. 1172 * 1173 * @vector must point to an array of #GInputVector structs and 1174 * @num_vectors must be the length of this array. These structs 1175 * describe the buffers that received data will be scattered into. 1176 * If @num_vectors is -1, then @vectors is assumed to be terminated 1177 * by a #GInputVector with a %NULL buffer pointer. 1178 * 1179 * As a special case, if @num_vectors is 0 (in which case, @vectors 1180 * may of course be %NULL), then a single byte is received and 1181 * discarded. This is to facilitate the common practice of sending a 1182 * single '\0' byte for the purposes of transferring ancillary data. 1183 * 1184 * @messages, if non-%NULL, will be set to point to a newly-allocated 1185 * array of #GSocketControlMessage instances or %NULL if no such 1186 * messages was received. These correspond to the control messages 1187 * received from the kernel, one #GSocketControlMessage per message 1188 * from the kernel. This array is %NULL-terminated and must be freed 1189 * by the caller using g_free() after calling g_object_unref() on each 1190 * element. If @messages is %NULL, any control messages received will 1191 * be discarded. 1192 * 1193 * @num_messages, if non-%NULL, will be set to the number of control 1194 * messages received. 1195 * 1196 * If both @messages and @num_messages are non-%NULL, then 1197 * @num_messages gives the number of #GSocketControlMessage instances 1198 * in @messages (ie: not including the %NULL terminator). 1199 * 1200 * @flags is an in/out parameter. The commonly available arguments 1201 * for this are available in the #GSocketMsgFlags enum, but the 1202 * values there are the same as the system values, and the flags 1203 * are passed in as-is, so you can pass in system-specific flags too 1204 * (and g_socket_receive_message() may pass system-specific flags out). 1205 * Flags passed in to the parameter affect the receive operation; flags returned 1206 * out of it are relevant to the specific returned message. 1207 * 1208 * As with g_socket_receive(), data may be discarded if @socket is 1209 * %G_SOCKET_TYPE_DATAGRAM or %G_SOCKET_TYPE_SEQPACKET and you do not 1210 * provide enough buffer space to read a complete message. You can pass 1211 * %G_SOCKET_MSG_PEEK in @flags to peek at the current message without 1212 * removing it from the receive queue, but there is no portable way to find 1213 * out the length of the message other than by reading it into a 1214 * sufficiently-large buffer. 1215 * 1216 * If the socket is in blocking mode the call will block until there 1217 * is some data to receive, the connection is closed, or there is an 1218 * error. If there is no data available and the socket is in 1219 * non-blocking mode, a %G_IO_ERROR_WOULD_BLOCK error will be 1220 * returned. To be notified when data is available, wait for the 1221 * %G_IO_IN condition. 1222 * 1223 * On error -1 is returned and @error is set accordingly. 1224 * 1225 * Params: 1226 * address = a pointer to a #GSocketAddress 1227 * pointer, or %NULL 1228 * vectors = an array of #GInputVector structs 1229 * messages = a pointer 1230 * which may be filled with an array of #GSocketControlMessages, or %NULL 1231 * flags = a pointer to an int containing #GSocketMsgFlags flags 1232 * cancellable = a %GCancellable or %NULL 1233 * 1234 * Returns: Number of bytes read, or 0 if the connection was closed by 1235 * the peer, or -1 on error 1236 * 1237 * Since: 2.22 1238 * 1239 * Throws: GException on failure. 1240 */ 1241 public ptrdiff_t receiveMessage(out SocketAddress address, GInputVector[] vectors, out SocketControlMessage[] messages, ref int flags, Cancellable cancellable) 1242 { 1243 GSocketAddress* outaddress = null; 1244 GSocketControlMessage** outmessages = null; 1245 int numMessages; 1246 GError* err = null; 1247 1248 auto p = g_socket_receive_message(gSocket, &outaddress, vectors.ptr, cast(int)vectors.length, &outmessages, &numMessages, &flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1249 1250 if (err !is null) 1251 { 1252 throw new GException( new ErrorG(err) ); 1253 } 1254 1255 address = ObjectG.getDObject!(SocketAddress)(outaddress); 1256 1257 messages = new SocketControlMessage[numMessages]; 1258 for(size_t i = 0; i < numMessages; i++) 1259 { 1260 messages[i] = ObjectG.getDObject!(SocketControlMessage)(cast(GSocketControlMessage*) outmessages[i]); 1261 } 1262 1263 return p; 1264 } 1265 1266 /** 1267 * Receive multiple data messages from @socket in one go. This is the most 1268 * complicated and fully-featured version of this call. For easier use, see 1269 * g_socket_receive(), g_socket_receive_from(), and g_socket_receive_message(). 1270 * 1271 * @messages must point to an array of #GInputMessage structs and 1272 * @num_messages must be the length of this array. Each #GInputMessage 1273 * contains a pointer to an array of #GInputVector structs describing the 1274 * buffers that the data received in each message will be written to. Using 1275 * multiple #GInputVectors is more memory-efficient than manually copying data 1276 * out of a single buffer to multiple sources, and more system-call-efficient 1277 * than making multiple calls to g_socket_receive(), such as in scenarios where 1278 * a lot of data packets need to be received (e.g. high-bandwidth video 1279 * streaming over RTP/UDP). 1280 * 1281 * @flags modify how all messages are received. The commonly available 1282 * arguments for this are available in the #GSocketMsgFlags enum, but the 1283 * values there are the same as the system values, and the flags 1284 * are passed in as-is, so you can pass in system-specific flags too. These 1285 * flags affect the overall receive operation. Flags affecting individual 1286 * messages are returned in #GInputMessage.flags. 1287 * 1288 * The other members of #GInputMessage are treated as described in its 1289 * documentation. 1290 * 1291 * If #GSocket:blocking is %TRUE the call will block until @num_messages have 1292 * been received, or the end of the stream is reached. 1293 * 1294 * If #GSocket:blocking is %FALSE the call will return up to @num_messages 1295 * without blocking, or %G_IO_ERROR_WOULD_BLOCK if no messages are queued in the 1296 * operating system to be received. 1297 * 1298 * In blocking mode, if #GSocket:timeout is positive and is reached before any 1299 * messages are received, %G_IO_ERROR_TIMED_OUT is returned, otherwise up to 1300 * @num_messages are returned. (Note: This is effectively the 1301 * behaviour of `MSG_WAITFORONE` with recvmmsg().) 1302 * 1303 * To be notified when messages are available, wait for the 1304 * %G_IO_IN condition. Note though that you may still receive 1305 * %G_IO_ERROR_WOULD_BLOCK from g_socket_receive_messages() even if you were 1306 * previously notified of a %G_IO_IN condition. 1307 * 1308 * If the remote peer closes the connection, any messages queued in the 1309 * operating system will be returned, and subsequent calls to 1310 * g_socket_receive_messages() will return 0 (with no error set). 1311 * 1312 * On error -1 is returned and @error is set accordingly. An error will only 1313 * be returned if zero messages could be received; otherwise the number of 1314 * messages successfully received before the error will be returned. 1315 * 1316 * Params: 1317 * messages = an array of #GInputMessage structs 1318 * flags = an int containing #GSocketMsgFlags flags for the overall operation 1319 * cancellable = a %GCancellable or %NULL 1320 * 1321 * Returns: number of messages received, or -1 on error. Note that the number 1322 * of messages received may be smaller than @num_messages if in non-blocking 1323 * mode, if the peer closed the connection, or if @num_messages 1324 * was larger than `UIO_MAXIOV` (1024), in which case the caller may re-try 1325 * to receive the remaining messages. 1326 * 1327 * Since: 2.48 1328 * 1329 * Throws: GException on failure. 1330 */ 1331 public int receiveMessages(GInputMessage[] messages, int flags, Cancellable cancellable) 1332 { 1333 GError* err = null; 1334 1335 auto p = g_socket_receive_messages(gSocket, messages.ptr, cast(uint)messages.length, flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1336 1337 if (err !is null) 1338 { 1339 throw new GException( new ErrorG(err) ); 1340 } 1341 1342 return p; 1343 } 1344 1345 /** 1346 * This behaves exactly the same as g_socket_receive(), except that 1347 * the choice of blocking or non-blocking behavior is determined by 1348 * the @blocking argument rather than by @socket's properties. 1349 * 1350 * Params: 1351 * buffer = a buffer to 1352 * read data into (which should be at least @size bytes long). 1353 * blocking = whether to do blocking or non-blocking I/O 1354 * cancellable = a %GCancellable or %NULL 1355 * 1356 * Returns: Number of bytes read, or 0 if the connection was closed by 1357 * the peer, or -1 on error 1358 * 1359 * Since: 2.26 1360 * 1361 * Throws: GException on failure. 1362 */ 1363 public ptrdiff_t receiveWithBlocking(string buffer, bool blocking, Cancellable cancellable) 1364 { 1365 GError* err = null; 1366 1367 auto p = g_socket_receive_with_blocking(gSocket, Str.toStringz(buffer), cast(size_t)buffer.length, blocking, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1368 1369 if (err !is null) 1370 { 1371 throw new GException( new ErrorG(err) ); 1372 } 1373 1374 return p; 1375 } 1376 1377 /** 1378 * Tries to send @size bytes from @buffer on the socket. This is 1379 * mainly used by connection-oriented sockets; it is identical to 1380 * g_socket_send_to() with @address set to %NULL. 1381 * 1382 * If the socket is in blocking mode the call will block until there is 1383 * space for the data in the socket queue. If there is no space available 1384 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1385 * will be returned. To be notified when space is available, wait for the 1386 * %G_IO_OUT condition. Note though that you may still receive 1387 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1388 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1389 * very common due to the way the underlying APIs work.) 1390 * 1391 * On error -1 is returned and @error is set accordingly. 1392 * 1393 * Params: 1394 * buffer = the buffer 1395 * containing the data to send. 1396 * cancellable = a %GCancellable or %NULL 1397 * 1398 * Returns: Number of bytes written (which may be less than @size), or -1 1399 * on error 1400 * 1401 * Since: 2.22 1402 * 1403 * Throws: GException on failure. 1404 */ 1405 public ptrdiff_t send(string buffer, Cancellable cancellable) 1406 { 1407 GError* err = null; 1408 1409 auto p = g_socket_send(gSocket, Str.toStringz(buffer), cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1410 1411 if (err !is null) 1412 { 1413 throw new GException( new ErrorG(err) ); 1414 } 1415 1416 return p; 1417 } 1418 1419 /** 1420 * Send data to @address on @socket. For sending multiple messages see 1421 * g_socket_send_messages(); for easier use, see 1422 * g_socket_send() and g_socket_send_to(). 1423 * 1424 * If @address is %NULL then the message is sent to the default receiver 1425 * (set by g_socket_connect()). 1426 * 1427 * @vectors must point to an array of #GOutputVector structs and 1428 * @num_vectors must be the length of this array. (If @num_vectors is -1, 1429 * then @vectors is assumed to be terminated by a #GOutputVector with a 1430 * %NULL buffer pointer.) The #GOutputVector structs describe the buffers 1431 * that the sent data will be gathered from. Using multiple 1432 * #GOutputVectors is more memory-efficient than manually copying 1433 * data from multiple sources into a single buffer, and more 1434 * network-efficient than making multiple calls to g_socket_send(). 1435 * 1436 * @messages, if non-%NULL, is taken to point to an array of @num_messages 1437 * #GSocketControlMessage instances. These correspond to the control 1438 * messages to be sent on the socket. 1439 * If @num_messages is -1 then @messages is treated as a %NULL-terminated 1440 * array. 1441 * 1442 * @flags modify how the message is sent. The commonly available arguments 1443 * for this are available in the #GSocketMsgFlags enum, but the 1444 * values there are the same as the system values, and the flags 1445 * are passed in as-is, so you can pass in system-specific flags too. 1446 * 1447 * If the socket is in blocking mode the call will block until there is 1448 * space for the data in the socket queue. If there is no space available 1449 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1450 * will be returned. To be notified when space is available, wait for the 1451 * %G_IO_OUT condition. Note though that you may still receive 1452 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1453 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1454 * very common due to the way the underlying APIs work.) 1455 * 1456 * On error -1 is returned and @error is set accordingly. 1457 * 1458 * Params: 1459 * address = a #GSocketAddress, or %NULL 1460 * vectors = an array of #GOutputVector structs 1461 * messages = a pointer to an 1462 * array of #GSocketControlMessages, or %NULL. 1463 * flags = an int containing #GSocketMsgFlags flags 1464 * cancellable = a %GCancellable or %NULL 1465 * 1466 * Returns: Number of bytes written (which may be less than @size), or -1 1467 * on error 1468 * 1469 * Since: 2.22 1470 * 1471 * Throws: GException on failure. 1472 */ 1473 public ptrdiff_t sendMessage(SocketAddress address, GOutputVector[] vectors, SocketControlMessage[] messages, int flags, Cancellable cancellable) 1474 { 1475 GSocketControlMessage*[] messagesArray = new GSocketControlMessage*[messages.length]; 1476 for ( int i = 0; i < messages.length; i++ ) 1477 { 1478 messagesArray[i] = messages[i].getSocketControlMessageStruct(); 1479 } 1480 1481 GError* err = null; 1482 1483 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); 1484 1485 if (err !is null) 1486 { 1487 throw new GException( new ErrorG(err) ); 1488 } 1489 1490 return p; 1491 } 1492 1493 /** 1494 * Send multiple data messages from @socket in one go. This is the most 1495 * complicated and fully-featured version of this call. For easier use, see 1496 * g_socket_send(), g_socket_send_to(), and g_socket_send_message(). 1497 * 1498 * @messages must point to an array of #GOutputMessage structs and 1499 * @num_messages must be the length of this array. Each #GOutputMessage 1500 * contains an address to send the data to, and a pointer to an array of 1501 * #GOutputVector structs to describe the buffers that the data to be sent 1502 * for each message will be gathered from. Using multiple #GOutputVectors is 1503 * more memory-efficient than manually copying data from multiple sources 1504 * into a single buffer, and more network-efficient than making multiple 1505 * calls to g_socket_send(). Sending multiple messages in one go avoids the 1506 * overhead of making a lot of syscalls in scenarios where a lot of data 1507 * packets need to be sent (e.g. high-bandwidth video streaming over RTP/UDP), 1508 * or where the same data needs to be sent to multiple recipients. 1509 * 1510 * @flags modify how the message is sent. The commonly available arguments 1511 * for this are available in the #GSocketMsgFlags enum, but the 1512 * values there are the same as the system values, and the flags 1513 * are passed in as-is, so you can pass in system-specific flags too. 1514 * 1515 * If the socket is in blocking mode the call will block until there is 1516 * space for all the data in the socket queue. If there is no space available 1517 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1518 * will be returned if no data was written at all, otherwise the number of 1519 * messages sent will be returned. To be notified when space is available, 1520 * wait for the %G_IO_OUT condition. Note though that you may still receive 1521 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1522 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1523 * very common due to the way the underlying APIs work.) 1524 * 1525 * On error -1 is returned and @error is set accordingly. An error will only 1526 * be returned if zero messages could be sent; otherwise the number of messages 1527 * successfully sent before the error will be returned. 1528 * 1529 * Params: 1530 * messages = an array of #GOutputMessage structs 1531 * flags = an int containing #GSocketMsgFlags flags 1532 * cancellable = a %GCancellable or %NULL 1533 * 1534 * Returns: number of messages sent, or -1 on error. Note that the number of 1535 * messages sent may be smaller than @num_messages if the socket is 1536 * non-blocking or if @num_messages was larger than UIO_MAXIOV (1024), 1537 * in which case the caller may re-try to send the remaining messages. 1538 * 1539 * Since: 2.44 1540 * 1541 * Throws: GException on failure. 1542 */ 1543 public int sendMessages(GOutputMessage[] messages, int flags, Cancellable cancellable) 1544 { 1545 GError* err = null; 1546 1547 auto p = g_socket_send_messages(gSocket, messages.ptr, cast(uint)messages.length, flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1548 1549 if (err !is null) 1550 { 1551 throw new GException( new ErrorG(err) ); 1552 } 1553 1554 return p; 1555 } 1556 1557 /** 1558 * Tries to send @size bytes from @buffer to @address. If @address is 1559 * %NULL then the message is sent to the default receiver (set by 1560 * g_socket_connect()). 1561 * 1562 * See g_socket_send() for additional information. 1563 * 1564 * Params: 1565 * address = a #GSocketAddress, or %NULL 1566 * buffer = the buffer 1567 * containing the data to send. 1568 * cancellable = a %GCancellable or %NULL 1569 * 1570 * Returns: Number of bytes written (which may be less than @size), or -1 1571 * on error 1572 * 1573 * Since: 2.22 1574 * 1575 * Throws: GException on failure. 1576 */ 1577 public ptrdiff_t sendTo(SocketAddress address, string buffer, Cancellable cancellable) 1578 { 1579 GError* err = null; 1580 1581 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); 1582 1583 if (err !is null) 1584 { 1585 throw new GException( new ErrorG(err) ); 1586 } 1587 1588 return p; 1589 } 1590 1591 /** 1592 * This behaves exactly the same as g_socket_send(), except that 1593 * the choice of blocking or non-blocking behavior is determined by 1594 * the @blocking argument rather than by @socket's properties. 1595 * 1596 * Params: 1597 * buffer = the buffer 1598 * containing the data to send. 1599 * blocking = whether to do blocking or non-blocking I/O 1600 * cancellable = a %GCancellable or %NULL 1601 * 1602 * Returns: Number of bytes written (which may be less than @size), or -1 1603 * on error 1604 * 1605 * Since: 2.26 1606 * 1607 * Throws: GException on failure. 1608 */ 1609 public ptrdiff_t sendWithBlocking(string buffer, bool blocking, Cancellable cancellable) 1610 { 1611 GError* err = null; 1612 1613 auto p = g_socket_send_with_blocking(gSocket, Str.toStringz(buffer), cast(size_t)buffer.length, blocking, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1614 1615 if (err !is null) 1616 { 1617 throw new GException( new ErrorG(err) ); 1618 } 1619 1620 return p; 1621 } 1622 1623 /** 1624 * Sets the blocking mode of the socket. In blocking mode 1625 * all operations (which don’t take an explicit blocking parameter) block until 1626 * they succeed or there is an error. In 1627 * non-blocking mode all functions return results immediately or 1628 * with a %G_IO_ERROR_WOULD_BLOCK error. 1629 * 1630 * All sockets are created in blocking mode. However, note that the 1631 * platform level socket is always non-blocking, and blocking mode 1632 * is a GSocket level feature. 1633 * 1634 * Params: 1635 * blocking = Whether to use blocking I/O or not. 1636 * 1637 * Since: 2.22 1638 */ 1639 public void setBlocking(bool blocking) 1640 { 1641 g_socket_set_blocking(gSocket, blocking); 1642 } 1643 1644 /** 1645 * Sets whether @socket should allow sending to broadcast addresses. 1646 * This is %FALSE by default. 1647 * 1648 * Params: 1649 * broadcast = whether @socket should allow sending to broadcast 1650 * addresses 1651 * 1652 * Since: 2.32 1653 */ 1654 public void setBroadcast(bool broadcast) 1655 { 1656 g_socket_set_broadcast(gSocket, broadcast); 1657 } 1658 1659 /** 1660 * Sets or unsets the %SO_KEEPALIVE flag on the underlying socket. When 1661 * this flag is set on a socket, the system will attempt to verify that the 1662 * remote socket endpoint is still present if a sufficiently long period of 1663 * time passes with no data being exchanged. If the system is unable to 1664 * verify the presence of the remote endpoint, it will automatically close 1665 * the connection. 1666 * 1667 * This option is only functional on certain kinds of sockets. (Notably, 1668 * %G_SOCKET_PROTOCOL_TCP sockets.) 1669 * 1670 * The exact time between pings is system- and protocol-dependent, but will 1671 * normally be at least two hours. Most commonly, you would set this flag 1672 * on a server socket if you want to allow clients to remain idle for long 1673 * periods of time, but also want to ensure that connections are eventually 1674 * garbage-collected if clients crash or become unreachable. 1675 * 1676 * Params: 1677 * keepalive = Value for the keepalive flag 1678 * 1679 * Since: 2.22 1680 */ 1681 public void setKeepalive(bool keepalive) 1682 { 1683 g_socket_set_keepalive(gSocket, keepalive); 1684 } 1685 1686 /** 1687 * Sets the maximum number of outstanding connections allowed 1688 * when listening on this socket. If more clients than this are 1689 * connecting to the socket and the application is not handling them 1690 * on time then the new connections will be refused. 1691 * 1692 * Note that this must be called before g_socket_listen() and has no 1693 * effect if called after that. 1694 * 1695 * Params: 1696 * backlog = the maximum number of pending connections. 1697 * 1698 * Since: 2.22 1699 */ 1700 public void setListenBacklog(int backlog) 1701 { 1702 g_socket_set_listen_backlog(gSocket, backlog); 1703 } 1704 1705 /** 1706 * Sets whether outgoing multicast packets will be received by sockets 1707 * listening on that multicast address on the same host. This is %TRUE 1708 * by default. 1709 * 1710 * Params: 1711 * loopback = whether @socket should receive messages sent to its 1712 * multicast groups from the local host 1713 * 1714 * Since: 2.32 1715 */ 1716 public void setMulticastLoopback(bool loopback) 1717 { 1718 g_socket_set_multicast_loopback(gSocket, loopback); 1719 } 1720 1721 /** 1722 * Sets the time-to-live for outgoing multicast datagrams on @socket. 1723 * By default, this is 1, meaning that multicast packets will not leave 1724 * the local network. 1725 * 1726 * Params: 1727 * ttl = the time-to-live value for all multicast datagrams on @socket 1728 * 1729 * Since: 2.32 1730 */ 1731 public void setMulticastTtl(uint ttl) 1732 { 1733 g_socket_set_multicast_ttl(gSocket, ttl); 1734 } 1735 1736 /** 1737 * Sets the value of an integer-valued option on @socket, as with 1738 * setsockopt(). (If you need to set a non-integer-valued option, 1739 * you will need to call setsockopt() directly.) 1740 * 1741 * The [<gio/gnetworking.h>][gio-gnetworking.h] 1742 * header pulls in system headers that will define most of the 1743 * standard/portable socket options. For unusual socket protocols or 1744 * platform-dependent options, you may need to include additional 1745 * headers. 1746 * 1747 * Params: 1748 * level = the "API level" of the option (eg, `SOL_SOCKET`) 1749 * optname = the "name" of the option (eg, `SO_BROADCAST`) 1750 * value = the value to set the option to 1751 * 1752 * Returns: success or failure. On failure, @error will be set, and 1753 * the system error value (`errno` or WSAGetLastError()) will still 1754 * be set to the result of the setsockopt() call. 1755 * 1756 * Since: 2.36 1757 * 1758 * Throws: GException on failure. 1759 */ 1760 public bool setOption(int level, int optname, int value) 1761 { 1762 GError* err = null; 1763 1764 auto p = g_socket_set_option(gSocket, level, optname, value, &err) != 0; 1765 1766 if (err !is null) 1767 { 1768 throw new GException( new ErrorG(err) ); 1769 } 1770 1771 return p; 1772 } 1773 1774 /** 1775 * Sets the time in seconds after which I/O operations on @socket will 1776 * time out if they have not yet completed. 1777 * 1778 * On a blocking socket, this means that any blocking #GSocket 1779 * operation will time out after @timeout seconds of inactivity, 1780 * returning %G_IO_ERROR_TIMED_OUT. 1781 * 1782 * On a non-blocking socket, calls to g_socket_condition_wait() will 1783 * also fail with %G_IO_ERROR_TIMED_OUT after the given time. Sources 1784 * created with g_socket_create_source() will trigger after 1785 * @timeout seconds of inactivity, with the requested condition 1786 * set, at which point calling g_socket_receive(), g_socket_send(), 1787 * g_socket_check_connect_result(), etc, will fail with 1788 * %G_IO_ERROR_TIMED_OUT. 1789 * 1790 * If @timeout is 0 (the default), operations will never time out 1791 * on their own. 1792 * 1793 * Note that if an I/O operation is interrupted by a signal, this may 1794 * cause the timeout to be reset. 1795 * 1796 * Params: 1797 * timeout = the timeout for @socket, in seconds, or 0 for none 1798 * 1799 * Since: 2.26 1800 */ 1801 public void setTimeout(uint timeout) 1802 { 1803 g_socket_set_timeout(gSocket, timeout); 1804 } 1805 1806 /** 1807 * Sets the time-to-live for outgoing unicast packets on @socket. 1808 * By default the platform-specific default value is used. 1809 * 1810 * Params: 1811 * ttl = the time-to-live value for all unicast packets on @socket 1812 * 1813 * Since: 2.32 1814 */ 1815 public void setTtl(uint ttl) 1816 { 1817 g_socket_set_ttl(gSocket, ttl); 1818 } 1819 1820 /** 1821 * Shut down part or all of a full-duplex connection. 1822 * 1823 * If @shutdown_read is %TRUE then the receiving side of the connection 1824 * is shut down, and further reading is disallowed. 1825 * 1826 * If @shutdown_write is %TRUE then the sending side of the connection 1827 * is shut down, and further writing is disallowed. 1828 * 1829 * It is allowed for both @shutdown_read and @shutdown_write to be %TRUE. 1830 * 1831 * One example where it is useful to shut down only one side of a connection is 1832 * graceful disconnect for TCP connections where you close the sending side, 1833 * then wait for the other side to close the connection, thus ensuring that the 1834 * other side saw all sent data. 1835 * 1836 * Params: 1837 * shutdownRead = whether to shut down the read side 1838 * shutdownWrite = whether to shut down the write side 1839 * 1840 * Returns: %TRUE on success, %FALSE on error 1841 * 1842 * Since: 2.22 1843 * 1844 * Throws: GException on failure. 1845 */ 1846 public bool shutdown(bool shutdownRead, bool shutdownWrite) 1847 { 1848 GError* err = null; 1849 1850 auto p = g_socket_shutdown(gSocket, shutdownRead, shutdownWrite, &err) != 0; 1851 1852 if (err !is null) 1853 { 1854 throw new GException( new ErrorG(err) ); 1855 } 1856 1857 return p; 1858 } 1859 1860 /** 1861 * Checks if a socket is capable of speaking IPv4. 1862 * 1863 * IPv4 sockets are capable of speaking IPv4. On some operating systems 1864 * and under some combinations of circumstances IPv6 sockets are also 1865 * capable of speaking IPv4. See RFC 3493 section 3.7 for more 1866 * information. 1867 * 1868 * No other types of sockets are currently considered as being capable 1869 * of speaking IPv4. 1870 * 1871 * Returns: %TRUE if this socket can be used with IPv4. 1872 * 1873 * Since: 2.22 1874 */ 1875 public bool speaksIpv4() 1876 { 1877 return g_socket_speaks_ipv4(gSocket) != 0; 1878 } 1879 }