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 773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
784 remote_candidate_(remote_candidate), | 784 remote_candidate_(remote_candidate), |
785 write_state_(STATE_WRITE_INIT), | 785 write_state_(STATE_WRITE_INIT), |
786 receiving_(false), | 786 receiving_(false), |
787 connected_(true), | 787 connected_(true), |
788 pruned_(false), | 788 pruned_(false), |
789 use_candidate_attr_(false), | 789 use_candidate_attr_(false), |
790 nominated_(false), | 790 nominated_(false), |
791 remote_ice_mode_(ICEMODE_FULL), | 791 remote_ice_mode_(ICEMODE_FULL), |
792 requests_(port->thread()), | 792 requests_(port->thread()), |
793 rtt_(DEFAULT_RTT), | 793 rtt_(DEFAULT_RTT), |
794 time_created_(rtc::Time()), | |
794 last_ping_sent_(0), | 795 last_ping_sent_(0), |
795 last_ping_received_(0), | 796 last_ping_received_(0), |
796 last_data_received_(0), | 797 last_data_received_(0), |
797 last_ping_response_received_(0), | 798 last_ping_response_received_(0), |
798 recv_rate_tracker_(100u, 10u), | 799 recv_rate_tracker_(100u, 10u), |
799 send_rate_tracker_(100u, 10u), | 800 send_rate_tracker_(100u, 10u), |
800 sent_packets_discarded_(0), | 801 sent_packets_discarded_(0), |
801 sent_packets_total_(0), | 802 sent_packets_total_(0), |
802 reported_(false), | 803 reported_(false), |
803 state_(STATE_WAITING), | 804 state_(STATE_WAITING), |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
906 set_receiving(true); | 905 set_receiving(true); |
907 last_data_received_ = rtc::Time(); | 906 last_data_received_ = rtc::Time(); |
908 recv_rate_tracker_.AddSamples(size); | 907 recv_rate_tracker_.AddSamples(size); |
909 SignalReadPacket(this, data, size, packet_time); | 908 SignalReadPacket(this, data, size, packet_time); |
910 | 909 |
911 // If timed out sending writability checks, start up again | 910 // If timed out sending writability checks, start up again |
912 if (!pruned_ && (write_state_ == STATE_WRITE_TIMEOUT)) { | 911 if (!pruned_ && (write_state_ == STATE_WRITE_TIMEOUT)) { |
913 LOG(LS_WARNING) << "Received a data packet on a timed-out Connection. " | 912 LOG(LS_WARNING) << "Received a data packet on a timed-out Connection. " |
914 << "Resetting state to STATE_WRITE_INIT."; | 913 << "Resetting state to STATE_WRITE_INIT."; |
915 set_write_state(STATE_WRITE_INIT); | 914 set_write_state(STATE_WRITE_INIT); |
915 Unprune(); | |
pthatcher1
2015/09/21 23:49:18
if (!pruned_) Unprune()?
Why would we unprune if
honghaiz3
2015/09/22 19:35:08
If it is not pruned, then you don't need to unprun
| |
916 } | 916 } |
917 } else if (!msg) { | 917 } else if (!msg) { |
918 // The packet was STUN, but failed a check and was handled internally. | 918 // The packet was STUN, but failed a check and was handled internally. |
919 } else { | 919 } else { |
920 // The packet is STUN and passed the Port checks. | 920 // The packet is STUN and passed the Port checks. |
921 // Perform our own checks to ensure this packet is valid. | 921 // Perform our own checks to ensure this packet is valid. |
922 // If this is a STUN request, then update the readable bit and respond. | 922 // If this is a STUN request, then update the readable bit and respond. |
923 // If this is a STUN response, then update the writable bit. | 923 // If this is a STUN response, then update the writable bit. |
924 // Log at LS_INFO if we receive a ping on an unwritable connection. | 924 // Log at LS_INFO if we receive a ping on an unwritable connection. |
925 rtc::LoggingSeverity sev = (!writable() ? rtc::LS_INFO : rtc::LS_VERBOSE); | 925 rtc::LoggingSeverity sev = (!writable() ? rtc::LS_INFO : rtc::LS_VERBOSE); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
996 if (write_state_ == STATE_WRITABLE) { | 996 if (write_state_ == STATE_WRITABLE) { |
997 SignalReadyToSend(this); | 997 SignalReadyToSend(this); |
998 } | 998 } |
999 } | 999 } |
1000 | 1000 |
1001 void Connection::Prune() { | 1001 void Connection::Prune() { |
1002 if (!pruned_) { | 1002 if (!pruned_) { |
1003 LOG_J(LS_VERBOSE, this) << "Connection pruned"; | 1003 LOG_J(LS_VERBOSE, this) << "Connection pruned"; |
1004 pruned_ = true; | 1004 pruned_ = true; |
1005 requests_.Clear(); | 1005 requests_.Clear(); |
1006 set_write_state(STATE_WRITE_TIMEOUT); | |
1007 } | 1006 } |
1008 } | 1007 } |
1009 | 1008 |
1009 void Connection::Unprune() { | |
1010 if (pruned_) { | |
1011 LOG_J(LS_VERBOSE, this) << "Connection un-pruned"; | |
1012 pruned_ = false; | |
1013 } | |
1014 } | |
1015 | |
1010 void Connection::Destroy() { | 1016 void Connection::Destroy() { |
1011 LOG_J(LS_VERBOSE, this) << "Connection destroyed"; | 1017 LOG_J(LS_VERBOSE, this) << "Connection destroyed"; |
1012 port_->thread()->Post(this, MSG_DELETE); | 1018 port_->thread()->Post(this, MSG_DELETE); |
1013 } | 1019 } |
1014 | 1020 |
1015 void Connection::PrintPingsSinceLastResponse(std::string* s, size_t max) { | 1021 void Connection::PrintPingsSinceLastResponse(std::string* s, size_t max) { |
1016 std::ostringstream oss; | 1022 std::ostringstream oss; |
1017 oss << std::boolalpha; | 1023 oss << std::boolalpha; |
1018 if (pings_since_last_response_.size() > max) { | 1024 if (pings_since_last_response_.size() > max) { |
1019 for (size_t i = 0; i < max; i++) { | 1025 for (size_t i = 0; i < max; i++) { |
(...skipping 18 matching lines...) Expand all Loading... | |
1038 LOG_J(LS_VERBOSE, this) << "UpdateState()" | 1044 LOG_J(LS_VERBOSE, this) << "UpdateState()" |
1039 << ", ms since last received response=" | 1045 << ", ms since last received response=" |
1040 << now - last_ping_response_received_ | 1046 << now - last_ping_response_received_ |
1041 << ", ms since last received data=" | 1047 << ", ms since last received data=" |
1042 << now - last_data_received_ | 1048 << now - last_data_received_ |
1043 << ", rtt=" << rtt | 1049 << ", rtt=" << rtt |
1044 << ", pings_since_last_response=" << pings; | 1050 << ", pings_since_last_response=" << pings; |
1045 } | 1051 } |
1046 | 1052 |
1047 // Check the writable state. (The order of these checks is important.) | 1053 // Check the writable state. (The order of these checks is important.) |
1054 // If the connection has not sent stun ping for a while (mostly because | |
pthatcher1
2015/09/21 23:49:18
sent stun ping => sent a stun ping
honghaiz3
2015/09/22 19:35:08
Done.
| |
1055 // it is pruned), it is considered stale. | |
1048 // | 1056 // |
1049 // Before becoming unwritable, we allow for a fixed number of pings to fail | 1057 // Before becoming unwritable, we allow for a fixed number of pings to fail |
1050 // (i.e., receive no response). We also have to give the response time to | 1058 // (i.e., receive no response). We also have to give the response time to |
1051 // get back, so we include a conservative estimate of this. | 1059 // get back, so we include a conservative estimate of this. |
1052 // | 1060 // |
1053 // Before timing out writability, we give a fixed amount of time. This is to | 1061 // Before timing out writability, we give a fixed amount of time. This is to |
1054 // allow for changes in network conditions. | 1062 // allow for changes in network conditions. |
1055 | 1063 |
1056 if ((write_state_ == STATE_WRITABLE) && | 1064 if (write_state_ == STATE_WRITABLE && |
1065 last_ping_sent_ + CONNECTION_WRITE_STALE_TIMEOUT < now) { | |
1066 LOG_J(LS_INFO, this) << "WriteState becomes stale"; | |
1067 set_write_state(STATE_WRITE_STALE); | |
1068 } | |
1069 if ((write_state_ == STATE_WRITABLE || write_state_ == STATE_WRITE_STALE) && | |
1057 TooManyFailures(pings_since_last_response_, | 1070 TooManyFailures(pings_since_last_response_, |
1058 CONNECTION_WRITE_CONNECT_FAILURES, | 1071 CONNECTION_WRITE_CONNECT_FAILURES, |
1059 rtt, | 1072 rtt, |
1060 now) && | 1073 now) && |
1061 TooLongWithoutResponse(pings_since_last_response_, | 1074 TooLongWithoutResponse(pings_since_last_response_, |
1062 CONNECTION_WRITE_CONNECT_TIMEOUT, | 1075 CONNECTION_WRITE_CONNECT_TIMEOUT, |
1063 now)) { | 1076 now)) { |
1064 uint32 max_pings = CONNECTION_WRITE_CONNECT_FAILURES; | 1077 uint32 max_pings = CONNECTION_WRITE_CONNECT_FAILURES; |
1065 LOG_J(LS_INFO, this) << "Unwritable after " << max_pings | 1078 LOG_J(LS_INFO, this) << "Unwritable after " << max_pings |
1066 << " ping failures and " | 1079 << " ping failures and " |
(...skipping 15 matching lines...) Expand all Loading... | |
1082 << now - pings_since_last_response_[0].sent_time | 1095 << now - pings_since_last_response_[0].sent_time |
1083 << " ms without a response" | 1096 << " ms without a response" |
1084 << ", rtt=" << rtt; | 1097 << ", rtt=" << rtt; |
1085 set_write_state(STATE_WRITE_TIMEOUT); | 1098 set_write_state(STATE_WRITE_TIMEOUT); |
1086 } | 1099 } |
1087 | 1100 |
1088 // Check the receiving state. | 1101 // Check the receiving state. |
1089 uint32 last_recv_time = last_received(); | 1102 uint32 last_recv_time = last_received(); |
1090 bool receiving = now <= last_recv_time + receiving_timeout_; | 1103 bool receiving = now <= last_recv_time + receiving_timeout_; |
1091 set_receiving(receiving); | 1104 set_receiving(receiving); |
1105 if (!receiving) { | |
1106 // If this connection has not received anything for a long time, | |
1107 // destroy the connection. If the connection has never received anything, | |
1108 // use the connection creating time as the starting time for calculating | |
1109 // the receiving timeout. | |
1110 if (now > std::max(last_recv_time, time_created_) + | |
1111 DEAD_CONNECTION_RECEIVE_TIMEOUT) { | |
1112 Destroy(); | |
1113 } | |
1114 } | |
1092 } | 1115 } |
1093 | 1116 |
1094 void Connection::Ping(uint32 now) { | 1117 void Connection::Ping(uint32 now) { |
1095 last_ping_sent_ = now; | 1118 last_ping_sent_ = now; |
1096 ConnectionRequest *req = new ConnectionRequest(this); | 1119 ConnectionRequest *req = new ConnectionRequest(this); |
1097 pings_since_last_response_.push_back(SentPing(req->id(), now)); | 1120 pings_since_last_response_.push_back(SentPing(req->id(), now)); |
1098 LOG_J(LS_VERBOSE, this) << "Sending STUN ping " | 1121 LOG_J(LS_VERBOSE, this) << "Sending STUN ping " |
1099 << ", id=" << rtc::hex_encode(req->id()); | 1122 << ", id=" << rtc::hex_encode(req->id()); |
1100 requests_.Send(req); | 1123 requests_.Send(req); |
1101 state_ = STATE_INPROGRESS; | 1124 state_ = STATE_INPROGRESS; |
(...skipping 25 matching lines...) Expand all Loading... | |
1127 | 1150 |
1128 std::string Connection::ToString() const { | 1151 std::string Connection::ToString() const { |
1129 const char CONNECT_STATE_ABBREV[2] = { | 1152 const char CONNECT_STATE_ABBREV[2] = { |
1130 '-', // not connected (false) | 1153 '-', // not connected (false) |
1131 'C', // connected (true) | 1154 'C', // connected (true) |
1132 }; | 1155 }; |
1133 const char RECEIVE_STATE_ABBREV[2] = { | 1156 const char RECEIVE_STATE_ABBREV[2] = { |
1134 '-', // not receiving (false) | 1157 '-', // not receiving (false) |
1135 'R', // receiving (true) | 1158 'R', // receiving (true) |
1136 }; | 1159 }; |
1137 const char WRITE_STATE_ABBREV[4] = { | 1160 const char WRITE_STATE_ABBREV[5] = { |
1138 'W', // STATE_WRITABLE | 1161 'W', // STATE_WRITABLE |
1162 's', // STATE_WRITE_STALE | |
pthatcher1
2015/09/21 23:49:18
Maybe a '?' would be good, since it means we don't
honghaiz3
2015/09/22 19:35:08
Dropped this (saved for a separate CL).
| |
1139 'w', // STATE_WRITE_UNRELIABLE | 1163 'w', // STATE_WRITE_UNRELIABLE |
1140 '-', // STATE_WRITE_INIT | 1164 '-', // STATE_WRITE_INIT |
1141 'x', // STATE_WRITE_TIMEOUT | 1165 'x', // STATE_WRITE_TIMEOUT |
1142 }; | 1166 }; |
1143 const std::string ICESTATE[4] = { | 1167 const std::string ICESTATE[4] = { |
1144 "W", // STATE_WAITING | 1168 "W", // STATE_WAITING |
1145 "I", // STATE_INPROGRESS | 1169 "I", // STATE_INPROGRESS |
1146 "S", // STATE_SUCCEEDED | 1170 "S", // STATE_SUCCEEDED |
1147 "F" // STATE_FAILED | 1171 "F" // STATE_FAILED |
1148 }; | 1172 }; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1244 | 1268 |
1245 void Connection::OnConnectionRequestSent(ConnectionRequest* request) { | 1269 void Connection::OnConnectionRequestSent(ConnectionRequest* request) { |
1246 // Log at LS_INFO if we send a ping on an unwritable connection. | 1270 // Log at LS_INFO if we send a ping on an unwritable connection. |
1247 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; | 1271 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; |
1248 bool use_candidate = use_candidate_attr(); | 1272 bool use_candidate = use_candidate_attr(); |
1249 LOG_JV(sev, this) << "Sent STUN ping" | 1273 LOG_JV(sev, this) << "Sent STUN ping" |
1250 << ", id=" << rtc::hex_encode(request->id()) | 1274 << ", id=" << rtc::hex_encode(request->id()) |
1251 << ", use_candidate=" << use_candidate; | 1275 << ", use_candidate=" << use_candidate; |
1252 } | 1276 } |
1253 | 1277 |
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 port_->thread()->Post(this, MSG_DELETE); | |
1258 } | |
1259 } | |
1260 | |
1261 void Connection::HandleRoleConflictFromPeer() { | 1278 void Connection::HandleRoleConflictFromPeer() { |
1262 port_->SignalRoleConflict(port_); | 1279 port_->SignalRoleConflict(port_); |
1263 } | 1280 } |
1264 | 1281 |
1265 void Connection::MaybeSetRemoteIceCredentials(const std::string& ice_ufrag, | 1282 void Connection::MaybeSetRemoteIceCredentials(const std::string& ice_ufrag, |
1266 const std::string& ice_pwd) { | 1283 const std::string& ice_pwd) { |
1267 if (remote_candidate_.username() == ice_ufrag && | 1284 if (remote_candidate_.username() == ice_ufrag && |
1268 remote_candidate_.password().empty()) { | 1285 remote_candidate_.password().empty()) { |
1269 remote_candidate_.set_password(ice_pwd); | 1286 remote_candidate_.set_password(ice_pwd); |
1270 } | 1287 } |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1402 ASSERT(sent < 0); | 1419 ASSERT(sent < 0); |
1403 error_ = port_->GetError(); | 1420 error_ = port_->GetError(); |
1404 sent_packets_discarded_++; | 1421 sent_packets_discarded_++; |
1405 } else { | 1422 } else { |
1406 send_rate_tracker_.AddSamples(sent); | 1423 send_rate_tracker_.AddSamples(sent); |
1407 } | 1424 } |
1408 return sent; | 1425 return sent; |
1409 } | 1426 } |
1410 | 1427 |
1411 } // namespace cricket | 1428 } // namespace cricket |
OLD | NEW |