summaryrefslogtreecommitdiff
path: root/send.h
blob: 76a35123c8b5b414b45eec935a525e8d906ae22a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/** \file send.h sender-related defines and structures */
/** the sender subcommands */
enum {SENDER_ADD, SENDER_DELETE, SENDER_ALLOW, SENDER_DENY, SENDER_ON, SENDER_OFF};

/** the number of sender subcommands */
#define NUM_SENDER_CMDS (SENDER_OFF + 1)

/**
 * describes one supported sender of para_server
 *
 * \sa http_send.c ortp_send.c
 */
struct sender {
/** the name of the sender */
	const char *name;
/**
 * the init function of this sender
 *
 * It must fill in all function pointers of \a s as well as the \a client_cmds
 * array, see below. It should also do all neccessary preparations to init
 * this sending facility, for example it could open a tcp port.
 */
	void (*init)(struct sender *s);
/** \p SENDER_ON or \p SENDER_OFF */
	int status;
/**
 * return the help text of this sender
 *
 * The result must be dynamically allocated and is freed by the caller.
 */
	char* (*help)(void);
/**
 * return current status info about this sender
 *
 * The result must be dynamically allocated and is freed by the caller.
 */
	char* (*info)(void);
/**
 * the send-hook
 *
 * It gets called whenever para_server is playing and the current
 * audio format handler indicates that another chunk of data should
 * be sent now. The two parameters \a current_chunk and \a chunks_sent
 * only differ if the stream was repositioned by the \a ff or \a jmp
 * command. Of course, \a buf is a pointer to the chunk of data which
 * should be sent, and \a len is the length of this buffer.
*/
	void (*send)(long unsigned current_chunk, long unsigned chunks_sent,
		const char *buf, size_t len);
/** add file descriptors to fd_sets
 *
 * The pre_select function of each supported sender is called just before
 * para_server enters its main select loop. Each sender may add its own
 * file descriptors to the \a rfds or the \a wfds set.
 *
 * If a file descriptor was added, \a max_fileno must be increased by
 * this function, if neccessary.
 *
 * \sa select(2)
*/
	void (*pre_select)(int *max_fileno, fd_set *rfds, fd_set *wfds);
/**
 * handle the file descriptors which are ready for I/O
 *
 * If the pre_select hook added one ore more file descriptors to the read or write
 * set, this is the hook to check the result and do any I/O on those descriptors
 * which are ready for reading/writing.
 */
	void (*post_select)(fd_set *rfds, fd_set *wfds);
/**
 * terminate all connected clients
 *
 * This is called e.g. if the stop command was executed. It should make the clients
 * aware of the end-of-file condition.
 */
	void (*shutdown_clients)(void);
/**
 * array of function pointers for the sender subcommands
 *
 * Each sender may implement any subset of the sender commands by filling in
 * the aprropriate function pointer in the array. A \p NULL pointer means this
 * command is not implemented by this sender.
 */
	int (*client_cmds[NUM_SENDER_CMDS])(struct sender_command_data*);
};

/**
 * check a file descriptor for writability
 *
 * \param fd the file desctiptor
 *
 * \return positive if fd is ready for writing, zero if it isn't, negative if
 * an error occured.
 */

static inline int write_ok(int fd)
{
	struct timeval tv = {0, 0};
	fd_set wfds;
	int ret;
again:
	FD_ZERO(&wfds);
	FD_SET(fd, &wfds);
	tv.tv_sec = 0;
	tv.tv_usec = 0;
	ret = select(fd + 1, NULL, &wfds, NULL, &tv);
	if (ret < 0 && errno == EINTR)
		goto again;
	return ret;
}