| 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 VirtualSocket::~VirtualSocket() { | 120 VirtualSocket::~VirtualSocket() { |
| 121 Close(); | 121 Close(); |
| 122 | 122 |
| 123 for (RecvBuffer::iterator it = recv_buffer_.begin(); it != recv_buffer_.end(); | 123 for (RecvBuffer::iterator it = recv_buffer_.begin(); it != recv_buffer_.end(); |
| 124 ++it) { | 124 ++it) { |
| 125 delete *it; | 125 delete *it; |
| 126 } | 126 } |
| 127 } | 127 } |
| 128 | 128 |
| 129 SocketAddress VirtualSocket::GetLocalAddress() const { | 129 SocketAddress VirtualSocket::GetLocalAddress() const { |
| 130 if (!alternative_local_addr_.IsNil()) | |
| 131 return alternative_local_addr_; | |
| 132 return local_addr_; | 130 return local_addr_; |
| 133 } | 131 } |
| 134 | 132 |
| 135 SocketAddress VirtualSocket::GetRemoteAddress() const { | 133 SocketAddress VirtualSocket::GetRemoteAddress() const { |
| 136 return remote_addr_; | 134 return remote_addr_; |
| 137 } | 135 } |
| 138 | 136 |
| 139 void VirtualSocket::SetLocalAddress(const SocketAddress& addr) { | 137 void VirtualSocket::SetLocalAddress(const SocketAddress& addr) { |
| 140 local_addr_ = addr; | 138 local_addr_ = addr; |
| 141 } | 139 } |
| 142 | 140 |
| 143 void VirtualSocket::SetAlternativeLocalAddress(const SocketAddress& addr) { | |
| 144 alternative_local_addr_ = addr; | |
| 145 } | |
| 146 | |
| 147 int VirtualSocket::Bind(const SocketAddress& addr) { | 141 int VirtualSocket::Bind(const SocketAddress& addr) { |
| 148 if (!local_addr_.IsNil()) { | 142 if (!local_addr_.IsNil()) { |
| 149 error_ = EINVAL; | 143 error_ = EINVAL; |
| 150 return -1; | 144 return -1; |
| 151 } | 145 } |
| 152 local_addr_ = addr; | 146 local_addr_ = addr; |
| 153 int result = server_->Bind(this, &local_addr_); | 147 int result = server_->Bind(this, &local_addr_); |
| 154 if (result != 0) { | 148 if (result != 0) { |
| 155 local_addr_.Clear(); | 149 local_addr_.Clear(); |
| 156 error_ = EADDRINUSE; | 150 error_ = EADDRINUSE; |
| (...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 // MessageQueue will ensure WakeUp is called if another thread sends a | 629 // MessageQueue will ensure WakeUp is called if another thread sends a |
| 636 // packet. | 630 // packet. |
| 637 wakeup_.Wait(cmsWait); | 631 wakeup_.Wait(cmsWait); |
| 638 return true; | 632 return true; |
| 639 } | 633 } |
| 640 | 634 |
| 641 void VirtualSocketServer::WakeUp() { | 635 void VirtualSocketServer::WakeUp() { |
| 642 wakeup_.Set(); | 636 wakeup_.Set(); |
| 643 } | 637 } |
| 644 | 638 |
| 639 void VirtualSocketServer::SetAlternativeLocalAddress( |
| 640 const rtc::IPAddress& address, |
| 641 const rtc::IPAddress& alternative) { |
| 642 alternative_address_mapping_[address] = alternative; |
| 643 } |
| 644 |
| 645 bool VirtualSocketServer::ProcessMessagesUntilIdle() { | 645 bool VirtualSocketServer::ProcessMessagesUntilIdle() { |
| 646 RTC_DCHECK(msg_queue_ == Thread::Current()); | 646 RTC_DCHECK(msg_queue_ == Thread::Current()); |
| 647 stop_on_idle_ = true; | 647 stop_on_idle_ = true; |
| 648 while (!msg_queue_->empty()) { | 648 while (!msg_queue_->empty()) { |
| 649 if (fake_clock_) { | 649 if (fake_clock_) { |
| 650 // If using a fake clock, advance it in millisecond increments until the | 650 // If using a fake clock, advance it in millisecond increments until the |
| 651 // queue is empty. | 651 // queue is empty. |
| 652 fake_clock_->AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); | 652 fake_clock_->AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); |
| 653 } else { | 653 } else { |
| 654 // Otherwise, run a normal message loop. | 654 // Otherwise, run a normal message loop. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 // Normalize the address (turns v6-mapped addresses into v4-addresses). | 692 // Normalize the address (turns v6-mapped addresses into v4-addresses). |
| 693 SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); | 693 SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
| 694 | 694 |
| 695 AddressMap::value_type entry(normalized, socket); | 695 AddressMap::value_type entry(normalized, socket); |
| 696 return bindings_->insert(entry).second ? 0 : -1; | 696 return bindings_->insert(entry).second ? 0 : -1; |
| 697 } | 697 } |
| 698 | 698 |
| 699 int VirtualSocketServer::Bind(VirtualSocket* socket, SocketAddress* addr) { | 699 int VirtualSocketServer::Bind(VirtualSocket* socket, SocketAddress* addr) { |
| 700 RTC_DCHECK(nullptr != socket); | 700 RTC_DCHECK(nullptr != socket); |
| 701 | 701 |
| 702 // Normalize the IP. |
| 702 if (!IPIsUnspec(addr->ipaddr())) { | 703 if (!IPIsUnspec(addr->ipaddr())) { |
| 703 addr->SetIP(addr->ipaddr().Normalized()); | 704 addr->SetIP(addr->ipaddr().Normalized()); |
| 704 } else { | 705 } else { |
| 705 RTC_NOTREACHED(); | 706 RTC_NOTREACHED(); |
| 706 } | 707 } |
| 707 | 708 |
| 709 // If the IP appears in |alternative_address_mapping_|, meaning the test has |
| 710 // configured sockets bound to this IP to actually use another IP, replace |
| 711 // the IP here. |
| 712 auto alternative = alternative_address_mapping_.find(addr->ipaddr()); |
| 713 if (alternative != alternative_address_mapping_.end()) { |
| 714 addr->SetIP(alternative->second); |
| 715 } |
| 716 |
| 717 // Assign a port if not assigned. |
| 708 if (addr->port() == 0) { | 718 if (addr->port() == 0) { |
| 709 for (int i = 0; i < kEphemeralPortCount; ++i) { | 719 for (int i = 0; i < kEphemeralPortCount; ++i) { |
| 710 addr->SetPort(GetNextPort()); | 720 addr->SetPort(GetNextPort()); |
| 711 if (bindings_->find(*addr) == bindings_->end()) { | 721 if (bindings_->find(*addr) == bindings_->end()) { |
| 712 break; | 722 break; |
| 713 } | 723 } |
| 714 } | 724 } |
| 715 } | 725 } |
| 716 | 726 |
| 717 return Bind(socket, *addr); | 727 return Bind(socket, *addr); |
| (...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1204 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { | 1214 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { |
| 1205 RTC_DCHECK(!IPIsAny(from_addr)); | 1215 RTC_DCHECK(!IPIsAny(from_addr)); |
| 1206 if (from_addr.family() == AF_INET) { | 1216 if (from_addr.family() == AF_INET) { |
| 1207 default_route_v4_ = from_addr; | 1217 default_route_v4_ = from_addr; |
| 1208 } else if (from_addr.family() == AF_INET6) { | 1218 } else if (from_addr.family() == AF_INET6) { |
| 1209 default_route_v6_ = from_addr; | 1219 default_route_v6_ = from_addr; |
| 1210 } | 1220 } |
| 1211 } | 1221 } |
| 1212 | 1222 |
| 1213 } // namespace rtc | 1223 } // namespace rtc |
| OLD | NEW |