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