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