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