Index: net/udp/udp_client_socket.cc |
diff --git a/net/udp/udp_client_socket.cc b/net/udp/udp_client_socket.cc |
index a991f10bd0c527a4cf211867a975f1778e7a1507..6b66fe1b08c555c10876a5afff500ec36b1bce70 100644 |
--- a/net/udp/udp_client_socket.cc |
+++ b/net/udp/udp_client_socket.cc |
@@ -13,15 +13,49 @@ UDPClientSocket::UDPClientSocket(DatagramSocket::BindType bind_type, |
const RandIntCallback& rand_int_cb, |
net::NetLog* net_log, |
const net::NetLog::Source& source) |
- : socket_(bind_type, rand_int_cb, net_log, source) { |
-} |
+ : socket_(bind_type, rand_int_cb, net_log, source), |
+ network_(NetworkChangeNotifier::kInvalidNetworkHandle) {} |
UDPClientSocket::~UDPClientSocket() { |
} |
int UDPClientSocket::BindToNetwork( |
NetworkChangeNotifier::NetworkHandle network) { |
- return socket_.BindToNetwork(network); |
+ int rv = socket_.BindToNetwork(network); |
+ if (rv == OK) |
+ network_ = network; |
+ return rv; |
+} |
+ |
+int UDPClientSocket::BindToDefaultNetwork() { |
+ if (!NetworkChangeNotifier::AreNetworkHandlesSupported()) |
+ return ERR_NOT_IMPLEMENTED; |
+ int rv; |
+ // Calling connect() will bind a socket to the default network, however there |
+ // is no way to determine what network the socket got bound to. The |
+ // alternative is to query what the default network is and bind the socket to |
+ // that network explicitly, however this is racy because the default network |
+ // can change in between when we query it and when we bind to it. This is |
+ // rare but should be accounted for. Since changes of the default network |
+ // should not come in quick succession, we can simply try again. Loop up to |
+ // three times for good measure. |
+ 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.
|
+ NetworkChangeNotifier::NetworkHandle network = |
+ NetworkChangeNotifier::GetDefaultNetwork(); |
+ if (network == NetworkChangeNotifier::kInvalidNetworkHandle) |
+ return ERR_INTERNET_DISCONNECTED; |
+ rv = BindToNetwork(network); |
+ // |network| may have disconnected between the call to GetDefaultNetwork() |
+ // and the call to BindToNetwork(). Loop if this is the case (|rv| will be |
+ // ERR_NETWORK_CHANGED). |
+ if (rv != ERR_NETWORK_CHANGED) |
+ return rv; |
+ } |
+ return rv; |
+} |
+ |
+NetworkChangeNotifier::NetworkHandle UDPClientSocket::GetBoundNetwork() { |
+ return network_; |
} |
int UDPClientSocket::Connect(const IPEndPoint& address) { |