4 #include <netinet/in.h>
14 #include "metacube2.h"
15 #include "mutexlock.h"
22 Stream::Stream(const string &url, size_t backlog_size, Encoding encoding)
25 data_fd(make_tempfile("")),
26 backlog_size(backlog_size),
28 last_suitable_starting_point(-1),
30 queued_data_last_starting_point(-1)
36 pthread_mutex_init(&queued_data_mutex, NULL);
46 Stream::Stream(const StreamProto &serialized, int data_fd)
47 : url(serialized.url()),
48 http_header(serialized.http_header()),
49 stream_header(serialized.stream_header()),
50 encoding(Stream::STREAM_ENCODING_RAW), // Will be changed later.
52 backlog_size(serialized.backlog_size()),
53 bytes_received(serialized.bytes_received()),
55 queued_data_last_starting_point(-1)
61 assert(serialized.has_last_suitable_starting_point());
62 last_suitable_starting_point = serialized.last_suitable_starting_point();
64 pthread_mutex_init(&queued_data_mutex, NULL);
67 StreamProto Stream::serialize()
69 StreamProto serialized;
70 serialized.set_http_header(http_header);
71 serialized.set_stream_header(stream_header);
72 serialized.add_data_fds(data_fd);
73 serialized.set_backlog_size(backlog_size);
74 serialized.set_bytes_received(bytes_received);
75 serialized.set_last_suitable_starting_point(last_suitable_starting_point);
76 serialized.set_url(url);
81 void Stream::set_backlog_size(size_t new_size)
83 if (backlog_size == new_size) {
88 if (!read_tempfile_and_close(data_fd, &existing_data)) {
92 // Unwrap the data so it's no longer circular.
93 if (bytes_received <= backlog_size) {
94 existing_data.resize(bytes_received);
96 size_t pos = bytes_received % backlog_size;
97 existing_data = existing_data.substr(pos, string::npos) +
98 existing_data.substr(0, pos);
101 // See if we need to discard data.
102 if (new_size < existing_data.size()) {
103 size_t to_discard = existing_data.size() - new_size;
104 existing_data = existing_data.substr(to_discard, string::npos);
107 // Create a new, empty data file.
108 data_fd = make_tempfile("");
112 backlog_size = new_size;
114 // Now cheat a bit by rewinding, and adding all the old data back.
115 bytes_received -= existing_data.size();
117 iov.iov_base = const_cast<char *>(existing_data.data());
118 iov.iov_len = existing_data.size();
125 void Stream::put_client_to_sleep(Client *client)
127 sleeping_clients.push_back(client);
130 // Return a new set of iovecs that contains only the first <bytes_wanted> bytes of <data>.
131 vector<iovec> collect_iovecs(const vector<iovec> &data, size_t bytes_wanted)
134 size_t max_iovecs = std::min<size_t>(data.size(), IOV_MAX);
135 for (size_t i = 0; i < max_iovecs && bytes_wanted > 0; ++i) {
136 if (data[i].iov_len <= bytes_wanted) {
137 // Consume the entire iovec.
138 ret.push_back(data[i]);
139 bytes_wanted -= data[i].iov_len;
141 // Take only parts of this iovec.
143 iov.iov_base = data[i].iov_base;
144 iov.iov_len = bytes_wanted;
152 // Return a new set of iovecs that contains all of <data> except the first <bytes_wanted> bytes.
153 vector<iovec> remove_iovecs(const vector<iovec> &data, size_t bytes_wanted)
157 for (i = 0; i < data.size() && bytes_wanted > 0; ++i) {
158 if (data[i].iov_len <= bytes_wanted) {
159 // Consume the entire iovec.
160 bytes_wanted -= data[i].iov_len;
162 // Take only parts of this iovec.
164 iov.iov_base = reinterpret_cast<char *>(data[i].iov_base) + bytes_wanted;
165 iov.iov_len = data[i].iov_len - bytes_wanted;
171 // Add the rest of the iovecs unchanged.
172 ret.insert(ret.end(), data.begin() + i, data.end());
176 void Stream::add_data_raw(const vector<iovec> &orig_data)
178 vector<iovec> data = orig_data;
179 while (!data.empty()) {
180 size_t pos = bytes_received % backlog_size;
182 // Collect as many iovecs as we can before we hit the point
183 // where the circular buffer wraps around.
184 vector<iovec> to_write = collect_iovecs(data, backlog_size - pos);
187 ret = pwritev(data_fd, to_write.data(), to_write.size(), pos);
188 } while (ret == -1 && errno == EINTR);
191 log_perror("pwritev");
192 // Dazed and confused, but trying to continue...
195 bytes_received += ret;
197 // Remove the data that was actually written from the set of iovecs.
198 data = remove_iovecs(data, ret);
202 void Stream::add_data_deferred(const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start)
204 MutexLock lock(&queued_data_mutex);
205 assert(suitable_for_stream_start == SUITABLE_FOR_STREAM_START ||
206 suitable_for_stream_start == NOT_SUITABLE_FOR_STREAM_START);
207 if (suitable_for_stream_start == SUITABLE_FOR_STREAM_START) {
208 queued_data_last_starting_point = queued_data.size();
211 if (encoding == Stream::STREAM_ENCODING_METACUBE) {
212 // Add a Metacube block header before the data.
213 metacube2_block_header hdr;
214 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
215 hdr.size = htonl(bytes);
216 hdr.flags = htons(0);
217 if (suitable_for_stream_start == NOT_SUITABLE_FOR_STREAM_START) {
218 hdr.flags |= htons(METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START);
220 hdr.csum = htons(metacube2_compute_crc(&hdr));
223 iov.iov_base = new char[bytes + sizeof(hdr)];
224 iov.iov_len = bytes + sizeof(hdr);
226 memcpy(iov.iov_base, &hdr, sizeof(hdr));
227 memcpy(reinterpret_cast<char *>(iov.iov_base) + sizeof(hdr), data, bytes);
229 queued_data.push_back(iov);
230 } else if (encoding == Stream::STREAM_ENCODING_RAW) {
231 // Just add the data itself.
233 iov.iov_base = new char[bytes];
234 memcpy(iov.iov_base, data, bytes);
237 queued_data.push_back(iov);
243 void Stream::process_queued_data()
245 std::vector<iovec> queued_data_copy;
246 int queued_data_last_starting_point_copy = -1;
248 // Hold the lock for as short as possible, since add_data_raw() can possibly
249 // write to disk, which might disturb the input thread.
251 MutexLock lock(&queued_data_mutex);
252 if (queued_data.empty()) {
256 swap(queued_data, queued_data_copy);
257 swap(queued_data_last_starting_point, queued_data_last_starting_point_copy);
260 // Update the last suitable starting point for the stream,
261 // if the queued data contains such a starting point.
262 assert(queued_data_last_starting_point_copy < ssize_t(queued_data_copy.size()));
263 if (queued_data_last_starting_point_copy >= 0) {
264 last_suitable_starting_point = bytes_received;
265 for (int i = 0; i < queued_data_last_starting_point_copy; ++i) {
266 last_suitable_starting_point += queued_data_copy[i].iov_len;
270 add_data_raw(queued_data_copy);
271 for (size_t i = 0; i < queued_data_copy.size(); ++i) {
272 char *data = reinterpret_cast<char *>(queued_data_copy[i].iov_base);
276 // We have more data, so wake up all clients.
277 if (to_process.empty()) {
278 swap(sleeping_clients, to_process);
280 to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
281 sleeping_clients.clear();