OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/socket/udp_socket.h" | 5 #include "extensions/browser/api/socket/udp_socket.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "extensions/browser/api/api_resource.h" | 10 #include "extensions/browser/api/api_resource.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 do { | 42 do { |
43 if (is_connected_) | 43 if (is_connected_) |
44 break; | 44 break; |
45 | 45 |
46 net::IPEndPoint ip_end_point; | 46 net::IPEndPoint ip_end_point; |
47 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) { | 47 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) { |
48 result = net::ERR_ADDRESS_INVALID; | 48 result = net::ERR_ADDRESS_INVALID; |
49 break; | 49 break; |
50 } | 50 } |
51 | 51 |
| 52 result = socket_.Open(ip_end_point.GetFamily()); |
| 53 if (result != net::OK) |
| 54 break; |
| 55 |
52 result = socket_.Connect(ip_end_point); | 56 result = socket_.Connect(ip_end_point); |
53 is_connected_ = (result == net::OK); | 57 if (result != net::OK) { |
| 58 socket_.Close(); |
| 59 break; |
| 60 } |
| 61 is_connected_ = true; |
54 } while (false); | 62 } while (false); |
55 | 63 |
56 callback.Run(result); | 64 callback.Run(result); |
57 } | 65 } |
58 | 66 |
59 int UDPSocket::Bind(const std::string& address, uint16 port) { | 67 int UDPSocket::Bind(const std::string& address, uint16 port) { |
60 if (IsBound()) | 68 if (IsBound()) |
61 return net::ERR_CONNECTION_FAILED; | 69 return net::ERR_CONNECTION_FAILED; |
62 | 70 |
63 net::IPEndPoint ip_end_point; | 71 net::IPEndPoint ip_end_point; |
64 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) | 72 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) |
65 return net::ERR_INVALID_ARGUMENT; | 73 return net::ERR_INVALID_ARGUMENT; |
66 | 74 |
67 return socket_.Bind(ip_end_point); | 75 int result = socket_.Open(ip_end_point.GetFamily()); |
| 76 if (result != net::OK) |
| 77 return result; |
| 78 |
| 79 result = socket_.Bind(ip_end_point); |
| 80 if (result != net::OK) |
| 81 socket_.Close(); |
| 82 return result; |
68 } | 83 } |
69 | 84 |
70 void UDPSocket::Disconnect() { | 85 void UDPSocket::Disconnect() { |
71 is_connected_ = false; | 86 is_connected_ = false; |
72 socket_.Close(); | 87 socket_.Close(); |
73 read_callback_.Reset(); | 88 read_callback_.Reset(); |
74 recv_from_callback_.Reset(); | 89 recv_from_callback_.Reset(); |
75 send_to_callback_.Reset(); | 90 send_to_callback_.Reset(); |
76 multicast_groups_.clear(); | 91 multicast_groups_.clear(); |
77 } | 92 } |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 | 303 |
289 ResumableUDPSocket::ResumableUDPSocket(const std::string& owner_extension_id) | 304 ResumableUDPSocket::ResumableUDPSocket(const std::string& owner_extension_id) |
290 : UDPSocket(owner_extension_id), | 305 : UDPSocket(owner_extension_id), |
291 persistent_(false), | 306 persistent_(false), |
292 buffer_size_(0), | 307 buffer_size_(0), |
293 paused_(false) {} | 308 paused_(false) {} |
294 | 309 |
295 bool ResumableUDPSocket::IsPersistent() const { return persistent(); } | 310 bool ResumableUDPSocket::IsPersistent() const { return persistent(); } |
296 | 311 |
297 } // namespace extensions | 312 } // namespace extensions |
OLD | NEW |