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