diff --git a/pc/datachannel.cc b/pc/datachannel.cc index 19f0e6855a..a632b8e7a9 100644 --- a/pc/datachannel.cc +++ b/pc/datachannel.cc @@ -12,7 +12,9 @@ #include #include +#include +#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 DataChannel::PacketQueue::PopFront() { + RTC_DCHECK(!packets_.empty()); byte_count_ -= packets_.front()->size(); + std::unique_ptr packet = std::move(packets_.front()); packets_.pop_front(); + return packet; } -void DataChannel::PacketQueue::Push(DataBuffer* packet) { +void DataChannel::PacketQueue::PushFront(std::unique_ptr packet) { byte_count_ += packet->size(); - packets_.push_back(packet); + packets_.push_front(std::move(packet)); +} + +void DataChannel::PacketQueue::PushBack(std::unique_ptr 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 buffer(new DataBuffer(payload, binary)); + auto buffer = absl::make_unique(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 buffer(queued_received_data_.Front()); + std::unique_ptr 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 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(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 buf(control_packets.Front()); + std::unique_ptr 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(buffer, true)); } bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) { diff --git a/pc/datachannel.h b/pc/datachannel.h index 22ea354c21..55a52dba27 100644 --- a/pc/datachannel.h +++ b/pc/datachannel.h @@ -12,6 +12,7 @@ #define PC_DATACHANNEL_H_ #include +#include #include #include @@ -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 PopFront(); - void Pop(); - - void Push(DataBuffer* packet); + void PushFront(std::unique_ptr packet); + void PushBack(std::unique_ptr packet); void Clear(); void Swap(PacketQueue* other); private: - std::deque packets_; - size_t byte_count_; + std::deque> packets_; + size_t byte_count_ = 0; }; // The OPEN(_ACK) signaling state.