Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a workaround for MultipartStream constant busy state #83

Merged
merged 2 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions include/web_video_server/multipart_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
namespace web_video_server
{

struct PendingFooter {
ros::Time timestamp;
boost::weak_ptr<std::string> contents;
};

class MultipartStream {
public:
MultipartStream(async_web_server_cpp::HttpConnectionPtr& connection,
Expand All @@ -17,7 +22,7 @@ class MultipartStream {

void sendInitialHeader();
void sendPartHeader(const ros::Time &time, const std::string& type, size_t payload_size);
void sendPartFooter();
void sendPartFooter(const ros::Time &time);
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);
Expand All @@ -29,7 +34,7 @@ class MultipartStream {
const std::size_t max_queue_size_;
async_web_server_cpp::HttpConnectionPtr connection_;
std::string boundry_;
std::queue<boost::weak_ptr<const void> > pending_footers_;
std::queue<PendingFooter> pending_footers_;
};

}
Expand Down
25 changes: 19 additions & 6 deletions src/multipart_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ void MultipartStream::sendPartHeader(const ros::Time &time, const std::string& t
connection_->write(async_web_server_cpp::HttpReply::to_buffers(*headers), headers);
}

void MultipartStream::sendPartFooter() {
void MultipartStream::sendPartFooter(const ros::Time &time) {
boost::shared_ptr<std::string> str(new std::string("\r\n--"+boundry_+"\r\n"));
PendingFooter pf;
pf.timestamp = time;
pf.contents = str;
connection_->write(boost::asio::buffer(*str), str);
if (max_queue_size_ > 0) pending_footers_.push(str);
if (max_queue_size_ > 0) pending_footers_.push(pf);
}

void MultipartStream::sendPartAndClear(const ros::Time &time, const std::string& type,
Expand All @@ -44,7 +47,7 @@ void MultipartStream::sendPartAndClear(const ros::Time &time, const std::string&
{
sendPartHeader(time, type, data.size());
connection_->write_and_clear(data);
sendPartFooter();
sendPartFooter(time);
}
}

Expand All @@ -55,14 +58,24 @@ void MultipartStream::sendPart(const ros::Time &time, const std::string& type,
{
sendPartHeader(time, type, boost::asio::buffer_size(buffer));
connection_->write(buffer, resource);
sendPartFooter();
sendPartFooter(time);
}
}

bool MultipartStream::isBusy() {
while (!pending_footers_.empty() && pending_footers_.front().expired())
ros::Time currentTime = ros::Time::now();
while (!pending_footers_.empty())
{
pending_footers_.pop();
if (pending_footers_.front().contents.expired()) {
pending_footers_.pop();
} else {
ros::Time footerTime = pending_footers_.front().timestamp;
if ((currentTime - footerTime).toSec() > 0.5) {
pending_footers_.pop();
} else {
break;
}
}
}
return !(max_queue_size_ == 0 || pending_footers_.size() < max_queue_size_);
}
Expand Down