multipart_stream.cpp
Go to the documentation of this file.
00001 #include "web_video_server/multipart_stream.h"
00002 #include "async_web_server_cpp/http_reply.hpp"
00003 
00004 namespace web_video_server
00005 {
00006 
00007 MultipartStream::MultipartStream(async_web_server_cpp::HttpConnectionPtr& connection, const std::string& boundry)
00008   : connection_(connection), boundry_(boundry) {}
00009 
00010 void MultipartStream::sendInitialHeader() {
00011   async_web_server_cpp::HttpReply::builder(async_web_server_cpp::HttpReply::ok).header("Connection", "close").header(
00012       "Server", "web_video_server").header("Cache-Control",
00013                                            "no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0").header(
00014       "Pragma", "no-cache").header("Content-type", "multipart/x-mixed-replace;boundary="+boundry_).header(
00015       "Access-Control-Allow-Origin", "*").write(connection_);
00016   connection_->write("--"+boundry_+"\r\n");
00017 }
00018 
00019 void MultipartStream::sendPartHeader(const ros::Time &time, const std::string& type, size_t payload_size) {
00020   char stamp[20];
00021   sprintf(stamp, "%.06lf", time.toSec());
00022   boost::shared_ptr<std::vector<async_web_server_cpp::HttpHeader> > headers(
00023       new std::vector<async_web_server_cpp::HttpHeader>());
00024   headers->push_back(async_web_server_cpp::HttpHeader("Content-type", type));
00025   headers->push_back(async_web_server_cpp::HttpHeader("X-Timestamp", stamp));
00026   headers->push_back(
00027       async_web_server_cpp::HttpHeader("Content-Length", boost::lexical_cast<std::string>(payload_size)));
00028   connection_->write(async_web_server_cpp::HttpReply::to_buffers(*headers), headers);
00029 }
00030 
00031 void MultipartStream::sendPartFooter() {
00032   connection_->write("\r\n--"+boundry_+"\r\n");
00033 }
00034 
00035 void MultipartStream::sendPartAndClear(const ros::Time &time, const std::string& type,
00036                                        std::vector<unsigned char> &data) {
00037   sendPartHeader(time, type, data.size());
00038   connection_->write_and_clear(data);
00039   sendPartFooter();
00040 }
00041 
00042 void MultipartStream::sendPart(const ros::Time &time, const std::string& type,
00043                                const boost::asio::const_buffer &buffer,
00044                                async_web_server_cpp::HttpConnection::ResourcePtr resource) {
00045   sendPartHeader(time, type, boost::asio::buffer_size(buffer));
00046   connection_->write(buffer, resource);
00047   sendPartFooter();
00048 }
00049 
00050 
00051 }


web_video_server
Author(s): Mitchell Wills
autogenerated on Sun Jan 22 2017 04:06:43