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