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 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 return -1; | 494 return -1; |
495 } | 495 } |
496 size_t consumed = std::min(cb, capacity); | 496 size_t consumed = std::min(cb, capacity); |
497 const char* cpv = static_cast<const char*>(pv); | 497 const char* cpv = static_cast<const char*>(pv); |
498 send_buffer_.insert(send_buffer_.end(), cpv, cpv + consumed); | 498 send_buffer_.insert(send_buffer_.end(), cpv, cpv + consumed); |
499 server_->SendTcp(this); | 499 server_->SendTcp(this); |
500 return static_cast<int>(consumed); | 500 return static_cast<int>(consumed); |
501 } | 501 } |
502 | 502 |
503 VirtualSocketServer::VirtualSocketServer(SocketServer* ss) | 503 VirtualSocketServer::VirtualSocketServer(SocketServer* ss) |
504 : server_(ss), server_owned_(false), msg_queue_(NULL), stop_on_idle_(false), | 504 : server_(ss), |
505 network_delay_(Time()), next_ipv4_(kInitialNextIPv4), | 505 server_owned_(false), |
506 next_ipv6_(kInitialNextIPv6), next_port_(kFirstEphemeralPort), | 506 msg_queue_(NULL), |
507 bindings_(new AddressMap()), connections_(new ConnectionMap()), | 507 stop_on_idle_(false), |
508 bandwidth_(0), network_capacity_(kDefaultNetworkCapacity), | 508 network_delay_(TimeMillis()), |
| 509 next_ipv4_(kInitialNextIPv4), |
| 510 next_ipv6_(kInitialNextIPv6), |
| 511 next_port_(kFirstEphemeralPort), |
| 512 bindings_(new AddressMap()), |
| 513 connections_(new ConnectionMap()), |
| 514 bandwidth_(0), |
| 515 network_capacity_(kDefaultNetworkCapacity), |
509 send_buffer_capacity_(kDefaultTcpBufferSize), | 516 send_buffer_capacity_(kDefaultTcpBufferSize), |
510 recv_buffer_capacity_(kDefaultTcpBufferSize), | 517 recv_buffer_capacity_(kDefaultTcpBufferSize), |
511 delay_mean_(0), delay_stddev_(0), delay_samples_(NUM_SAMPLES), | 518 delay_mean_(0), |
512 delay_dist_(NULL), drop_prob_(0.0) { | 519 delay_stddev_(0), |
| 520 delay_samples_(NUM_SAMPLES), |
| 521 delay_dist_(NULL), |
| 522 drop_prob_(0.0) { |
513 if (!server_) { | 523 if (!server_) { |
514 server_ = new PhysicalSocketServer(); | 524 server_ = new PhysicalSocketServer(); |
515 server_owned_ = true; | 525 server_owned_ = true; |
516 } | 526 } |
517 UpdateDelayDistribution(); | 527 UpdateDelayDistribution(); |
518 } | 528 } |
519 | 529 |
520 VirtualSocketServer::~VirtualSocketServer() { | 530 VirtualSocketServer::~VirtualSocketServer() { |
521 delete bindings_; | 531 delete bindings_; |
522 delete connections_; | 532 delete connections_; |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 } | 795 } |
786 | 796 |
787 if (!CanInteractWith(socket, recipient)) { | 797 if (!CanInteractWith(socket, recipient)) { |
788 LOG(LS_VERBOSE) << "Incompatible address families: " | 798 LOG(LS_VERBOSE) << "Incompatible address families: " |
789 << socket->GetLocalAddress() << " and " << remote_addr; | 799 << socket->GetLocalAddress() << " and " << remote_addr; |
790 return -1; | 800 return -1; |
791 } | 801 } |
792 | 802 |
793 CritScope cs(&socket->crit_); | 803 CritScope cs(&socket->crit_); |
794 | 804 |
795 uint32_t cur_time = Time(); | 805 int64_t cur_time = TimeMillis(); |
796 PurgeNetworkPackets(socket, cur_time); | 806 PurgeNetworkPackets(socket, cur_time); |
797 | 807 |
798 // Determine whether we have enough bandwidth to accept this packet. To do | 808 // Determine whether we have enough bandwidth to accept this packet. To do |
799 // this, we need to update the send queue. Once we know it's current size, | 809 // this, we need to update the send queue. Once we know it's current size, |
800 // we know whether we can fit this packet. | 810 // we know whether we can fit this packet. |
801 // | 811 // |
802 // NOTE: There are better algorithms for maintaining such a queue (such as | 812 // NOTE: There are better algorithms for maintaining such a queue (such as |
803 // "Derivative Random Drop"); however, this algorithm is a more accurate | 813 // "Derivative Random Drop"); however, this algorithm is a more accurate |
804 // simulation of what a normal network would do. | 814 // simulation of what a normal network would do. |
805 | 815 |
(...skipping 19 matching lines...) Expand all Loading... |
825 // Lookup the local/remote pair in the connections table. | 835 // Lookup the local/remote pair in the connections table. |
826 VirtualSocket* recipient = LookupConnection(socket->local_addr_, | 836 VirtualSocket* recipient = LookupConnection(socket->local_addr_, |
827 socket->remote_addr_); | 837 socket->remote_addr_); |
828 if (!recipient) { | 838 if (!recipient) { |
829 LOG(LS_VERBOSE) << "Sending data to no one."; | 839 LOG(LS_VERBOSE) << "Sending data to no one."; |
830 return; | 840 return; |
831 } | 841 } |
832 | 842 |
833 CritScope cs(&socket->crit_); | 843 CritScope cs(&socket->crit_); |
834 | 844 |
835 uint32_t cur_time = Time(); | 845 int64_t cur_time = TimeMillis(); |
836 PurgeNetworkPackets(socket, cur_time); | 846 PurgeNetworkPackets(socket, cur_time); |
837 | 847 |
838 while (true) { | 848 while (true) { |
839 size_t available = recv_buffer_capacity_ - recipient->recv_buffer_size_; | 849 size_t available = recv_buffer_capacity_ - recipient->recv_buffer_size_; |
840 size_t max_data_size = | 850 size_t max_data_size = |
841 std::min<size_t>(available, TCP_MSS - TCP_HEADER_SIZE); | 851 std::min<size_t>(available, TCP_MSS - TCP_HEADER_SIZE); |
842 size_t data_size = std::min(socket->send_buffer_.size(), max_data_size); | 852 size_t data_size = std::min(socket->send_buffer_.size(), max_data_size); |
843 if (0 == data_size) | 853 if (0 == data_size) |
844 break; | 854 break; |
845 | 855 |
(...skipping 14 matching lines...) Expand all Loading... |
860 | 870 |
861 if (socket->write_enabled_ | 871 if (socket->write_enabled_ |
862 && (socket->send_buffer_.size() < send_buffer_capacity_)) { | 872 && (socket->send_buffer_.size() < send_buffer_capacity_)) { |
863 socket->write_enabled_ = false; | 873 socket->write_enabled_ = false; |
864 socket->SignalWriteEvent(socket); | 874 socket->SignalWriteEvent(socket); |
865 } | 875 } |
866 } | 876 } |
867 | 877 |
868 void VirtualSocketServer::AddPacketToNetwork(VirtualSocket* sender, | 878 void VirtualSocketServer::AddPacketToNetwork(VirtualSocket* sender, |
869 VirtualSocket* recipient, | 879 VirtualSocket* recipient, |
870 uint32_t cur_time, | 880 int64_t cur_time, |
871 const char* data, | 881 const char* data, |
872 size_t data_size, | 882 size_t data_size, |
873 size_t header_size, | 883 size_t header_size, |
874 bool ordered) { | 884 bool ordered) { |
875 VirtualSocket::NetworkEntry entry; | 885 VirtualSocket::NetworkEntry entry; |
876 entry.size = data_size + header_size; | 886 entry.size = data_size + header_size; |
877 | 887 |
878 sender->network_size_ += entry.size; | 888 sender->network_size_ += entry.size; |
879 uint32_t send_delay = SendDelay(static_cast<uint32_t>(sender->network_size_)); | 889 uint32_t send_delay = SendDelay(static_cast<uint32_t>(sender->network_size_)); |
880 entry.done_time = cur_time + send_delay; | 890 entry.done_time = cur_time + send_delay; |
881 sender->network_.push_back(entry); | 891 sender->network_.push_back(entry); |
882 | 892 |
883 // Find the delay for crossing the many virtual hops of the network. | 893 // Find the delay for crossing the many virtual hops of the network. |
884 uint32_t transit_delay = GetRandomTransitDelay(); | 894 uint32_t transit_delay = GetRandomTransitDelay(); |
885 | 895 |
886 // When the incoming packet is from a binding of the any address, translate it | 896 // When the incoming packet is from a binding of the any address, translate it |
887 // to the default route here such that the recipient will see the default | 897 // to the default route here such that the recipient will see the default |
888 // route. | 898 // route. |
889 SocketAddress sender_addr = sender->local_addr_; | 899 SocketAddress sender_addr = sender->local_addr_; |
890 IPAddress default_ip = GetDefaultRoute(sender_addr.ipaddr().family()); | 900 IPAddress default_ip = GetDefaultRoute(sender_addr.ipaddr().family()); |
891 if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) { | 901 if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) { |
892 sender_addr.SetIP(default_ip); | 902 sender_addr.SetIP(default_ip); |
893 } | 903 } |
894 | 904 |
895 // Post the packet as a message to be delivered (on our own thread) | 905 // Post the packet as a message to be delivered (on our own thread) |
896 Packet* p = new Packet(data, data_size, sender_addr); | 906 Packet* p = new Packet(data, data_size, sender_addr); |
897 | 907 |
898 uint32_t ts = TimeAfter(send_delay + transit_delay); | 908 int64_t ts = TimeAfter(send_delay + transit_delay); |
899 if (ordered) { | 909 if (ordered) { |
900 // Ensure that new packets arrive after previous ones | 910 // Ensure that new packets arrive after previous ones |
901 // TODO: consider ordering on a per-socket basis, since this | 911 // TODO: consider ordering on a per-socket basis, since this |
902 // introduces artifical delay. | 912 // introduces artificial delay. |
903 ts = TimeMax(ts, network_delay_); | 913 ts = std::max(ts, network_delay_); |
904 } | 914 } |
905 msg_queue_->PostAt(ts, recipient, MSG_ID_PACKET, p); | 915 msg_queue_->PostAt(ts, recipient, MSG_ID_PACKET, p); |
906 network_delay_ = TimeMax(ts, network_delay_); | 916 network_delay_ = std::max(ts, network_delay_); |
907 } | 917 } |
908 | 918 |
909 void VirtualSocketServer::PurgeNetworkPackets(VirtualSocket* socket, | 919 void VirtualSocketServer::PurgeNetworkPackets(VirtualSocket* socket, |
910 uint32_t cur_time) { | 920 int64_t cur_time) { |
911 while (!socket->network_.empty() && | 921 while (!socket->network_.empty() && |
912 (socket->network_.front().done_time <= cur_time)) { | 922 (socket->network_.front().done_time <= cur_time)) { |
913 ASSERT(socket->network_size_ >= socket->network_.front().size); | 923 ASSERT(socket->network_size_ >= socket->network_.front().size); |
914 socket->network_size_ -= socket->network_.front().size; | 924 socket->network_size_ -= socket->network_.front().size; |
915 socket->network_.pop_front(); | 925 socket->network_.pop_front(); |
916 } | 926 } |
917 } | 927 } |
918 | 928 |
919 uint32_t VirtualSocketServer::SendDelay(uint32_t size) { | 929 uint32_t VirtualSocketServer::SendDelay(uint32_t size) { |
920 if (bandwidth_ == 0) | 930 if (bandwidth_ == 0) |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1123 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { | 1133 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { |
1124 RTC_DCHECK(!IPIsAny(from_addr)); | 1134 RTC_DCHECK(!IPIsAny(from_addr)); |
1125 if (from_addr.family() == AF_INET) { | 1135 if (from_addr.family() == AF_INET) { |
1126 default_route_v4_ = from_addr; | 1136 default_route_v4_ = from_addr; |
1127 } else if (from_addr.family() == AF_INET6) { | 1137 } else if (from_addr.family() == AF_INET6) { |
1128 default_route_v6_ = from_addr; | 1138 default_route_v6_ = from_addr; |
1129 } | 1139 } |
1130 } | 1140 } |
1131 | 1141 |
1132 } // namespace rtc | 1142 } // namespace rtc |
OLD | NEW |