Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "net/udp/udp_client_socket.h" | 5 #include "net/udp/udp_client_socket.h" |
| 6 | 6 |
| 7 #include "net/base/net_errors.h" | 7 #include "net/base/net_errors.h" |
| 8 #include "net/log/net_log.h" | 8 #include "net/log/net_log.h" |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| 11 | 11 |
| 12 UDPClientSocket::UDPClientSocket(DatagramSocket::BindType bind_type, | 12 UDPClientSocket::UDPClientSocket(DatagramSocket::BindType bind_type, |
| 13 const RandIntCallback& rand_int_cb, | 13 const RandIntCallback& rand_int_cb, |
| 14 net::NetLog* net_log, | 14 net::NetLog* net_log, |
| 15 const net::NetLog::Source& source) | 15 const net::NetLog::Source& source) |
| 16 : socket_(bind_type, rand_int_cb, net_log, source) { | 16 : socket_(bind_type, rand_int_cb, net_log, source), |
| 17 } | 17 network_(NetworkChangeNotifier::kInvalidNetworkHandle) {} |
| 18 | 18 |
| 19 UDPClientSocket::~UDPClientSocket() { | 19 UDPClientSocket::~UDPClientSocket() { |
| 20 } | 20 } |
| 21 | 21 |
| 22 int UDPClientSocket::BindToNetwork( | 22 int UDPClientSocket::BindToNetwork( |
| 23 NetworkChangeNotifier::NetworkHandle network) { | 23 NetworkChangeNotifier::NetworkHandle network) { |
| 24 return socket_.BindToNetwork(network); | 24 int rv = socket_.BindToNetwork(network); |
| 25 if (rv == OK) | |
| 26 network_ = network; | |
| 27 return rv; | |
| 28 } | |
| 29 | |
| 30 int UDPClientSocket::BindToDefaultNetwork() { | |
| 31 if (!NetworkChangeNotifier::AreNetworkHandlesSupported()) | |
| 32 return ERR_NOT_IMPLEMENTED; | |
| 33 int rv; | |
| 34 // Calling connect() will bind a socket to the default network, however there | |
| 35 // is no way to determine what network the socket got bound to. The | |
| 36 // alternative is to query what the default network is and bind the socket to | |
| 37 // that network explicitly, however this is racy because the default network | |
| 38 // can change in between when we query it and when we bind to it. This is | |
| 39 // rare but should be accounted for. Since changes of the default network | |
| 40 // should not come in quick succession, we can simply try again. Loop up to | |
| 41 // three times for good measure. | |
| 42 for (int attempt = 0; attempt < 3; attempt++) { | |
|
Jana
2015/12/04 20:30:16
attempt < 2 should be enough for this, right? is i
pauljensen
2015/12/07 13:05:55
I think 2 might be enough but as I said in the com
Jana
2015/12/07 17:30:52
I don't think it's a matter of cost; it's a matter
pauljensen
2015/12/08 14:31:19
I changed it to 2.
| |
| 43 NetworkChangeNotifier::NetworkHandle network = | |
| 44 NetworkChangeNotifier::GetDefaultNetwork(); | |
| 45 if (network == NetworkChangeNotifier::kInvalidNetworkHandle) | |
| 46 return ERR_INTERNET_DISCONNECTED; | |
| 47 rv = BindToNetwork(network); | |
| 48 // |network| may have disconnected between the call to GetDefaultNetwork() | |
| 49 // and the call to BindToNetwork(). Loop if this is the case (|rv| will be | |
| 50 // ERR_NETWORK_CHANGED). | |
| 51 if (rv != ERR_NETWORK_CHANGED) | |
| 52 return rv; | |
| 53 } | |
| 54 return rv; | |
| 55 } | |
| 56 | |
| 57 NetworkChangeNotifier::NetworkHandle UDPClientSocket::GetBoundNetwork() { | |
| 58 return network_; | |
| 25 } | 59 } |
| 26 | 60 |
| 27 int UDPClientSocket::Connect(const IPEndPoint& address) { | 61 int UDPClientSocket::Connect(const IPEndPoint& address) { |
| 28 int rv = socket_.Open(address.GetFamily()); | 62 int rv = socket_.Open(address.GetFamily()); |
| 29 if (rv != OK) | 63 if (rv != OK) |
| 30 return rv; | 64 return rv; |
| 31 return socket_.Connect(address); | 65 return socket_.Connect(address); |
| 32 } | 66 } |
| 33 | 67 |
| 34 int UDPClientSocket::Read(IOBuffer* buf, | 68 int UDPClientSocket::Read(IOBuffer* buf, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 return socket_.NetLog(); | 101 return socket_.NetLog(); |
| 68 } | 102 } |
| 69 | 103 |
| 70 #if defined(OS_WIN) | 104 #if defined(OS_WIN) |
| 71 void UDPClientSocket::UseNonBlockingIO() { | 105 void UDPClientSocket::UseNonBlockingIO() { |
| 72 socket_.UseNonBlockingIO(); | 106 socket_.UseNonBlockingIO(); |
| 73 } | 107 } |
| 74 #endif | 108 #endif |
| 75 | 109 |
| 76 } // namespace net | 110 } // namespace net |
| OLD | NEW |