multipart_stream.cpp
Go to the documentation of this file.
2 #include "async_web_server_cpp/http_reply.hpp"
3 
4 namespace web_video_server
5 {
6 
8  async_web_server_cpp::HttpConnectionPtr& connection,
9  const std::string& boundry,
10  std::size_t max_queue_size)
11  : connection_(connection), boundry_(boundry), max_queue_size_(max_queue_size)
12 {}
13 
15  async_web_server_cpp::HttpReply::builder(async_web_server_cpp::HttpReply::ok).header("Connection", "close").header(
16  "Server", "web_video_server").header("Cache-Control",
17  "no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0").header(
18  "Pragma", "no-cache").header("Content-type", "multipart/x-mixed-replace;boundary="+boundry_).header(
19  "Access-Control-Allow-Origin", "*").write(connection_);
20  connection_->write("--"+boundry_+"\r\n");
21 }
22 
23 void MultipartStream::sendPartHeader(const ros::Time &time, const std::string& type, size_t payload_size) {
24  char stamp[20];
25  sprintf(stamp, "%.06lf", time.toSec());
27  new std::vector<async_web_server_cpp::HttpHeader>());
28  headers->push_back(async_web_server_cpp::HttpHeader("Content-type", type));
29  headers->push_back(async_web_server_cpp::HttpHeader("X-Timestamp", stamp));
30  headers->push_back(
31  async_web_server_cpp::HttpHeader("Content-Length", boost::lexical_cast<std::string>(payload_size)));
32  connection_->write(async_web_server_cpp::HttpReply::to_buffers(*headers), headers);
33 }
34 
36  boost::shared_ptr<std::string> str(new std::string("\r\n--"+boundry_+"\r\n"));
37  PendingFooter pf;
38  pf.timestamp = time;
39  pf.contents = str;
40  connection_->write(boost::asio::buffer(*str), str);
41  if (max_queue_size_ > 0) pending_footers_.push(pf);
42 }
43 
44 void MultipartStream::sendPartAndClear(const ros::Time &time, const std::string& type,
45  std::vector<unsigned char> &data) {
46  if (!isBusy())
47  {
48  sendPartHeader(time, type, data.size());
49  connection_->write_and_clear(data);
50  sendPartFooter(time);
51  }
52 }
53 
54 void MultipartStream::sendPart(const ros::Time &time, const std::string& type,
55  const boost::asio::const_buffer &buffer,
56  async_web_server_cpp::HttpConnection::ResourcePtr resource) {
57  if (!isBusy())
58  {
59  sendPartHeader(time, type, boost::asio::buffer_size(buffer));
60  connection_->write(buffer, resource);
61  sendPartFooter(time);
62  }
63 }
64 
66  ros::Time currentTime = ros::Time::now();
67  while (!pending_footers_.empty())
68  {
69  if (pending_footers_.front().contents.expired()) {
70  pending_footers_.pop();
71  } else {
72  ros::Time footerTime = pending_footers_.front().timestamp;
73  if ((currentTime - footerTime).toSec() > 0.5) {
74  pending_footers_.pop();
75  } else {
76  break;
77  }
78  }
79  }
80  return !(max_queue_size_ == 0 || pending_footers_.size() < max_queue_size_);
81 }
82 
83 }
void sendPartHeader(const ros::Time &time, const std::string &type, size_t payload_size)
async_web_server_cpp::HttpConnectionPtr connection_
MultipartStream(async_web_server_cpp::HttpConnectionPtr &connection, const std::string &boundry="boundarydonotcross", std::size_t max_queue_size=1)
void sendPartAndClear(const ros::Time &time, const std::string &type, std::vector< unsigned char > &data)
void sendPart(const ros::Time &time, const std::string &type, const boost::asio::const_buffer &buffer, async_web_server_cpp::HttpConnection::ResourcePtr resource)
boost::weak_ptr< std::string > contents
void sendPartFooter(const ros::Time &time)
std::queue< PendingFooter > pending_footers_
static Time now()


web_video_server
Author(s): Mitchell Wills
autogenerated on Wed Jun 5 2019 20:38:04