OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/socket/udp_socket.h" | 5 #include "chrome/browser/extensions/api/socket/udp_socket.h" |
6 | 6 |
7 #include "chrome/browser/extensions/api/api_resource.h" | 7 #include "chrome/browser/extensions/api/api_resource.h" |
8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" | 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" |
9 #include "net/base/ip_endpoint.h" | 9 #include "net/base/ip_endpoint.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 #include "net/udp/datagram_socket.h" | 11 #include "net/udp/datagram_socket.h" |
12 #include "net/udp/udp_client_socket.h" | 12 #include "net/udp/udp_client_socket.h" |
13 | 13 |
14 namespace extensions { | 14 namespace extensions { |
15 | 15 |
16 UDPSocket::UDPSocket(const std::string& address, int port, | 16 UDPSocket::UDPSocket(APIResourceEventNotifier* event_notifier) |
17 APIResourceEventNotifier* event_notifier) | 17 : Socket(event_notifier), |
18 : Socket(address, port, event_notifier), | 18 socket_(new net::UDPSocket(net::DatagramSocket::DEFAULT_BIND, |
19 socket_(new net::UDPClientSocket(net::DatagramSocket::DEFAULT_BIND, | 19 net::RandIntCallback(), |
20 net::RandIntCallback(), | 20 NULL, |
21 NULL, | 21 net::NetLog::Source())) { |
22 net::NetLog::Source())) { | |
23 } | |
24 | |
25 // For testing. | |
26 UDPSocket::UDPSocket(net::DatagramClientSocket* datagram_client_socket, | |
27 const std::string& address, int port, | |
28 APIResourceEventNotifier* event_notifier) | |
29 : Socket(address, port, event_notifier), | |
30 socket_(datagram_client_socket) { | |
31 } | |
32 | |
33 // static | |
34 UDPSocket* UDPSocket::CreateSocketForTesting( | |
35 net::DatagramClientSocket* datagram_client_socket, | |
36 const std::string& address, int port, | |
37 APIResourceEventNotifier* event_notifier) { | |
38 return new UDPSocket(datagram_client_socket, address, port, event_notifier); | |
39 } | 22 } |
40 | 23 |
41 UDPSocket::~UDPSocket() { | 24 UDPSocket::~UDPSocket() { |
42 if (is_connected_) { | 25 if (is_connected_) { |
43 Disconnect(); | 26 Disconnect(); |
44 } | 27 } |
45 } | 28 } |
46 | 29 |
47 bool UDPSocket::IsValid() { | 30 int UDPSocket::Connect(const std::string& address, int port) { |
48 return socket_ != NULL; | 31 if (is_connected_) |
49 } | 32 return net::ERR_CONNECTION_FAILED; |
50 | 33 |
51 net::Socket* UDPSocket::socket() { | 34 net::IPEndPoint ip_end_point; |
52 return socket_.get(); | 35 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) |
53 } | 36 return net::ERR_INVALID_ARGUMENT; |
54 | 37 |
55 int UDPSocket::Connect() { | 38 int result = socket_->Connect(ip_end_point); |
56 net::IPAddressNumber ip_number; | 39 is_connected_ = (result == net::OK); |
57 if (!net::ParseIPLiteralToNumber(address_, &ip_number)) | |
58 return net::ERR_INVALID_ARGUMENT; | |
59 int result = socket_->Connect(net::IPEndPoint(ip_number, port_)); | |
60 is_connected_ = result == net::OK; | |
61 return result; | 40 return result; |
62 } | 41 } |
63 | 42 |
| 43 int UDPSocket::Bind(const std::string& address, int port) { |
| 44 net::IPEndPoint ip_end_point; |
| 45 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) |
| 46 return net::ERR_INVALID_ARGUMENT; |
| 47 |
| 48 return socket_->Bind(ip_end_point); |
| 49 } |
| 50 |
64 void UDPSocket::Disconnect() { | 51 void UDPSocket::Disconnect() { |
65 is_connected_ = false; | 52 is_connected_ = false; |
66 socket_->Close(); | 53 socket_->Close(); |
67 } | 54 } |
68 | 55 |
| 56 int UDPSocket::Read(scoped_refptr<net::IOBuffer> io_buffer, int io_buffer_len) { |
| 57 return socket_->Read( |
| 58 io_buffer.get(), |
| 59 io_buffer_len, |
| 60 base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer, |
| 61 static_cast<net::IPEndPoint*>(NULL))); |
| 62 } |
| 63 |
| 64 int UDPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) { |
| 65 return socket_->Write( |
| 66 io_buffer.get(), byte_count, |
| 67 base::Bind(&Socket::OnWriteComplete, base::Unretained(this))); |
| 68 } |
| 69 |
| 70 int UDPSocket::RecvFrom(scoped_refptr<net::IOBuffer> io_buffer, |
| 71 int io_buffer_len, |
| 72 net::IPEndPoint* address) { |
| 73 return socket_->RecvFrom( |
| 74 io_buffer.get(), |
| 75 io_buffer_len, |
| 76 address, |
| 77 base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer, |
| 78 address)); |
| 79 } |
| 80 |
| 81 int UDPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
| 82 int byte_count, |
| 83 const std::string& address, |
| 84 int port) { |
| 85 net::IPEndPoint ip_end_point; |
| 86 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) |
| 87 return net::ERR_INVALID_ARGUMENT; |
| 88 return socket_->SendTo( |
| 89 io_buffer.get(), |
| 90 byte_count, |
| 91 ip_end_point, |
| 92 base::Bind(&Socket::OnWriteComplete, base::Unretained(this))); |
| 93 } |
| 94 |
69 } // namespace extensions | 95 } // namespace extensions |
OLD | NEW |