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