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