OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 enum BindState { STATE_UNBOUND, STATE_BINDING, STATE_BOUND }; | 133 enum BindState { STATE_UNBOUND, STATE_BINDING, STATE_BOUND }; |
134 TurnEntry(TurnPort* port, int channel_id, | 134 TurnEntry(TurnPort* port, int channel_id, |
135 const rtc::SocketAddress& ext_addr); | 135 const rtc::SocketAddress& ext_addr); |
136 | 136 |
137 TurnPort* port() { return port_; } | 137 TurnPort* port() { return port_; } |
138 | 138 |
139 int channel_id() const { return channel_id_; } | 139 int channel_id() const { return channel_id_; } |
140 const rtc::SocketAddress& address() const { return ext_addr_; } | 140 const rtc::SocketAddress& address() const { return ext_addr_; } |
141 BindState state() const { return state_; } | 141 BindState state() const { return state_; } |
142 | 142 |
143 uint32_t delayed_destroy_msg_id() { return delayed_destroy_msg_id_; } | |
144 void set_delayed_destroy_msg_id(uint32_t msg_id) { | |
145 delayed_destroy_msg_id_ = msg_id; | |
146 } | |
pthatcher1
2015/11/12 00:40:20
As we discussed, let's try and make this scheduled
honghaiz3
2015/11/12 02:24:04
When I tried to implement that, I found it became
pthatcher1
2015/11/13 01:59:15
I think we can get two birds with one stone here.
honghaiz3
2015/11/13 22:44:15
Revised as we discussed. We can avoid checking the
| |
147 | |
143 // Helper methods to send permission and channel bind requests. | 148 // Helper methods to send permission and channel bind requests. |
144 void SendCreatePermissionRequest(); | 149 void SendCreatePermissionRequest(); |
145 void SendChannelBindRequest(int delay); | 150 void SendChannelBindRequest(int delay); |
146 // Sends a packet to the given destination address. | 151 // Sends a packet to the given destination address. |
147 // This will wrap the packet in STUN if necessary. | 152 // This will wrap the packet in STUN if necessary. |
148 int Send(const void* data, size_t size, bool payload, | 153 int Send(const void* data, size_t size, bool payload, |
149 const rtc::PacketOptions& options); | 154 const rtc::PacketOptions& options); |
150 | 155 |
151 void OnCreatePermissionSuccess(); | 156 void OnCreatePermissionSuccess(); |
152 void OnCreatePermissionError(StunMessage* response, int code); | 157 void OnCreatePermissionError(StunMessage* response, int code); |
153 void OnChannelBindSuccess(); | 158 void OnChannelBindSuccess(); |
154 void OnChannelBindError(StunMessage* response, int code); | 159 void OnChannelBindError(StunMessage* response, int code); |
155 // Signal sent when TurnEntry is destroyed. | 160 // Signal sent when TurnEntry is destroyed. |
156 sigslot::signal1<TurnEntry*> SignalDestroyed; | 161 sigslot::signal1<TurnEntry*> SignalDestroyed; |
157 | 162 |
158 private: | 163 private: |
159 TurnPort* port_; | 164 TurnPort* port_; |
160 int channel_id_; | 165 int channel_id_; |
161 rtc::SocketAddress ext_addr_; | 166 rtc::SocketAddress ext_addr_; |
162 BindState state_; | 167 BindState state_; |
168 // A non-zero value indicates that this entry is scheduled to be destroyed | |
169 // with the following message ID, which can be used to cancel the scheduled | |
170 // destruction. | |
171 uint32_t delayed_destroy_msg_id_ = 0; | |
163 }; | 172 }; |
164 | 173 |
165 TurnPort::TurnPort(rtc::Thread* thread, | 174 TurnPort::TurnPort(rtc::Thread* thread, |
166 rtc::PacketSocketFactory* factory, | 175 rtc::PacketSocketFactory* factory, |
167 rtc::Network* network, | 176 rtc::Network* network, |
168 rtc::AsyncPacketSocket* socket, | 177 rtc::AsyncPacketSocket* socket, |
169 const std::string& username, | 178 const std::string& username, |
170 const std::string& password, | 179 const std::string& password, |
171 const ProtocolAddress& server_address, | 180 const ProtocolAddress& server_address, |
172 const RelayCredentials& credentials, | 181 const RelayCredentials& credentials, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 | 241 |
233 // release the allocation by sending a refresh with | 242 // release the allocation by sending a refresh with |
234 // lifetime 0. | 243 // lifetime 0. |
235 if (ready()) { | 244 if (ready()) { |
236 TurnRefreshRequest bye(this); | 245 TurnRefreshRequest bye(this); |
237 bye.set_lifetime(0); | 246 bye.set_lifetime(0); |
238 SendRequest(&bye, 0); | 247 SendRequest(&bye, 0); |
239 } | 248 } |
240 | 249 |
241 while (!entries_.empty()) { | 250 while (!entries_.empty()) { |
242 DestroyEntry(entries_.front()->address()); | 251 DestroyEntry(entries_.front()); |
243 } | 252 } |
244 if (resolver_) { | 253 if (resolver_) { |
245 resolver_->Destroy(false); | 254 resolver_->Destroy(false); |
246 } | 255 } |
247 if (!SharedSocket()) { | 256 if (!SharedSocket()) { |
248 delete socket_; | 257 delete socket_; |
249 } | 258 } |
250 } | 259 } |
251 | 260 |
252 rtc::SocketAddress TurnPort::GetLocalAddress() const { | 261 rtc::SocketAddress TurnPort::GetLocalAddress() const { |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 | 440 |
432 if (!IsCompatibleAddress(address.address())) { | 441 if (!IsCompatibleAddress(address.address())) { |
433 return NULL; | 442 return NULL; |
434 } | 443 } |
435 | 444 |
436 if (state_ == STATE_DISCONNECTED) { | 445 if (state_ == STATE_DISCONNECTED) { |
437 return NULL; | 446 return NULL; |
438 } | 447 } |
439 | 448 |
440 // Create an entry, if needed, so we can get our permissions set up correctly. | 449 // Create an entry, if needed, so we can get our permissions set up correctly. |
441 CreateEntry(address.address()); | 450 GetOrCreateEntry(address.address()); |
442 | 451 |
443 // A TURN port will have two candiates, STUN and TURN. STUN may not | 452 // A TURN port will have two candiates, STUN and TURN. STUN may not |
444 // present in all cases. If present stun candidate will be added first | 453 // present in all cases. If present stun candidate will be added first |
445 // and TURN candidate later. | 454 // and TURN candidate later. |
446 for (size_t index = 0; index < Candidates().size(); ++index) { | 455 for (size_t index = 0; index < Candidates().size(); ++index) { |
447 if (Candidates()[index].type() == RELAY_PORT_TYPE) { | 456 if (Candidates()[index].type() == RELAY_PORT_TYPE) { |
448 ProxyConnection* conn = new ProxyConnection(this, index, address); | 457 ProxyConnection* conn = new ProxyConnection(this, index, address); |
449 conn->SignalDestroyed.connect(this, &TurnPort::OnConnectionDestroyed); | 458 conn->SignalDestroyed.connect(this, &TurnPort::OnConnectionDestroyed); |
450 AddConnection(conn); | 459 AddConnection(conn); |
451 return conn; | 460 return conn; |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
706 // with the alternate server. PrepareAddress will send stun binding once | 715 // with the alternate server. PrepareAddress will send stun binding once |
707 // the new socket is connected. | 716 // the new socket is connected. |
708 ASSERT(server_address().proto == PROTO_TCP); | 717 ASSERT(server_address().proto == PROTO_TCP); |
709 ASSERT(!SharedSocket()); | 718 ASSERT(!SharedSocket()); |
710 delete socket_; | 719 delete socket_; |
711 socket_ = NULL; | 720 socket_ = NULL; |
712 PrepareAddress(); | 721 PrepareAddress(); |
713 } | 722 } |
714 return; | 723 return; |
715 } | 724 } |
716 | |
717 Port::OnMessage(message); | 725 Port::OnMessage(message); |
718 } | 726 } |
719 | 727 |
720 void TurnPort::OnAllocateRequestTimeout() { | 728 void TurnPort::OnAllocateRequestTimeout() { |
721 OnAllocateError(); | 729 OnAllocateError(); |
722 } | 730 } |
723 | 731 |
724 void TurnPort::HandleDataIndication(const char* data, size_t size, | 732 void TurnPort::HandleDataIndication(const char* data, size_t size, |
725 const rtc::PacketTime& packet_time) { | 733 const rtc::PacketTime& packet_time) { |
726 // Read in the message, and process according to RFC5766, Section 10.4. | 734 // Read in the message, and process according to RFC5766, Section 10.4. |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
891 | 899 |
892 static bool MatchesChannelId(TurnEntry* e, int id) { | 900 static bool MatchesChannelId(TurnEntry* e, int id) { |
893 return e->channel_id() == id; | 901 return e->channel_id() == id; |
894 } | 902 } |
895 TurnEntry* TurnPort::FindEntry(int channel_id) const { | 903 TurnEntry* TurnPort::FindEntry(int channel_id) const { |
896 EntryList::const_iterator it = std::find_if(entries_.begin(), entries_.end(), | 904 EntryList::const_iterator it = std::find_if(entries_.begin(), entries_.end(), |
897 std::bind2nd(std::ptr_fun(MatchesChannelId), channel_id)); | 905 std::bind2nd(std::ptr_fun(MatchesChannelId), channel_id)); |
898 return (it != entries_.end()) ? *it : NULL; | 906 return (it != entries_.end()) ? *it : NULL; |
899 } | 907 } |
900 | 908 |
901 TurnEntry* TurnPort::CreateEntry(const rtc::SocketAddress& addr) { | 909 TurnEntry* TurnPort::GetOrCreateEntry(const rtc::SocketAddress& addr) { |
902 ASSERT(FindEntry(addr) == NULL); | 910 TurnEntry* entry = FindEntry(addr); |
903 TurnEntry* entry = new TurnEntry(this, next_channel_number_++, addr); | 911 if (entry == nullptr) { |
904 entries_.push_back(entry); | 912 entry = new TurnEntry(this, next_channel_number_++, addr); |
913 entries_.push_back(entry); | |
914 } else { | |
915 CancelEntryDestruction(entry); | |
916 } | |
905 return entry; | 917 return entry; |
906 } | 918 } |
907 | 919 |
908 void TurnPort::DestroyEntry(const rtc::SocketAddress& addr) { | 920 void TurnPort::DestroyEntry(TurnEntry* entry) { |
909 TurnEntry* entry = FindEntry(addr); | |
910 ASSERT(entry != NULL); | 921 ASSERT(entry != NULL); |
911 entry->SignalDestroyed(entry); | 922 entry->SignalDestroyed(entry); |
912 entries_.remove(entry); | 923 entries_.remove(entry); |
913 delete entry; | 924 delete entry; |
914 } | 925 } |
915 | 926 |
916 void TurnPort::OnConnectionDestroyed(Connection* conn) { | 927 void TurnPort::OnConnectionDestroyed(Connection* conn) { |
917 // Destroying TurnEntry for the connection, which is already destroyed. | 928 // Destroying TurnEntry for the connection, which is already destroyed. |
918 DestroyEntry(conn->remote_candidate().address()); | 929 rtc::SocketAddress remote_address = conn->remote_candidate().address(); |
930 TurnEntry* entry = FindEntry(remote_address); | |
931 ASSERT(entry != NULL); | |
932 ScheduleEntryDestruction(entry); | |
933 } | |
934 | |
935 void TurnPort::ScheduleEntryDestruction(TurnEntry* entry) { | |
936 ASSERT(entry->delayed_destroy_msg_id() == 0); | |
937 ++last_destroy_entry_msg_id_; | |
938 entry->set_delayed_destroy_msg_id(last_destroy_entry_msg_id_); | |
939 invoker_.AsyncInvokeDelayed<void>( | |
940 thread(), rtc::Bind(&TurnPort::DestroyEntry, this, entry), | |
941 TURN_PERMISSION_TIMEOUT, last_destroy_entry_msg_id_); | |
942 } | |
943 | |
944 void TurnPort::CancelEntryDestruction(TurnEntry* entry) { | |
945 ASSERT(entry->delayed_destroy_msg_id() != 0); | |
946 thread()->Clear(&invoker_, entry->delayed_destroy_msg_id()); | |
947 entry->set_delayed_destroy_msg_id(0); | |
919 } | 948 } |
920 | 949 |
921 TurnAllocateRequest::TurnAllocateRequest(TurnPort* port) | 950 TurnAllocateRequest::TurnAllocateRequest(TurnPort* port) |
922 : StunRequest(new TurnMessage()), | 951 : StunRequest(new TurnMessage()), |
923 port_(port) { | 952 port_(port) { |
924 } | 953 } |
925 | 954 |
926 void TurnAllocateRequest::Prepare(StunMessage* request) { | 955 void TurnAllocateRequest::Prepare(StunMessage* request) { |
927 // Create the request as indicated in RFC 5766, Section 6.1. | 956 // Create the request as indicated in RFC 5766, Section 6.1. |
928 request->SetType(TURN_ALLOCATE_REQUEST); | 957 request->SetType(TURN_ALLOCATE_REQUEST); |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1362 // bind request as per http://tools.ietf.org/html/rfc5766#section-11.3 | 1391 // bind request as per http://tools.ietf.org/html/rfc5766#section-11.3 |
1363 if (code == STUN_ERROR_STALE_NONCE) { | 1392 if (code == STUN_ERROR_STALE_NONCE) { |
1364 if (port_->UpdateNonce(response)) { | 1393 if (port_->UpdateNonce(response)) { |
1365 // Send channel bind request with fresh nonce. | 1394 // Send channel bind request with fresh nonce. |
1366 SendChannelBindRequest(0); | 1395 SendChannelBindRequest(0); |
1367 } | 1396 } |
1368 } | 1397 } |
1369 } | 1398 } |
1370 | 1399 |
1371 } // namespace cricket | 1400 } // namespace cricket |
OLD | NEW |