xref: /qemu/include/io/channel.h (revision 666a3af9c858c22d254e545de3ffc3fb197a3187)
1*666a3af9SDaniel P. Berrange /*
2*666a3af9SDaniel P. Berrange  * QEMU I/O channels
3*666a3af9SDaniel P. Berrange  *
4*666a3af9SDaniel P. Berrange  * Copyright (c) 2015 Red Hat, Inc.
5*666a3af9SDaniel P. Berrange  *
6*666a3af9SDaniel P. Berrange  * This library is free software; you can redistribute it and/or
7*666a3af9SDaniel P. Berrange  * modify it under the terms of the GNU Lesser General Public
8*666a3af9SDaniel P. Berrange  * License as published by the Free Software Foundation; either
9*666a3af9SDaniel P. Berrange  * version 2 of the License, or (at your option) any later version.
10*666a3af9SDaniel P. Berrange  *
11*666a3af9SDaniel P. Berrange  * This library is distributed in the hope that it will be useful,
12*666a3af9SDaniel P. Berrange  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*666a3af9SDaniel P. Berrange  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*666a3af9SDaniel P. Berrange  * Lesser General Public License for more details.
15*666a3af9SDaniel P. Berrange  *
16*666a3af9SDaniel P. Berrange  * You should have received a copy of the GNU Lesser General Public
17*666a3af9SDaniel P. Berrange  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18*666a3af9SDaniel P. Berrange  *
19*666a3af9SDaniel P. Berrange  */
20*666a3af9SDaniel P. Berrange 
21*666a3af9SDaniel P. Berrange #ifndef QIO_CHANNEL_H__
22*666a3af9SDaniel P. Berrange #define QIO_CHANNEL_H__
23*666a3af9SDaniel P. Berrange 
24*666a3af9SDaniel P. Berrange #include "qemu-common.h"
25*666a3af9SDaniel P. Berrange #include "qapi/error.h"
26*666a3af9SDaniel P. Berrange #include "qom/object.h"
27*666a3af9SDaniel P. Berrange 
28*666a3af9SDaniel P. Berrange #define TYPE_QIO_CHANNEL "qio-channel"
29*666a3af9SDaniel P. Berrange #define QIO_CHANNEL(obj)                                    \
30*666a3af9SDaniel P. Berrange     OBJECT_CHECK(QIOChannel, (obj), TYPE_QIO_CHANNEL)
31*666a3af9SDaniel P. Berrange #define QIO_CHANNEL_CLASS(klass)                                    \
32*666a3af9SDaniel P. Berrange     OBJECT_CLASS_CHECK(QIOChannelClass, klass, TYPE_QIO_CHANNEL)
33*666a3af9SDaniel P. Berrange #define QIO_CHANNEL_GET_CLASS(obj)                                  \
34*666a3af9SDaniel P. Berrange     OBJECT_GET_CLASS(QIOChannelClass, obj, TYPE_QIO_CHANNEL)
35*666a3af9SDaniel P. Berrange 
36*666a3af9SDaniel P. Berrange typedef struct QIOChannel QIOChannel;
37*666a3af9SDaniel P. Berrange typedef struct QIOChannelClass QIOChannelClass;
38*666a3af9SDaniel P. Berrange 
39*666a3af9SDaniel P. Berrange #define QIO_CHANNEL_ERR_BLOCK -2
40*666a3af9SDaniel P. Berrange 
41*666a3af9SDaniel P. Berrange typedef enum QIOChannelFeature QIOChannelFeature;
42*666a3af9SDaniel P. Berrange 
43*666a3af9SDaniel P. Berrange enum QIOChannelFeature {
44*666a3af9SDaniel P. Berrange     QIO_CHANNEL_FEATURE_FD_PASS  = (1 << 0),
45*666a3af9SDaniel P. Berrange     QIO_CHANNEL_FEATURE_SHUTDOWN = (1 << 1),
46*666a3af9SDaniel P. Berrange };
47*666a3af9SDaniel P. Berrange 
48*666a3af9SDaniel P. Berrange 
49*666a3af9SDaniel P. Berrange typedef enum QIOChannelShutdown QIOChannelShutdown;
50*666a3af9SDaniel P. Berrange 
51*666a3af9SDaniel P. Berrange enum QIOChannelShutdown {
52*666a3af9SDaniel P. Berrange     QIO_CHANNEL_SHUTDOWN_BOTH,
53*666a3af9SDaniel P. Berrange     QIO_CHANNEL_SHUTDOWN_READ,
54*666a3af9SDaniel P. Berrange     QIO_CHANNEL_SHUTDOWN_WRITE,
55*666a3af9SDaniel P. Berrange };
56*666a3af9SDaniel P. Berrange 
57*666a3af9SDaniel P. Berrange typedef gboolean (*QIOChannelFunc)(QIOChannel *ioc,
58*666a3af9SDaniel P. Berrange                                    GIOCondition condition,
59*666a3af9SDaniel P. Berrange                                    gpointer data);
60*666a3af9SDaniel P. Berrange 
61*666a3af9SDaniel P. Berrange /**
62*666a3af9SDaniel P. Berrange  * QIOChannel:
63*666a3af9SDaniel P. Berrange  *
64*666a3af9SDaniel P. Berrange  * The QIOChannel defines the core API for a generic I/O channel
65*666a3af9SDaniel P. Berrange  * class hierarchy. It is inspired by GIOChannel, but has the
66*666a3af9SDaniel P. Berrange  * following differences
67*666a3af9SDaniel P. Berrange  *
68*666a3af9SDaniel P. Berrange  *  - Use QOM to properly support arbitrary subclassing
69*666a3af9SDaniel P. Berrange  *  - Support use of iovecs for efficient I/O with multiple blocks
70*666a3af9SDaniel P. Berrange  *  - None of the character set translation, binary data exclusively
71*666a3af9SDaniel P. Berrange  *  - Direct support for QEMU Error object reporting
72*666a3af9SDaniel P. Berrange  *  - File descriptor passing
73*666a3af9SDaniel P. Berrange  *
74*666a3af9SDaniel P. Berrange  * This base class is abstract so cannot be instantiated. There
75*666a3af9SDaniel P. Berrange  * will be subclasses for dealing with sockets, files, and higher
76*666a3af9SDaniel P. Berrange  * level protocols such as TLS, WebSocket, etc.
77*666a3af9SDaniel P. Berrange  */
78*666a3af9SDaniel P. Berrange 
79*666a3af9SDaniel P. Berrange struct QIOChannel {
80*666a3af9SDaniel P. Berrange     Object parent;
81*666a3af9SDaniel P. Berrange     unsigned int features; /* bitmask of QIOChannelFeatures */
82*666a3af9SDaniel P. Berrange };
83*666a3af9SDaniel P. Berrange 
84*666a3af9SDaniel P. Berrange /**
85*666a3af9SDaniel P. Berrange  * QIOChannelClass:
86*666a3af9SDaniel P. Berrange  *
87*666a3af9SDaniel P. Berrange  * This class defines the contract that all subclasses
88*666a3af9SDaniel P. Berrange  * must follow to provide specific channel implementations.
89*666a3af9SDaniel P. Berrange  * The first five callbacks are mandatory to support, others
90*666a3af9SDaniel P. Berrange  * provide additional optional features.
91*666a3af9SDaniel P. Berrange  *
92*666a3af9SDaniel P. Berrange  * Consult the corresponding public API docs for a description
93*666a3af9SDaniel P. Berrange  * of the semantics of each callback
94*666a3af9SDaniel P. Berrange  */
95*666a3af9SDaniel P. Berrange struct QIOChannelClass {
96*666a3af9SDaniel P. Berrange     ObjectClass parent;
97*666a3af9SDaniel P. Berrange 
98*666a3af9SDaniel P. Berrange     /* Mandatory callbacks */
99*666a3af9SDaniel P. Berrange     ssize_t (*io_writev)(QIOChannel *ioc,
100*666a3af9SDaniel P. Berrange                          const struct iovec *iov,
101*666a3af9SDaniel P. Berrange                          size_t niov,
102*666a3af9SDaniel P. Berrange                          int *fds,
103*666a3af9SDaniel P. Berrange                          size_t nfds,
104*666a3af9SDaniel P. Berrange                          Error **errp);
105*666a3af9SDaniel P. Berrange     ssize_t (*io_readv)(QIOChannel *ioc,
106*666a3af9SDaniel P. Berrange                         const struct iovec *iov,
107*666a3af9SDaniel P. Berrange                         size_t niov,
108*666a3af9SDaniel P. Berrange                         int **fds,
109*666a3af9SDaniel P. Berrange                         size_t *nfds,
110*666a3af9SDaniel P. Berrange                         Error **errp);
111*666a3af9SDaniel P. Berrange     int (*io_close)(QIOChannel *ioc,
112*666a3af9SDaniel P. Berrange                     Error **errp);
113*666a3af9SDaniel P. Berrange     GSource * (*io_create_watch)(QIOChannel *ioc,
114*666a3af9SDaniel P. Berrange                                  GIOCondition condition);
115*666a3af9SDaniel P. Berrange     int (*io_set_blocking)(QIOChannel *ioc,
116*666a3af9SDaniel P. Berrange                            bool enabled,
117*666a3af9SDaniel P. Berrange                            Error **errp);
118*666a3af9SDaniel P. Berrange 
119*666a3af9SDaniel P. Berrange     /* Optional callbacks */
120*666a3af9SDaniel P. Berrange     int (*io_shutdown)(QIOChannel *ioc,
121*666a3af9SDaniel P. Berrange                        QIOChannelShutdown how,
122*666a3af9SDaniel P. Berrange                        Error **errp);
123*666a3af9SDaniel P. Berrange     void (*io_set_cork)(QIOChannel *ioc,
124*666a3af9SDaniel P. Berrange                         bool enabled);
125*666a3af9SDaniel P. Berrange     void (*io_set_delay)(QIOChannel *ioc,
126*666a3af9SDaniel P. Berrange                          bool enabled);
127*666a3af9SDaniel P. Berrange     off_t (*io_seek)(QIOChannel *ioc,
128*666a3af9SDaniel P. Berrange                      off_t offset,
129*666a3af9SDaniel P. Berrange                      int whence,
130*666a3af9SDaniel P. Berrange                      Error **errp);
131*666a3af9SDaniel P. Berrange };
132*666a3af9SDaniel P. Berrange 
133*666a3af9SDaniel P. Berrange /* General I/O handling functions */
134*666a3af9SDaniel P. Berrange 
135*666a3af9SDaniel P. Berrange /**
136*666a3af9SDaniel P. Berrange  * qio_channel_has_feature:
137*666a3af9SDaniel P. Berrange  * @ioc: the channel object
138*666a3af9SDaniel P. Berrange  * @feature: the feature to check support of
139*666a3af9SDaniel P. Berrange  *
140*666a3af9SDaniel P. Berrange  * Determine whether the channel implementation supports
141*666a3af9SDaniel P. Berrange  * the optional feature named in @feature.
142*666a3af9SDaniel P. Berrange  *
143*666a3af9SDaniel P. Berrange  * Returns: true if supported, false otherwise.
144*666a3af9SDaniel P. Berrange  */
145*666a3af9SDaniel P. Berrange bool qio_channel_has_feature(QIOChannel *ioc,
146*666a3af9SDaniel P. Berrange                              QIOChannelFeature feature);
147*666a3af9SDaniel P. Berrange 
148*666a3af9SDaniel P. Berrange /**
149*666a3af9SDaniel P. Berrange  * qio_channel_readv_full:
150*666a3af9SDaniel P. Berrange  * @ioc: the channel object
151*666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
152*666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
153*666a3af9SDaniel P. Berrange  * @fds: pointer to an array that will received file handles
154*666a3af9SDaniel P. Berrange  * @nfds: pointer filled with number of elements in @fds on return
155*666a3af9SDaniel P. Berrange  * @errp: pointer to an uninitialized error object
156*666a3af9SDaniel P. Berrange  *
157*666a3af9SDaniel P. Berrange  * Read data from the IO channel, storing it in the
158*666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
159*666a3af9SDaniel P. Berrange  * in the @iov will be fully populated with data
160*666a3af9SDaniel P. Berrange  * before the next one is used. The @niov parameter
161*666a3af9SDaniel P. Berrange  * specifies the total number of elements in @iov.
162*666a3af9SDaniel P. Berrange  *
163*666a3af9SDaniel P. Berrange  * It is not required for all @iov to be filled with
164*666a3af9SDaniel P. Berrange  * data. If the channel is in blocking mode, at least
165*666a3af9SDaniel P. Berrange  * one byte of data will be read, but no more is
166*666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
167*666a3af9SDaniel P. Berrange  * data is available, it will return QIO_CHANNEL_ERR_BLOCK
168*666a3af9SDaniel P. Berrange  *
169*666a3af9SDaniel P. Berrange  * If the channel has passed any file descriptors,
170*666a3af9SDaniel P. Berrange  * the @fds array pointer will be allocated and
171*666a3af9SDaniel P. Berrange  * the elements filled with the received file
172*666a3af9SDaniel P. Berrange  * descriptors. The @nfds pointer will be updated
173*666a3af9SDaniel P. Berrange  * to indicate the size of the @fds array that
174*666a3af9SDaniel P. Berrange  * was allocated. It is the callers responsibility
175*666a3af9SDaniel P. Berrange  * to call close() on each file descriptor and to
176*666a3af9SDaniel P. Berrange  * call g_free() on the array pointer in @fds.
177*666a3af9SDaniel P. Berrange  *
178*666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
179*666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
180*666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
181*666a3af9SDaniel P. Berrange  *
182*666a3af9SDaniel P. Berrange  * Returns: the number of bytes read, or -1 on error,
183*666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is available
184*666a3af9SDaniel P. Berrange  * and the channel is non-blocking
185*666a3af9SDaniel P. Berrange  */
186*666a3af9SDaniel P. Berrange ssize_t qio_channel_readv_full(QIOChannel *ioc,
187*666a3af9SDaniel P. Berrange                                const struct iovec *iov,
188*666a3af9SDaniel P. Berrange                                size_t niov,
189*666a3af9SDaniel P. Berrange                                int **fds,
190*666a3af9SDaniel P. Berrange                                size_t *nfds,
191*666a3af9SDaniel P. Berrange                                Error **errp);
192*666a3af9SDaniel P. Berrange 
193*666a3af9SDaniel P. Berrange 
194*666a3af9SDaniel P. Berrange /**
195*666a3af9SDaniel P. Berrange  * qio_channel_writev_full:
196*666a3af9SDaniel P. Berrange  * @ioc: the channel object
197*666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
198*666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
199*666a3af9SDaniel P. Berrange  * @fds: an array of file handles to send
200*666a3af9SDaniel P. Berrange  * @nfds: number of file handles in @fds
201*666a3af9SDaniel P. Berrange  * @errp: pointer to an uninitialized error object
202*666a3af9SDaniel P. Berrange  *
203*666a3af9SDaniel P. Berrange  * Write data to the IO channel, reading it from the
204*666a3af9SDaniel P. Berrange  * memory regions referenced by @iov. Each element
205*666a3af9SDaniel P. Berrange  * in the @iov will be fully sent, before the next
206*666a3af9SDaniel P. Berrange  * one is used. The @niov parameter specifies the
207*666a3af9SDaniel P. Berrange  * total number of elements in @iov.
208*666a3af9SDaniel P. Berrange  *
209*666a3af9SDaniel P. Berrange  * It is not required for all @iov data to be fully
210*666a3af9SDaniel P. Berrange  * sent. If the channel is in blocking mode, at least
211*666a3af9SDaniel P. Berrange  * one byte of data will be sent, but no more is
212*666a3af9SDaniel P. Berrange  * guaranteed. If the channel is non-blocking and no
213*666a3af9SDaniel P. Berrange  * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
214*666a3af9SDaniel P. Berrange  *
215*666a3af9SDaniel P. Berrange  * If there are file descriptors to send, the @fds
216*666a3af9SDaniel P. Berrange  * array should be non-NULL and provide the handles.
217*666a3af9SDaniel P. Berrange  * All file descriptors will be sent if at least one
218*666a3af9SDaniel P. Berrange  * byte of data was sent.
219*666a3af9SDaniel P. Berrange  *
220*666a3af9SDaniel P. Berrange  * It is an error to pass a non-NULL @fds parameter
221*666a3af9SDaniel P. Berrange  * unless qio_channel_has_feature() returns a true
222*666a3af9SDaniel P. Berrange  * value for the QIO_CHANNEL_FEATURE_FD_PASS constant.
223*666a3af9SDaniel P. Berrange  *
224*666a3af9SDaniel P. Berrange  * Returns: the number of bytes sent, or -1 on error,
225*666a3af9SDaniel P. Berrange  * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
226*666a3af9SDaniel P. Berrange  * and the channel is non-blocking
227*666a3af9SDaniel P. Berrange  */
228*666a3af9SDaniel P. Berrange ssize_t qio_channel_writev_full(QIOChannel *ioc,
229*666a3af9SDaniel P. Berrange                                 const struct iovec *iov,
230*666a3af9SDaniel P. Berrange                                 size_t niov,
231*666a3af9SDaniel P. Berrange                                 int *fds,
232*666a3af9SDaniel P. Berrange                                 size_t nfds,
233*666a3af9SDaniel P. Berrange                                 Error **errp);
234*666a3af9SDaniel P. Berrange 
235*666a3af9SDaniel P. Berrange /**
236*666a3af9SDaniel P. Berrange  * qio_channel_readv:
237*666a3af9SDaniel P. Berrange  * @ioc: the channel object
238*666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to read data into
239*666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
240*666a3af9SDaniel P. Berrange  * @errp: pointer to an uninitialized error object
241*666a3af9SDaniel P. Berrange  *
242*666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
243*666a3af9SDaniel P. Berrange  * receiving of file handles.
244*666a3af9SDaniel P. Berrange  */
245*666a3af9SDaniel P. Berrange ssize_t qio_channel_readv(QIOChannel *ioc,
246*666a3af9SDaniel P. Berrange                           const struct iovec *iov,
247*666a3af9SDaniel P. Berrange                           size_t niov,
248*666a3af9SDaniel P. Berrange                           Error **errp);
249*666a3af9SDaniel P. Berrange 
250*666a3af9SDaniel P. Berrange /**
251*666a3af9SDaniel P. Berrange  * qio_channel_writev:
252*666a3af9SDaniel P. Berrange  * @ioc: the channel object
253*666a3af9SDaniel P. Berrange  * @iov: the array of memory regions to write data from
254*666a3af9SDaniel P. Berrange  * @niov: the length of the @iov array
255*666a3af9SDaniel P. Berrange  * @errp: pointer to an uninitialized error object
256*666a3af9SDaniel P. Berrange  *
257*666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
258*666a3af9SDaniel P. Berrange  * sending of file handles.
259*666a3af9SDaniel P. Berrange  */
260*666a3af9SDaniel P. Berrange ssize_t qio_channel_writev(QIOChannel *ioc,
261*666a3af9SDaniel P. Berrange                            const struct iovec *iov,
262*666a3af9SDaniel P. Berrange                            size_t niov,
263*666a3af9SDaniel P. Berrange                            Error **errp);
264*666a3af9SDaniel P. Berrange 
265*666a3af9SDaniel P. Berrange /**
266*666a3af9SDaniel P. Berrange  * qio_channel_readv:
267*666a3af9SDaniel P. Berrange  * @ioc: the channel object
268*666a3af9SDaniel P. Berrange  * @buf: the memory region to read data into
269*666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
270*666a3af9SDaniel P. Berrange  * @errp: pointer to an uninitialized error object
271*666a3af9SDaniel P. Berrange  *
272*666a3af9SDaniel P. Berrange  * Behaves as qio_channel_readv_full() but does not support
273*666a3af9SDaniel P. Berrange  * receiving of file handles, and only supports reading into
274*666a3af9SDaniel P. Berrange  * a single memory region.
275*666a3af9SDaniel P. Berrange  */
276*666a3af9SDaniel P. Berrange ssize_t qio_channel_read(QIOChannel *ioc,
277*666a3af9SDaniel P. Berrange                          char *buf,
278*666a3af9SDaniel P. Berrange                          size_t buflen,
279*666a3af9SDaniel P. Berrange                          Error **errp);
280*666a3af9SDaniel P. Berrange 
281*666a3af9SDaniel P. Berrange /**
282*666a3af9SDaniel P. Berrange  * qio_channel_writev:
283*666a3af9SDaniel P. Berrange  * @ioc: the channel object
284*666a3af9SDaniel P. Berrange  * @buf: the memory regions to send data from
285*666a3af9SDaniel P. Berrange  * @buflen: the length of @buf
286*666a3af9SDaniel P. Berrange  * @errp: pointer to an uninitialized error object
287*666a3af9SDaniel P. Berrange  *
288*666a3af9SDaniel P. Berrange  * Behaves as qio_channel_writev_full() but does not support
289*666a3af9SDaniel P. Berrange  * sending of file handles, and only supports writing from a
290*666a3af9SDaniel P. Berrange  * single memory region.
291*666a3af9SDaniel P. Berrange  */
292*666a3af9SDaniel P. Berrange ssize_t qio_channel_write(QIOChannel *ioc,
293*666a3af9SDaniel P. Berrange                           const char *buf,
294*666a3af9SDaniel P. Berrange                           size_t buflen,
295*666a3af9SDaniel P. Berrange                           Error **errp);
296*666a3af9SDaniel P. Berrange 
297*666a3af9SDaniel P. Berrange /**
298*666a3af9SDaniel P. Berrange  * qio_channel_set_blocking:
299*666a3af9SDaniel P. Berrange  * @ioc: the channel object
300*666a3af9SDaniel P. Berrange  * @enabled: the blocking flag state
301*666a3af9SDaniel P. Berrange  * @errp: pointer to an uninitialized error object
302*666a3af9SDaniel P. Berrange  *
303*666a3af9SDaniel P. Berrange  * If @enabled is true, then the channel is put into
304*666a3af9SDaniel P. Berrange  * blocking mode, otherwise it will be non-blocking.
305*666a3af9SDaniel P. Berrange  *
306*666a3af9SDaniel P. Berrange  * In non-blocking mode, read/write operations may
307*666a3af9SDaniel P. Berrange  * return QIO_CHANNEL_ERR_BLOCK if they would otherwise
308*666a3af9SDaniel P. Berrange  * block on I/O
309*666a3af9SDaniel P. Berrange  */
310*666a3af9SDaniel P. Berrange int qio_channel_set_blocking(QIOChannel *ioc,
311*666a3af9SDaniel P. Berrange                              bool enabled,
312*666a3af9SDaniel P. Berrange                              Error **errp);
313*666a3af9SDaniel P. Berrange 
314*666a3af9SDaniel P. Berrange /**
315*666a3af9SDaniel P. Berrange  * qio_channel_close:
316*666a3af9SDaniel P. Berrange  * @ioc: the channel object
317*666a3af9SDaniel P. Berrange  * @errp: pointer to an uninitialized error object
318*666a3af9SDaniel P. Berrange  *
319*666a3af9SDaniel P. Berrange  * Close the channel, flushing any pending I/O
320*666a3af9SDaniel P. Berrange  *
321*666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
322*666a3af9SDaniel P. Berrange  */
323*666a3af9SDaniel P. Berrange int qio_channel_close(QIOChannel *ioc,
324*666a3af9SDaniel P. Berrange                       Error **errp);
325*666a3af9SDaniel P. Berrange 
326*666a3af9SDaniel P. Berrange /**
327*666a3af9SDaniel P. Berrange  * qio_channel_shutdown:
328*666a3af9SDaniel P. Berrange  * @ioc: the channel object
329*666a3af9SDaniel P. Berrange  * @how: the direction to shutdown
330*666a3af9SDaniel P. Berrange  * @errp: pointer to an uninitialized error object
331*666a3af9SDaniel P. Berrange  *
332*666a3af9SDaniel P. Berrange  * Shutdowns transmission and/or receiving of data
333*666a3af9SDaniel P. Berrange  * without closing the underlying transport.
334*666a3af9SDaniel P. Berrange  *
335*666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
336*666a3af9SDaniel P. Berrange  * so may report an error. To avoid errors, the
337*666a3af9SDaniel P. Berrange  * caller may check for the feature flag
338*666a3af9SDaniel P. Berrange  * QIO_CHANNEL_FEATURE_SHUTDOWN prior to calling
339*666a3af9SDaniel P. Berrange  * this method.
340*666a3af9SDaniel P. Berrange  *
341*666a3af9SDaniel P. Berrange  * Returns: 0 on success, -1 on error
342*666a3af9SDaniel P. Berrange  */
343*666a3af9SDaniel P. Berrange int qio_channel_shutdown(QIOChannel *ioc,
344*666a3af9SDaniel P. Berrange                          QIOChannelShutdown how,
345*666a3af9SDaniel P. Berrange                          Error **errp);
346*666a3af9SDaniel P. Berrange 
347*666a3af9SDaniel P. Berrange /**
348*666a3af9SDaniel P. Berrange  * qio_channel_set_delay:
349*666a3af9SDaniel P. Berrange  * @ioc: the channel object
350*666a3af9SDaniel P. Berrange  * @enabled: the new flag state
351*666a3af9SDaniel P. Berrange  *
352*666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
353*666a3af9SDaniel P. Berrange  * permitted to delay writes in order to merge
354*666a3af9SDaniel P. Berrange  * small packets. If @enabled is true, then the
355*666a3af9SDaniel P. Berrange  * writes may be delayed in order to opportunistically
356*666a3af9SDaniel P. Berrange  * merge small packets into larger ones. If @enabled
357*666a3af9SDaniel P. Berrange  * is false, writes are dispatched immediately with
358*666a3af9SDaniel P. Berrange  * no delay.
359*666a3af9SDaniel P. Berrange  *
360*666a3af9SDaniel P. Berrange  * When @enabled is false, applications may wish to
361*666a3af9SDaniel P. Berrange  * use the qio_channel_set_cork() method to explicitly
362*666a3af9SDaniel P. Berrange  * control write merging.
363*666a3af9SDaniel P. Berrange  *
364*666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
365*666a3af9SDaniel P. Berrange  * API corresponds to the inverse of TCP_NODELAY flag,
366*666a3af9SDaniel P. Berrange  * controlling whether the Nagle algorithm is active.
367*666a3af9SDaniel P. Berrange  *
368*666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
369*666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
370*666a3af9SDaniel P. Berrange  * error.
371*666a3af9SDaniel P. Berrange  */
372*666a3af9SDaniel P. Berrange void qio_channel_set_delay(QIOChannel *ioc,
373*666a3af9SDaniel P. Berrange                            bool enabled);
374*666a3af9SDaniel P. Berrange 
375*666a3af9SDaniel P. Berrange /**
376*666a3af9SDaniel P. Berrange  * qio_channel_set_cork:
377*666a3af9SDaniel P. Berrange  * @ioc: the channel object
378*666a3af9SDaniel P. Berrange  * @enabled: the new flag state
379*666a3af9SDaniel P. Berrange  *
380*666a3af9SDaniel P. Berrange  * Controls whether the underlying transport is
381*666a3af9SDaniel P. Berrange  * permitted to dispatch data that is written.
382*666a3af9SDaniel P. Berrange  * If @enabled is true, then any data written will
383*666a3af9SDaniel P. Berrange  * be queued in local buffers until @enabled is
384*666a3af9SDaniel P. Berrange  * set to false once again.
385*666a3af9SDaniel P. Berrange  *
386*666a3af9SDaniel P. Berrange  * This feature is typically used when the automatic
387*666a3af9SDaniel P. Berrange  * write coalescing facility is disabled via the
388*666a3af9SDaniel P. Berrange  * qio_channel_set_delay() method.
389*666a3af9SDaniel P. Berrange  *
390*666a3af9SDaniel P. Berrange  * On channels which are backed by a socket, this
391*666a3af9SDaniel P. Berrange  * API corresponds to the TCP_CORK flag.
392*666a3af9SDaniel P. Berrange  *
393*666a3af9SDaniel P. Berrange  * This setting is merely a hint, so implementations are
394*666a3af9SDaniel P. Berrange  * free to ignore this without it being considered an
395*666a3af9SDaniel P. Berrange  * error.
396*666a3af9SDaniel P. Berrange  */
397*666a3af9SDaniel P. Berrange void qio_channel_set_cork(QIOChannel *ioc,
398*666a3af9SDaniel P. Berrange                           bool enabled);
399*666a3af9SDaniel P. Berrange 
400*666a3af9SDaniel P. Berrange 
401*666a3af9SDaniel P. Berrange /**
402*666a3af9SDaniel P. Berrange  * qio_channel_seek:
403*666a3af9SDaniel P. Berrange  * @ioc: the channel object
404*666a3af9SDaniel P. Berrange  * @offset: the position to seek to, relative to @whence
405*666a3af9SDaniel P. Berrange  * @whence: one of the (POSIX) SEEK_* constants listed below
406*666a3af9SDaniel P. Berrange  * @errp: pointer to an uninitialized error object
407*666a3af9SDaniel P. Berrange  *
408*666a3af9SDaniel P. Berrange  * Moves the current I/O position within the channel
409*666a3af9SDaniel P. Berrange  * @ioc, to be @offset. The value of @offset is
410*666a3af9SDaniel P. Berrange  * interpreted relative to @whence:
411*666a3af9SDaniel P. Berrange  *
412*666a3af9SDaniel P. Berrange  * SEEK_SET - the position is set to @offset bytes
413*666a3af9SDaniel P. Berrange  * SEEK_CUR - the position is moved by @offset bytes
414*666a3af9SDaniel P. Berrange  * SEEK_END - the position is set to end of the file plus @offset bytes
415*666a3af9SDaniel P. Berrange  *
416*666a3af9SDaniel P. Berrange  * Not all implementations will support this facility,
417*666a3af9SDaniel P. Berrange  * so may report an error.
418*666a3af9SDaniel P. Berrange  *
419*666a3af9SDaniel P. Berrange  * Returns: the new position on success, (off_t)-1 on failure
420*666a3af9SDaniel P. Berrange  */
421*666a3af9SDaniel P. Berrange off_t qio_channel_io_seek(QIOChannel *ioc,
422*666a3af9SDaniel P. Berrange                           off_t offset,
423*666a3af9SDaniel P. Berrange                           int whence,
424*666a3af9SDaniel P. Berrange                           Error **errp);
425*666a3af9SDaniel P. Berrange 
426*666a3af9SDaniel P. Berrange 
427*666a3af9SDaniel P. Berrange /**
428*666a3af9SDaniel P. Berrange  * qio_channel_create_watch:
429*666a3af9SDaniel P. Berrange  * @ioc: the channel object
430*666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
431*666a3af9SDaniel P. Berrange  *
432*666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
433*666a3af9SDaniel P. Berrange  * for the I/O condition @condition. Typically the
434*666a3af9SDaniel P. Berrange  * qio_channel_add_watch() method would be used instead
435*666a3af9SDaniel P. Berrange  * of this, since it directly attaches a callback to
436*666a3af9SDaniel P. Berrange  * the source
437*666a3af9SDaniel P. Berrange  *
438*666a3af9SDaniel P. Berrange  * Returns: the new main loop source.
439*666a3af9SDaniel P. Berrange  */
440*666a3af9SDaniel P. Berrange GSource *qio_channel_create_watch(QIOChannel *ioc,
441*666a3af9SDaniel P. Berrange                                   GIOCondition condition);
442*666a3af9SDaniel P. Berrange 
443*666a3af9SDaniel P. Berrange /**
444*666a3af9SDaniel P. Berrange  * qio_channel_add_watch:
445*666a3af9SDaniel P. Berrange  * @ioc: the channel object
446*666a3af9SDaniel P. Berrange  * @condition: the I/O condition to monitor
447*666a3af9SDaniel P. Berrange  * @func: callback to invoke when the source becomes ready
448*666a3af9SDaniel P. Berrange  * @user_data: opaque data to pass to @func
449*666a3af9SDaniel P. Berrange  * @notify: callback to free @user_data
450*666a3af9SDaniel P. Berrange  *
451*666a3af9SDaniel P. Berrange  * Create a new main loop source that is used to watch
452*666a3af9SDaniel P. Berrange  * for the I/O condition @condition. The callback @func
453*666a3af9SDaniel P. Berrange  * will be registered against the source, to be invoked
454*666a3af9SDaniel P. Berrange  * when the source becomes ready. The optional @user_data
455*666a3af9SDaniel P. Berrange  * will be passed to @func when it is invoked. The @notify
456*666a3af9SDaniel P. Berrange  * callback will be used to free @user_data when the
457*666a3af9SDaniel P. Berrange  * watch is deleted
458*666a3af9SDaniel P. Berrange  *
459*666a3af9SDaniel P. Berrange  * The returned source ID can be used with g_source_remove()
460*666a3af9SDaniel P. Berrange  * to remove and free the source when no longer required.
461*666a3af9SDaniel P. Berrange  * Alternatively the @func callback can return a FALSE
462*666a3af9SDaniel P. Berrange  * value.
463*666a3af9SDaniel P. Berrange  *
464*666a3af9SDaniel P. Berrange  * Returns: the source ID
465*666a3af9SDaniel P. Berrange  */
466*666a3af9SDaniel P. Berrange guint qio_channel_add_watch(QIOChannel *ioc,
467*666a3af9SDaniel P. Berrange                             GIOCondition condition,
468*666a3af9SDaniel P. Berrange                             QIOChannelFunc func,
469*666a3af9SDaniel P. Berrange                             gpointer user_data,
470*666a3af9SDaniel P. Berrange                             GDestroyNotify notify);
471*666a3af9SDaniel P. Berrange 
472*666a3af9SDaniel P. Berrange 
473*666a3af9SDaniel P. Berrange /**
474*666a3af9SDaniel P. Berrange  * qio_channel_yield:
475*666a3af9SDaniel P. Berrange  * @ioc: the channel object
476*666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
477*666a3af9SDaniel P. Berrange  *
478*666a3af9SDaniel P. Berrange  * Yields execution from the current coroutine until
479*666a3af9SDaniel P. Berrange  * the condition indicated by @condition becomes
480*666a3af9SDaniel P. Berrange  * available.
481*666a3af9SDaniel P. Berrange  *
482*666a3af9SDaniel P. Berrange  * This must only be called from coroutine context
483*666a3af9SDaniel P. Berrange  */
484*666a3af9SDaniel P. Berrange void qio_channel_yield(QIOChannel *ioc,
485*666a3af9SDaniel P. Berrange                        GIOCondition condition);
486*666a3af9SDaniel P. Berrange 
487*666a3af9SDaniel P. Berrange /**
488*666a3af9SDaniel P. Berrange  * qio_channel_wait:
489*666a3af9SDaniel P. Berrange  * @ioc: the channel object
490*666a3af9SDaniel P. Berrange  * @condition: the I/O condition to wait for
491*666a3af9SDaniel P. Berrange  *
492*666a3af9SDaniel P. Berrange  * Block execution from the current thread until
493*666a3af9SDaniel P. Berrange  * the condition indicated by @condition becomes
494*666a3af9SDaniel P. Berrange  * available.
495*666a3af9SDaniel P. Berrange  *
496*666a3af9SDaniel P. Berrange  * This will enter a nested event loop to perform
497*666a3af9SDaniel P. Berrange  * the wait.
498*666a3af9SDaniel P. Berrange  */
499*666a3af9SDaniel P. Berrange void qio_channel_wait(QIOChannel *ioc,
500*666a3af9SDaniel P. Berrange                       GIOCondition condition);
501*666a3af9SDaniel P. Berrange 
502*666a3af9SDaniel P. Berrange #endif /* QIO_CHANNEL_H__ */
503