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