Skip to content

Commit d288606

Browse files
schallerrjihoonl
authored andcommitted
Avoid queuing of images on slow ethernet connection (#64)
1 parent 2dc7312 commit d288606

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

include/web_video_server/multipart_stream.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
#include <ros/ros.h>
55
#include <async_web_server_cpp/http_connection.hpp>
66

7+
#include <queue>
8+
79
namespace web_video_server
810
{
911

1012
class MultipartStream {
1113
public:
12-
MultipartStream(async_web_server_cpp::HttpConnectionPtr& connection, const std::string& boundry="boundarydonotcross");
14+
MultipartStream(async_web_server_cpp::HttpConnectionPtr& connection,
15+
const std::string& boundry="boundarydonotcross",
16+
std::size_t max_queue_size=1);
1317

1418
void sendInitialHeader();
1519
void sendPartHeader(const ros::Time &time, const std::string& type, size_t payload_size);
@@ -19,8 +23,13 @@ class MultipartStream {
1923
async_web_server_cpp::HttpConnection::ResourcePtr resource);
2024

2125
private:
26+
bool isBusy();
27+
28+
private:
29+
const std::size_t max_queue_size_;
2230
async_web_server_cpp::HttpConnectionPtr connection_;
2331
std::string boundry_;
32+
std::queue<boost::weak_ptr<const void> > pending_footers_;
2433
};
2534

2635
}

src/multipart_stream.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
namespace web_video_server
55
{
66

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+
{}
913

1014
void MultipartStream::sendInitialHeader() {
1115
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
2933
}
3034

3135
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);
3339
}
3440

3541
void MultipartStream::sendPartAndClear(const ros::Time &time, const std::string& type,
3642
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+
}
4049
}
4150

4251
void MultipartStream::sendPart(const ros::Time &time, const std::string& type,
4352
const boost::asio::const_buffer &buffer,
4453
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+
}
4860
}
4961

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+
}
5069

5170
}

0 commit comments

Comments
 (0)