1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.DatagramBasedT;
26 
27 public  import gio.Cancellable;
28 public  import glib.ErrorG;
29 public  import glib.GException;
30 public  import glib.Source;
31 public  import gtkc.gio;
32 public  import gtkc.giotypes;
33 
34 
35 /**
36  * A #GDatagramBased is a networking interface for representing datagram-based
37  * communications. It is a more or less direct mapping of the core parts of the
38  * BSD socket API in a portable GObject interface. It is implemented by
39  * #GSocket, which wraps the UNIX socket API on UNIX and winsock2 on Windows.
40  * 
41  * #GDatagramBased is entirely platform independent, and is intended to be used
42  * alongside higher-level networking APIs such as #GIOStream.
43  * 
44  * It uses vectored scatter/gather I/O by default, allowing for many messages
45  * to be sent or received in a single call. Where possible, implementations of
46  * the interface should take advantage of vectored I/O to minimise processing
47  * or system calls. For example, #GSocket uses recvmmsg() and sendmmsg() where
48  * possible. Callers should take advantage of scatter/gather I/O (the use of
49  * multiple buffers per message) to avoid unnecessary copying of data to
50  * assemble or disassemble a message.
51  * 
52  * Each #GDatagramBased operation has a timeout parameter which may be negative
53  * for blocking behaviour, zero for non-blocking behaviour, or positive for
54  * timeout behaviour. A blocking operation blocks until finished or there is an
55  * error. A non-blocking operation will return immediately with a
56  * %G_IO_ERROR_WOULD_BLOCK error if it cannot make progress. A timeout operation
57  * will block until the operation is complete or the timeout expires; if the
58  * timeout expires it will return what progress it made, or
59  * %G_IO_ERROR_TIMED_OUT if no progress was made. To know when a call would
60  * successfully run you can call g_datagram_based_condition_check() or
61  * g_datagram_based_condition_wait(). You can also use
62  * g_datagram_based_create_source() and attach it to a #GMainContext to get
63  * callbacks when I/O is possible.
64  * 
65  * When running a non-blocking operation applications should always be able to
66  * handle getting a %G_IO_ERROR_WOULD_BLOCK error even when some other function
67  * said that I/O was possible. This can easily happen in case of a race
68  * condition in the application, but it can also happen for other reasons. For
69  * instance, on Windows a socket is always seen as writable until a write
70  * returns %G_IO_ERROR_WOULD_BLOCK.
71  * 
72  * As with #GSocket, #GDatagramBaseds can be either connection oriented (for
73  * example, SCTP) or connectionless (for example, UDP). #GDatagramBaseds must be
74  * datagram-based, not stream-based. The interface does not cover connection
75  * establishment — use methods on the underlying type to establish a connection
76  * before sending and receiving data through the #GDatagramBased API. For
77  * connectionless socket types the target/source address is specified or
78  * received in each I/O operation.
79  * 
80  * Like most other APIs in GLib, #GDatagramBased is not inherently thread safe.
81  * To use a #GDatagramBased concurrently from multiple threads, you must
82  * implement your own locking.
83  *
84  * Since: 2.48
85  */
86 public template DatagramBasedT(TStruct)
87 {
88 	/** Get the main Gtk struct */
89 	public GDatagramBased* getDatagramBasedStruct()
90 	{
91 		return cast(GDatagramBased*)getStruct();
92 	}
93 
94 
95 	/**
96 	 * Checks on the readiness of @datagram_based to perform operations. The
97 	 * operations specified in @condition are checked for and masked against the
98 	 * currently-satisfied conditions on @datagram_based. The result is returned.
99 	 *
100 	 * %G_IO_IN will be set in the return value if data is available to read with
101 	 * g_datagram_based_receive_messages(), or if the connection is closed remotely
102 	 * (EOS); and if the datagram_based has not been closed locally using some
103 	 * implementation-specific method (such as g_socket_close() or
104 	 * g_socket_shutdown() with @shutdown_read set, if it’s a #GSocket).
105 	 *
106 	 * If the connection is shut down or closed (by calling g_socket_close() or
107 	 * g_socket_shutdown() with @shutdown_read set, if it’s a #GSocket, for
108 	 * example), all calls to this function will return %G_IO_ERROR_CLOSED.
109 	 *
110 	 * %G_IO_OUT will be set if it is expected that at least one byte can be sent
111 	 * using g_datagram_based_send_messages() without blocking. It will not be set
112 	 * if the datagram_based has been closed locally.
113 	 *
114 	 * %G_IO_HUP will be set if the connection has been closed locally.
115 	 *
116 	 * %G_IO_ERR will be set if there was an asynchronous error in transmitting data
117 	 * previously enqueued using g_datagram_based_send_messages().
118 	 *
119 	 * Note that on Windows, it is possible for an operation to return
120 	 * %G_IO_ERROR_WOULD_BLOCK even immediately after
121 	 * g_datagram_based_condition_check() has claimed that the #GDatagramBased is
122 	 * ready for writing. Rather than calling g_datagram_based_condition_check() and
123 	 * then writing to the #GDatagramBased if it succeeds, it is generally better to
124 	 * simply try writing right away, and try again later if the initial attempt
125 	 * returns %G_IO_ERROR_WOULD_BLOCK.
126 	 *
127 	 * It is meaningless to specify %G_IO_ERR or %G_IO_HUP in @condition; these
128 	 * conditions will always be set in the output if they are true. Apart from
129 	 * these flags, the output is guaranteed to be masked by @condition.
130 	 *
131 	 * This call never blocks.
132 	 *
133 	 * Params:
134 	 *     condition = a #GIOCondition mask to check
135 	 *
136 	 * Return: the #GIOCondition mask of the current state
137 	 *
138 	 * Since: 2.48
139 	 */
140 	public GIOCondition conditionCheck(GIOCondition condition)
141 	{
142 		return g_datagram_based_condition_check(getDatagramBasedStruct(), condition);
143 	}
144 
145 	/**
146 	 * Waits for up to @timeout microseconds for condition to become true on
147 	 * @datagram_based. If the condition is met, %TRUE is returned.
148 	 *
149 	 * If @cancellable is cancelled before the condition is met, or if @timeout is
150 	 * reached before the condition is met, then %FALSE is returned and @error is
151 	 * set appropriately (%G_IO_ERROR_CANCELLED or %G_IO_ERROR_TIMED_OUT).
152 	 *
153 	 * Params:
154 	 *     condition = a #GIOCondition mask to wait for
155 	 *     timeout = the maximum time (in microseconds) to wait, 0 to not block, or -1
156 	 *         to block indefinitely
157 	 *     cancellable = a #GCancellable
158 	 *
159 	 * Return: %TRUE if the condition was met, %FALSE otherwise
160 	 *
161 	 * Since: 2.48
162 	 *
163 	 * Throws: GException on failure.
164 	 */
165 	public bool conditionWait(GIOCondition condition, long timeout, Cancellable cancellable)
166 	{
167 		GError* err = null;
168 		
169 		auto p = g_datagram_based_condition_wait(getDatagramBasedStruct(), condition, timeout, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
170 		
171 		if (err !is null)
172 		{
173 			throw new GException( new ErrorG(err) );
174 		}
175 		
176 		return p;
177 	}
178 
179 	/**
180 	 * Creates a #GSource that can be attached to a #GMainContext to monitor for
181 	 * the availability of the specified @condition on the #GDatagramBased. The
182 	 * #GSource keeps a reference to the @datagram_based.
183 	 *
184 	 * The callback on the source is of the #GDatagramBasedSourceFunc type.
185 	 *
186 	 * It is meaningless to specify %G_IO_ERR or %G_IO_HUP in @condition; these
187 	 * conditions will always be reported in the callback if they are true.
188 	 *
189 	 * If non-%NULL, @cancellable can be used to cancel the source, which will
190 	 * cause the source to trigger, reporting the current condition (which is
191 	 * likely 0 unless cancellation happened at the same time as a condition
192 	 * change). You can check for this in the callback using
193 	 * g_cancellable_is_cancelled().
194 	 *
195 	 * Params:
196 	 *     condition = a #GIOCondition mask to monitor
197 	 *     cancellable = a #GCancellable
198 	 *
199 	 * Return: a newly allocated #GSource
200 	 *
201 	 * Since: 2.48
202 	 */
203 	public Source createSource(GIOCondition condition, Cancellable cancellable)
204 	{
205 		auto p = g_datagram_based_create_source(getDatagramBasedStruct(), condition, (cancellable is null) ? null : cancellable.getCancellableStruct());
206 		
207 		if(p is null)
208 		{
209 			return null;
210 		}
211 		
212 		return new Source(cast(GSource*) p, true);
213 	}
214 
215 	/**
216 	 * Receive one or more data messages from @datagram_based in one go.
217 	 *
218 	 * @messages must point to an array of #GInputMessage structs and
219 	 * @num_messages must be the length of this array. Each #GInputMessage
220 	 * contains a pointer to an array of #GInputVector structs describing the
221 	 * buffers that the data received in each message will be written to.
222 	 *
223 	 * @flags modify how all messages are received. The commonly available
224 	 * arguments for this are available in the #GSocketMsgFlags enum, but the
225 	 * values there are the same as the system values, and the flags
226 	 * are passed in as-is, so you can pass in system-specific flags too. These
227 	 * flags affect the overall receive operation. Flags affecting individual
228 	 * messages are returned in #GInputMessage.flags.
229 	 *
230 	 * The other members of #GInputMessage are treated as described in its
231 	 * documentation.
232 	 *
233 	 * If @timeout is negative the call will block until @num_messages have been
234 	 * received, the connection is closed remotely (EOS), @cancellable is cancelled,
235 	 * or an error occurs.
236 	 *
237 	 * If @timeout is 0 the call will return up to @num_messages without blocking,
238 	 * or %G_IO_ERROR_WOULD_BLOCK if no messages are queued in the operating system
239 	 * to be received.
240 	 *
241 	 * If @timeout is positive the call will block on the same conditions as if
242 	 * @timeout were negative. If the timeout is reached
243 	 * before any messages are received, %G_IO_ERROR_TIMED_OUT is returned,
244 	 * otherwise it will return the number of messages received before timing out.
245 	 * (Note: This is effectively the behaviour of `MSG_WAITFORONE` with
246 	 * recvmmsg().)
247 	 *
248 	 * To be notified when messages are available, wait for the %G_IO_IN condition.
249 	 * Note though that you may still receive %G_IO_ERROR_WOULD_BLOCK from
250 	 * g_datagram_based_receive_messages() even if you were previously notified of a
251 	 * %G_IO_IN condition.
252 	 *
253 	 * If the remote peer closes the connection, any messages queued in the
254 	 * underlying receive buffer will be returned, and subsequent calls to
255 	 * g_datagram_based_receive_messages() will return 0 (with no error set).
256 	 *
257 	 * If the connection is shut down or closed (by calling g_socket_close() or
258 	 * g_socket_shutdown() with @shutdown_read set, if it’s a #GSocket, for
259 	 * example), all calls to this function will return %G_IO_ERROR_CLOSED.
260 	 *
261 	 * On error -1 is returned and @error is set accordingly. An error will only
262 	 * be returned if zero messages could be received; otherwise the number of
263 	 * messages successfully received before the error will be returned. If
264 	 * @cancellable is cancelled, %G_IO_ERROR_CANCELLED is returned as with any
265 	 * other error.
266 	 *
267 	 * Params:
268 	 *     messages = an array of #GInputMessage structs
269 	 *     numMessages = the number of elements in @messages
270 	 *     flags = an int containing #GSocketMsgFlags flags for the overall operation
271 	 *     timeout = the maximum time (in microseconds) to wait, 0 to not block, or -1
272 	 *         to block indefinitely
273 	 *     cancellable = a %GCancellable
274 	 *
275 	 * Return: number of messages received, or -1 on error. Note that the number
276 	 *     of messages received may be smaller than @num_messages if @timeout is
277 	 *     zero or positive, if the peer closed the connection, or if @num_messages
278 	 *     was larger than `UIO_MAXIOV` (1024), in which case the caller may re-try
279 	 *     to receive the remaining messages.
280 	 *
281 	 * Since: 2.48
282 	 *
283 	 * Throws: GException on failure.
284 	 */
285 	public int receiveMessages(GInputMessage[] messages, int flags, long timeout, Cancellable cancellable)
286 	{
287 		GError* err = null;
288 		
289 		auto p = g_datagram_based_receive_messages(getDatagramBasedStruct(), messages.ptr, cast(uint)messages.length, flags, timeout, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
290 		
291 		if (err !is null)
292 		{
293 			throw new GException( new ErrorG(err) );
294 		}
295 		
296 		return p;
297 	}
298 
299 	/**
300 	 * Send one or more data messages from @datagram_based in one go.
301 	 *
302 	 * @messages must point to an array of #GOutputMessage structs and
303 	 * @num_messages must be the length of this array. Each #GOutputMessage
304 	 * contains an address to send the data to, and a pointer to an array of
305 	 * #GOutputVector structs to describe the buffers that the data to be sent
306 	 * for each message will be gathered from.
307 	 *
308 	 * @flags modify how the message is sent. The commonly available arguments
309 	 * for this are available in the #GSocketMsgFlags enum, but the
310 	 * values there are the same as the system values, and the flags
311 	 * are passed in as-is, so you can pass in system-specific flags too.
312 	 *
313 	 * The other members of #GOutputMessage are treated as described in its
314 	 * documentation.
315 	 *
316 	 * If @timeout is negative the call will block until @num_messages have been
317 	 * sent, @cancellable is cancelled, or an error occurs.
318 	 *
319 	 * If @timeout is 0 the call will send up to @num_messages without blocking,
320 	 * or will return %G_IO_ERROR_WOULD_BLOCK if there is no space to send messages.
321 	 *
322 	 * If @timeout is positive the call will block on the same conditions as if
323 	 * @timeout were negative. If the timeout is reached before any messages are
324 	 * sent, %G_IO_ERROR_TIMED_OUT is returned, otherwise it will return the number
325 	 * of messages sent before timing out.
326 	 *
327 	 * To be notified when messages can be sent, wait for the %G_IO_OUT condition.
328 	 * Note though that you may still receive %G_IO_ERROR_WOULD_BLOCK from
329 	 * g_datagram_based_send_messages() even if you were previously notified of a
330 	 * %G_IO_OUT condition. (On Windows in particular, this is very common due to
331 	 * the way the underlying APIs work.)
332 	 *
333 	 * If the connection is shut down or closed (by calling g_socket_close() or
334 	 * g_socket_shutdown() with @shutdown_write set, if it’s a #GSocket, for
335 	 * example), all calls to this function will return %G_IO_ERROR_CLOSED.
336 	 *
337 	 * On error -1 is returned and @error is set accordingly. An error will only
338 	 * be returned if zero messages could be sent; otherwise the number of messages
339 	 * successfully sent before the error will be returned. If @cancellable is
340 	 * cancelled, %G_IO_ERROR_CANCELLED is returned as with any other error.
341 	 *
342 	 * Params:
343 	 *     messages = an array of #GOutputMessage structs
344 	 *     numMessages = the number of elements in @messages
345 	 *     flags = an int containing #GSocketMsgFlags flags
346 	 *     timeout = the maximum time (in microseconds) to wait, 0 to not block, or -1
347 	 *         to block indefinitely
348 	 *     cancellable = a %GCancellable
349 	 *
350 	 * Return: number of messages sent, or -1 on error. Note that the number of
351 	 *     messages sent may be smaller than @num_messages if @timeout is zero
352 	 *     or positive, or if @num_messages was larger than `UIO_MAXIOV` (1024), in
353 	 *     which case the caller may re-try to send the remaining messages.
354 	 *
355 	 * Since: 2.48
356 	 *
357 	 * Throws: GException on failure.
358 	 */
359 	public int sendMessages(GOutputMessage[] messages, int flags, long timeout, Cancellable cancellable)
360 	{
361 		GError* err = null;
362 		
363 		auto p = g_datagram_based_send_messages(getDatagramBasedStruct(), messages.ptr, cast(uint)messages.length, flags, timeout, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
364 		
365 		if (err !is null)
366 		{
367 			throw new GException( new ErrorG(err) );
368 		}
369 		
370 		return p;
371 	}
372 }