2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file send_common.c Functions used by more than one paraslash sender. */
20 #include "close_on_fork.h"
21 #include "chunk_queue.h"
26 * Open a passive socket of given layer4 type.
28 * Set the resultig file descriptor to nonblocking mode and add it to the list
29 * of fds that are being closed in the child process when the server calls
32 * \param l4type The transport-layer protocol.
33 * \param port The port number.
35 * \return The listening fd on success, negative on errors.
37 int open_sender(unsigned l4type, int port)
39 int fd, ret = para_listen(AF_UNSPEC, l4type, port);
44 ret = mark_fd_nonblocking(fd);
49 add_close_on_fork_list(fd);
54 * Shut down a connected client.
56 * \param sc The client to be shut down.
58 * Close the file descriptor given by \a sc, remove it from the close-on-fork
59 * list, destroy the chunk queue of this client, delete the client from the
60 * list of connected clients and free the sender_client struct.
62 void shutdown_client(struct sender_client *sc)
64 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
67 del_close_on_fork_list(sc->fd);
70 free(sc->private_data);
75 * Write a buffer to a non-blocking file descriptor.
77 * \param fd The file descriptor.
78 * \param buf the buffer to write.
79 * \param len the number of bytes of \a buf.
80 * \param max_bytes_per_write Do not write more than that many bytes at once.
82 * If \a max_bytes_per_write is non-zero, do not send more than that many bytes
85 * EAGAIN is not considered an error condition. For example CCID3 has a
86 * sending wait queue which fills up and is emptied asynchronously. The EAGAIN
87 * case means that there is currently no space in the wait queue, but this can
88 * change at any moment.
90 * \return Negative on errors, number of bytes written else.
92 static int write_nonblock(int fd, const char *buf, size_t len,
93 size_t max_bytes_per_write)
98 while (written < len) {
99 size_t num = len - written;
101 if (max_bytes_per_write && max_bytes_per_write < num)
102 num = max_bytes_per_write;
103 ret = write(fd, buf + written, num);
104 if (ret < 0 && errno == EAGAIN)
107 return -ERRNO_TO_PARA_ERROR(errno);
113 static int queue_chunk_or_shutdown(struct sender_client *sc,
114 long unsigned chunk_num, size_t sent)
116 int ret = cq_enqueue(sc->cq, chunk_num, sent);
122 /* return: negative on errors, zero if not everything was sent, one otherwise */
123 static int send_queued_chunks(struct sender_client *sc,
124 size_t max_bytes_per_write)
126 struct queued_chunk *qc;
127 while ((qc = cq_peek(sc->cq))) {
131 cq_get(qc, &buf, &len);
132 ret = write_nonblock(sc->fd, buf, len, max_bytes_per_write);
135 cq_update(sc->cq, ret);
144 * Send one chunk of audio data to a connected client.
146 * \param sc The client.
147 * \param max_bytes_per_write Split writes to chunks of at most that many bytes.
148 * \param current_chunk The number of the chunk to write.
149 * \param buf The data to write.
150 * \param len The number of bytes of \a buf.
152 * On errors, the client is shut down. If only a part of the buffer could be
153 * written, the remainder is put into the chunk queue for that client.
155 void send_chunk(struct sender_client *sc, size_t max_bytes_per_write,
156 long unsigned current_chunk, const char *buf, size_t len)
160 if (!sc->header_sent && current_chunk) {
162 char *header_buf = vss_get_header(&header_len);
163 if (header_buf && header_len > 0) {
164 ret = queue_chunk_or_shutdown(sc, -1U, 0);
170 ret = send_queued_chunks(sc, max_bytes_per_write);
177 if (!ret) { /* still data left in the queue */
178 ret = queue_chunk_or_shutdown(sc, current_chunk, 0);
181 ret = write_nonblock(sc->fd, buf, len, max_bytes_per_write);
187 ret = queue_chunk_or_shutdown(sc, current_chunk, ret);
190 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));