Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(670)

Side by Side Diff: webrtc/base/network.cc

Issue 1391703003: Create network change notifier. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Minor fix on the comment Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 30 matching lines...) Expand all
41 #if defined(WEBRTC_WIN) 41 #if defined(WEBRTC_WIN)
42 #include "webrtc/base/win32.h" 42 #include "webrtc/base/win32.h"
43 #include <Iphlpapi.h> 43 #include <Iphlpapi.h>
44 #endif 44 #endif
45 45
46 #include <stdio.h> 46 #include <stdio.h>
47 47
48 #include <algorithm> 48 #include <algorithm>
49 49
50 #include "webrtc/base/logging.h" 50 #include "webrtc/base/logging.h"
51 #include "webrtc/base/networkchangenotifier.h"
51 #include "webrtc/base/scoped_ptr.h" 52 #include "webrtc/base/scoped_ptr.h"
52 #include "webrtc/base/socket.h" // includes something that makes windows happy 53 #include "webrtc/base/socket.h" // includes something that makes windows happy
53 #include "webrtc/base/stream.h" 54 #include "webrtc/base/stream.h"
54 #include "webrtc/base/stringencode.h" 55 #include "webrtc/base/stringencode.h"
55 #include "webrtc/base/thread.h" 56 #include "webrtc/base/thread.h"
56 57
57 namespace rtc { 58 namespace rtc {
58 namespace { 59 namespace {
59 60
60 // Turning on IPv6 could make many IPv6 interfaces available for connectivity 61 // Turning on IPv6 could make many IPv6 interfaces available for connectivity
61 // check and delay the call setup time. kMaxIPv6Networks is the default upper 62 // check and delay the call setup time. kMaxIPv6Networks is the default upper
62 // limit of IPv6 networks but could be changed by set_max_ipv6_networks(). 63 // limit of IPv6 networks but could be changed by set_max_ipv6_networks().
63 const int kMaxIPv6Networks = 5; 64 const int kMaxIPv6Networks = 5;
64 65
65 const uint32 kUpdateNetworksMessage = 1; 66 const uint32 kUpdateNetworksMessage = 1;
pthatcher1 2015/10/09 23:11:29 Similarly, kUpdateNetworksContinuallyMessage
honghaiz3 2015/10/13 23:19:56 I kept the message name because now I do not need
66 const uint32 kSignalNetworksMessage = 2; 67 const uint32 kSignalNetworksMessage = 2;
68 const uint32 kUpdateNetworksOnceMessage = 3;
67 69
68 // Fetch list of networks every two seconds. 70 // Fetch list of networks every two seconds.
69 const int kNetworksUpdateIntervalMs = 2000; 71 const int kNetworksUpdateIntervalMs = 2000;
70 72
71 const int kHighestNetworkPreference = 127; 73 const int kHighestNetworkPreference = 127;
72 74
73 typedef struct { 75 typedef struct {
74 Network* net; 76 Network* net;
75 std::vector<InterfaceAddress> ips; 77 std::vector<InterfaceAddress> ips;
76 } AddressList; 78 } AddressList;
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 324
323 BasicNetworkManager::BasicNetworkManager() 325 BasicNetworkManager::BasicNetworkManager()
324 : thread_(NULL), sent_first_update_(false), start_count_(0), 326 : thread_(NULL), sent_first_update_(false), start_count_(0),
325 network_ignore_mask_(kDefaultNetworkIgnoreMask), 327 network_ignore_mask_(kDefaultNetworkIgnoreMask),
326 ignore_non_default_routes_(false) { 328 ignore_non_default_routes_(false) {
327 } 329 }
328 330
329 BasicNetworkManager::~BasicNetworkManager() { 331 BasicNetworkManager::~BasicNetworkManager() {
330 } 332 }
331 333
334 void BasicNetworkManager::OnNetworkChangeNotified() {
pthatcher1 2015/10/09 23:11:29 I think OnNetworksChanged would be a better name.
honghaiz3 2015/10/13 23:19:56 Done.
335 LOG(LS_VERBOSE) << "Network change was notified at the network manager";
336 thread_->Post(this, kUpdateNetworksOnceMessage);
337 }
338
332 #if defined(__native_client__) 339 #if defined(__native_client__)
333 340
334 bool BasicNetworkManager::CreateNetworks(bool include_ignored, 341 bool BasicNetworkManager::CreateNetworks(bool include_ignored,
335 NetworkList* networks) const { 342 NetworkList* networks) const {
336 ASSERT(false); 343 ASSERT(false);
337 LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet"; 344 LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet";
338 return false; 345 return false;
339 } 346 }
340 347
341 #elif defined(WEBRTC_POSIX) 348 #elif defined(WEBRTC_POSIX)
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 void BasicNetworkManager::StartUpdating() { 663 void BasicNetworkManager::StartUpdating() {
657 thread_ = Thread::Current(); 664 thread_ = Thread::Current();
658 if (start_count_) { 665 if (start_count_) {
659 // If network interfaces are already discovered and signal is sent, 666 // If network interfaces are already discovered and signal is sent,
660 // we should trigger network signal immediately for the new clients 667 // we should trigger network signal immediately for the new clients
661 // to start allocating ports. 668 // to start allocating ports.
662 if (sent_first_update_) 669 if (sent_first_update_)
663 thread_->Post(this, kSignalNetworksMessage); 670 thread_->Post(this, kSignalNetworksMessage);
664 } else { 671 } else {
665 thread_->Post(this, kUpdateNetworksMessage); 672 thread_->Post(this, kUpdateNetworksMessage);
673 StartNetworkChangeNotifier();
666 } 674 }
667 ++start_count_; 675 ++start_count_;
668 } 676 }
669 677
670 void BasicNetworkManager::StopUpdating() { 678 void BasicNetworkManager::StopUpdating() {
671 ASSERT(Thread::Current() == thread_); 679 ASSERT(Thread::Current() == thread_);
672 if (!start_count_) 680 if (!start_count_)
673 return; 681 return;
674 682
675 --start_count_; 683 --start_count_;
676 if (!start_count_) { 684 if (!start_count_) {
677 thread_->Clear(this); 685 thread_->Clear(this);
678 sent_first_update_ = false; 686 sent_first_update_ = false;
687 StopNetworkChangeNotifier();
679 } 688 }
680 } 689 }
681 690
691 void BasicNetworkManager::StartNetworkChangeNotifier() {
692 NetworkChangeNotifierFactory* factory = NetworkChangeNotifier::GetFactory();
693 if (factory == nullptr) {
694 return;
695 }
696 network_change_notifier_.reset(factory->CreateNotifier());
pthatcher1 2015/10/09 23:11:29 Why do we have GetFactory()->CreateNotifier()? Wh
honghaiz3 2015/10/13 23:19:56 I keep the factory as it helps unittests and allow
697 if (!network_change_notifier_) {
698 return;
699 }
700 network_change_notifier_->SignalNetworksChanged.connect(
701 this, &BasicNetworkManager::OnNetworkChangeNotified);
702 }
703
704 void BasicNetworkManager::StopNetworkChangeNotifier() {
705 network_change_notifier_.reset();
706 }
707
682 void BasicNetworkManager::OnMessage(Message* msg) { 708 void BasicNetworkManager::OnMessage(Message* msg) {
683 switch (msg->message_id) { 709 switch (msg->message_id) {
684 case kUpdateNetworksMessage: { 710 case kUpdateNetworksMessage: {
711 UpdateNetworks();
712 break;
713 }
714 case kUpdateNetworksOnceMessage: {
685 DoUpdateNetworks(); 715 DoUpdateNetworks();
686 break; 716 break;
687 } 717 }
688 case kSignalNetworksMessage: { 718 case kSignalNetworksMessage: {
689 SignalNetworksChanged(); 719 SignalNetworksChanged();
690 break; 720 break;
691 } 721 }
692 default: 722 default:
693 ASSERT(false); 723 ASSERT(false);
694 } 724 }
695 } 725 }
696 726
697 void BasicNetworkManager::DoUpdateNetworks() { 727 void BasicNetworkManager::DoUpdateNetworks() {
698 if (!start_count_) 728 if (!start_count_)
699 return; 729 return;
700 730
701 ASSERT(Thread::Current() == thread_); 731 ASSERT(Thread::Current() == thread_);
702 732
703 NetworkList list; 733 NetworkList list;
704 if (!CreateNetworks(false, &list)) { 734 if (!CreateNetworks(false, &list)) {
705 SignalError(); 735 SignalError();
706 } else { 736 } else {
707 bool changed; 737 bool changed;
708 MergeNetworkList(list, &changed); 738 MergeNetworkList(list, &changed);
709 if (changed || !sent_first_update_) { 739 if (changed || !sent_first_update_) {
710 SignalNetworksChanged(); 740 SignalNetworksChanged();
711 sent_first_update_ = true; 741 sent_first_update_ = true;
712 } 742 }
713 } 743 }
744 }
714 745
746 void BasicNetworkManager::UpdateNetworks() {
747 DoUpdateNetworks();
715 thread_->PostDelayed(kNetworksUpdateIntervalMs, this, kUpdateNetworksMessage); 748 thread_->PostDelayed(kNetworksUpdateIntervalMs, this, kUpdateNetworksMessage);
716 } 749 }
717 750
718 void BasicNetworkManager::DumpNetworks(bool include_ignored) { 751 void BasicNetworkManager::DumpNetworks(bool include_ignored) {
719 NetworkList list; 752 NetworkList list;
720 CreateNetworks(include_ignored, &list); 753 CreateNetworks(include_ignored, &list);
721 LOG(LS_INFO) << "NetworkManager detected " << list.size() << " networks:"; 754 LOG(LS_INFO) << "NetworkManager detected " << list.size() << " networks:";
722 for (const Network* network : list) { 755 for (const Network* network : list) {
723 if (!network->ignored() || include_ignored) { 756 if (!network->ignored() || include_ignored) {
724 LOG(LS_INFO) << network->ToString() << ": " 757 LOG(LS_INFO) << network->ToString() << ": "
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 std::stringstream ss; 845 std::stringstream ss;
813 // Print out the first space-terminated token of the network desc, plus 846 // Print out the first space-terminated token of the network desc, plus
814 // the IP address. 847 // the IP address.
815 ss << "Net[" << description_.substr(0, description_.find(' ')) 848 ss << "Net[" << description_.substr(0, description_.find(' '))
816 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ 849 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_
817 << ":" << AdapterTypeToString(type_) << "]"; 850 << ":" << AdapterTypeToString(type_) << "]";
818 return ss.str(); 851 return ss.str();
819 } 852 }
820 853
821 } // namespace rtc 854 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698