root/trunk/src/gio/Socket.d

Revision 938, 45.8 kB (checked in by Mike Wey, 3 months ago)

Update Gio to 2.28.

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