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