4
4
namespace web_video_server
5
5
{
6
6
7
- MultipartStream::MultipartStream (async_web_server_cpp::HttpConnectionPtr& connection, const std::string& boundry)
8
- : connection_(connection), boundry_(boundry) {}
7
+ MultipartStream::MultipartStream (
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
+ {}
9
13
10
14
void MultipartStream::sendInitialHeader () {
11
15
async_web_server_cpp::HttpReply::builder (async_web_server_cpp::HttpReply::ok).header (" Connection" , " close" ).header (
@@ -29,23 +33,38 @@ void MultipartStream::sendPartHeader(const ros::Time &time, const std::string& t
29
33
}
30
34
31
35
void MultipartStream::sendPartFooter () {
32
- connection_->write (" \r\n --" +boundry_+" \r\n " );
36
+ boost::shared_ptr<std::string> str (new std::string (" \r\n --" +boundry_+" \r\n " ));
37
+ connection_->write (boost::asio::buffer (*str), str);
38
+ if (max_queue_size_ > 0 ) pending_footers_.push (str);
33
39
}
34
40
35
41
void MultipartStream::sendPartAndClear (const ros::Time &time, const std::string& type,
36
42
std::vector<unsigned char > &data) {
37
- sendPartHeader (time, type, data.size ());
38
- connection_->write_and_clear (data);
39
- sendPartFooter ();
43
+ if (!isBusy ())
44
+ {
45
+ sendPartHeader (time, type, data.size ());
46
+ connection_->write_and_clear (data);
47
+ sendPartFooter ();
48
+ }
40
49
}
41
50
42
51
void MultipartStream::sendPart (const ros::Time &time, const std::string& type,
43
52
const boost::asio::const_buffer &buffer,
44
53
async_web_server_cpp::HttpConnection::ResourcePtr resource) {
45
- sendPartHeader (time, type, boost::asio::buffer_size (buffer));
46
- connection_->write (buffer, resource);
47
- sendPartFooter ();
54
+ if (!isBusy ())
55
+ {
56
+ sendPartHeader (time, type, boost::asio::buffer_size (buffer));
57
+ connection_->write (buffer, resource);
58
+ sendPartFooter ();
59
+ }
48
60
}
49
61
62
+ bool MultipartStream::isBusy () {
63
+ while (!pending_footers_.empty () && pending_footers_.front ().expired ())
64
+ {
65
+ pending_footers_.pop ();
66
+ }
67
+ return !(max_queue_size_ == 0 || pending_footers_.size () < max_queue_size_);
68
+ }
50
69
51
70
}
0 commit comments