OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 ready_to_send_ = true; | 508 ready_to_send_ = true; |
509 SignalWriteEvent(this); | 509 SignalWriteEvent(this); |
510 } else { | 510 } else { |
511 RTC_DCHECK(type_ == SOCK_STREAM); | 511 RTC_DCHECK(type_ == SOCK_STREAM); |
512 // This will attempt to empty the full send buffer, and will fire | 512 // This will attempt to empty the full send buffer, and will fire |
513 // SignalWriteEvent if successful. | 513 // SignalWriteEvent if successful. |
514 server_->SendTcp(this); | 514 server_->SendTcp(this); |
515 } | 515 } |
516 } | 516 } |
517 | 517 |
518 VirtualSocketServer::VirtualSocketServer(SocketServer* ss) | 518 VirtualSocketServer::VirtualSocketServer() |
519 : server_(ss), | 519 : wakeup_(/*manual_reset=*/false, /*initially_signaled=*/false), |
520 server_owned_(false), | |
521 msg_queue_(nullptr), | 520 msg_queue_(nullptr), |
522 stop_on_idle_(false), | 521 stop_on_idle_(false), |
523 next_ipv4_(kInitialNextIPv4), | 522 next_ipv4_(kInitialNextIPv4), |
524 next_ipv6_(kInitialNextIPv6), | 523 next_ipv6_(kInitialNextIPv6), |
525 next_port_(kFirstEphemeralPort), | 524 next_port_(kFirstEphemeralPort), |
526 bindings_(new AddressMap()), | 525 bindings_(new AddressMap()), |
527 connections_(new ConnectionMap()), | 526 connections_(new ConnectionMap()), |
528 bandwidth_(0), | 527 bandwidth_(0), |
529 network_capacity_(kDefaultNetworkCapacity), | 528 network_capacity_(kDefaultNetworkCapacity), |
530 send_buffer_capacity_(kDefaultTcpBufferSize), | 529 send_buffer_capacity_(kDefaultTcpBufferSize), |
531 recv_buffer_capacity_(kDefaultTcpBufferSize), | 530 recv_buffer_capacity_(kDefaultTcpBufferSize), |
532 delay_mean_(0), | 531 delay_mean_(0), |
533 delay_stddev_(0), | 532 delay_stddev_(0), |
534 delay_samples_(NUM_SAMPLES), | 533 delay_samples_(NUM_SAMPLES), |
535 drop_prob_(0.0) { | 534 drop_prob_(0.0) { |
536 if (!server_) { | |
537 server_ = new PhysicalSocketServer(); | |
538 server_owned_ = true; | |
539 } | |
540 UpdateDelayDistribution(); | 535 UpdateDelayDistribution(); |
541 } | 536 } |
542 | 537 |
543 VirtualSocketServer::~VirtualSocketServer() { | 538 VirtualSocketServer::~VirtualSocketServer() { |
544 delete bindings_; | 539 delete bindings_; |
545 delete connections_; | 540 delete connections_; |
546 if (server_owned_) { | |
547 delete server_; | |
548 } | |
549 } | 541 } |
550 | 542 |
551 IPAddress VirtualSocketServer::GetNextIP(int family) { | 543 IPAddress VirtualSocketServer::GetNextIP(int family) { |
552 if (family == AF_INET) { | 544 if (family == AF_INET) { |
553 IPAddress next_ip(next_ipv4_); | 545 IPAddress next_ip(next_ipv4_); |
554 next_ipv4_.s_addr = | 546 next_ipv4_.s_addr = |
555 HostToNetwork32(NetworkToHost32(next_ipv4_.s_addr) + 1); | 547 HostToNetwork32(NetworkToHost32(next_ipv4_.s_addr) + 1); |
556 return next_ip; | 548 return next_ip; |
557 } else if (family == AF_INET6) { | 549 } else if (family == AF_INET6) { |
558 IPAddress next_ip(next_ipv6_); | 550 IPAddress next_ip(next_ipv6_); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 msg_queue_->SignalQueueDestroyed.connect(this, | 606 msg_queue_->SignalQueueDestroyed.connect(this, |
615 &VirtualSocketServer::OnMessageQueueDestroyed); | 607 &VirtualSocketServer::OnMessageQueueDestroyed); |
616 } | 608 } |
617 } | 609 } |
618 | 610 |
619 bool VirtualSocketServer::Wait(int cmsWait, bool process_io) { | 611 bool VirtualSocketServer::Wait(int cmsWait, bool process_io) { |
620 RTC_DCHECK(msg_queue_ == Thread::Current()); | 612 RTC_DCHECK(msg_queue_ == Thread::Current()); |
621 if (stop_on_idle_ && Thread::Current()->empty()) { | 613 if (stop_on_idle_ && Thread::Current()->empty()) { |
622 return false; | 614 return false; |
623 } | 615 } |
624 return socketserver()->Wait(cmsWait, process_io); | 616 // Note: we don't need to do anything with |process_io| since we don't have |
| 617 // any real I/O. Received packets come in the form of queued messages, so |
| 618 // MessageQueue will ensure WakeUp is called if another thread sends a |
| 619 // packet. |
| 620 wakeup_.Wait(cmsWait); |
| 621 return true; |
625 } | 622 } |
626 | 623 |
627 void VirtualSocketServer::WakeUp() { | 624 void VirtualSocketServer::WakeUp() { |
628 socketserver()->WakeUp(); | 625 wakeup_.Set(); |
629 } | 626 } |
630 | 627 |
631 bool VirtualSocketServer::ProcessMessagesUntilIdle() { | 628 bool VirtualSocketServer::ProcessMessagesUntilIdle() { |
632 RTC_DCHECK(msg_queue_ == Thread::Current()); | 629 RTC_DCHECK(msg_queue_ == Thread::Current()); |
633 stop_on_idle_ = true; | 630 stop_on_idle_ = true; |
634 while (!msg_queue_->empty()) { | 631 while (!msg_queue_->empty()) { |
635 Message msg; | 632 Message msg; |
636 if (msg_queue_->Get(&msg, Thread::kForever)) { | 633 if (msg_queue_->Get(&msg, Thread::kForever)) { |
637 msg_queue_->Dispatch(&msg); | 634 msg_queue_->Dispatch(&msg); |
638 } | 635 } |
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1183 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { | 1180 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { |
1184 RTC_DCHECK(!IPIsAny(from_addr)); | 1181 RTC_DCHECK(!IPIsAny(from_addr)); |
1185 if (from_addr.family() == AF_INET) { | 1182 if (from_addr.family() == AF_INET) { |
1186 default_route_v4_ = from_addr; | 1183 default_route_v4_ = from_addr; |
1187 } else if (from_addr.family() == AF_INET6) { | 1184 } else if (from_addr.family() == AF_INET6) { |
1188 default_route_v6_ = from_addr; | 1185 default_route_v6_ = from_addr; |
1189 } | 1186 } |
1190 } | 1187 } |
1191 | 1188 |
1192 } // namespace rtc | 1189 } // namespace rtc |
OLD | NEW |