Use unique_ptr in DataChannel PacketQueue

Bug: None
Change-Id: I629d42c5a2e736ae352ef5df01eb19b2a9498e7f
Reviewed-on: https://webrtc-review.googlesource.com/c/114261
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Reviewed-by: Amit Hilbuch <amithi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26008}
This commit is contained in:
Steve Anton 2018-12-13 14:19:10 -08:00 committed by Commit Bot
parent cca13f622f
commit 944c7557e1
2 changed files with 30 additions and 43 deletions

View File

@ -12,7 +12,9 @@
#include <memory>
#include <string>
#include <utility>
#include "absl/memory/memory.h"
#include "media/sctp/sctptransportinternal.h"
#include "pc/sctputils.h"
#include "rtc_base/checks.h"
@ -63,39 +65,30 @@ bool SctpSidAllocator::IsSidAvailable(int sid) const {
return used_sids_.find(sid) == used_sids_.end();
}
DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {}
DataChannel::PacketQueue::~PacketQueue() {
Clear();
}
bool DataChannel::PacketQueue::Empty() const {
return packets_.empty();
}
DataBuffer* DataChannel::PacketQueue::Front() {
return packets_.front();
}
void DataChannel::PacketQueue::Pop() {
if (packets_.empty()) {
return;
}
std::unique_ptr<DataBuffer> DataChannel::PacketQueue::PopFront() {
RTC_DCHECK(!packets_.empty());
byte_count_ -= packets_.front()->size();
std::unique_ptr<DataBuffer> packet = std::move(packets_.front());
packets_.pop_front();
return packet;
}
void DataChannel::PacketQueue::Push(DataBuffer* packet) {
void DataChannel::PacketQueue::PushFront(std::unique_ptr<DataBuffer> packet) {
byte_count_ += packet->size();
packets_.push_back(packet);
packets_.push_front(std::move(packet));
}
void DataChannel::PacketQueue::PushBack(std::unique_ptr<DataBuffer> packet) {
byte_count_ += packet->size();
packets_.push_back(std::move(packet));
}
void DataChannel::PacketQueue::Clear() {
while (!packets_.empty()) {
delete packets_.front();
packets_.pop_front();
}
packets_.clear();
byte_count_ = 0;
}
@ -393,7 +386,7 @@ void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
}
bool binary = (params.type == cricket::DMT_BINARY);
std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary));
auto buffer = absl::make_unique<DataBuffer>(payload, binary);
if (state_ == kOpen && observer_) {
++messages_received_;
bytes_received_ += buffer->size();
@ -410,7 +403,7 @@ void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params,
return;
}
queued_received_data_.Push(buffer.release());
queued_received_data_.PushBack(std::move(buffer));
}
}
@ -541,11 +534,10 @@ void DataChannel::DeliverQueuedReceivedData() {
}
while (!queued_received_data_.Empty()) {
std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front());
std::unique_ptr<DataBuffer> buffer = queued_received_data_.PopFront();
++messages_received_;
bytes_received_ += buffer->size();
observer_->OnMessage(*buffer);
queued_received_data_.Pop();
}
}
@ -558,13 +550,12 @@ void DataChannel::SendQueuedDataMessages() {
uint64_t start_buffered_amount = buffered_amount();
while (!queued_send_data_.Empty()) {
DataBuffer* buffer = queued_send_data_.Front();
std::unique_ptr<DataBuffer> buffer = queued_send_data_.PopFront();
if (!SendDataMessage(*buffer, false)) {
// Leave the message in the queue if sending is aborted.
// Return the message to the front of the queue if sending is aborted.
queued_send_data_.PushFront(std::move(buffer));
break;
}
queued_send_data_.Pop();
delete buffer;
}
if (observer_ && buffered_amount() < start_buffered_amount) {
@ -628,7 +619,7 @@ bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) {
RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel.";
return false;
}
queued_send_data_.Push(new DataBuffer(buffer));
queued_send_data_.PushBack(absl::make_unique<DataBuffer>(buffer));
// The buffer can have length zero, in which case there is no change.
if (observer_ && buffered_amount() > start_buffered_amount) {
@ -642,14 +633,13 @@ void DataChannel::SendQueuedControlMessages() {
control_packets.Swap(&queued_control_data_);
while (!control_packets.Empty()) {
std::unique_ptr<DataBuffer> buf(control_packets.Front());
std::unique_ptr<DataBuffer> buf = control_packets.PopFront();
SendControlMessage(buf->data);
control_packets.Pop();
}
}
void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) {
queued_control_data_.Push(new DataBuffer(buffer, true));
queued_control_data_.PushBack(absl::make_unique<DataBuffer>(buffer, true));
}
bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) {

View File

@ -12,6 +12,7 @@
#define PC_DATACHANNEL_H_
#include <deque>
#include <memory>
#include <set>
#include <string>
@ -211,28 +212,24 @@ class DataChannel : public DataChannelInterface, public sigslot::has_slots<> {
private:
// A packet queue which tracks the total queued bytes. Queued packets are
// owned by this class.
class PacketQueue {
class PacketQueue final {
public:
PacketQueue();
~PacketQueue();
size_t byte_count() const { return byte_count_; }
bool Empty() const;
DataBuffer* Front();
std::unique_ptr<DataBuffer> PopFront();
void Pop();
void Push(DataBuffer* packet);
void PushFront(std::unique_ptr<DataBuffer> packet);
void PushBack(std::unique_ptr<DataBuffer> packet);
void Clear();
void Swap(PacketQueue* other);
private:
std::deque<DataBuffer*> packets_;
size_t byte_count_;
std::deque<std::unique_ptr<DataBuffer>> packets_;
size_t byte_count_ = 0;
};
// The OPEN(_ACK) signaling state.