Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Side by Side Diff: chrome/browser/extensions/api/socket/udp_socket.cc

Issue 10134008: Add bind(), recvFrom(), sendTo() for UDP socket. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: wip Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 } 22 }
24 23
25 // For testing. 24 // For testing.
26 UDPSocket::UDPSocket(net::DatagramClientSocket* datagram_client_socket, 25 UDPSocket::UDPSocket(net::UDPSocket* udp_socket,
27 const std::string& address, int port,
28 APIResourceEventNotifier* event_notifier) 26 APIResourceEventNotifier* event_notifier)
29 : Socket(address, port, event_notifier), 27 : Socket(event_notifier),
30 socket_(datagram_client_socket) { 28 socket_(udp_socket) {
31 } 29 }
32 30
33 // static 31 // static
34 UDPSocket* UDPSocket::CreateSocketForTesting( 32 UDPSocket* UDPSocket::CreateSocketForTesting(
35 net::DatagramClientSocket* datagram_client_socket, 33 net::UDPSocket* udp_socket,
36 const std::string& address, int port,
37 APIResourceEventNotifier* event_notifier) { 34 APIResourceEventNotifier* event_notifier) {
38 return new UDPSocket(datagram_client_socket, address, port, event_notifier); 35 return new UDPSocket(udp_socket, event_notifier);
39 } 36 }
40 37
41 UDPSocket::~UDPSocket() { 38 UDPSocket::~UDPSocket() {
42 if (is_connected_) { 39 if (is_connected_) {
43 Disconnect(); 40 Disconnect();
44 } 41 }
45 } 42 }
46 43
47 bool UDPSocket::IsValid() { 44 bool UDPSocket::IsValid() {
48 return socket_ != NULL; 45 return socket_ != NULL;
49 } 46 }
50 47
51 net::Socket* UDPSocket::socket() { 48 int UDPSocket::Connect(const std::string& address, int port) {
52 return socket_.get(); 49 if (is_connected_)
53 } 50 return net::ERR_CONNECTION_FAILED;
54 51
55 int UDPSocket::Connect() { 52 net::IPEndPoint ip_end_point;
56 net::IPAddressNumber ip_number; 53 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point))
57 if (!net::ParseIPLiteralToNumber(address_, &ip_number))
58 return net::ERR_INVALID_ARGUMENT; 54 return net::ERR_INVALID_ARGUMENT;
59 int result = socket_->Connect(net::IPEndPoint(ip_number, port_)); 55
60 is_connected_ = result == net::OK; 56 int result = socket_->Connect(ip_end_point);
57 is_connected_ = (result == net::OK);
61 return result; 58 return result;
62 } 59 }
63 60
61 int UDPSocket::Bind(const std::string& address, int port) {
62 net::IPEndPoint ip_end_point;
63 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point))
64 return net::ERR_INVALID_ARGUMENT;
65
66 int result = socket_->Bind(ip_end_point);
67 return result;
68 }
69
64 void UDPSocket::Disconnect() { 70 void UDPSocket::Disconnect() {
65 is_connected_ = false; 71 is_connected_ = false;
66 socket_->Close(); 72 socket_->Close();
67 } 73 }
68 74
75 int UDPSocket::Read(scoped_refptr<net::IOBuffer> io_buffer, int io_buffer_len) {
76 return socket_->Read(
77 io_buffer.get(),
78 io_buffer_len,
79 base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer,
80 (net::IPEndPoint *)NULL));
81 }
82
83 int UDPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) {
84 return socket_->Write(
85 io_buffer.get(), byte_count,
86 base::Bind(&Socket::OnWriteComplete, base::Unretained(this)));
87 }
88
89 int UDPSocket::RecvFrom(scoped_refptr<net::IOBuffer> io_buffer,
90 int io_buffer_len,
91 net::IPEndPoint *address) {
92 return socket_->RecvFrom(
93 io_buffer.get(),
94 io_buffer_len,
95 address,
96 base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer,
97 address));
98 }
99
100 int UDPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer,
101 int byte_count,
102 const std::string& address,
103 int port) {
104 net::IPEndPoint ip_end_point;
105 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point))
106 return net::ERR_INVALID_ARGUMENT;
107 return socket_->SendTo(
108 io_buffer.get(),
109 byte_count,
110 ip_end_point,
111 base::Bind(&Socket::OnWriteComplete, base::Unretained(this)));
112 }
113
114
69 } // namespace extensions 115 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698