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