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. |
| 41 for (int attempt = 0; attempt < 2; attempt++) { |
| 42 NetworkChangeNotifier::NetworkHandle network = |
| 43 NetworkChangeNotifier::GetDefaultNetwork(); |
| 44 if (network == NetworkChangeNotifier::kInvalidNetworkHandle) |
| 45 return ERR_INTERNET_DISCONNECTED; |
| 46 rv = BindToNetwork(network); |
| 47 // |network| may have disconnected between the call to GetDefaultNetwork() |
| 48 // and the call to BindToNetwork(). Loop if this is the case (|rv| will be |
| 49 // ERR_NETWORK_CHANGED). |
| 50 if (rv != ERR_NETWORK_CHANGED) |
| 51 return rv; |
| 52 } |
| 53 return rv; |
| 54 } |
| 55 |
| 56 NetworkChangeNotifier::NetworkHandle UDPClientSocket::GetBoundNetwork() { |
| 57 return network_; |
25 } | 58 } |
26 | 59 |
27 int UDPClientSocket::Connect(const IPEndPoint& address) { | 60 int UDPClientSocket::Connect(const IPEndPoint& address) { |
28 int rv = socket_.Open(address.GetFamily()); | 61 int rv = socket_.Open(address.GetFamily()); |
29 if (rv != OK) | 62 if (rv != OK) |
30 return rv; | 63 return rv; |
31 return socket_.Connect(address); | 64 return socket_.Connect(address); |
32 } | 65 } |
33 | 66 |
34 int UDPClientSocket::Read(IOBuffer* buf, | 67 int UDPClientSocket::Read(IOBuffer* buf, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 return socket_.NetLog(); | 100 return socket_.NetLog(); |
68 } | 101 } |
69 | 102 |
70 #if defined(OS_WIN) | 103 #if defined(OS_WIN) |
71 void UDPClientSocket::UseNonBlockingIO() { | 104 void UDPClientSocket::UseNonBlockingIO() { |
72 socket_.UseNonBlockingIO(); | 105 socket_.UseNonBlockingIO(); |
73 } | 106 } |
74 #endif | 107 #endif |
75 | 108 |
76 } // namespace net | 109 } // namespace net |
OLD | NEW |