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 18 matching lines...) Expand all Loading... |
29 UDPSocket::UDPSocket(const std::string& owner_extension_id) | 29 UDPSocket::UDPSocket(const std::string& owner_extension_id) |
30 : Socket(owner_extension_id), | 30 : Socket(owner_extension_id), |
31 socket_(net::DatagramSocket::DEFAULT_BIND, | 31 socket_(net::DatagramSocket::DEFAULT_BIND, |
32 net::RandIntCallback(), | 32 net::RandIntCallback(), |
33 NULL, | 33 NULL, |
34 net::NetLog::Source()) {} | 34 net::NetLog::Source()) {} |
35 | 35 |
36 UDPSocket::~UDPSocket() { Disconnect(); } | 36 UDPSocket::~UDPSocket() { Disconnect(); } |
37 | 37 |
38 void UDPSocket::Connect(const std::string& address, | 38 void UDPSocket::Connect(const std::string& address, |
39 int port, | 39 uint16 port, |
40 const CompletionCallback& callback) { | 40 const CompletionCallback& callback) { |
41 int result = net::ERR_CONNECTION_FAILED; | 41 int result = net::ERR_CONNECTION_FAILED; |
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_.Connect(ip_end_point); | 52 result = socket_.Connect(ip_end_point); |
53 is_connected_ = (result == net::OK); | 53 is_connected_ = (result == net::OK); |
54 } while (false); | 54 } while (false); |
55 | 55 |
56 callback.Run(result); | 56 callback.Run(result); |
57 } | 57 } |
58 | 58 |
59 int UDPSocket::Bind(const std::string& address, int port) { | 59 int UDPSocket::Bind(const std::string& address, uint16 port) { |
60 if (IsBound()) | 60 if (IsBound()) |
61 return net::ERR_CONNECTION_FAILED; | 61 return net::ERR_CONNECTION_FAILED; |
62 | 62 |
63 net::IPEndPoint ip_end_point; | 63 net::IPEndPoint ip_end_point; |
64 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) | 64 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) |
65 return net::ERR_INVALID_ARGUMENT; | 65 return net::ERR_INVALID_ARGUMENT; |
66 | 66 |
67 return socket_.Bind(ip_end_point); | 67 return socket_.Bind(ip_end_point); |
68 } | 68 } |
69 | 69 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 address)); | 156 address)); |
157 } while (false); | 157 } while (false); |
158 | 158 |
159 if (result != net::ERR_IO_PENDING) | 159 if (result != net::ERR_IO_PENDING) |
160 OnRecvFromComplete(io_buffer, address, result); | 160 OnRecvFromComplete(io_buffer, address, result); |
161 } | 161 } |
162 | 162 |
163 void UDPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, | 163 void UDPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
164 int byte_count, | 164 int byte_count, |
165 const std::string& address, | 165 const std::string& address, |
166 int port, | 166 uint16 port, |
167 const CompletionCallback& callback) { | 167 const CompletionCallback& callback) { |
168 DCHECK(!callback.is_null()); | 168 DCHECK(!callback.is_null()); |
169 | 169 |
170 if (!send_to_callback_.is_null()) { | 170 if (!send_to_callback_.is_null()) { |
171 // TODO(penghuang): Put requests in a pending queue to support multiple | 171 // TODO(penghuang): Put requests in a pending queue to support multiple |
172 // sendTo calls. | 172 // sendTo calls. |
173 callback.Run(net::ERR_IO_PENDING); | 173 callback.Run(net::ERR_IO_PENDING); |
174 return; | 174 return; |
175 } else { | 175 } else { |
176 send_to_callback_ = callback; | 176 send_to_callback_ = callback; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 DCHECK(!read_callback_.is_null()); | 217 DCHECK(!read_callback_.is_null()); |
218 read_callback_.Run(result, io_buffer); | 218 read_callback_.Run(result, io_buffer); |
219 read_callback_.Reset(); | 219 read_callback_.Reset(); |
220 } | 220 } |
221 | 221 |
222 void UDPSocket::OnRecvFromComplete(scoped_refptr<net::IOBuffer> io_buffer, | 222 void UDPSocket::OnRecvFromComplete(scoped_refptr<net::IOBuffer> io_buffer, |
223 scoped_refptr<IPEndPoint> address, | 223 scoped_refptr<IPEndPoint> address, |
224 int result) { | 224 int result) { |
225 DCHECK(!recv_from_callback_.is_null()); | 225 DCHECK(!recv_from_callback_.is_null()); |
226 std::string ip; | 226 std::string ip; |
227 int port = 0; | 227 uint16 port = 0; |
228 if (result > 0 && address.get()) { | 228 if (result > 0 && address.get()) { |
229 IPEndPointToStringAndPort(address->data, &ip, &port); | 229 IPEndPointToStringAndPort(address->data, &ip, &port); |
230 } | 230 } |
231 recv_from_callback_.Run(result, io_buffer, ip, port); | 231 recv_from_callback_.Run(result, io_buffer, ip, port); |
232 recv_from_callback_.Reset(); | 232 recv_from_callback_.Reset(); |
233 } | 233 } |
234 | 234 |
235 void UDPSocket::OnSendToComplete(int result) { | 235 void UDPSocket::OnSendToComplete(int result) { |
236 DCHECK(!send_to_callback_.is_null()); | 236 DCHECK(!send_to_callback_.is_null()); |
237 send_to_callback_.Run(result); | 237 send_to_callback_.Run(result); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 | 288 |
289 ResumableUDPSocket::ResumableUDPSocket(const std::string& owner_extension_id) | 289 ResumableUDPSocket::ResumableUDPSocket(const std::string& owner_extension_id) |
290 : UDPSocket(owner_extension_id), | 290 : UDPSocket(owner_extension_id), |
291 persistent_(false), | 291 persistent_(false), |
292 buffer_size_(0), | 292 buffer_size_(0), |
293 paused_(false) {} | 293 paused_(false) {} |
294 | 294 |
295 bool ResumableUDPSocket::IsPersistent() const { return persistent(); } | 295 bool ResumableUDPSocket::IsPersistent() const { return persistent(); } |
296 | 296 |
297 } // namespace extensions | 297 } // namespace extensions |
OLD | NEW |