Index: chrome/browser/extensions/api/socket/udp_socket.cc |
diff --git a/chrome/browser/extensions/api/socket/udp_socket.cc b/chrome/browser/extensions/api/socket/udp_socket.cc |
index 467df66ec93da7f6584fa6dbc6fcece4a3023b5f..d83b484418feacccac27405409c9f22ed2b04883 100644 |
--- a/chrome/browser/extensions/api/socket/udp_socket.cc |
+++ b/chrome/browser/extensions/api/socket/udp_socket.cc |
@@ -13,29 +13,26 @@ |
namespace extensions { |
-UDPSocket::UDPSocket(const std::string& address, int port, |
- APIResourceEventNotifier* event_notifier) |
- : Socket(address, port, event_notifier), |
- socket_(new net::UDPClientSocket(net::DatagramSocket::DEFAULT_BIND, |
- net::RandIntCallback(), |
- NULL, |
- net::NetLog::Source())) { |
+UDPSocket::UDPSocket(APIResourceEventNotifier* event_notifier) |
+ : Socket(event_notifier), |
+ socket_(new net::UDPSocket(net::DatagramSocket::DEFAULT_BIND, |
+ net::RandIntCallback(), |
+ NULL, |
+ net::NetLog::Source())) { |
} |
// For testing. |
-UDPSocket::UDPSocket(net::DatagramClientSocket* datagram_client_socket, |
- const std::string& address, int port, |
+UDPSocket::UDPSocket(net::UDPSocket* udp_socket, |
APIResourceEventNotifier* event_notifier) |
- : Socket(address, port, event_notifier), |
- socket_(datagram_client_socket) { |
+ : Socket(event_notifier), |
+ socket_(udp_socket) { |
} |
// static |
UDPSocket* UDPSocket::CreateSocketForTesting( |
- net::DatagramClientSocket* datagram_client_socket, |
- const std::string& address, int port, |
+ net::UDPSocket* udp_socket, |
APIResourceEventNotifier* event_notifier) { |
- return new UDPSocket(datagram_client_socket, address, port, event_notifier); |
+ return new UDPSocket(udp_socket, event_notifier); |
} |
UDPSocket::~UDPSocket() { |
@@ -48,16 +45,25 @@ bool UDPSocket::IsValid() { |
return socket_ != NULL; |
} |
-net::Socket* UDPSocket::socket() { |
- return socket_.get(); |
+int UDPSocket::Connect(const std::string& address, int port) { |
+ if (is_connected_) |
+ return net::ERR_CONNECTION_FAILED; |
+ |
+ net::IPEndPoint ip_end_point; |
+ if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) |
+ return net::ERR_INVALID_ARGUMENT; |
+ |
+ int result = socket_->Connect(ip_end_point); |
+ is_connected_ = (result == net::OK); |
+ return result; |
} |
-int UDPSocket::Connect() { |
- net::IPAddressNumber ip_number; |
- if (!net::ParseIPLiteralToNumber(address_, &ip_number)) |
+int UDPSocket::Bind(const std::string& address, int port) { |
+ net::IPEndPoint ip_end_point; |
+ if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) |
return net::ERR_INVALID_ARGUMENT; |
- int result = socket_->Connect(net::IPEndPoint(ip_number, port_)); |
- is_connected_ = result == net::OK; |
+ |
+ int result = socket_->Bind(ip_end_point); |
return result; |
} |
@@ -66,4 +72,44 @@ void UDPSocket::Disconnect() { |
socket_->Close(); |
} |
+int UDPSocket::Read(scoped_refptr<net::IOBuffer> io_buffer, int io_buffer_len) { |
+ return socket_->Read( |
+ io_buffer.get(), |
+ io_buffer_len, |
+ base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer, |
+ (net::IPEndPoint *)NULL)); |
+} |
+ |
+int UDPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) { |
+ return socket_->Write( |
+ io_buffer.get(), byte_count, |
+ base::Bind(&Socket::OnWriteComplete, base::Unretained(this))); |
+} |
+ |
+int UDPSocket::RecvFrom(scoped_refptr<net::IOBuffer> io_buffer, |
+ int io_buffer_len, |
+ net::IPEndPoint *address) { |
+ return socket_->RecvFrom( |
+ io_buffer.get(), |
+ io_buffer_len, |
+ address, |
+ base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer, |
+ address)); |
+} |
+ |
+int UDPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, |
+ int byte_count, |
+ const std::string& address, |
+ int port) { |
+ net::IPEndPoint ip_end_point; |
+ if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) |
+ return net::ERR_INVALID_ARGUMENT; |
+ return socket_->SendTo( |
+ io_buffer.get(), |
+ byte_count, |
+ ip_end_point, |
+ base::Bind(&Socket::OnWriteComplete, base::Unretained(this))); |
+} |
+ |
+ |
} // namespace extensions |