Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / remoting / protocol / chromium_socket_factory.cc
blob57fb33bad02674ac789bcb5f8da6cd4b0f4732f6
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
3 // found in the LICENSE file.
5 #include "remoting/protocol/chromium_socket_factory.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "jingle/glue/utils.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/base/net_errors.h"
14 #include "net/udp/udp_server_socket.h"
15 #include "remoting/protocol/socket_util.h"
16 #include "third_party/webrtc/base/asyncpacketsocket.h"
17 #include "third_party/webrtc/base/nethelpers.h"
19 namespace remoting {
20 namespace protocol {
22 namespace {
24 // Size of the buffer to allocate for RecvFrom().
25 const int kReceiveBufferSize = 65536;
27 // Maximum amount of data in the send buffers. This is necessary to
28 // prevent out-of-memory crashes if the caller sends data faster than
29 // Pepper's UDP API can handle it. This maximum should never be
30 // reached under normal conditions.
31 const int kMaxSendBufferSize = 256 * 1024;
33 class UdpPacketSocket : public rtc::AsyncPacketSocket {
34 public:
35 UdpPacketSocket();
36 virtual ~UdpPacketSocket();
38 bool Init(const rtc::SocketAddress& local_address,
39 int min_port, int max_port);
41 // rtc::AsyncPacketSocket interface.
42 virtual rtc::SocketAddress GetLocalAddress() const OVERRIDE;
43 virtual rtc::SocketAddress GetRemoteAddress() const OVERRIDE;
44 virtual int Send(const void* data, size_t data_size,
45 const rtc::PacketOptions& options) OVERRIDE;
46 virtual int SendTo(const void* data, size_t data_size,
47 const rtc::SocketAddress& address,
48 const rtc::PacketOptions& options) OVERRIDE;
49 virtual int Close() OVERRIDE;
50 virtual State GetState() const OVERRIDE;
51 virtual int GetOption(rtc::Socket::Option option, int* value) OVERRIDE;
52 virtual int SetOption(rtc::Socket::Option option, int value) OVERRIDE;
53 virtual int GetError() const OVERRIDE;
54 virtual void SetError(int error) OVERRIDE;
56 private:
57 struct PendingPacket {
58 PendingPacket(const void* buffer,
59 int buffer_size,
60 const net::IPEndPoint& address);
62 scoped_refptr<net::IOBufferWithSize> data;
63 net::IPEndPoint address;
64 bool retried;
67 void OnBindCompleted(int error);
69 void DoSend();
70 void OnSendCompleted(int result);
72 void DoRead();
73 void OnReadCompleted(int result);
74 void HandleReadResult(int result);
76 scoped_ptr<net::UDPServerSocket> socket_;
78 State state_;
79 int error_;
81 rtc::SocketAddress local_address_;
83 // Receive buffer and address are populated by asynchronous reads.
84 scoped_refptr<net::IOBuffer> receive_buffer_;
85 net::IPEndPoint receive_address_;
87 bool send_pending_;
88 std::list<PendingPacket> send_queue_;
89 int send_queue_size_;
91 DISALLOW_COPY_AND_ASSIGN(UdpPacketSocket);
94 UdpPacketSocket::PendingPacket::PendingPacket(
95 const void* buffer,
96 int buffer_size,
97 const net::IPEndPoint& address)
98 : data(new net::IOBufferWithSize(buffer_size)),
99 address(address),
100 retried(false) {
101 memcpy(data->data(), buffer, buffer_size);
104 UdpPacketSocket::UdpPacketSocket()
105 : state_(STATE_CLOSED),
106 error_(0),
107 send_pending_(false),
108 send_queue_size_(0) {
111 UdpPacketSocket::~UdpPacketSocket() {
112 Close();
115 bool UdpPacketSocket::Init(const rtc::SocketAddress& local_address,
116 int min_port, int max_port) {
117 net::IPEndPoint local_endpoint;
118 if (!jingle_glue::SocketAddressToIPEndPoint(
119 local_address, &local_endpoint)) {
120 return false;
123 for (int port = min_port; port <= max_port; ++port) {
124 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source()));
125 int result = socket_->Listen(
126 net::IPEndPoint(local_endpoint.address(), port));
127 if (result == net::OK) {
128 break;
129 } else {
130 socket_.reset();
134 if (!socket_.get()) {
135 // Failed to bind the socket.
136 return false;
139 if (socket_->GetLocalAddress(&local_endpoint) != net::OK ||
140 !jingle_glue::IPEndPointToSocketAddress(local_endpoint,
141 &local_address_)) {
142 return false;
145 state_ = STATE_BOUND;
146 DoRead();
148 return true;
151 rtc::SocketAddress UdpPacketSocket::GetLocalAddress() const {
152 DCHECK_EQ(state_, STATE_BOUND);
153 return local_address_;
156 rtc::SocketAddress UdpPacketSocket::GetRemoteAddress() const {
157 // UDP sockets are not connected - this method should never be called.
158 NOTREACHED();
159 return rtc::SocketAddress();
162 int UdpPacketSocket::Send(const void* data, size_t data_size,
163 const rtc::PacketOptions& options) {
164 // UDP sockets are not connected - this method should never be called.
165 NOTREACHED();
166 return EWOULDBLOCK;
169 int UdpPacketSocket::SendTo(const void* data, size_t data_size,
170 const rtc::SocketAddress& address,
171 const rtc::PacketOptions& options) {
172 if (state_ != STATE_BOUND) {
173 NOTREACHED();
174 return EINVAL;
177 if (error_ != 0) {
178 return error_;
181 net::IPEndPoint endpoint;
182 if (!jingle_glue::SocketAddressToIPEndPoint(address, &endpoint)) {
183 return EINVAL;
186 if (send_queue_size_ >= kMaxSendBufferSize) {
187 return EWOULDBLOCK;
190 send_queue_.push_back(PendingPacket(data, data_size, endpoint));
191 send_queue_size_ += data_size;
193 DoSend();
194 return data_size;
197 int UdpPacketSocket::Close() {
198 state_ = STATE_CLOSED;
199 socket_.reset();
200 return 0;
203 rtc::AsyncPacketSocket::State UdpPacketSocket::GetState() const {
204 return state_;
207 int UdpPacketSocket::GetOption(rtc::Socket::Option option, int* value) {
208 // This method is never called by libjingle.
209 NOTIMPLEMENTED();
210 return -1;
213 int UdpPacketSocket::SetOption(rtc::Socket::Option option, int value) {
214 if (state_ != STATE_BOUND) {
215 NOTREACHED();
216 return EINVAL;
219 switch (option) {
220 case rtc::Socket::OPT_DONTFRAGMENT:
221 NOTIMPLEMENTED();
222 return -1;
224 case rtc::Socket::OPT_RCVBUF: {
225 int net_error = socket_->SetReceiveBufferSize(value);
226 return (net_error == net::OK) ? 0 : -1;
229 case rtc::Socket::OPT_SNDBUF: {
230 int net_error = socket_->SetSendBufferSize(value);
231 return (net_error == net::OK) ? 0 : -1;
234 case rtc::Socket::OPT_NODELAY:
235 // OPT_NODELAY is only for TCP sockets.
236 NOTREACHED();
237 return -1;
239 case rtc::Socket::OPT_IPV6_V6ONLY:
240 NOTIMPLEMENTED();
241 return -1;
243 case rtc::Socket::OPT_DSCP:
244 NOTIMPLEMENTED();
245 return -1;
247 case rtc::Socket::OPT_RTP_SENDTIME_EXTN_ID:
248 NOTIMPLEMENTED();
249 return -1;
252 NOTREACHED();
253 return -1;
256 int UdpPacketSocket::GetError() const {
257 return error_;
260 void UdpPacketSocket::SetError(int error) {
261 error_ = error;
264 void UdpPacketSocket::DoSend() {
265 if (send_pending_ || send_queue_.empty())
266 return;
268 PendingPacket& packet = send_queue_.front();
269 int result = socket_->SendTo(
270 packet.data.get(),
271 packet.data->size(),
272 packet.address,
273 base::Bind(&UdpPacketSocket::OnSendCompleted, base::Unretained(this)));
274 if (result == net::ERR_IO_PENDING) {
275 send_pending_ = true;
276 } else {
277 OnSendCompleted(result);
281 void UdpPacketSocket::OnSendCompleted(int result) {
282 send_pending_ = false;
284 if (result < 0) {
285 SocketErrorAction action = GetSocketErrorAction(result);
286 switch (action) {
287 case SOCKET_ERROR_ACTION_FAIL:
288 LOG(ERROR) << "Send failed on a UDP socket: " << result;
289 error_ = EINVAL;
290 return;
292 case SOCKET_ERROR_ACTION_RETRY:
293 // Retry resending only once.
294 if (!send_queue_.front().retried) {
295 send_queue_.front().retried = true;
296 DoSend();
297 return;
299 break;
301 case SOCKET_ERROR_ACTION_IGNORE:
302 break;
306 // Don't need to worry about partial sends because this is a datagram
307 // socket.
308 send_queue_size_ -= send_queue_.front().data->size();
309 send_queue_.pop_front();
310 DoSend();
313 void UdpPacketSocket::DoRead() {
314 int result = 0;
315 while (result >= 0) {
316 receive_buffer_ = new net::IOBuffer(kReceiveBufferSize);
317 result = socket_->RecvFrom(
318 receive_buffer_.get(),
319 kReceiveBufferSize,
320 &receive_address_,
321 base::Bind(&UdpPacketSocket::OnReadCompleted, base::Unretained(this)));
322 HandleReadResult(result);
326 void UdpPacketSocket::OnReadCompleted(int result) {
327 HandleReadResult(result);
328 if (result >= 0) {
329 DoRead();
333 void UdpPacketSocket::HandleReadResult(int result) {
334 if (result == net::ERR_IO_PENDING) {
335 return;
338 if (result > 0) {
339 rtc::SocketAddress address;
340 if (!jingle_glue::IPEndPointToSocketAddress(receive_address_, &address)) {
341 NOTREACHED();
342 LOG(ERROR) << "Failed to convert address received from RecvFrom().";
343 return;
345 SignalReadPacket(this, receive_buffer_->data(), result, address,
346 rtc::CreatePacketTime(0));
347 } else {
348 LOG(ERROR) << "Received error when reading from UDP socket: " << result;
352 } // namespace
354 ChromiumPacketSocketFactory::ChromiumPacketSocketFactory() {
357 ChromiumPacketSocketFactory::~ChromiumPacketSocketFactory() {
360 rtc::AsyncPacketSocket* ChromiumPacketSocketFactory::CreateUdpSocket(
361 const rtc::SocketAddress& local_address,
362 int min_port, int max_port) {
363 scoped_ptr<UdpPacketSocket> result(new UdpPacketSocket());
364 if (!result->Init(local_address, min_port, max_port))
365 return NULL;
366 return result.release();
369 rtc::AsyncPacketSocket*
370 ChromiumPacketSocketFactory::CreateServerTcpSocket(
371 const rtc::SocketAddress& local_address,
372 int min_port, int max_port,
373 int opts) {
374 // We don't use TCP sockets for remoting connections.
375 NOTIMPLEMENTED();
376 return NULL;
379 rtc::AsyncPacketSocket*
380 ChromiumPacketSocketFactory::CreateClientTcpSocket(
381 const rtc::SocketAddress& local_address,
382 const rtc::SocketAddress& remote_address,
383 const rtc::ProxyInfo& proxy_info,
384 const std::string& user_agent,
385 int opts) {
386 // We don't use TCP sockets for remoting connections.
387 NOTREACHED();
388 return NULL;
391 rtc::AsyncResolverInterface*
392 ChromiumPacketSocketFactory::CreateAsyncResolver() {
393 return new rtc::AsyncResolver();
396 } // namespace protocol
397 } // namespace remoting