1 /*
2  * Copyright (C) 2000-2019 the xine project,
3  *
4  * This file is part of xine, a free video player.
5  *
6  * xine is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * xine is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * abortable i/o helper functions
21  */
22 
23 #ifndef IO_HELPER_H
24 #define IO_HELPER_H
25 
26 #include "xine_internal.h"
27 
28 
29 /* select states */
30 #define XIO_READ_READY    1
31 #define XIO_WRITE_READY   2
32 
33 /* xine select return codes */
34 #define XIO_READY         0
35 #define XIO_ERROR         1
36 #define XIO_ABORTED       2
37 #define XIO_TIMEOUT       3
38 
39 
40 /*
41  * Waits for a file descriptor/socket to change status.
42  *
43  * network input plugins should use this function in order to
44  * not freeze the engine.
45  *
46  * params :
47  *   stream        needed for aborting and reporting errors but may be NULL
48  *   fd            file/socket descriptor
49  *   state         XIO_READ_READY, XIO_WRITE_READY
50  *   timeout_sec   timeout in seconds
51  *
52  * An other thread can abort this function if stream != NULL by setting
53  * stream->demux_action_pending.
54  *
55  * return value :
56  *   XIO_READY     the file descriptor is ready for cmd
57  *   XIO_ERROR     an i/o error occured
58  *   XIO_ABORTED   command aborted by an other thread
59  *   XIO_TIMEOUT   the file descriptor is not ready after timeout_msec milliseconds
60  */
61 int _x_io_select (xine_stream_t *stream, int fd, int state, int timeout_msec) XINE_PROTECTED XINE_USED;
62 
63 
64 /*
65  * open a tcp connection
66  *
67  * params :
68  *   stream        needed for reporting errors but may be NULL
69  *   host          address of target
70  *   port          port on target
71  *
72  * returns a socket descriptor or -1 if an error occured
73  */
74 int _x_io_tcp_connect(xine_stream_t *stream, const char *host, int port) XINE_PROTECTED XINE_USED;
75 
76 /* connect and handshake. */
77 typedef enum {
78   /* return success. */
79   XIO_HANDSHAKE_OK = 1,
80   /* reopen same target, and try a different handshake (eg with/without tls). */
81   XIO_HANDSHAKE_TRY_SAME = 2,
82   /* try next target, if any. */
83   XIO_HANDSHAKE_TRY_NEXT = 3,
84   /* return failure (eg when handshake has hit a -1 EINTR). */
85   XIO_HANDSHAKE_INTR = 4
86 } xio_handshake_status_t;
87 /* use _x_io_* () below. */
88 typedef xio_handshake_status_t (xio_handshake_cb_t)(void *userdata, int fd);
89 /* like _x_io_tcp_connect (). */
90 int _x_io_tcp_handshake_connect (xine_stream_t *stream, const char *host, int port,
91   xio_handshake_cb_t *handshake_cb, void *userdata) XINE_PROTECTED XINE_USED;
92 
93 /*
94  * wait for finish connection
95  *
96  * params :
97  *   stream        needed for aborting and reporting errors but may be NULL
98  *   fd            socket descriptor
99  *   timeout_msec  timeout in milliseconds
100  *
101  * return value:
102  *   XIO_READY     host respond, the socket is ready for cmd
103  *   XIO_ERROR     an i/o error occured
104  *   XIO_ABORTED   command aborted by an other thread
105  *   XIO_TIMEOUT   the file descriptor is not ready after timeout
106  */
107 int _x_io_tcp_connect_finish(xine_stream_t *stream, int fd, int timeout_msec) XINE_PROTECTED XINE_USED;
108 
109 /* The next 5 read/write functions will try to transfer todo/min bytes unless
110  * - the end of stream is reached (0), or
111  * - no data is available for user network timeout seconds (-1 ETIMEDOUT), or
112  * - an io error hits (-1 Exxx), or
113  * - xine engine wants to seek/stop (-1 EINTR).
114  * _x_io_tcp_part_read (stream, s, buf, 0, len) may also yield (-1 EAGAIN), and will never wait.
115  * network input plugins should use these functions in order not to freeze the engine.
116  * "off_t" is just there for historical reasons, "(s)size_t" should be enough.
117  */
118 off_t _x_io_tcp_read (xine_stream_t *stream, int s, void *buf, off_t todo) XINE_PROTECTED XINE_USED;
119 /* like _x_io_tcp_read () but:
120  * 1. wait until we have at least min bytes, then
121  * 2. return up to max bytes that are already there. */
122 ssize_t _x_io_tcp_part_read (xine_stream_t *stream, int s, void *buf, size_t min, size_t max) XINE_PROTECTED XINE_USED;
123 off_t _x_io_tcp_write (xine_stream_t *stream, int s, const void *buf, off_t todo) XINE_PROTECTED XINE_USED;
124 off_t _x_io_file_read (xine_stream_t *stream, int fd, void *buf, off_t todo) XINE_PROTECTED XINE_USED;
125 off_t _x_io_file_write (xine_stream_t *stream, int fd, const void *buf, off_t todo) XINE_PROTECTED XINE_USED;
126 
127 /* XXX this is slow.
128  * read a string from socket, return string length (same as strlen)
129  * the string is always '\0' terminated but given buffer size is never exceeded
130  * that is, _x_io_tcp_read_line(,,,X) <= (X-1) ; X > 0
131  */
132 int _x_io_tcp_read_line(xine_stream_t *stream, int sock, char *str, int size) XINE_PROTECTED XINE_USED;
133 
134 /*
135  * Close socket
136  */
137 int _x_io_tcp_close(xine_stream_t *stream, int fd) XINE_PROTECTED;
138 
139 #endif
140