1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/socket/udp_socket.h"
#include "chrome/browser/extensions/api/api_resource.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/udp/datagram_socket.h"
#include "net/udp/udp_client_socket.h"
namespace extensions {
UDPSocket::UDPSocket(const std::string& owner_extension_id)
: Socket(owner_extension_id),
socket_(net::DatagramSocket::DEFAULT_BIND,
net::RandIntCallback(),
NULL,
net::NetLog::Source()) {
}
UDPSocket::~UDPSocket() {
if (is_connected_) {
Disconnect();
}
}
void UDPSocket::Connect(const std::string& address,
int port,
const CompletionCallback& callback) {
int result = net::ERR_CONNECTION_FAILED;
do {
if (is_connected_)
break;
net::IPEndPoint ip_end_point;
if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) {
result = net::ERR_ADDRESS_INVALID;
break;
}
result = socket_.Connect(ip_end_point);
is_connected_ = (result == net::OK);
} while (false);
callback.Run(result);
}
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;
return socket_.Bind(ip_end_point);
}
void UDPSocket::Disconnect() {
is_connected_ = false;
socket_.Close();
}
void UDPSocket::Read(int count,
const ReadCompletionCallback& callback) {
DCHECK(!callback.is_null());
if (!read_callback_.is_null()) {
callback.Run(net::ERR_IO_PENDING, NULL);
return;
} else {
read_callback_ = callback;
}
int result = net::ERR_FAILED;
scoped_refptr<net::IOBuffer> io_buffer;
do {
if (count < 0) {
result = net::ERR_INVALID_ARGUMENT;
break;
}
if (!socket_.is_connected()) {
result = net::ERR_SOCKET_NOT_CONNECTED;
break;
}
io_buffer = new net::IOBuffer(count);
result = socket_.Read(io_buffer.get(), count,
base::Bind(&UDPSocket::OnReadComplete, base::Unretained(this),
io_buffer));
} while (false);
if (result != net::ERR_IO_PENDING)
OnReadComplete(io_buffer, result);
}
int UDPSocket::WriteImpl(net::IOBuffer* io_buffer,
int io_buffer_size,
const net::CompletionCallback& callback) {
if (!socket_.is_connected())
return net::ERR_SOCKET_NOT_CONNECTED;
else
return socket_.Write(io_buffer, io_buffer_size, callback);
}
void UDPSocket::RecvFrom(int count,
const RecvFromCompletionCallback& callback) {
DCHECK(!callback.is_null());
if (!recv_from_callback_.is_null()) {
callback.Run(net::ERR_IO_PENDING, NULL, std::string(), 0);
return;
} else {
recv_from_callback_ = callback;
}
int result = net::ERR_FAILED;
scoped_refptr<net::IOBuffer> io_buffer;
scoped_refptr<IPEndPoint> address;
do {
if (count < 0) {
result = net::ERR_INVALID_ARGUMENT;
break;
}
if (!socket_.is_connected()) {
result = net::ERR_SOCKET_NOT_CONNECTED;
break;
}
io_buffer = new net::IOBuffer(count);
address = new IPEndPoint();
result = socket_.RecvFrom(
io_buffer.get(),
count,
&address->data,
base::Bind(&UDPSocket::OnRecvFromComplete,
base::Unretained(this),
io_buffer,
address));
} while (false);
if (result != net::ERR_IO_PENDING)
OnRecvFromComplete(io_buffer, address, result);
}
void UDPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer,
int byte_count,
const std::string& address,
int port,
const CompletionCallback& callback) {
DCHECK(!callback.is_null());
if (!send_to_callback_.is_null()) {
// TODO(penghuang): Put requests in a pending queue to support multiple
// sendTo calls.
callback.Run(net::ERR_IO_PENDING);
return;
} else {
send_to_callback_ = callback;
}
int result = net::ERR_FAILED;
do {
net::IPEndPoint ip_end_point;
if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) {
result = net::ERR_ADDRESS_INVALID;
break;
}
if (!socket_.is_connected()) {
result = net::ERR_SOCKET_NOT_CONNECTED;
break;
}
result = socket_.SendTo(
io_buffer.get(),
byte_count,
ip_end_point,
base::Bind(&UDPSocket::OnSendToComplete, base::Unretained(this)));
} while (false);
if (result != net::ERR_IO_PENDING)
OnSendToComplete(result);
}
bool UDPSocket::GetPeerAddress(net::IPEndPoint* address) {
return !socket_.GetPeerAddress(address);
}
bool UDPSocket::GetLocalAddress(net::IPEndPoint* address) {
return !socket_.GetLocalAddress(address);
}
Socket::SocketType UDPSocket::GetSocketType() const {
return Socket::TYPE_UDP;
}
void UDPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer,
int result) {
DCHECK(!read_callback_.is_null());
read_callback_.Run(result, io_buffer);
read_callback_.Reset();
}
void UDPSocket::OnRecvFromComplete(scoped_refptr<net::IOBuffer> io_buffer,
scoped_refptr<IPEndPoint> address,
int result) {
DCHECK(!recv_from_callback_.is_null());
std::string ip;
int port = 0;
if (result > 0 && address.get()) {
IPEndPointToStringAndPort(address->data, &ip, &port);
}
recv_from_callback_.Run(result, io_buffer, ip, port);
recv_from_callback_.Reset();
}
void UDPSocket::OnSendToComplete(int result) {
DCHECK(!send_to_callback_.is_null());
send_to_callback_.Run(result);
send_to_callback_.Reset();
}
} // namespace extensions
|