| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/notifier/base/async_network_alive.h" | 5 #include "chrome/browser/sync/notifier/base/async_network_alive.h" |
| 6 | 6 |
| 7 #include <winsock2.h> | 7 #include <winsock2.h> |
| 8 | 8 |
| 9 #include "base/scoped_handle_win.h" | 9 #include "base/scoped_handle_win.h" |
| 10 #include "chrome/browser/sync/notifier/base/utils.h" | 10 #include "chrome/browser/sync/notifier/base/utils.h" |
| 11 #include "talk/base/common.h" | 11 #include "talk/base/common.h" |
| 12 #include "talk/base/criticalsection.h" | 12 #include "talk/base/criticalsection.h" |
| 13 #include "talk/base/logging.h" | 13 #include "talk/base/logging.h" |
| 14 #include "talk/base/physicalsocketserver.h" |
| 14 #include "talk/base/scoped_ptr.h" | 15 #include "talk/base/scoped_ptr.h" |
| 15 | 16 |
| 16 namespace notifier { | 17 namespace notifier { |
| 17 | 18 |
| 18 class PlatformNetworkInfo { | 19 class PlatformNetworkInfo { |
| 19 public: | 20 public: |
| 20 PlatformNetworkInfo() : ws_handle_(NULL), event_handle_(NULL) { | 21 PlatformNetworkInfo() : ws_handle_(NULL), event_handle_(NULL) { |
| 21 } | 22 } |
| 22 | 23 |
| 23 ~PlatformNetworkInfo() { | 24 ~PlatformNetworkInfo() { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 if (result == WSAEFAULT) { | 96 if (result == WSAEFAULT) { |
| 96 alive = true; | 97 alive = true; |
| 97 flush_previous_result = true; | 98 flush_previous_result = true; |
| 98 } else { | 99 } else { |
| 99 LOG(WARNING) << "failed:" << result; | 100 LOG(WARNING) << "failed:" << result; |
| 100 *error = true; | 101 *error = true; |
| 101 break; | 102 break; |
| 102 } | 103 } |
| 103 } | 104 } |
| 104 } while (true); | 105 } while (true); |
| 106 |
| 105 LOG(INFO) << "alive: " << alive; | 107 LOG(INFO) << "alive: " << alive; |
| 106 return alive; | 108 return alive; |
| 107 } | 109 } |
| 108 | 110 |
| 109 bool WaitForChange() { | 111 bool WaitForChange() { |
| 110 // IsAlive must be called first. | 112 // IsAlive must be called first. |
| 111 int junk1 = 0, junk2 = 0; | 113 int junk1 = 0, junk2 = 0; |
| 112 DWORD bytes_returned = 0; | 114 DWORD bytes_returned = 0; |
| 113 int result = SOCKET_ERROR; | 115 int result = SOCKET_ERROR; |
| 114 { | 116 { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 } else { | 210 } else { |
| 209 // Since network_info is set, it means that | 211 // Since network_info is set, it means that |
| 210 // we are suppose to wait for network state changes. | 212 // we are suppose to wait for network state changes. |
| 211 if (!network_info_->WaitForChange()) { | 213 if (!network_info_->WaitForChange()) { |
| 212 // The wait was aborted so we must be shutting down. | 214 // The wait was aborted so we must be shutting down. |
| 213 alive_ = false; | 215 alive_ = false; |
| 214 error_ = true; | 216 error_ = true; |
| 215 return; | 217 return; |
| 216 } | 218 } |
| 217 } | 219 } |
| 218 alive_ = network_info_->IsAlive(&error_); | 220 |
| 221 if (network_info_->IsAlive(&error_)) { |
| 222 // If there is an active connection, check that www.google.com:80 |
| 223 // is reachable. |
| 224 talk_base::PhysicalSocketServer physical; |
| 225 scoped_ptr<talk_base::Socket> socket(physical.CreateSocket(SOCK_STREAM)); |
| 226 if (socket->Connect(talk_base::SocketAddress("talk.google.com", 5222))) { |
| 227 alive_ = false; |
| 228 } else { |
| 229 alive_ = true; |
| 230 } |
| 231 } else { |
| 232 // If there are no available connections, then we aren't alive. |
| 233 alive_ = false; |
| 234 } |
| 219 } | 235 } |
| 220 | 236 |
| 221 virtual void OnWorkStop() { | 237 virtual void OnWorkStop() { |
| 222 if (network_info_) { | 238 if (network_info_) { |
| 223 network_info_->Close(); | 239 network_info_->Close(); |
| 224 } | 240 } |
| 225 } | 241 } |
| 226 | 242 |
| 227 private: | 243 private: |
| 228 DISALLOW_COPY_AND_ASSIGN(AsyncNetworkAliveWin32); | 244 DISALLOW_COPY_AND_ASSIGN(AsyncNetworkAliveWin32); |
| 229 }; | 245 }; |
| 230 | 246 |
| 231 AsyncNetworkAlive* AsyncNetworkAlive::Create() { | 247 AsyncNetworkAlive* AsyncNetworkAlive::Create() { |
| 232 return new AsyncNetworkAliveWin32(); | 248 return new AsyncNetworkAliveWin32(); |
| 233 } | 249 } |
| 234 | 250 |
| 235 } // namespace notifier | 251 } // namespace notifier |
| OLD | NEW |