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