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

Side by Side Diff: net/udp/udp_socket_libevent.cc

Issue 690903002: Remove timing limitation of SetOption invocation for PPAPI sockets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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
« no previous file with comments | « net/udp/udp_socket_libevent.h ('k') | net/udp/udp_socket_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "net/udp/udp_socket_libevent.h" 5 #include "net/udp/udp_socket_libevent.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <netdb.h> 9 #include <netdb.h>
10 #include <net/if.h> 10 #include <net/if.h>
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 if (rv < 0) { 310 if (rv < 0) {
311 Close(); 311 Close();
312 return rv; 312 return rv;
313 } 313 }
314 local_address_.reset(); 314 local_address_.reset();
315 return rv; 315 return rv;
316 } 316 }
317 317
318 int UDPSocketLibevent::SetReceiveBufferSize(int32 size) { 318 int UDPSocketLibevent::SetReceiveBufferSize(int32 size) {
319 DCHECK(CalledOnValidThread()); 319 DCHECK(CalledOnValidThread());
320 int rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF, 320 if (!is_connected()) {
321 reinterpret_cast<const char*>(&size), sizeof(size)); 321 socket_options_ |= SOCKET_OPTION_RCVBUF_SIZE;
322 int last_error = errno; 322 rcvbuf_size_ = size;
323 DCHECK(!rv) << "Could not set socket receive buffer size: " << last_error; 323 return OK;
mmenke 2014/10/30 14:40:38 Sockets can be disconnected and then reconnected,
hidehiko 2014/11/03 13:34:37 oops, sorry, I forgot to reply this comment. I mad
324 return rv == 0 ? OK : MapSystemError(last_error); 324 }
325 return SetReceiveBufferSizeInternal(size);
325 } 326 }
326 327
327 int UDPSocketLibevent::SetSendBufferSize(int32 size) { 328 int UDPSocketLibevent::SetSendBufferSize(int32 size) {
328 DCHECK(CalledOnValidThread()); 329 DCHECK(CalledOnValidThread());
329 int rv = setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, 330 if (!is_connected()) {
330 reinterpret_cast<const char*>(&size), sizeof(size)); 331 socket_options_ |= SOCKET_OPTION_SNDBUF_SIZE;
331 int last_error = errno; 332 sndbuf_size_ = size;
332 DCHECK(!rv) << "Could not set socket send buffer size: " << last_error; 333 return OK;
333 return rv == 0 ? OK : MapSystemError(last_error); 334 }
335 return SetSendBufferSizeInternal(size);
334 } 336 }
335 337
336 void UDPSocketLibevent::AllowAddressReuse() { 338 int UDPSocketLibevent::AllowAddressReuse() {
337 DCHECK(CalledOnValidThread()); 339 DCHECK(CalledOnValidThread());
338 DCHECK(!is_connected()); 340 if (!is_connected()) {
339 341 socket_options_ |= SOCKET_OPTION_REUSE_ADDRESS;
340 socket_options_ |= SOCKET_OPTION_REUSE_ADDRESS; 342 return OK;
343 }
344 return SetAddressReuseInternal(true);
341 } 345 }
342 346
343 void UDPSocketLibevent::AllowBroadcast() { 347 int UDPSocketLibevent::DisallowAddressReuse() {
344 DCHECK(CalledOnValidThread()); 348 DCHECK(CalledOnValidThread());
345 DCHECK(!is_connected()); 349 if (!is_connected()) {
350 socket_options_ &= ~SOCKET_OPTION_REUSE_ADDRESS;
351 return OK;
352 }
353 return SetAddressReuseInternal(false);
354 }
346 355
347 socket_options_ |= SOCKET_OPTION_BROADCAST; 356 int UDPSocketLibevent::AllowBroadcast() {
357 DCHECK(CalledOnValidThread());
358 if (!is_connected()) {
359 socket_options_ |= SOCKET_OPTION_BROADCAST;
360 return OK;
361 }
362 return SetBroadcastInternal(true);
363 }
364
365 int UDPSocketLibevent::DisallowBroadcast() {
366 DCHECK(CalledOnValidThread());
367 if (!is_connected()) {
368 socket_options_ &= ~SOCKET_OPTION_BROADCAST;
369 return OK;
370 }
371 return SetBroadcastInternal(false);
348 } 372 }
349 373
350 void UDPSocketLibevent::ReadWatcher::OnFileCanReadWithoutBlocking(int) { 374 void UDPSocketLibevent::ReadWatcher::OnFileCanReadWithoutBlocking(int) {
351 if (!socket_->read_callback_.is_null()) 375 if (!socket_->read_callback_.is_null())
352 socket_->DidCompleteRead(); 376 socket_->DidCompleteRead();
353 } 377 }
354 378
355 void UDPSocketLibevent::WriteWatcher::OnFileCanWriteWithoutBlocking(int) { 379 void UDPSocketLibevent::WriteWatcher::OnFileCanWriteWithoutBlocking(int) {
356 if (!socket_->write_callback_.is_null()) 380 if (!socket_->write_callback_.is_null())
357 socket_->DidCompleteWrite(); 381 socket_->DidCompleteWrite();
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 addr, 533 addr,
510 storage.addr_len)); 534 storage.addr_len));
511 if (result < 0) 535 if (result < 0)
512 result = MapSystemError(errno); 536 result = MapSystemError(errno);
513 if (result != ERR_IO_PENDING) 537 if (result != ERR_IO_PENDING)
514 LogWrite(result, buf->data(), address); 538 LogWrite(result, buf->data(), address);
515 return result; 539 return result;
516 } 540 }
517 541
518 int UDPSocketLibevent::SetSocketOptions() { 542 int UDPSocketLibevent::SetSocketOptions() {
519 int true_value = 1; 543 DCHECK(CalledOnValidThread());
520 if (socket_options_ & SOCKET_OPTION_REUSE_ADDRESS) { 544 if (socket_options_ & SOCKET_OPTION_REUSE_ADDRESS) {
521 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value, 545 int rv = SetAddressReuseInternal(true);
522 sizeof(true_value)); 546 if (rv != OK)
523 if (rv < 0) 547 return rv;
524 return MapSystemError(errno);
525 } 548 }
526 if (socket_options_ & SOCKET_OPTION_BROADCAST) { 549 if (socket_options_ & SOCKET_OPTION_BROADCAST) {
527 int rv; 550 int rv = SetBroadcastInternal(true);
528 #if defined(OS_MACOSX) 551 if (rv != OK)
529 // SO_REUSEPORT on OSX permits multiple processes to each receive 552 return rv;
530 // UDP multicast or broadcast datagrams destined for the bound
531 // port.
532 rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEPORT, &true_value,
533 sizeof(true_value));
534 #else
535 rv = setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, &true_value,
536 sizeof(true_value));
537 #endif // defined(OS_MACOSX)
538 if (rv < 0)
539 return MapSystemError(errno);
540 } 553 }
541
542 if (!(socket_options_ & SOCKET_OPTION_MULTICAST_LOOP)) { 554 if (!(socket_options_ & SOCKET_OPTION_MULTICAST_LOOP)) {
543 int rv; 555 int rv;
544 if (addr_family_ == AF_INET) { 556 if (addr_family_ == AF_INET) {
545 u_char loop = 0; 557 u_char loop = 0;
546 rv = setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_LOOP, 558 rv = setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_LOOP,
547 &loop, sizeof(loop)); 559 &loop, sizeof(loop));
548 } else { 560 } else {
549 u_int loop = 0; 561 u_int loop = 0;
550 rv = setsockopt(socket_, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, 562 rv = setsockopt(socket_, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
551 &loop, sizeof(loop)); 563 &loop, sizeof(loop));
552 } 564 }
553 if (rv < 0) 565 if (rv < 0)
554 return MapSystemError(errno); 566 return MapSystemError(errno);
555 } 567 }
568 if (socket_options_ & SOCKET_OPTION_RCVBUF_SIZE) {
569 int rv = SetReceiveBufferSizeInternal(rcvbuf_size_);
570 if (rv != OK)
571 return rv;
572 }
573 if (socket_options_ & SOCKET_OPTION_SNDBUF_SIZE) {
574 int rv = SetSendBufferSizeInternal(sndbuf_size_);
575 if (rv != OK)
576 return rv;
577 }
578
556 if (multicast_time_to_live_ != IP_DEFAULT_MULTICAST_TTL) { 579 if (multicast_time_to_live_ != IP_DEFAULT_MULTICAST_TTL) {
557 int rv; 580 int rv;
558 if (addr_family_ == AF_INET) { 581 if (addr_family_ == AF_INET) {
559 u_char ttl = multicast_time_to_live_; 582 u_char ttl = multicast_time_to_live_;
560 rv = setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_TTL, 583 rv = setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_TTL,
561 &ttl, sizeof(ttl)); 584 &ttl, sizeof(ttl));
562 } else { 585 } else {
563 // Signed integer. -1 to use route default. 586 // Signed integer. -1 to use route default.
564 int ttl = multicast_time_to_live_; 587 int ttl = multicast_time_to_live_;
565 rv = setsockopt(socket_, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 588 rv = setsockopt(socket_, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 break; 621 break;
599 } 622 }
600 default: 623 default:
601 NOTREACHED() << "Invalid address family"; 624 NOTREACHED() << "Invalid address family";
602 return ERR_ADDRESS_INVALID; 625 return ERR_ADDRESS_INVALID;
603 } 626 }
604 } 627 }
605 return OK; 628 return OK;
606 } 629 }
607 630
631 int UDPSocketLibevent::SetAddressReuseInternal(bool value) {
632 DCHECK(CalledOnValidThread());
633 DCHECK(is_connected());
634 int opt_value = value ? 1 : 0;
635 int rv = setsockopt(
636 socket_, SOL_SOCKET, SO_REUSEADDR, &opt_value, sizeof(opt_value));
637 return rv == 0 ? OK : MapSystemError(errno);
638 }
639
640 int UDPSocketLibevent::SetBroadcastInternal(bool value) {
641 DCHECK(CalledOnValidThread());
642 DCHECK(is_connected());
643 int opt_value = value ? 1 : 0;
644 #if defined(OS_MACOSX)
645 // SO_REUSEPORT on OSX permits multiple processes to each receive
646 // UDP multicast or broadcast datagrams destined for the bound
647 // port.
648 const int kOptName = SO_REUSEPORT;
649 #else
650 const int kOptName = SO_BROADCAST;
651 #endif
652 int rv = setsockopt(
653 socket_, SOL_SOCKET, kOptName, &opt_value, sizeof(opt_value));
654 return rv == 0 ? OK : MapSystemError(errno);
655 }
656
657 int UDPSocketLibevent::SetReceiveBufferSizeInternal(int32 size) {
658 DCHECK(CalledOnValidThread());
659 DCHECK(is_connected());
660 int rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF,
661 reinterpret_cast<const char*>(&size), sizeof(size));
662 int last_error = errno;
663 DCHECK(!rv) << "Could not set socket receive buffer size: " << last_error;
664 return rv == 0 ? OK : MapSystemError(last_error);
665 }
666
667 int UDPSocketLibevent::SetSendBufferSizeInternal(int32 size) {
668 DCHECK(CalledOnValidThread());
669 DCHECK(is_connected());
670 int rv = setsockopt(socket_, SOL_SOCKET, SO_SNDBUF,
671 reinterpret_cast<const char*>(&size), sizeof(size));
672 int last_error = errno;
673 DCHECK(!rv) << "Could not set socket send buffer size: " << last_error;
674 return rv == 0 ? OK : MapSystemError(last_error);
675 }
676
608 int UDPSocketLibevent::DoBind(const IPEndPoint& address) { 677 int UDPSocketLibevent::DoBind(const IPEndPoint& address) {
609 SockaddrStorage storage; 678 SockaddrStorage storage;
610 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) 679 if (!address.ToSockAddr(storage.addr, &storage.addr_len))
611 return ERR_ADDRESS_INVALID; 680 return ERR_ADDRESS_INVALID;
612 int rv = bind(socket_, storage.addr, storage.addr_len); 681 int rv = bind(socket_, storage.addr, storage.addr_len);
613 if (rv == 0) 682 if (rv == 0)
614 return OK; 683 return OK;
615 int last_error = errno; 684 int last_error = errno;
616 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.UdpSocketBindErrorFromPosix", last_error); 685 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.UdpSocketBindErrorFromPosix", last_error);
617 #if defined(OS_CHROMEOS) 686 #if defined(OS_CHROMEOS)
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
767 return MapSystemError(errno); 836 return MapSystemError(errno);
768 837
769 return OK; 838 return OK;
770 } 839 }
771 840
772 void UDPSocketLibevent::DetachFromThread() { 841 void UDPSocketLibevent::DetachFromThread() {
773 base::NonThreadSafe::DetachFromThread(); 842 base::NonThreadSafe::DetachFromThread();
774 } 843 }
775 844
776 } // namespace net 845 } // namespace net
OLDNEW
« no previous file with comments | « net/udp/udp_socket_libevent.h ('k') | net/udp/udp_socket_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698