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 msg_id() { return msg_id_; } | |
144 void set_msg_id(uint32_t msg_id) { msg_id_ = msg_id; } | |
145 | |
143 // Helper methods to send permission and channel bind requests. | 146 // Helper methods to send permission and channel bind requests. |
144 void SendCreatePermissionRequest(); | 147 void SendCreatePermissionRequest(); |
145 void SendChannelBindRequest(int delay); | 148 void SendChannelBindRequest(int delay); |
146 // Sends a packet to the given destination address. | 149 // Sends a packet to the given destination address. |
147 // This will wrap the packet in STUN if necessary. | 150 // This will wrap the packet in STUN if necessary. |
148 int Send(const void* data, size_t size, bool payload, | 151 int Send(const void* data, size_t size, bool payload, |
149 const rtc::PacketOptions& options); | 152 const rtc::PacketOptions& options); |
150 | 153 |
151 void OnCreatePermissionSuccess(); | 154 void OnCreatePermissionSuccess(); |
152 void OnCreatePermissionError(StunMessage* response, int code); | 155 void OnCreatePermissionError(StunMessage* response, int code); |
153 void OnChannelBindSuccess(); | 156 void OnChannelBindSuccess(); |
154 void OnChannelBindError(StunMessage* response, int code); | 157 void OnChannelBindError(StunMessage* response, int code); |
155 // Signal sent when TurnEntry is destroyed. | 158 // Signal sent when TurnEntry is destroyed. |
156 sigslot::signal1<TurnEntry*> SignalDestroyed; | 159 sigslot::signal1<TurnEntry*> SignalDestroyed; |
157 | 160 |
158 private: | 161 private: |
159 TurnPort* port_; | 162 TurnPort* port_; |
160 int channel_id_; | 163 int channel_id_; |
161 rtc::SocketAddress ext_addr_; | 164 rtc::SocketAddress ext_addr_; |
162 BindState state_; | 165 BindState state_; |
166 // If this is not zero, there is a delayed message that will destroy the | |
167 // TurnEntry. |msg_id_| is the message id, used to cancel the message. | |
168 uint32_t msg_id_ = 0; | |
pthatcher1
2015/11/11 01:08:41
I think this would be more clear as delayed_destro
honghaiz3
2015/11/11 23:02:55
If we use the map, the map would have to be map<Tu
| |
163 }; | 169 }; |
164 | 170 |
165 TurnPort::TurnPort(rtc::Thread* thread, | 171 TurnPort::TurnPort(rtc::Thread* thread, |
166 rtc::PacketSocketFactory* factory, | 172 rtc::PacketSocketFactory* factory, |
167 rtc::Network* network, | 173 rtc::Network* network, |
168 rtc::AsyncPacketSocket* socket, | 174 rtc::AsyncPacketSocket* socket, |
169 const std::string& username, | 175 const std::string& username, |
170 const std::string& password, | 176 const std::string& password, |
171 const ProtocolAddress& server_address, | 177 const ProtocolAddress& server_address, |
172 const RelayCredentials& credentials, | 178 const RelayCredentials& credentials, |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 | 437 |
432 if (!IsCompatibleAddress(address.address())) { | 438 if (!IsCompatibleAddress(address.address())) { |
433 return NULL; | 439 return NULL; |
434 } | 440 } |
435 | 441 |
436 if (state_ == STATE_DISCONNECTED) { | 442 if (state_ == STATE_DISCONNECTED) { |
437 return NULL; | 443 return NULL; |
438 } | 444 } |
439 | 445 |
440 // Create an entry, if needed, so we can get our permissions set up correctly. | 446 // Create an entry, if needed, so we can get our permissions set up correctly. |
441 CreateEntry(address.address()); | 447 CreateEntryIfNeeded(address.address()); |
442 | 448 |
443 // A TURN port will have two candiates, STUN and TURN. STUN may not | 449 // 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 | 450 // present in all cases. If present stun candidate will be added first |
445 // and TURN candidate later. | 451 // and TURN candidate later. |
446 for (size_t index = 0; index < Candidates().size(); ++index) { | 452 for (size_t index = 0; index < Candidates().size(); ++index) { |
447 if (Candidates()[index].type() == RELAY_PORT_TYPE) { | 453 if (Candidates()[index].type() == RELAY_PORT_TYPE) { |
448 ProxyConnection* conn = new ProxyConnection(this, index, address); | 454 ProxyConnection* conn = new ProxyConnection(this, index, address); |
449 conn->SignalDestroyed.connect(this, &TurnPort::OnConnectionDestroyed); | 455 conn->SignalDestroyed.connect(this, &TurnPort::OnConnectionDestroyed); |
450 AddConnection(conn); | 456 AddConnection(conn); |
451 return conn; | 457 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 | 712 // with the alternate server. PrepareAddress will send stun binding once |
707 // the new socket is connected. | 713 // the new socket is connected. |
708 ASSERT(server_address().proto == PROTO_TCP); | 714 ASSERT(server_address().proto == PROTO_TCP); |
709 ASSERT(!SharedSocket()); | 715 ASSERT(!SharedSocket()); |
710 delete socket_; | 716 delete socket_; |
711 socket_ = NULL; | 717 socket_ = NULL; |
712 PrepareAddress(); | 718 PrepareAddress(); |
713 } | 719 } |
714 return; | 720 return; |
715 } | 721 } |
716 | |
717 Port::OnMessage(message); | 722 Port::OnMessage(message); |
718 } | 723 } |
719 | 724 |
720 void TurnPort::OnAllocateRequestTimeout() { | 725 void TurnPort::OnAllocateRequestTimeout() { |
721 OnAllocateError(); | 726 OnAllocateError(); |
722 } | 727 } |
723 | 728 |
724 void TurnPort::HandleDataIndication(const char* data, size_t size, | 729 void TurnPort::HandleDataIndication(const char* data, size_t size, |
725 const rtc::PacketTime& packet_time) { | 730 const rtc::PacketTime& packet_time) { |
726 // Read in the message, and process according to RFC5766, Section 10.4. | 731 // 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 | 896 |
892 static bool MatchesChannelId(TurnEntry* e, int id) { | 897 static bool MatchesChannelId(TurnEntry* e, int id) { |
893 return e->channel_id() == id; | 898 return e->channel_id() == id; |
894 } | 899 } |
895 TurnEntry* TurnPort::FindEntry(int channel_id) const { | 900 TurnEntry* TurnPort::FindEntry(int channel_id) const { |
896 EntryList::const_iterator it = std::find_if(entries_.begin(), entries_.end(), | 901 EntryList::const_iterator it = std::find_if(entries_.begin(), entries_.end(), |
897 std::bind2nd(std::ptr_fun(MatchesChannelId), channel_id)); | 902 std::bind2nd(std::ptr_fun(MatchesChannelId), channel_id)); |
898 return (it != entries_.end()) ? *it : NULL; | 903 return (it != entries_.end()) ? *it : NULL; |
899 } | 904 } |
900 | 905 |
901 TurnEntry* TurnPort::CreateEntry(const rtc::SocketAddress& addr) { | 906 TurnEntry* TurnPort::CreateEntryIfNeeded(const rtc::SocketAddress& addr) { |
pthatcher1
2015/11/11 01:08:41
I think it would make sense to call this GetOrCrea
honghaiz3
2015/11/11 23:02:55
Done.
| |
902 ASSERT(FindEntry(addr) == NULL); | 907 TurnEntry* entry = FindEntry(addr); |
903 TurnEntry* entry = new TurnEntry(this, next_channel_number_++, addr); | 908 if (entry == nullptr) { |
904 entries_.push_back(entry); | 909 entry = new TurnEntry(this, next_channel_number_++, addr); |
910 entries_.push_back(entry); | |
911 } else { | |
912 ASSERT(entry->msg_id() != 0); | |
913 thread()->Clear(&invoker_, entry->msg_id()); | |
pthatcher1
2015/11/11 01:08:41
I feel like this logic should be in its own method
honghaiz3
2015/11/11 23:02:55
Done.
| |
914 entry->set_msg_id(0); | |
915 } | |
905 return entry; | 916 return entry; |
906 } | 917 } |
907 | 918 |
908 void TurnPort::DestroyEntry(const rtc::SocketAddress& addr) { | 919 void TurnPort::DestroyEntry(const rtc::SocketAddress& addr) { |
909 TurnEntry* entry = FindEntry(addr); | 920 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 && entry->msg_id() == 0); | |
932 ++last_msg_id_; | |
933 entry->set_msg_id(last_msg_id_); | |
934 invoker_.AsyncInvokeDelayed<void>( | |
935 thread(), rtc::Bind(&TurnPort::DestroyEntry, this, remote_address), | |
936 TURN_PERMISSION_TIMEOUT, last_msg_id_); | |
pthatcher1
2015/11/11 01:08:41
I think this is where a method called "ScheduleEnt
honghaiz3
2015/11/11 23:02:55
Done.
| |
919 } | 937 } |
920 | 938 |
921 TurnAllocateRequest::TurnAllocateRequest(TurnPort* port) | 939 TurnAllocateRequest::TurnAllocateRequest(TurnPort* port) |
922 : StunRequest(new TurnMessage()), | 940 : StunRequest(new TurnMessage()), |
923 port_(port) { | 941 port_(port) { |
924 } | 942 } |
925 | 943 |
926 void TurnAllocateRequest::Prepare(StunMessage* request) { | 944 void TurnAllocateRequest::Prepare(StunMessage* request) { |
927 // Create the request as indicated in RFC 5766, Section 6.1. | 945 // Create the request as indicated in RFC 5766, Section 6.1. |
928 request->SetType(TURN_ALLOCATE_REQUEST); | 946 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 | 1380 // bind request as per http://tools.ietf.org/html/rfc5766#section-11.3 |
1363 if (code == STUN_ERROR_STALE_NONCE) { | 1381 if (code == STUN_ERROR_STALE_NONCE) { |
1364 if (port_->UpdateNonce(response)) { | 1382 if (port_->UpdateNonce(response)) { |
1365 // Send channel bind request with fresh nonce. | 1383 // Send channel bind request with fresh nonce. |
1366 SendChannelBindRequest(0); | 1384 SendChannelBindRequest(0); |
1367 } | 1385 } |
1368 } | 1386 } |
1369 } | 1387 } |
1370 | 1388 |
1371 } // namespace cricket | 1389 } // namespace cricket |
OLD | NEW |