OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 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 783 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
794 last_ping_sent_(0), | 794 last_ping_sent_(0), |
795 last_ping_received_(0), | 795 last_ping_received_(0), |
796 last_data_received_(0), | 796 last_data_received_(0), |
797 last_ping_response_received_(0), | 797 last_ping_response_received_(0), |
798 recv_rate_tracker_(100u, 10u), | 798 recv_rate_tracker_(100u, 10u), |
799 send_rate_tracker_(100u, 10u), | 799 send_rate_tracker_(100u, 10u), |
800 sent_packets_discarded_(0), | 800 sent_packets_discarded_(0), |
801 sent_packets_total_(0), | 801 sent_packets_total_(0), |
802 reported_(false), | 802 reported_(false), |
803 state_(STATE_WAITING), | 803 state_(STATE_WAITING), |
804 receiving_timeout_(WEAK_CONNECTION_RECEIVE_TIMEOUT) { | 804 receiving_timeout_(WEAK_CONNECTION_RECEIVE_TIMEOUT), |
805 time_created_ms_(rtc::Time()) { | |
805 // All of our connections start in WAITING state. | 806 // All of our connections start in WAITING state. |
806 // TODO(mallinath) - Start connections from STATE_FROZEN. | 807 // TODO(mallinath) - Start connections from STATE_FROZEN. |
807 // Wire up to send stun packets | 808 // Wire up to send stun packets |
808 requests_.SignalSendPacket.connect(this, &Connection::OnSendStunPacket); | 809 requests_.SignalSendPacket.connect(this, &Connection::OnSendStunPacket); |
809 LOG_J(LS_INFO, this) << "Connection created"; | 810 LOG_J(LS_INFO, this) << "Connection created"; |
810 } | 811 } |
811 | 812 |
812 Connection::~Connection() { | 813 Connection::~Connection() { |
813 } | 814 } |
814 | 815 |
(...skipping 27 matching lines...) Expand all Loading... | |
842 return priority; | 843 return priority; |
843 } | 844 } |
844 | 845 |
845 void Connection::set_write_state(WriteState value) { | 846 void Connection::set_write_state(WriteState value) { |
846 WriteState old_value = write_state_; | 847 WriteState old_value = write_state_; |
847 write_state_ = value; | 848 write_state_ = value; |
848 if (value != old_value) { | 849 if (value != old_value) { |
849 LOG_J(LS_VERBOSE, this) << "set_write_state from: " << old_value << " to " | 850 LOG_J(LS_VERBOSE, this) << "set_write_state from: " << old_value << " to " |
850 << value; | 851 << value; |
851 SignalStateChange(this); | 852 SignalStateChange(this); |
852 CheckTimeout(); | |
853 } | 853 } |
854 } | 854 } |
855 | 855 |
856 void Connection::set_receiving(bool value) { | 856 void Connection::set_receiving(bool value) { |
857 if (value != receiving_) { | 857 if (value != receiving_) { |
858 LOG_J(LS_VERBOSE, this) << "set_receiving to " << value; | 858 LOG_J(LS_VERBOSE, this) << "set_receiving to " << value; |
859 receiving_ = value; | 859 receiving_ = value; |
860 SignalStateChange(this); | 860 SignalStateChange(this); |
861 CheckTimeout(); | |
862 } | 861 } |
863 } | 862 } |
864 | 863 |
865 void Connection::set_state(State state) { | 864 void Connection::set_state(State state) { |
866 State old_state = state_; | 865 State old_state = state_; |
867 state_ = state; | 866 state_ = state; |
868 if (state != old_state) { | 867 if (state != old_state) { |
869 LOG_J(LS_VERBOSE, this) << "set_state"; | 868 LOG_J(LS_VERBOSE, this) << "set_state"; |
870 } | 869 } |
871 } | 870 } |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1082 << now - pings_since_last_response_[0].sent_time | 1081 << now - pings_since_last_response_[0].sent_time |
1083 << " ms without a response" | 1082 << " ms without a response" |
1084 << ", rtt=" << rtt; | 1083 << ", rtt=" << rtt; |
1085 set_write_state(STATE_WRITE_TIMEOUT); | 1084 set_write_state(STATE_WRITE_TIMEOUT); |
1086 } | 1085 } |
1087 | 1086 |
1088 // Check the receiving state. | 1087 // Check the receiving state. |
1089 uint32 last_recv_time = last_received(); | 1088 uint32 last_recv_time = last_received(); |
1090 bool receiving = now <= last_recv_time + receiving_timeout_; | 1089 bool receiving = now <= last_recv_time + receiving_timeout_; |
1091 set_receiving(receiving); | 1090 set_receiving(receiving); |
1091 if (dead(now)) { | |
1092 Destroy(); | |
1093 } | |
1092 } | 1094 } |
1093 | 1095 |
1094 void Connection::Ping(uint32 now) { | 1096 void Connection::Ping(uint32 now) { |
1095 last_ping_sent_ = now; | 1097 last_ping_sent_ = now; |
1096 ConnectionRequest *req = new ConnectionRequest(this); | 1098 ConnectionRequest *req = new ConnectionRequest(this); |
1097 pings_since_last_response_.push_back(SentPing(req->id(), now)); | 1099 pings_since_last_response_.push_back(SentPing(req->id(), now)); |
1098 LOG_J(LS_VERBOSE, this) << "Sending STUN ping " | 1100 LOG_J(LS_VERBOSE, this) << "Sending STUN ping " |
1099 << ", id=" << rtc::hex_encode(req->id()); | 1101 << ", id=" << rtc::hex_encode(req->id()); |
1100 requests_.Send(req); | 1102 requests_.Send(req); |
1101 state_ = STATE_INPROGRESS; | 1103 state_ = STATE_INPROGRESS; |
(...skipping 10 matching lines...) Expand all Loading... | |
1112 // So if we're not already, become writable. We may be bringing a pruned | 1114 // So if we're not already, become writable. We may be bringing a pruned |
1113 // connection back to life, but if we don't really want it, we can always | 1115 // connection back to life, but if we don't really want it, we can always |
1114 // prune it again. | 1116 // prune it again. |
1115 set_receiving(true); | 1117 set_receiving(true); |
1116 set_write_state(STATE_WRITABLE); | 1118 set_write_state(STATE_WRITABLE); |
1117 set_state(STATE_SUCCEEDED); | 1119 set_state(STATE_SUCCEEDED); |
1118 pings_since_last_response_.clear(); | 1120 pings_since_last_response_.clear(); |
1119 last_ping_response_received_ = rtc::Time(); | 1121 last_ping_response_received_ = rtc::Time(); |
1120 } | 1122 } |
1121 | 1123 |
1124 bool Connection::dead(uint32 now) const { | |
1125 if (now < (time_created_ms_ + MIN_CONNECTION_LIFETIME)) { | |
1126 // A connection that hasn't passed its minimum lifetime is still alive. | |
1127 // We do this to prevent connections from being pruned too quickly | |
1128 // during a network change event when two networks would be up | |
1129 // simultaneously but only for a brief period. | |
1130 return false; | |
1131 } | |
1132 | |
1133 if (receiving_) { | |
1134 // A connection that is receiving is alive. | |
1135 return false; | |
1136 } | |
1137 | |
1138 // A connection is alive until it is inactive. | |
1139 return !active(); | |
1140 | |
1141 // TODO(honghaiz): Move from using the write state to using the receiving | |
1142 // state with something like the following: | |
1143 // return (now > (last_received() + DEAD_CONNECTION_RECEIVE_TIMEOUT)); | |
1144 } | |
1145 | |
1122 std::string Connection::ToDebugId() const { | 1146 std::string Connection::ToDebugId() const { |
1123 std::stringstream ss; | 1147 std::stringstream ss; |
1124 ss << std::hex << this; | 1148 ss << std::hex << this; |
1125 return ss.str(); | 1149 return ss.str(); |
1126 } | 1150 } |
1127 | 1151 |
1128 std::string Connection::ToString() const { | 1152 std::string Connection::ToString() const { |
1129 const char CONNECT_STATE_ABBREV[2] = { | 1153 const char CONNECT_STATE_ABBREV[2] = { |
1130 '-', // not connected (false) | 1154 '-', // not connected (false) |
1131 'C', // connected (true) | 1155 'C', // connected (true) |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1222 error_code == STUN_ERROR_UNAUTHORIZED) { | 1246 error_code == STUN_ERROR_UNAUTHORIZED) { |
1223 // Recoverable error, retry | 1247 // Recoverable error, retry |
1224 } else if (error_code == STUN_ERROR_STALE_CREDENTIALS) { | 1248 } else if (error_code == STUN_ERROR_STALE_CREDENTIALS) { |
1225 // Race failure, retry | 1249 // Race failure, retry |
1226 } else if (error_code == STUN_ERROR_ROLE_CONFLICT) { | 1250 } else if (error_code == STUN_ERROR_ROLE_CONFLICT) { |
1227 HandleRoleConflictFromPeer(); | 1251 HandleRoleConflictFromPeer(); |
1228 } else { | 1252 } else { |
1229 // This is not a valid connection. | 1253 // This is not a valid connection. |
1230 LOG_J(LS_ERROR, this) << "Received STUN error response, code=" | 1254 LOG_J(LS_ERROR, this) << "Received STUN error response, code=" |
1231 << error_code << "; killing connection"; | 1255 << error_code << "; killing connection"; |
1232 set_state(STATE_FAILED); | 1256 Destroy(); |
pthatcher1
2015/09/29 22:56:52
Don't we still need the call to set_state(STATE_FA
honghaiz3
2015/09/29 23:22:39
Done.
| |
1233 set_write_state(STATE_WRITE_TIMEOUT); | |
1234 } | 1257 } |
1235 } | 1258 } |
1236 | 1259 |
1237 void Connection::OnConnectionRequestTimeout(ConnectionRequest* request) { | 1260 void Connection::OnConnectionRequestTimeout(ConnectionRequest* request) { |
1238 // Log at LS_INFO if we miss a ping on a writable connection. | 1261 // Log at LS_INFO if we miss a ping on a writable connection. |
1239 rtc::LoggingSeverity sev = writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; | 1262 rtc::LoggingSeverity sev = writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; |
1240 LOG_JV(sev, this) << "Timing-out STUN ping " | 1263 LOG_JV(sev, this) << "Timing-out STUN ping " |
1241 << rtc::hex_encode(request->id()) | 1264 << rtc::hex_encode(request->id()) |
1242 << " after " << request->Elapsed() << " ms"; | 1265 << " after " << request->Elapsed() << " ms"; |
1243 } | 1266 } |
1244 | 1267 |
1245 void Connection::OnConnectionRequestSent(ConnectionRequest* request) { | 1268 void Connection::OnConnectionRequestSent(ConnectionRequest* request) { |
1246 // Log at LS_INFO if we send a ping on an unwritable connection. | 1269 // Log at LS_INFO if we send a ping on an unwritable connection. |
1247 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; | 1270 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; |
1248 bool use_candidate = use_candidate_attr(); | 1271 bool use_candidate = use_candidate_attr(); |
1249 LOG_JV(sev, this) << "Sent STUN ping" | 1272 LOG_JV(sev, this) << "Sent STUN ping" |
1250 << ", id=" << rtc::hex_encode(request->id()) | 1273 << ", id=" << rtc::hex_encode(request->id()) |
1251 << ", use_candidate=" << use_candidate; | 1274 << ", use_candidate=" << use_candidate; |
1252 } | 1275 } |
1253 | 1276 |
1254 void Connection::CheckTimeout() { | |
1255 // If write has timed out and it is not receiving, remove the connection. | |
1256 if (!receiving_ && write_state_ == STATE_WRITE_TIMEOUT) { | |
1257 Destroy(); | |
1258 } | |
1259 } | |
1260 | |
1261 void Connection::HandleRoleConflictFromPeer() { | 1277 void Connection::HandleRoleConflictFromPeer() { |
1262 port_->SignalRoleConflict(port_); | 1278 port_->SignalRoleConflict(port_); |
1263 } | 1279 } |
1264 | 1280 |
1265 void Connection::MaybeSetRemoteIceCredentials(const std::string& ice_ufrag, | 1281 void Connection::MaybeSetRemoteIceCredentials(const std::string& ice_ufrag, |
1266 const std::string& ice_pwd) { | 1282 const std::string& ice_pwd) { |
1267 if (remote_candidate_.username() == ice_ufrag && | 1283 if (remote_candidate_.username() == ice_ufrag && |
1268 remote_candidate_.password().empty()) { | 1284 remote_candidate_.password().empty()) { |
1269 remote_candidate_.set_password(ice_pwd); | 1285 remote_candidate_.set_password(ice_pwd); |
1270 } | 1286 } |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1402 ASSERT(sent < 0); | 1418 ASSERT(sent < 0); |
1403 error_ = port_->GetError(); | 1419 error_ = port_->GetError(); |
1404 sent_packets_discarded_++; | 1420 sent_packets_discarded_++; |
1405 } else { | 1421 } else { |
1406 send_rate_tracker_.AddSamples(sent); | 1422 send_rate_tracker_.AddSamples(sent); |
1407 } | 1423 } |
1408 return sent; | 1424 return sent; |
1409 } | 1425 } |
1410 | 1426 |
1411 } // namespace cricket | 1427 } // namespace cricket |
OLD | NEW |