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