OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 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 13 matching lines...) Expand all Loading... |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 void IncrementCounterByAddress(std::map<rtc::IPAddress, int>* counter_per_ip, | 27 void IncrementCounterByAddress(std::map<rtc::IPAddress, int>* counter_per_ip, |
28 const rtc::IPAddress& ip) { | 28 const rtc::IPAddress& ip) { |
29 counter_per_ip->insert(std::make_pair(ip, 0)).first->second++; | 29 counter_per_ip->insert(std::make_pair(ip, 0)).first->second++; |
30 } | 30 } |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
| 34 // A requester tracks the requests and responses from a single socket to many |
| 35 // STUN servers |
| 36 class StunProber::Requester { |
| 37 public: |
| 38 // Each Request maps to a request and response. |
| 39 struct Request { |
| 40 // Actual time the STUN bind request was sent. |
| 41 int64 sent_time_ms = 0; |
| 42 // Time the response was received. |
| 43 int64 received_time_ms = 0; |
| 44 |
| 45 // See whether the observed address returned matches the |
| 46 // local address as in StunProber.local_addr_. |
| 47 bool behind_nat = false; |
| 48 |
| 49 // Server reflexive address from STUN response for this given request. |
| 50 rtc::SocketAddress srflx_addr; |
| 51 |
| 52 rtc::IPAddress server_addr; |
| 53 |
| 54 int64 rtt() { return received_time_ms - sent_time_ms; } |
| 55 void ProcessResponse(rtc::ByteBuffer* message, |
| 56 int buf_len, |
| 57 const rtc::IPAddress& local_addr); |
| 58 }; |
| 59 |
| 60 // StunProber provides |server_ips| for Requester to probe. For shared |
| 61 // socket mode, it'll be all the resolved IP addresses. For non-shared mode, |
| 62 // it'll just be a single address. |
| 63 Requester(StunProber* prober, |
| 64 ServerSocketInterface* socket, |
| 65 const std::vector<rtc::SocketAddress>& server_ips); |
| 66 virtual ~Requester(); |
| 67 |
| 68 // There is no callback for SendStunRequest as the underneath socket send is |
| 69 // expected to be completed immediately. Otherwise, it'll skip this request |
| 70 // and move to the next one. |
| 71 void SendStunRequest(); |
| 72 |
| 73 void ReadStunResponse(); |
| 74 |
| 75 // |result| is the positive return value from RecvFrom when data is |
| 76 // available. |
| 77 void OnStunResponseReceived(int result); |
| 78 |
| 79 const std::vector<Request*>& requests() { return requests_; } |
| 80 |
| 81 // Whether this Requester has completed all requests. |
| 82 bool Done() { |
| 83 return static_cast<size_t>(num_request_sent_) == server_ips_.size(); |
| 84 } |
| 85 |
| 86 private: |
| 87 Request* GetRequestByAddress(const rtc::IPAddress& ip); |
| 88 |
| 89 StunProber* prober_; |
| 90 |
| 91 // The socket for this session. |
| 92 rtc::scoped_ptr<ServerSocketInterface> socket_; |
| 93 |
| 94 // Temporary SocketAddress and buffer for RecvFrom. |
| 95 rtc::SocketAddress addr_; |
| 96 rtc::scoped_ptr<rtc::ByteBuffer> response_packet_; |
| 97 |
| 98 std::vector<Request*> requests_; |
| 99 std::vector<rtc::SocketAddress> server_ips_; |
| 100 int16 num_request_sent_ = 0; |
| 101 int16 num_response_received_ = 0; |
| 102 |
| 103 rtc::ThreadChecker& thread_checker_; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(Requester); |
| 106 }; |
| 107 |
34 StunProber::Requester::Requester( | 108 StunProber::Requester::Requester( |
35 StunProber* prober, | 109 StunProber* prober, |
36 ServerSocketInterface* socket, | 110 ServerSocketInterface* socket, |
37 const std::vector<rtc::SocketAddress>& server_ips) | 111 const std::vector<rtc::SocketAddress>& server_ips) |
38 : prober_(prober), | 112 : prober_(prober), |
39 socket_(socket), | 113 socket_(socket), |
40 response_packet_(new rtc::ByteBuffer(nullptr, kMaxUdpBufferSize)), | 114 response_packet_(new rtc::ByteBuffer(nullptr, kMaxUdpBufferSize)), |
41 server_ips_(server_ips), | 115 server_ips_(server_ips), |
42 thread_checker_(prober->thread_checker_) { | 116 thread_checker_(prober->thread_checker_) { |
43 } | 117 } |
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 if (!finished_callback_.empty()) { | 527 if (!finished_callback_.empty()) { |
454 AsyncCallback callback = finished_callback_; | 528 AsyncCallback callback = finished_callback_; |
455 finished_callback_ = AsyncCallback(); | 529 finished_callback_ = AsyncCallback(); |
456 | 530 |
457 // Callback at the last since the prober might be deleted in the callback. | 531 // Callback at the last since the prober might be deleted in the callback. |
458 callback(status); | 532 callback(status); |
459 } | 533 } |
460 } | 534 } |
461 | 535 |
462 } // namespace stunprober | 536 } // namespace stunprober |
OLD | NEW |