| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/services/network/udp_socket_impl.h" | 5 #include "mojo/services/network/udp_socket_impl.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | |
| 9 #include <algorithm> | 8 #include <algorithm> |
| 10 #include <limits> | 9 #include <limits> |
| 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "mojo/services/network/net_adapters.h" | 15 #include "mojo/services/network/net_adapters.h" |
| 16 #include "mojo/services/network/net_address_type_converters.h" | 16 #include "mojo/services/network/net_address_type_converters.h" |
| 17 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/base/rand_callback.h" | 19 #include "net/base/rand_callback.h" |
| 20 #include "net/udp/datagram_socket.h" | 20 #include "net/udp/datagram_socket.h" |
| 21 | 21 |
| 22 namespace mojo { | 22 namespace mojo { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const int kMaxReadSize = 128 * 1024; | 26 const int kMaxReadSize = 128 * 1024; |
| 27 const size_t kMaxWriteSize = 128 * 1024; | 27 const size_t kMaxWriteSize = 128 * 1024; |
| 28 const size_t kMaxPendingSendRequestsUpperbound = 128; | 28 const size_t kMaxPendingSendRequestsUpperbound = 128; |
| 29 const size_t kDefaultMaxPendingSendRequests = 32; | 29 const size_t kDefaultMaxPendingSendRequests = 32; |
| 30 | 30 |
| 31 } // namespace | 31 } // namespace |
| 32 | 32 |
| 33 UDPSocketImpl::PendingSendRequest::PendingSendRequest() {} | 33 UDPSocketImpl::PendingSendRequest::PendingSendRequest() {} |
| 34 | 34 |
| 35 UDPSocketImpl::PendingSendRequest::~PendingSendRequest() {} | 35 UDPSocketImpl::PendingSendRequest::~PendingSendRequest() {} |
| 36 | 36 |
| 37 UDPSocketImpl::UDPSocketImpl(InterfaceRequest<UDPSocket> request, | 37 UDPSocketImpl::UDPSocketImpl(InterfaceRequest<UDPSocket> request, |
| 38 scoped_ptr<mojo::AppRefCount> app_refcount) | 38 scoped_ptr<mojo::AppRefCount> app_refcount) |
| 39 : binding_(this, request.Pass()), | 39 : binding_(this, std::move(request)), |
| 40 socket_(net::DatagramSocket::DEFAULT_BIND, | 40 socket_(net::DatagramSocket::DEFAULT_BIND, |
| 41 net::RandIntCallback(), | 41 net::RandIntCallback(), |
| 42 nullptr, | 42 nullptr, |
| 43 net::NetLog::Source()), | 43 net::NetLog::Source()), |
| 44 state_(NOT_BOUND_OR_CONNECTED), | 44 state_(NOT_BOUND_OR_CONNECTED), |
| 45 allow_address_reuse_(false), | 45 allow_address_reuse_(false), |
| 46 remaining_recv_slots_(0), | 46 remaining_recv_slots_(0), |
| 47 max_pending_send_requests_(kDefaultMaxPendingSendRequests), | 47 max_pending_send_requests_(kDefaultMaxPendingSendRequests), |
| 48 app_refcount_(app_refcount.Pass()) { | 48 app_refcount_(std::move(app_refcount)) {} |
| 49 } | |
| 50 | 49 |
| 51 UDPSocketImpl::~UDPSocketImpl() { | 50 UDPSocketImpl::~UDPSocketImpl() { |
| 52 STLDeleteElements(&pending_send_requests_); | 51 STLDeleteElements(&pending_send_requests_); |
| 53 } | 52 } |
| 54 | 53 |
| 55 void UDPSocketImpl::AllowAddressReuse( | 54 void UDPSocketImpl::AllowAddressReuse( |
| 56 const Callback<void(NetworkErrorPtr)>& callback) { | 55 const Callback<void(NetworkErrorPtr)>& callback) { |
| 57 if (IsBoundOrConnected()) { | 56 if (IsBoundOrConnected()) { |
| 58 callback.Run(MakeNetworkError(net::ERR_FAILED)); | 57 callback.Run(MakeNetworkError(net::ERR_FAILED)); |
| 59 return; | 58 return; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 return; | 253 return; |
| 255 } | 254 } |
| 256 | 255 |
| 257 if (sendto_buffer_.get()) { | 256 if (sendto_buffer_.get()) { |
| 258 if (pending_send_requests_.size() >= max_pending_send_requests_) { | 257 if (pending_send_requests_.size() >= max_pending_send_requests_) { |
| 259 callback.Run(MakeNetworkError(net::ERR_INSUFFICIENT_RESOURCES)); | 258 callback.Run(MakeNetworkError(net::ERR_INSUFFICIENT_RESOURCES)); |
| 260 return; | 259 return; |
| 261 } | 260 } |
| 262 | 261 |
| 263 PendingSendRequest* request = new PendingSendRequest; | 262 PendingSendRequest* request = new PendingSendRequest; |
| 264 request->addr = dest_addr.Pass(); | 263 request->addr = std::move(dest_addr); |
| 265 request->data = data.Pass(); | 264 request->data = std::move(data); |
| 266 request->callback = callback; | 265 request->callback = callback; |
| 267 pending_send_requests_.push_back(request); | 266 pending_send_requests_.push_back(request); |
| 268 return; | 267 return; |
| 269 } | 268 } |
| 270 | 269 |
| 271 DCHECK_EQ(0u, pending_send_requests_.size()); | 270 DCHECK_EQ(0u, pending_send_requests_.size()); |
| 272 | 271 |
| 273 DoSendTo(dest_addr.Pass(), data.Pass(), callback); | 272 DoSendTo(std::move(dest_addr), std::move(data), callback); |
| 274 } | 273 } |
| 275 | 274 |
| 276 void UDPSocketImpl::DoRecvFrom() { | 275 void UDPSocketImpl::DoRecvFrom() { |
| 277 DCHECK(IsBoundOrConnected()); | 276 DCHECK(IsBoundOrConnected()); |
| 278 DCHECK(receiver_); | 277 DCHECK(receiver_); |
| 279 DCHECK(!recvfrom_buffer_.get()); | 278 DCHECK(!recvfrom_buffer_.get()); |
| 280 DCHECK_GT(remaining_recv_slots_, 0u); | 279 DCHECK_GT(remaining_recv_slots_, 0u); |
| 281 | 280 |
| 282 recvfrom_buffer_ = new net::IOBuffer(kMaxReadSize); | 281 recvfrom_buffer_ = new net::IOBuffer(kMaxReadSize); |
| 283 | 282 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 net_address = NetAddress::From(recvfrom_address_); | 341 net_address = NetAddress::From(recvfrom_address_); |
| 343 | 342 |
| 344 std::vector<uint8_t> data(net_result); | 343 std::vector<uint8_t> data(net_result); |
| 345 if (net_result > 0) | 344 if (net_result > 0) |
| 346 memcpy(&data[0], recvfrom_buffer_->data(), net_result); | 345 memcpy(&data[0], recvfrom_buffer_->data(), net_result); |
| 347 | 346 |
| 348 array.Swap(&data); | 347 array.Swap(&data); |
| 349 } | 348 } |
| 350 recvfrom_buffer_ = nullptr; | 349 recvfrom_buffer_ = nullptr; |
| 351 | 350 |
| 352 receiver_->OnReceived(MakeNetworkError(net_result), net_address.Pass(), | 351 receiver_->OnReceived(MakeNetworkError(net_result), std::move(net_address), |
| 353 array.Pass()); | 352 std::move(array)); |
| 354 DCHECK_GT(remaining_recv_slots_, 0u); | 353 DCHECK_GT(remaining_recv_slots_, 0u); |
| 355 remaining_recv_slots_--; | 354 remaining_recv_slots_--; |
| 356 if (remaining_recv_slots_ > 0) | 355 if (remaining_recv_slots_ > 0) |
| 357 DoRecvFrom(); | 356 DoRecvFrom(); |
| 358 } | 357 } |
| 359 | 358 |
| 360 void UDPSocketImpl::OnSendToCompleted( | 359 void UDPSocketImpl::OnSendToCompleted( |
| 361 const Callback<void(NetworkErrorPtr)>& callback, | 360 const Callback<void(NetworkErrorPtr)>& callback, |
| 362 int net_result) { | 361 int net_result) { |
| 363 DCHECK(sendto_buffer_.get()); | 362 DCHECK(sendto_buffer_.get()); |
| 364 | 363 |
| 365 sendto_buffer_ = nullptr; | 364 sendto_buffer_ = nullptr; |
| 366 | 365 |
| 367 callback.Run(MakeNetworkError(net_result)); | 366 callback.Run(MakeNetworkError(net_result)); |
| 368 | 367 |
| 369 if (pending_send_requests_.empty()) | 368 if (pending_send_requests_.empty()) |
| 370 return; | 369 return; |
| 371 | 370 |
| 372 scoped_ptr<PendingSendRequest> request(pending_send_requests_.front()); | 371 scoped_ptr<PendingSendRequest> request(pending_send_requests_.front()); |
| 373 pending_send_requests_.pop_front(); | 372 pending_send_requests_.pop_front(); |
| 374 | 373 |
| 375 DoSendTo(request->addr.Pass(), request->data.Pass(), request->callback); | 374 DoSendTo(std::move(request->addr), std::move(request->data), |
| 375 request->callback); |
| 376 } | 376 } |
| 377 | 377 |
| 378 } // namespace mojo | 378 } // namespace mojo |
| OLD | NEW |