henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/datachannel.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 13 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | #include <string> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "media/sctp/sctptransportinternal.h" |
| 17 | #include "pc/sctputils.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/logging.h" |
| 20 | #include "rtc_base/refcount.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 24 | static size_t kMaxQueuedReceivedDataBytes = 16 * 1024 * 1024; |
| 25 | static size_t kMaxQueuedSendDataBytes = 16 * 1024 * 1024; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 26 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 27 | enum { |
| 28 | MSG_CHANNELREADY, |
| 29 | }; |
| 30 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 31 | bool SctpSidAllocator::AllocateSid(rtc::SSLRole role, int* sid) { |
| 32 | int potential_sid = (role == rtc::SSL_CLIENT) ? 0 : 1; |
| 33 | while (!IsSidAvailable(potential_sid)) { |
| 34 | potential_sid += 2; |
| 35 | if (potential_sid > static_cast<int>(cricket::kMaxSctpSid)) { |
| 36 | return false; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | *sid = potential_sid; |
| 41 | used_sids_.insert(potential_sid); |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | bool SctpSidAllocator::ReserveSid(int sid) { |
| 46 | if (!IsSidAvailable(sid)) { |
| 47 | return false; |
| 48 | } |
| 49 | used_sids_.insert(sid); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | void SctpSidAllocator::ReleaseSid(int sid) { |
| 54 | auto it = used_sids_.find(sid); |
| 55 | if (it != used_sids_.end()) { |
| 56 | used_sids_.erase(it); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | bool SctpSidAllocator::IsSidAvailable(int sid) const { |
Taylor Brandstetter | 1d7a637 | 2016-08-24 13:15:27 -0700 | [diff] [blame] | 61 | if (sid < static_cast<int>(cricket::kMinSctpSid) || |
| 62 | sid > static_cast<int>(cricket::kMaxSctpSid)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 63 | return false; |
| 64 | } |
| 65 | return used_sids_.find(sid) == used_sids_.end(); |
| 66 | } |
| 67 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 68 | DataChannel::PacketQueue::PacketQueue() : byte_count_(0) {} |
| 69 | |
| 70 | DataChannel::PacketQueue::~PacketQueue() { |
| 71 | Clear(); |
| 72 | } |
| 73 | |
| 74 | bool DataChannel::PacketQueue::Empty() const { |
| 75 | return packets_.empty(); |
| 76 | } |
| 77 | |
| 78 | DataBuffer* DataChannel::PacketQueue::Front() { |
| 79 | return packets_.front(); |
| 80 | } |
| 81 | |
| 82 | void DataChannel::PacketQueue::Pop() { |
| 83 | if (packets_.empty()) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | byte_count_ -= packets_.front()->size(); |
| 88 | packets_.pop_front(); |
| 89 | } |
| 90 | |
| 91 | void DataChannel::PacketQueue::Push(DataBuffer* packet) { |
| 92 | byte_count_ += packet->size(); |
| 93 | packets_.push_back(packet); |
| 94 | } |
| 95 | |
| 96 | void DataChannel::PacketQueue::Clear() { |
| 97 | while (!packets_.empty()) { |
| 98 | delete packets_.front(); |
| 99 | packets_.pop_front(); |
| 100 | } |
| 101 | byte_count_ = 0; |
| 102 | } |
| 103 | |
| 104 | void DataChannel::PacketQueue::Swap(PacketQueue* other) { |
| 105 | size_t other_byte_count = other->byte_count_; |
| 106 | other->byte_count_ = byte_count_; |
| 107 | byte_count_ = other_byte_count; |
| 108 | |
| 109 | other->packets_.swap(packets_); |
| 110 | } |
| 111 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 112 | rtc::scoped_refptr<DataChannel> DataChannel::Create( |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 113 | DataChannelProviderInterface* provider, |
| 114 | cricket::DataChannelType dct, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | const std::string& label, |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 116 | const InternalDataChannelInit& config) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 117 | rtc::scoped_refptr<DataChannel> channel( |
| 118 | new rtc::RefCountedObject<DataChannel>(provider, dct, label)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | if (!channel->Init(config)) { |
| 120 | return NULL; |
| 121 | } |
| 122 | return channel; |
| 123 | } |
| 124 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 125 | DataChannel::DataChannel( |
| 126 | DataChannelProviderInterface* provider, |
| 127 | cricket::DataChannelType dct, |
| 128 | const std::string& label) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 129 | : label_(label), |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 130 | observer_(nullptr), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 131 | state_(kConnecting), |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 132 | messages_sent_(0), |
| 133 | bytes_sent_(0), |
| 134 | messages_received_(0), |
| 135 | bytes_received_(0), |
henrika@webrtc.org | 44461fa | 2014-01-13 09:35:02 +0000 | [diff] [blame] | 136 | data_channel_type_(dct), |
| 137 | provider_(provider), |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 138 | handshake_state_(kHandshakeInit), |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 139 | connected_to_provider_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 140 | send_ssrc_set_(false), |
henrika@webrtc.org | 44461fa | 2014-01-13 09:35:02 +0000 | [diff] [blame] | 141 | receive_ssrc_set_(false), |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 142 | writable_(false), |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 143 | send_ssrc_(0), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 144 | receive_ssrc_(0) { |
| 145 | } |
| 146 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 147 | bool DataChannel::Init(const InternalDataChannelInit& config) { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 148 | if (data_channel_type_ == cricket::DCT_RTP) { |
| 149 | if (config.reliable || |
| 150 | config.id != -1 || |
| 151 | config.maxRetransmits != -1 || |
| 152 | config.maxRetransmitTime != -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 153 | RTC_LOG(LS_ERROR) << "Failed to initialize the RTP data channel due to " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 154 | "invalid DataChannelInit."; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | handshake_state_ = kHandshakeReady; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 158 | } else if (data_channel_type_ == cricket::DCT_SCTP) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 159 | if (config.id < -1 || |
| 160 | config.maxRetransmits < -1 || |
| 161 | config.maxRetransmitTime < -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 162 | RTC_LOG(LS_ERROR) << "Failed to initialize the SCTP data channel due to " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 163 | "invalid DataChannelInit."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | return false; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 165 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 166 | if (config.maxRetransmits != -1 && config.maxRetransmitTime != -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 167 | RTC_LOG(LS_ERROR) |
| 168 | << "maxRetransmits and maxRetransmitTime should not be both set."; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 169 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 171 | config_ = config; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 173 | switch (config_.open_handshake_role) { |
| 174 | case webrtc::InternalDataChannelInit::kNone: // pre-negotiated |
| 175 | handshake_state_ = kHandshakeReady; |
| 176 | break; |
| 177 | case webrtc::InternalDataChannelInit::kOpener: |
| 178 | handshake_state_ = kHandshakeShouldSendOpen; |
| 179 | break; |
| 180 | case webrtc::InternalDataChannelInit::kAcker: |
| 181 | handshake_state_ = kHandshakeShouldSendAck; |
| 182 | break; |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 183 | } |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 184 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 185 | // Try to connect to the transport in case the transport channel already |
| 186 | // exists. |
| 187 | OnTransportChannelCreated(); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 188 | |
| 189 | // Checks if the transport is ready to send because the initial channel |
| 190 | // ready signal may have been sent before the DataChannel creation. |
| 191 | // This has to be done async because the upper layer objects (e.g. |
| 192 | // Chrome glue and WebKit) are not wired up properly until after this |
| 193 | // function returns. |
| 194 | if (provider_->ReadyToSendData()) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 195 | rtc::Thread::Current()->Post(RTC_FROM_HERE, this, MSG_CHANNELREADY, NULL); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 196 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 200 | } |
| 201 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 202 | DataChannel::~DataChannel() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 203 | |
| 204 | void DataChannel::RegisterObserver(DataChannelObserver* observer) { |
| 205 | observer_ = observer; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 206 | DeliverQueuedReceivedData(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void DataChannel::UnregisterObserver() { |
| 210 | observer_ = NULL; |
| 211 | } |
| 212 | |
| 213 | bool DataChannel::reliable() const { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 214 | if (data_channel_type_ == cricket::DCT_RTP) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 215 | return false; |
| 216 | } else { |
| 217 | return config_.maxRetransmits == -1 && |
| 218 | config_.maxRetransmitTime == -1; |
| 219 | } |
| 220 | } |
| 221 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 222 | uint64_t DataChannel::buffered_amount() const { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 223 | return queued_send_data_.byte_count(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void DataChannel::Close() { |
| 227 | if (state_ == kClosed) |
| 228 | return; |
| 229 | send_ssrc_ = 0; |
| 230 | send_ssrc_set_ = false; |
| 231 | SetState(kClosing); |
| 232 | UpdateState(); |
| 233 | } |
| 234 | |
| 235 | bool DataChannel::Send(const DataBuffer& buffer) { |
| 236 | if (state_ != kOpen) { |
| 237 | return false; |
| 238 | } |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 239 | |
| 240 | // TODO(jiayl): the spec is unclear about if the remote side should get the |
| 241 | // onmessage event. We need to figure out the expected behavior and change the |
| 242 | // code accordingly. |
| 243 | if (buffer.size() == 0) { |
| 244 | return true; |
| 245 | } |
| 246 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 247 | // If the queue is non-empty, we're waiting for SignalReadyToSend, |
| 248 | // so just add to the end of the queue and keep waiting. |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 249 | if (!queued_send_data_.Empty()) { |
| 250 | // Only SCTP DataChannel queues the outgoing data when the transport is |
| 251 | // blocked. |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 252 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 253 | if (!QueueSendDataMessage(buffer)) { |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 254 | Close(); |
| 255 | } |
| 256 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 257 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 258 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 259 | bool success = SendDataMessage(buffer, true); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 260 | if (data_channel_type_ == cricket::DCT_RTP) { |
| 261 | return success; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 262 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 263 | |
| 264 | // Always return true for SCTP DataChannel per the spec. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 265 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 268 | void DataChannel::SetReceiveSsrc(uint32_t receive_ssrc) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 269 | RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 270 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 271 | if (receive_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | receive_ssrc_ = receive_ssrc; |
| 275 | receive_ssrc_set_ = true; |
| 276 | UpdateState(); |
| 277 | } |
| 278 | |
| 279 | // The remote peer request that this channel shall be closed. |
| 280 | void DataChannel::RemotePeerRequestClose() { |
| 281 | DoClose(); |
| 282 | } |
| 283 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 284 | void DataChannel::SetSctpSid(int sid) { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 285 | RTC_DCHECK_LT(config_.id, 0); |
| 286 | RTC_DCHECK_GE(sid, 0); |
| 287 | RTC_DCHECK_EQ(data_channel_type_, cricket::DCT_SCTP); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 288 | if (config_.id == sid) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 289 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 290 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 291 | |
| 292 | config_.id = sid; |
| 293 | provider_->AddSctpDataStream(sid); |
| 294 | } |
| 295 | |
| 296 | void DataChannel::OnTransportChannelCreated() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 297 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 298 | if (!connected_to_provider_) { |
| 299 | connected_to_provider_ = provider_->ConnectDataChannel(this); |
| 300 | } |
| 301 | // The sid may have been unassigned when provider_->ConnectDataChannel was |
| 302 | // done. So always add the streams even if connected_to_provider_ is true. |
| 303 | if (config_.id >= 0) { |
| 304 | provider_->AddSctpDataStream(config_.id); |
| 305 | } |
| 306 | } |
| 307 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 308 | void DataChannel::OnTransportChannelDestroyed() { |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 309 | // This method needs to synchronously close the data channel, which means any |
| 310 | // queued data needs to be discarded. |
| 311 | queued_send_data_.Clear(); |
| 312 | queued_control_data_.Clear(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 313 | DoClose(); |
| 314 | } |
| 315 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 316 | void DataChannel::SetSendSsrc(uint32_t send_ssrc) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 317 | RTC_DCHECK(data_channel_type_ == cricket::DCT_RTP); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 318 | if (send_ssrc_set_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | send_ssrc_ = send_ssrc; |
| 322 | send_ssrc_set_ = true; |
| 323 | UpdateState(); |
| 324 | } |
| 325 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 326 | void DataChannel::OnMessage(rtc::Message* msg) { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 327 | switch (msg->message_id) { |
| 328 | case MSG_CHANNELREADY: |
| 329 | OnChannelReady(true); |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 334 | void DataChannel::OnDataReceived(const cricket::ReceiveDataParams& params, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 335 | const rtc::CopyOnWriteBuffer& payload) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 336 | if (data_channel_type_ == cricket::DCT_RTP && params.ssrc != receive_ssrc_) { |
| 337 | return; |
| 338 | } |
| 339 | if (data_channel_type_ == cricket::DCT_SCTP && params.sid != config_.id) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 343 | if (params.type == cricket::DMT_CONTROL) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 344 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 345 | if (handshake_state_ != kHandshakeWaitingForAck) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 346 | // Ignore it if we are not expecting an ACK message. |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 347 | RTC_LOG(LS_WARNING) |
| 348 | << "DataChannel received unexpected CONTROL message, sid = " |
| 349 | << params.sid; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | if (ParseDataChannelOpenAckMessage(payload)) { |
| 353 | // We can send unordered as soon as we receive the ACK message. |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 354 | handshake_state_ = kHandshakeReady; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 355 | RTC_LOG(LS_INFO) << "DataChannel received OPEN_ACK message, sid = " |
| 356 | << params.sid; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 357 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 358 | RTC_LOG(LS_WARNING) |
| 359 | << "DataChannel failed to parse OPEN_ACK message, sid = " |
| 360 | << params.sid; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 361 | } |
| 362 | return; |
| 363 | } |
| 364 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 365 | RTC_DCHECK(params.type == cricket::DMT_BINARY || |
| 366 | params.type == cricket::DMT_TEXT); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 367 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 368 | RTC_LOG(LS_VERBOSE) << "DataChannel received DATA message, sid = " |
| 369 | << params.sid; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 370 | // We can send unordered as soon as we receive any DATA message since the |
| 371 | // remote side must have received the OPEN (and old clients do not send |
| 372 | // OPEN_ACK). |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 373 | if (handshake_state_ == kHandshakeWaitingForAck) { |
| 374 | handshake_state_ = kHandshakeReady; |
| 375 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 376 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 377 | bool binary = (params.type == cricket::DMT_BINARY); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 378 | std::unique_ptr<DataBuffer> buffer(new DataBuffer(payload, binary)); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 379 | if (state_ == kOpen && observer_) { |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 380 | ++messages_received_; |
| 381 | bytes_received_ += buffer->size(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 382 | observer_->OnMessage(*buffer.get()); |
| 383 | } else { |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 384 | if (queued_received_data_.byte_count() + payload.size() > |
| 385 | kMaxQueuedReceivedDataBytes) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 386 | RTC_LOG(LS_ERROR) << "Queued received data exceeds the max buffer size."; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 387 | |
| 388 | queued_received_data_.Clear(); |
| 389 | if (data_channel_type_ != cricket::DCT_RTP) { |
| 390 | Close(); |
| 391 | } |
| 392 | |
| 393 | return; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 394 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 395 | queued_received_data_.Push(buffer.release()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 399 | void DataChannel::OnStreamClosedRemotely(int sid) { |
| 400 | if (data_channel_type_ == cricket::DCT_SCTP && sid == config_.id) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 401 | Close(); |
| 402 | } |
| 403 | } |
| 404 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 405 | void DataChannel::OnChannelReady(bool writable) { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 406 | writable_ = writable; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 407 | if (!writable) { |
| 408 | return; |
| 409 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 410 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 411 | SendQueuedControlMessages(); |
| 412 | SendQueuedDataMessages(); |
| 413 | UpdateState(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 414 | } |
| 415 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 416 | void DataChannel::DoClose() { |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 417 | if (state_ == kClosed) |
| 418 | return; |
| 419 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 420 | receive_ssrc_set_ = false; |
| 421 | send_ssrc_set_ = false; |
| 422 | SetState(kClosing); |
| 423 | UpdateState(); |
| 424 | } |
| 425 | |
| 426 | void DataChannel::UpdateState() { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 427 | // UpdateState determines what to do from a few state variables. Include |
| 428 | // all conditions required for each state transition here for |
| 429 | // clarity. OnChannelReady(true) will send any queued data and then invoke |
| 430 | // UpdateState(). |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 431 | switch (state_) { |
| 432 | case kConnecting: { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 433 | if (send_ssrc_set_ == receive_ssrc_set_) { |
| 434 | if (data_channel_type_ == cricket::DCT_RTP && !connected_to_provider_) { |
| 435 | connected_to_provider_ = provider_->ConnectDataChannel(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 436 | } |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 437 | if (connected_to_provider_) { |
| 438 | if (handshake_state_ == kHandshakeShouldSendOpen) { |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 439 | rtc::CopyOnWriteBuffer payload; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 440 | WriteDataChannelOpenMessage(label_, config_, &payload); |
| 441 | SendControlMessage(payload); |
| 442 | } else if (handshake_state_ == kHandshakeShouldSendAck) { |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 443 | rtc::CopyOnWriteBuffer payload; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 444 | WriteDataChannelOpenAckMessage(&payload); |
| 445 | SendControlMessage(payload); |
| 446 | } |
| 447 | if (writable_ && |
| 448 | (handshake_state_ == kHandshakeReady || |
| 449 | handshake_state_ == kHandshakeWaitingForAck)) { |
| 450 | SetState(kOpen); |
| 451 | // If we have received buffers before the channel got writable. |
| 452 | // Deliver them now. |
| 453 | DeliverQueuedReceivedData(); |
| 454 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | break; |
| 458 | } |
| 459 | case kOpen: { |
| 460 | break; |
| 461 | } |
| 462 | case kClosing: { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 463 | if (queued_send_data_.Empty() && queued_control_data_.Empty()) { |
| 464 | if (connected_to_provider_) { |
| 465 | DisconnectFromProvider(); |
| 466 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 467 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 468 | if (!connected_to_provider_ && !send_ssrc_set_ && !receive_ssrc_set_) { |
| 469 | SetState(kClosed); |
| 470 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 471 | } |
| 472 | break; |
| 473 | } |
| 474 | case kClosed: |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | void DataChannel::SetState(DataState state) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 480 | if (state_ == state) { |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 481 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 482 | } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 483 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 484 | state_ = state; |
| 485 | if (observer_) { |
| 486 | observer_->OnStateChange(); |
| 487 | } |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 488 | if (state_ == kOpen) { |
| 489 | SignalOpened(this); |
| 490 | } else if (state_ == kClosed) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 491 | SignalClosed(this); |
| 492 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 495 | void DataChannel::DisconnectFromProvider() { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 496 | if (!connected_to_provider_) |
| 497 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 498 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 499 | provider_->DisconnectDataChannel(this); |
| 500 | connected_to_provider_ = false; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 501 | |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 502 | if (data_channel_type_ == cricket::DCT_SCTP && config_.id >= 0) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 503 | provider_->RemoveSctpDataStream(config_.id); |
| 504 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 505 | } |
| 506 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 507 | void DataChannel::DeliverQueuedReceivedData() { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 508 | if (!observer_) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 509 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 510 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 511 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 512 | while (!queued_received_data_.Empty()) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 513 | std::unique_ptr<DataBuffer> buffer(queued_received_data_.Front()); |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 514 | ++messages_received_; |
| 515 | bytes_received_ += buffer->size(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 516 | observer_->OnMessage(*buffer); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 517 | queued_received_data_.Pop(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 521 | void DataChannel::SendQueuedDataMessages() { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 522 | if (queued_send_data_.Empty()) { |
| 523 | return; |
| 524 | } |
| 525 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 526 | RTC_DCHECK(state_ == kOpen || state_ == kClosing); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 527 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 528 | uint64_t start_buffered_amount = buffered_amount(); |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 529 | while (!queued_send_data_.Empty()) { |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 530 | DataBuffer* buffer = queued_send_data_.Front(); |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 531 | if (!SendDataMessage(*buffer, false)) { |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 532 | // Leave the message in the queue if sending is aborted. |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 533 | break; |
| 534 | } |
| 535 | queued_send_data_.Pop(); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 536 | delete buffer; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 537 | } |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 538 | |
| 539 | if (observer_ && buffered_amount() < start_buffered_amount) { |
| 540 | observer_->OnBufferedAmountChange(start_buffered_amount); |
| 541 | } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 542 | } |
| 543 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 544 | bool DataChannel::SendDataMessage(const DataBuffer& buffer, |
| 545 | bool queue_if_blocked) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 546 | cricket::SendDataParams send_params; |
| 547 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 548 | if (data_channel_type_ == cricket::DCT_SCTP) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 549 | send_params.ordered = config_.ordered; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 550 | // Send as ordered if it is still going through OPEN/ACK signaling. |
| 551 | if (handshake_state_ != kHandshakeReady && !config_.ordered) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 552 | send_params.ordered = true; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 553 | RTC_LOG(LS_VERBOSE) |
| 554 | << "Sending data as ordered for unordered DataChannel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 555 | "because the OPEN_ACK message has not been received."; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 556 | } |
| 557 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 558 | send_params.max_rtx_count = config_.maxRetransmits; |
| 559 | send_params.max_rtx_ms = config_.maxRetransmitTime; |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 560 | send_params.sid = config_.id; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 561 | } else { |
| 562 | send_params.ssrc = send_ssrc_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 563 | } |
| 564 | send_params.type = buffer.binary ? cricket::DMT_BINARY : cricket::DMT_TEXT; |
| 565 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 566 | cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
| 567 | bool success = provider_->SendData(send_params, buffer.data, &send_result); |
| 568 | |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 569 | if (success) { |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 570 | ++messages_sent_; |
| 571 | bytes_sent_ += buffer.size(); |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 572 | return true; |
| 573 | } |
| 574 | |
| 575 | if (data_channel_type_ != cricket::DCT_SCTP) { |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | if (send_result == cricket::SDR_BLOCK) { |
| 580 | if (!queue_if_blocked || QueueSendDataMessage(buffer)) { |
| 581 | return false; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 584 | // Close the channel if the error is not SDR_BLOCK, or if queuing the |
| 585 | // message failed. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 586 | RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send data, " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 587 | "send_result = " |
| 588 | << send_result; |
jiayl@webrtc.org | 6ca6190 | 2014-11-12 17:28:40 +0000 | [diff] [blame] | 589 | Close(); |
| 590 | |
| 591 | return false; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 592 | } |
| 593 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 594 | bool DataChannel::QueueSendDataMessage(const DataBuffer& buffer) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 595 | size_t start_buffered_amount = buffered_amount(); |
| 596 | if (start_buffered_amount >= kMaxQueuedSendDataBytes) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 597 | RTC_LOG(LS_ERROR) << "Can't buffer any more data for the data channel."; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 598 | return false; |
| 599 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 600 | queued_send_data_.Push(new DataBuffer(buffer)); |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 601 | |
| 602 | // The buffer can have length zero, in which case there is no change. |
| 603 | if (observer_ && buffered_amount() > start_buffered_amount) { |
| 604 | observer_->OnBufferedAmountChange(start_buffered_amount); |
| 605 | } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 606 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 607 | } |
| 608 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 609 | void DataChannel::SendQueuedControlMessages() { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 610 | PacketQueue control_packets; |
| 611 | control_packets.Swap(&queued_control_data_); |
| 612 | |
| 613 | while (!control_packets.Empty()) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 614 | std::unique_ptr<DataBuffer> buf(control_packets.Front()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 615 | SendControlMessage(buf->data); |
| 616 | control_packets.Pop(); |
| 617 | } |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 618 | } |
| 619 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 620 | void DataChannel::QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 621 | queued_control_data_.Push(new DataBuffer(buffer, true)); |
| 622 | } |
| 623 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 624 | bool DataChannel::SendControlMessage(const rtc::CopyOnWriteBuffer& buffer) { |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 625 | bool is_open_message = handshake_state_ == kHandshakeShouldSendOpen; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 626 | |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 627 | RTC_DCHECK_EQ(data_channel_type_, cricket::DCT_SCTP); |
| 628 | RTC_DCHECK(writable_); |
| 629 | RTC_DCHECK_GE(config_.id, 0); |
| 630 | RTC_DCHECK(!is_open_message || !config_.negotiated); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 631 | |
| 632 | cricket::SendDataParams send_params; |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 633 | send_params.sid = config_.id; |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 634 | // Send data as ordered before we receive any message from the remote peer to |
| 635 | // make sure the remote peer will not receive any data before it receives the |
| 636 | // OPEN message. |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 637 | send_params.ordered = config_.ordered || is_open_message; |
| 638 | send_params.type = cricket::DMT_CONTROL; |
| 639 | |
| 640 | cricket::SendDataResult send_result = cricket::SDR_SUCCESS; |
| 641 | bool retval = provider_->SendData(send_params, buffer, &send_result); |
| 642 | if (retval) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 643 | RTC_LOG(LS_INFO) << "Sent CONTROL message on channel " << config_.id; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 644 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 645 | if (handshake_state_ == kHandshakeShouldSendAck) { |
| 646 | handshake_state_ = kHandshakeReady; |
| 647 | } else if (handshake_state_ == kHandshakeShouldSendOpen) { |
| 648 | handshake_state_ = kHandshakeWaitingForAck; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 649 | } |
| 650 | } else if (send_result == cricket::SDR_BLOCK) { |
| 651 | QueueControlMessage(buffer); |
| 652 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 653 | RTC_LOG(LS_ERROR) << "Closing the DataChannel due to a failure to send" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 654 | " the CONTROL message, send_result = " |
| 655 | << send_result; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 656 | Close(); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 657 | } |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 658 | return retval; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 659 | } |
| 660 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 661 | } // namespace webrtc |