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 remote 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 * To bind to a given source-specific multicast address, use 986 * g_socket_join_multicast_group_ssm() instead. 987 * 988 * Params: 989 * group = a #GInetAddress specifying the group address to join. 990 * sourceSpecific = %TRUE if source-specific multicast should be used 991 * iface = Name of the interface to use, or %NULL 992 * 993 * Returns: %TRUE on success, %FALSE on error. 994 * 995 * Since: 2.32 996 * 997 * Throws: GException on failure. 998 */ 999 public bool joinMulticastGroup(InetAddress group, bool sourceSpecific, string iface) 1000 { 1001 GError* err = null; 1002 1003 auto p = g_socket_join_multicast_group(gSocket, (group is null) ? null : group.getInetAddressStruct(), sourceSpecific, Str.toStringz(iface), &err) != 0; 1004 1005 if (err !is null) 1006 { 1007 throw new GException( new ErrorG(err) ); 1008 } 1009 1010 return p; 1011 } 1012 1013 /** 1014 * Registers @socket to receive multicast messages sent to @group. 1015 * @socket must be a %G_SOCKET_TYPE_DATAGRAM socket, and must have 1016 * been bound to an appropriate interface and port with 1017 * g_socket_bind(). 1018 * 1019 * If @iface is %NULL, the system will automatically pick an interface 1020 * to bind to based on @group. 1021 * 1022 * If @source_specific is not %NULL, use source-specific multicast as 1023 * defined in RFC 4604. Note that on older platforms this may fail 1024 * with a %G_IO_ERROR_NOT_SUPPORTED error. 1025 * 1026 * Note that this function can be called multiple times for the same 1027 * @group with different @source_specific in order to receive multicast 1028 * packets from more than one source. 1029 * 1030 * Params: 1031 * group = a #GInetAddress specifying the group address to join. 1032 * sourceSpecific = a #GInetAddress specifying the 1033 * source-specific multicast address or %NULL to ignore. 1034 * iface = Name of the interface to use, or %NULL 1035 * 1036 * Returns: %TRUE on success, %FALSE on error. 1037 * 1038 * Since: 2.56 1039 * 1040 * Throws: GException on failure. 1041 */ 1042 public bool joinMulticastGroupSsm(InetAddress group, InetAddress sourceSpecific, string iface) 1043 { 1044 GError* err = null; 1045 1046 auto p = g_socket_join_multicast_group_ssm(gSocket, (group is null) ? null : group.getInetAddressStruct(), (sourceSpecific is null) ? null : sourceSpecific.getInetAddressStruct(), Str.toStringz(iface), &err) != 0; 1047 1048 if (err !is null) 1049 { 1050 throw new GException( new ErrorG(err) ); 1051 } 1052 1053 return p; 1054 } 1055 1056 /** 1057 * Removes @socket from the multicast group defined by @group, @iface, 1058 * and @source_specific (which must all have the same values they had 1059 * when you joined the group). 1060 * 1061 * @socket remains bound to its address and port, and can still receive 1062 * unicast messages after calling this. 1063 * 1064 * To unbind to a given source-specific multicast address, use 1065 * g_socket_leave_multicast_group_ssm() instead. 1066 * 1067 * Params: 1068 * group = a #GInetAddress specifying the group address to leave. 1069 * sourceSpecific = %TRUE if source-specific multicast was used 1070 * iface = Interface used 1071 * 1072 * Returns: %TRUE on success, %FALSE on error. 1073 * 1074 * Since: 2.32 1075 * 1076 * Throws: GException on failure. 1077 */ 1078 public bool leaveMulticastGroup(InetAddress group, bool sourceSpecific, string iface) 1079 { 1080 GError* err = null; 1081 1082 auto p = g_socket_leave_multicast_group(gSocket, (group is null) ? null : group.getInetAddressStruct(), sourceSpecific, Str.toStringz(iface), &err) != 0; 1083 1084 if (err !is null) 1085 { 1086 throw new GException( new ErrorG(err) ); 1087 } 1088 1089 return p; 1090 } 1091 1092 /** 1093 * Removes @socket from the multicast group defined by @group, @iface, 1094 * and @source_specific (which must all have the same values they had 1095 * when you joined the group). 1096 * 1097 * @socket remains bound to its address and port, and can still receive 1098 * unicast messages after calling this. 1099 * 1100 * Params: 1101 * group = a #GInetAddress specifying the group address to leave. 1102 * sourceSpecific = a #GInetAddress specifying the 1103 * source-specific multicast address or %NULL to ignore. 1104 * iface = Name of the interface to use, or %NULL 1105 * 1106 * Returns: %TRUE on success, %FALSE on error. 1107 * 1108 * Since: 2.56 1109 * 1110 * Throws: GException on failure. 1111 */ 1112 public bool leaveMulticastGroupSsm(InetAddress group, InetAddress sourceSpecific, string iface) 1113 { 1114 GError* err = null; 1115 1116 auto p = g_socket_leave_multicast_group_ssm(gSocket, (group is null) ? null : group.getInetAddressStruct(), (sourceSpecific is null) ? null : sourceSpecific.getInetAddressStruct(), Str.toStringz(iface), &err) != 0; 1117 1118 if (err !is null) 1119 { 1120 throw new GException( new ErrorG(err) ); 1121 } 1122 1123 return p; 1124 } 1125 1126 /** 1127 * Marks the socket as a server socket, i.e. a socket that is used 1128 * to accept incoming requests using g_socket_accept(). 1129 * 1130 * Before calling this the socket must be bound to a local address using 1131 * g_socket_bind(). 1132 * 1133 * To set the maximum amount of outstanding clients, use 1134 * g_socket_set_listen_backlog(). 1135 * 1136 * Returns: %TRUE on success, %FALSE on error. 1137 * 1138 * Since: 2.22 1139 * 1140 * Throws: GException on failure. 1141 */ 1142 public bool listen() 1143 { 1144 GError* err = null; 1145 1146 auto p = g_socket_listen(gSocket, &err) != 0; 1147 1148 if (err !is null) 1149 { 1150 throw new GException( new ErrorG(err) ); 1151 } 1152 1153 return p; 1154 } 1155 1156 /** 1157 * Receive data (up to @size bytes) from a socket. This is mainly used by 1158 * connection-oriented sockets; it is identical to g_socket_receive_from() 1159 * with @address set to %NULL. 1160 * 1161 * For %G_SOCKET_TYPE_DATAGRAM and %G_SOCKET_TYPE_SEQPACKET sockets, 1162 * g_socket_receive() will always read either 0 or 1 complete messages from 1163 * the socket. If the received message is too large to fit in @buffer, then 1164 * the data beyond @size bytes will be discarded, without any explicit 1165 * indication that this has occurred. 1166 * 1167 * For %G_SOCKET_TYPE_STREAM sockets, g_socket_receive() can return any 1168 * number of bytes, up to @size. If more than @size bytes have been 1169 * received, the additional data will be returned in future calls to 1170 * g_socket_receive(). 1171 * 1172 * If the socket is in blocking mode the call will block until there 1173 * is some data to receive, the connection is closed, or there is an 1174 * error. If there is no data available and the socket is in 1175 * non-blocking mode, a %G_IO_ERROR_WOULD_BLOCK error will be 1176 * returned. To be notified when data is available, wait for the 1177 * %G_IO_IN condition. 1178 * 1179 * On error -1 is returned and @error is set accordingly. 1180 * 1181 * Params: 1182 * buffer = a buffer to 1183 * read data into (which should be at least @size bytes long). 1184 * cancellable = a %GCancellable or %NULL 1185 * 1186 * Returns: Number of bytes read, or 0 if the connection was closed by 1187 * the peer, or -1 on error 1188 * 1189 * Since: 2.22 1190 * 1191 * Throws: GException on failure. 1192 */ 1193 public ptrdiff_t receive(ref char[] buffer, Cancellable cancellable) 1194 { 1195 GError* err = null; 1196 1197 auto p = g_socket_receive(gSocket, buffer.ptr, cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1198 1199 if (err !is null) 1200 { 1201 throw new GException( new ErrorG(err) ); 1202 } 1203 1204 return p; 1205 } 1206 1207 /** 1208 * Receive data (up to @size bytes) from a socket. 1209 * 1210 * If @address is non-%NULL then @address will be set equal to the 1211 * source address of the received packet. 1212 * @address is owned by the caller. 1213 * 1214 * See g_socket_receive() for additional information. 1215 * 1216 * Params: 1217 * address = a pointer to a #GSocketAddress 1218 * pointer, or %NULL 1219 * buffer = a buffer to 1220 * read data into (which should be at least @size bytes long). 1221 * cancellable = a %GCancellable or %NULL 1222 * 1223 * Returns: Number of bytes read, or 0 if the connection was closed by 1224 * the peer, or -1 on error 1225 * 1226 * Since: 2.22 1227 * 1228 * Throws: GException on failure. 1229 */ 1230 public ptrdiff_t receiveFrom(out SocketAddress address, ref char[] buffer, Cancellable cancellable) 1231 { 1232 GSocketAddress* outaddress = null; 1233 GError* err = null; 1234 1235 auto p = g_socket_receive_from(gSocket, &outaddress, buffer.ptr, cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1236 1237 if (err !is null) 1238 { 1239 throw new GException( new ErrorG(err) ); 1240 } 1241 1242 address = ObjectG.getDObject!(SocketAddress)(outaddress); 1243 1244 return p; 1245 } 1246 1247 /** 1248 * Receive data from a socket. For receiving multiple messages, see 1249 * g_socket_receive_messages(); for easier use, see 1250 * g_socket_receive() and g_socket_receive_from(). 1251 * 1252 * If @address is non-%NULL then @address will be set equal to the 1253 * source address of the received packet. 1254 * @address is owned by the caller. 1255 * 1256 * @vector must point to an array of #GInputVector structs and 1257 * @num_vectors must be the length of this array. These structs 1258 * describe the buffers that received data will be scattered into. 1259 * If @num_vectors is -1, then @vectors is assumed to be terminated 1260 * by a #GInputVector with a %NULL buffer pointer. 1261 * 1262 * As a special case, if @num_vectors is 0 (in which case, @vectors 1263 * may of course be %NULL), then a single byte is received and 1264 * discarded. This is to facilitate the common practice of sending a 1265 * single '\0' byte for the purposes of transferring ancillary data. 1266 * 1267 * @messages, if non-%NULL, will be set to point to a newly-allocated 1268 * array of #GSocketControlMessage instances or %NULL if no such 1269 * messages was received. These correspond to the control messages 1270 * received from the kernel, one #GSocketControlMessage per message 1271 * from the kernel. This array is %NULL-terminated and must be freed 1272 * by the caller using g_free() after calling g_object_unref() on each 1273 * element. If @messages is %NULL, any control messages received will 1274 * be discarded. 1275 * 1276 * @num_messages, if non-%NULL, will be set to the number of control 1277 * messages received. 1278 * 1279 * If both @messages and @num_messages are non-%NULL, then 1280 * @num_messages gives the number of #GSocketControlMessage instances 1281 * in @messages (ie: not including the %NULL terminator). 1282 * 1283 * @flags is an in/out parameter. The commonly available arguments 1284 * for this are available in the #GSocketMsgFlags enum, but the 1285 * values there are the same as the system values, and the flags 1286 * are passed in as-is, so you can pass in system-specific flags too 1287 * (and g_socket_receive_message() may pass system-specific flags out). 1288 * Flags passed in to the parameter affect the receive operation; flags returned 1289 * out of it are relevant to the specific returned message. 1290 * 1291 * As with g_socket_receive(), data may be discarded if @socket is 1292 * %G_SOCKET_TYPE_DATAGRAM or %G_SOCKET_TYPE_SEQPACKET and you do not 1293 * provide enough buffer space to read a complete message. You can pass 1294 * %G_SOCKET_MSG_PEEK in @flags to peek at the current message without 1295 * removing it from the receive queue, but there is no portable way to find 1296 * out the length of the message other than by reading it into a 1297 * sufficiently-large buffer. 1298 * 1299 * If the socket is in blocking mode the call will block until there 1300 * is some data to receive, the connection is closed, or there is an 1301 * error. If there is no data available and the socket is in 1302 * non-blocking mode, a %G_IO_ERROR_WOULD_BLOCK error will be 1303 * returned. To be notified when data is available, wait for the 1304 * %G_IO_IN condition. 1305 * 1306 * On error -1 is returned and @error is set accordingly. 1307 * 1308 * Params: 1309 * address = a pointer to a #GSocketAddress 1310 * pointer, or %NULL 1311 * vectors = an array of #GInputVector structs 1312 * messages = a pointer 1313 * which may be filled with an array of #GSocketControlMessages, or %NULL 1314 * flags = a pointer to an int containing #GSocketMsgFlags flags 1315 * cancellable = a %GCancellable or %NULL 1316 * 1317 * Returns: Number of bytes read, or 0 if the connection was closed by 1318 * the peer, or -1 on error 1319 * 1320 * Since: 2.22 1321 * 1322 * Throws: GException on failure. 1323 */ 1324 public ptrdiff_t receiveMessage(out SocketAddress address, GInputVector[] vectors, out SocketControlMessage[] messages, ref int flags, Cancellable cancellable) 1325 { 1326 GSocketAddress* outaddress = null; 1327 GSocketControlMessage** outmessages = null; 1328 int numMessages; 1329 GError* err = null; 1330 1331 auto p = g_socket_receive_message(gSocket, &outaddress, vectors.ptr, cast(int)vectors.length, &outmessages, &numMessages, &flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1332 1333 if (err !is null) 1334 { 1335 throw new GException( new ErrorG(err) ); 1336 } 1337 1338 address = ObjectG.getDObject!(SocketAddress)(outaddress); 1339 1340 messages = new SocketControlMessage[numMessages]; 1341 for(size_t i = 0; i < numMessages; i++) 1342 { 1343 messages[i] = ObjectG.getDObject!(SocketControlMessage)(cast(GSocketControlMessage*) outmessages[i]); 1344 } 1345 1346 return p; 1347 } 1348 1349 /** 1350 * Receive multiple data messages from @socket in one go. This is the most 1351 * complicated and fully-featured version of this call. For easier use, see 1352 * g_socket_receive(), g_socket_receive_from(), and g_socket_receive_message(). 1353 * 1354 * @messages must point to an array of #GInputMessage structs and 1355 * @num_messages must be the length of this array. Each #GInputMessage 1356 * contains a pointer to an array of #GInputVector structs describing the 1357 * buffers that the data received in each message will be written to. Using 1358 * multiple #GInputVectors is more memory-efficient than manually copying data 1359 * out of a single buffer to multiple sources, and more system-call-efficient 1360 * than making multiple calls to g_socket_receive(), such as in scenarios where 1361 * a lot of data packets need to be received (e.g. high-bandwidth video 1362 * streaming over RTP/UDP). 1363 * 1364 * @flags modify how all messages are received. The commonly available 1365 * arguments for this are available in the #GSocketMsgFlags enum, but the 1366 * values there are the same as the system values, and the flags 1367 * are passed in as-is, so you can pass in system-specific flags too. These 1368 * flags affect the overall receive operation. Flags affecting individual 1369 * messages are returned in #GInputMessage.flags. 1370 * 1371 * The other members of #GInputMessage are treated as described in its 1372 * documentation. 1373 * 1374 * If #GSocket:blocking is %TRUE the call will block until @num_messages have 1375 * been received, or the end of the stream is reached. 1376 * 1377 * If #GSocket:blocking is %FALSE the call will return up to @num_messages 1378 * without blocking, or %G_IO_ERROR_WOULD_BLOCK if no messages are queued in the 1379 * operating system to be received. 1380 * 1381 * In blocking mode, if #GSocket:timeout is positive and is reached before any 1382 * messages are received, %G_IO_ERROR_TIMED_OUT is returned, otherwise up to 1383 * @num_messages are returned. (Note: This is effectively the 1384 * behaviour of `MSG_WAITFORONE` with recvmmsg().) 1385 * 1386 * To be notified when messages are available, wait for the 1387 * %G_IO_IN condition. Note though that you may still receive 1388 * %G_IO_ERROR_WOULD_BLOCK from g_socket_receive_messages() even if you were 1389 * previously notified of a %G_IO_IN condition. 1390 * 1391 * If the remote peer closes the connection, any messages queued in the 1392 * operating system will be returned, and subsequent calls to 1393 * g_socket_receive_messages() will return 0 (with no error set). 1394 * 1395 * On error -1 is returned and @error is set accordingly. An error will only 1396 * be returned if zero messages could be received; otherwise the number of 1397 * messages successfully received before the error will be returned. 1398 * 1399 * Params: 1400 * messages = an array of #GInputMessage structs 1401 * flags = an int containing #GSocketMsgFlags flags for the overall operation 1402 * cancellable = a %GCancellable or %NULL 1403 * 1404 * Returns: number of messages received, or -1 on error. Note that the number 1405 * of messages received may be smaller than @num_messages if in non-blocking 1406 * mode, if the peer closed the connection, or if @num_messages 1407 * was larger than `UIO_MAXIOV` (1024), in which case the caller may re-try 1408 * to receive the remaining messages. 1409 * 1410 * Since: 2.48 1411 * 1412 * Throws: GException on failure. 1413 */ 1414 public int receiveMessages(GInputMessage[] messages, int flags, Cancellable cancellable) 1415 { 1416 GError* err = null; 1417 1418 auto p = g_socket_receive_messages(gSocket, messages.ptr, cast(uint)messages.length, flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1419 1420 if (err !is null) 1421 { 1422 throw new GException( new ErrorG(err) ); 1423 } 1424 1425 return p; 1426 } 1427 1428 /** 1429 * This behaves exactly the same as g_socket_receive(), except that 1430 * the choice of blocking or non-blocking behavior is determined by 1431 * the @blocking argument rather than by @socket's properties. 1432 * 1433 * Params: 1434 * buffer = a buffer to 1435 * read data into (which should be at least @size bytes long). 1436 * blocking = whether to do blocking or non-blocking I/O 1437 * cancellable = a %GCancellable or %NULL 1438 * 1439 * Returns: Number of bytes read, or 0 if the connection was closed by 1440 * the peer, or -1 on error 1441 * 1442 * Since: 2.26 1443 * 1444 * Throws: GException on failure. 1445 */ 1446 public ptrdiff_t receiveWithBlocking(string buffer, bool blocking, Cancellable cancellable) 1447 { 1448 GError* err = null; 1449 1450 auto p = g_socket_receive_with_blocking(gSocket, Str.toStringz(buffer), cast(size_t)buffer.length, blocking, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1451 1452 if (err !is null) 1453 { 1454 throw new GException( new ErrorG(err) ); 1455 } 1456 1457 return p; 1458 } 1459 1460 /** 1461 * Tries to send @size bytes from @buffer on the socket. This is 1462 * mainly used by connection-oriented sockets; it is identical to 1463 * g_socket_send_to() with @address set to %NULL. 1464 * 1465 * If the socket is in blocking mode the call will block until there is 1466 * space for the data in the socket queue. If there is no space available 1467 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1468 * will be returned. To be notified when space is available, wait for the 1469 * %G_IO_OUT condition. Note though that you may still receive 1470 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1471 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1472 * very common due to the way the underlying APIs work.) 1473 * 1474 * On error -1 is returned and @error is set accordingly. 1475 * 1476 * Params: 1477 * buffer = the buffer 1478 * containing the data to send. 1479 * cancellable = a %GCancellable or %NULL 1480 * 1481 * Returns: Number of bytes written (which may be less than @size), or -1 1482 * on error 1483 * 1484 * Since: 2.22 1485 * 1486 * Throws: GException on failure. 1487 */ 1488 public ptrdiff_t send(string buffer, Cancellable cancellable) 1489 { 1490 GError* err = null; 1491 1492 auto p = g_socket_send(gSocket, Str.toStringz(buffer), cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1493 1494 if (err !is null) 1495 { 1496 throw new GException( new ErrorG(err) ); 1497 } 1498 1499 return p; 1500 } 1501 1502 /** 1503 * Send data to @address on @socket. For sending multiple messages see 1504 * g_socket_send_messages(); for easier use, see 1505 * g_socket_send() and g_socket_send_to(). 1506 * 1507 * If @address is %NULL then the message is sent to the default receiver 1508 * (set by g_socket_connect()). 1509 * 1510 * @vectors must point to an array of #GOutputVector structs and 1511 * @num_vectors must be the length of this array. (If @num_vectors is -1, 1512 * then @vectors is assumed to be terminated by a #GOutputVector with a 1513 * %NULL buffer pointer.) The #GOutputVector structs describe the buffers 1514 * that the sent data will be gathered from. Using multiple 1515 * #GOutputVectors is more memory-efficient than manually copying 1516 * data from multiple sources into a single buffer, and more 1517 * network-efficient than making multiple calls to g_socket_send(). 1518 * 1519 * @messages, if non-%NULL, is taken to point to an array of @num_messages 1520 * #GSocketControlMessage instances. These correspond to the control 1521 * messages to be sent on the socket. 1522 * If @num_messages is -1 then @messages is treated as a %NULL-terminated 1523 * array. 1524 * 1525 * @flags modify how the message is sent. The commonly available arguments 1526 * for this are available in the #GSocketMsgFlags enum, but the 1527 * values there are the same as the system values, and the flags 1528 * are passed in as-is, so you can pass in system-specific flags too. 1529 * 1530 * If the socket is in blocking mode the call will block until there is 1531 * space for the data in the socket queue. If there is no space available 1532 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1533 * will be returned. To be notified when space is available, wait for the 1534 * %G_IO_OUT condition. Note though that you may still receive 1535 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1536 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1537 * very common due to the way the underlying APIs work.) 1538 * 1539 * On error -1 is returned and @error is set accordingly. 1540 * 1541 * Params: 1542 * address = a #GSocketAddress, or %NULL 1543 * vectors = an array of #GOutputVector structs 1544 * messages = a pointer to an 1545 * array of #GSocketControlMessages, or %NULL. 1546 * flags = an int containing #GSocketMsgFlags flags 1547 * cancellable = a %GCancellable or %NULL 1548 * 1549 * Returns: Number of bytes written (which may be less than @size), or -1 1550 * on error 1551 * 1552 * Since: 2.22 1553 * 1554 * Throws: GException on failure. 1555 */ 1556 public ptrdiff_t sendMessage(SocketAddress address, GOutputVector[] vectors, SocketControlMessage[] messages, int flags, Cancellable cancellable) 1557 { 1558 GSocketControlMessage*[] messagesArray = new GSocketControlMessage*[messages.length]; 1559 for ( int i = 0; i < messages.length; i++ ) 1560 { 1561 messagesArray[i] = messages[i].getSocketControlMessageStruct(); 1562 } 1563 1564 GError* err = null; 1565 1566 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); 1567 1568 if (err !is null) 1569 { 1570 throw new GException( new ErrorG(err) ); 1571 } 1572 1573 return p; 1574 } 1575 1576 /** 1577 * Send multiple data messages from @socket in one go. This is the most 1578 * complicated and fully-featured version of this call. For easier use, see 1579 * g_socket_send(), g_socket_send_to(), and g_socket_send_message(). 1580 * 1581 * @messages must point to an array of #GOutputMessage structs and 1582 * @num_messages must be the length of this array. Each #GOutputMessage 1583 * contains an address to send the data to, and a pointer to an array of 1584 * #GOutputVector structs to describe the buffers that the data to be sent 1585 * for each message will be gathered from. Using multiple #GOutputVectors is 1586 * more memory-efficient than manually copying data from multiple sources 1587 * into a single buffer, and more network-efficient than making multiple 1588 * calls to g_socket_send(). Sending multiple messages in one go avoids the 1589 * overhead of making a lot of syscalls in scenarios where a lot of data 1590 * packets need to be sent (e.g. high-bandwidth video streaming over RTP/UDP), 1591 * or where the same data needs to be sent to multiple recipients. 1592 * 1593 * @flags modify how the message is sent. The commonly available arguments 1594 * for this are available in the #GSocketMsgFlags enum, but the 1595 * values there are the same as the system values, and the flags 1596 * are passed in as-is, so you can pass in system-specific flags too. 1597 * 1598 * If the socket is in blocking mode the call will block until there is 1599 * space for all the data in the socket queue. If there is no space available 1600 * and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error 1601 * will be returned if no data was written at all, otherwise the number of 1602 * messages sent will be returned. To be notified when space is available, 1603 * wait for the %G_IO_OUT condition. Note though that you may still receive 1604 * %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously 1605 * notified of a %G_IO_OUT condition. (On Windows in particular, this is 1606 * very common due to the way the underlying APIs work.) 1607 * 1608 * On error -1 is returned and @error is set accordingly. An error will only 1609 * be returned if zero messages could be sent; otherwise the number of messages 1610 * successfully sent before the error will be returned. 1611 * 1612 * Params: 1613 * messages = an array of #GOutputMessage structs 1614 * flags = an int containing #GSocketMsgFlags flags 1615 * cancellable = a %GCancellable or %NULL 1616 * 1617 * Returns: number of messages sent, or -1 on error. Note that the number of 1618 * messages sent may be smaller than @num_messages if the socket is 1619 * non-blocking or if @num_messages was larger than UIO_MAXIOV (1024), 1620 * in which case the caller may re-try to send the remaining messages. 1621 * 1622 * Since: 2.44 1623 * 1624 * Throws: GException on failure. 1625 */ 1626 public int sendMessages(GOutputMessage[] messages, int flags, Cancellable cancellable) 1627 { 1628 GError* err = null; 1629 1630 auto p = g_socket_send_messages(gSocket, messages.ptr, cast(uint)messages.length, flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1631 1632 if (err !is null) 1633 { 1634 throw new GException( new ErrorG(err) ); 1635 } 1636 1637 return p; 1638 } 1639 1640 /** 1641 * Tries to send @size bytes from @buffer to @address. If @address is 1642 * %NULL then the message is sent to the default receiver (set by 1643 * g_socket_connect()). 1644 * 1645 * See g_socket_send() for additional information. 1646 * 1647 * Params: 1648 * address = a #GSocketAddress, or %NULL 1649 * buffer = the buffer 1650 * containing the data to send. 1651 * cancellable = a %GCancellable or %NULL 1652 * 1653 * Returns: Number of bytes written (which may be less than @size), or -1 1654 * on error 1655 * 1656 * Since: 2.22 1657 * 1658 * Throws: GException on failure. 1659 */ 1660 public ptrdiff_t sendTo(SocketAddress address, string buffer, Cancellable cancellable) 1661 { 1662 GError* err = null; 1663 1664 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); 1665 1666 if (err !is null) 1667 { 1668 throw new GException( new ErrorG(err) ); 1669 } 1670 1671 return p; 1672 } 1673 1674 /** 1675 * This behaves exactly the same as g_socket_send(), except that 1676 * the choice of blocking or non-blocking behavior is determined by 1677 * the @blocking argument rather than by @socket's properties. 1678 * 1679 * Params: 1680 * buffer = the buffer 1681 * containing the data to send. 1682 * blocking = whether to do blocking or non-blocking I/O 1683 * cancellable = a %GCancellable or %NULL 1684 * 1685 * Returns: Number of bytes written (which may be less than @size), or -1 1686 * on error 1687 * 1688 * Since: 2.26 1689 * 1690 * Throws: GException on failure. 1691 */ 1692 public ptrdiff_t sendWithBlocking(string buffer, bool blocking, Cancellable cancellable) 1693 { 1694 GError* err = null; 1695 1696 auto p = g_socket_send_with_blocking(gSocket, Str.toStringz(buffer), cast(size_t)buffer.length, blocking, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 1697 1698 if (err !is null) 1699 { 1700 throw new GException( new ErrorG(err) ); 1701 } 1702 1703 return p; 1704 } 1705 1706 /** 1707 * Sets the blocking mode of the socket. In blocking mode 1708 * all operations (which don’t take an explicit blocking parameter) block until 1709 * they succeed or there is an error. In 1710 * non-blocking mode all functions return results immediately or 1711 * with a %G_IO_ERROR_WOULD_BLOCK error. 1712 * 1713 * All sockets are created in blocking mode. However, note that the 1714 * platform level socket is always non-blocking, and blocking mode 1715 * is a GSocket level feature. 1716 * 1717 * Params: 1718 * blocking = Whether to use blocking I/O or not. 1719 * 1720 * Since: 2.22 1721 */ 1722 public void setBlocking(bool blocking) 1723 { 1724 g_socket_set_blocking(gSocket, blocking); 1725 } 1726 1727 /** 1728 * Sets whether @socket should allow sending to broadcast addresses. 1729 * This is %FALSE by default. 1730 * 1731 * Params: 1732 * broadcast = whether @socket should allow sending to broadcast 1733 * addresses 1734 * 1735 * Since: 2.32 1736 */ 1737 public void setBroadcast(bool broadcast) 1738 { 1739 g_socket_set_broadcast(gSocket, broadcast); 1740 } 1741 1742 /** 1743 * Sets or unsets the %SO_KEEPALIVE flag on the underlying socket. When 1744 * this flag is set on a socket, the system will attempt to verify that the 1745 * remote socket endpoint is still present if a sufficiently long period of 1746 * time passes with no data being exchanged. If the system is unable to 1747 * verify the presence of the remote endpoint, it will automatically close 1748 * the connection. 1749 * 1750 * This option is only functional on certain kinds of sockets. (Notably, 1751 * %G_SOCKET_PROTOCOL_TCP sockets.) 1752 * 1753 * The exact time between pings is system- and protocol-dependent, but will 1754 * normally be at least two hours. Most commonly, you would set this flag 1755 * on a server socket if you want to allow clients to remain idle for long 1756 * periods of time, but also want to ensure that connections are eventually 1757 * garbage-collected if clients crash or become unreachable. 1758 * 1759 * Params: 1760 * keepalive = Value for the keepalive flag 1761 * 1762 * Since: 2.22 1763 */ 1764 public void setKeepalive(bool keepalive) 1765 { 1766 g_socket_set_keepalive(gSocket, keepalive); 1767 } 1768 1769 /** 1770 * Sets the maximum number of outstanding connections allowed 1771 * when listening on this socket. If more clients than this are 1772 * connecting to the socket and the application is not handling them 1773 * on time then the new connections will be refused. 1774 * 1775 * Note that this must be called before g_socket_listen() and has no 1776 * effect if called after that. 1777 * 1778 * Params: 1779 * backlog = the maximum number of pending connections. 1780 * 1781 * Since: 2.22 1782 */ 1783 public void setListenBacklog(int backlog) 1784 { 1785 g_socket_set_listen_backlog(gSocket, backlog); 1786 } 1787 1788 /** 1789 * Sets whether outgoing multicast packets will be received by sockets 1790 * listening on that multicast address on the same host. This is %TRUE 1791 * by default. 1792 * 1793 * Params: 1794 * loopback = whether @socket should receive messages sent to its 1795 * multicast groups from the local host 1796 * 1797 * Since: 2.32 1798 */ 1799 public void setMulticastLoopback(bool loopback) 1800 { 1801 g_socket_set_multicast_loopback(gSocket, loopback); 1802 } 1803 1804 /** 1805 * Sets the time-to-live for outgoing multicast datagrams on @socket. 1806 * By default, this is 1, meaning that multicast packets will not leave 1807 * the local network. 1808 * 1809 * Params: 1810 * ttl = the time-to-live value for all multicast datagrams on @socket 1811 * 1812 * Since: 2.32 1813 */ 1814 public void setMulticastTtl(uint ttl) 1815 { 1816 g_socket_set_multicast_ttl(gSocket, ttl); 1817 } 1818 1819 /** 1820 * Sets the value of an integer-valued option on @socket, as with 1821 * setsockopt(). (If you need to set a non-integer-valued option, 1822 * you will need to call setsockopt() directly.) 1823 * 1824 * The [<gio/gnetworking.h>][gio-gnetworking.h] 1825 * header pulls in system headers that will define most of the 1826 * standard/portable socket options. For unusual socket protocols or 1827 * platform-dependent options, you may need to include additional 1828 * headers. 1829 * 1830 * Params: 1831 * level = the "API level" of the option (eg, `SOL_SOCKET`) 1832 * optname = the "name" of the option (eg, `SO_BROADCAST`) 1833 * value = the value to set the option to 1834 * 1835 * Returns: success or failure. On failure, @error will be set, and 1836 * the system error value (`errno` or WSAGetLastError()) will still 1837 * be set to the result of the setsockopt() call. 1838 * 1839 * Since: 2.36 1840 * 1841 * Throws: GException on failure. 1842 */ 1843 public bool setOption(int level, int optname, int value) 1844 { 1845 GError* err = null; 1846 1847 auto p = g_socket_set_option(gSocket, level, optname, value, &err) != 0; 1848 1849 if (err !is null) 1850 { 1851 throw new GException( new ErrorG(err) ); 1852 } 1853 1854 return p; 1855 } 1856 1857 /** 1858 * Sets the time in seconds after which I/O operations on @socket will 1859 * time out if they have not yet completed. 1860 * 1861 * On a blocking socket, this means that any blocking #GSocket 1862 * operation will time out after @timeout seconds of inactivity, 1863 * returning %G_IO_ERROR_TIMED_OUT. 1864 * 1865 * On a non-blocking socket, calls to g_socket_condition_wait() will 1866 * also fail with %G_IO_ERROR_TIMED_OUT after the given time. Sources 1867 * created with g_socket_create_source() will trigger after 1868 * @timeout seconds of inactivity, with the requested condition 1869 * set, at which point calling g_socket_receive(), g_socket_send(), 1870 * g_socket_check_connect_result(), etc, will fail with 1871 * %G_IO_ERROR_TIMED_OUT. 1872 * 1873 * If @timeout is 0 (the default), operations will never time out 1874 * on their own. 1875 * 1876 * Note that if an I/O operation is interrupted by a signal, this may 1877 * cause the timeout to be reset. 1878 * 1879 * Params: 1880 * timeout = the timeout for @socket, in seconds, or 0 for none 1881 * 1882 * Since: 2.26 1883 */ 1884 public void setTimeout(uint timeout) 1885 { 1886 g_socket_set_timeout(gSocket, timeout); 1887 } 1888 1889 /** 1890 * Sets the time-to-live for outgoing unicast packets on @socket. 1891 * By default the platform-specific default value is used. 1892 * 1893 * Params: 1894 * ttl = the time-to-live value for all unicast packets on @socket 1895 * 1896 * Since: 2.32 1897 */ 1898 public void setTtl(uint ttl) 1899 { 1900 g_socket_set_ttl(gSocket, ttl); 1901 } 1902 1903 /** 1904 * Shut down part or all of a full-duplex connection. 1905 * 1906 * If @shutdown_read is %TRUE then the receiving side of the connection 1907 * is shut down, and further reading is disallowed. 1908 * 1909 * If @shutdown_write is %TRUE then the sending side of the connection 1910 * is shut down, and further writing is disallowed. 1911 * 1912 * It is allowed for both @shutdown_read and @shutdown_write to be %TRUE. 1913 * 1914 * One example where it is useful to shut down only one side of a connection is 1915 * graceful disconnect for TCP connections where you close the sending side, 1916 * then wait for the other side to close the connection, thus ensuring that the 1917 * other side saw all sent data. 1918 * 1919 * Params: 1920 * shutdownRead = whether to shut down the read side 1921 * shutdownWrite = whether to shut down the write side 1922 * 1923 * Returns: %TRUE on success, %FALSE on error 1924 * 1925 * Since: 2.22 1926 * 1927 * Throws: GException on failure. 1928 */ 1929 public bool shutdown(bool shutdownRead, bool shutdownWrite) 1930 { 1931 GError* err = null; 1932 1933 auto p = g_socket_shutdown(gSocket, shutdownRead, shutdownWrite, &err) != 0; 1934 1935 if (err !is null) 1936 { 1937 throw new GException( new ErrorG(err) ); 1938 } 1939 1940 return p; 1941 } 1942 1943 /** 1944 * Checks if a socket is capable of speaking IPv4. 1945 * 1946 * IPv4 sockets are capable of speaking IPv4. On some operating systems 1947 * and under some combinations of circumstances IPv6 sockets are also 1948 * capable of speaking IPv4. See RFC 3493 section 3.7 for more 1949 * information. 1950 * 1951 * No other types of sockets are currently considered as being capable 1952 * of speaking IPv4. 1953 * 1954 * Returns: %TRUE if this socket can be used with IPv4. 1955 * 1956 * Since: 2.22 1957 */ 1958 public bool speaksIpv4() 1959 { 1960 return g_socket_speaks_ipv4(gSocket) != 0; 1961 } 1962 }