QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/quartc/simulated_packet_transport.h" |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 6 | |
| 7 | #include <utility> |
| 8 | |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/common/platform/api/quiche_str_cat.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | |
| 11 | namespace quic { |
| 12 | namespace simulator { |
| 13 | |
| 14 | SimulatedQuartcPacketTransport::SimulatedQuartcPacketTransport( |
| 15 | Simulator* simulator, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 16 | const std::string& name, |
| 17 | const std::string& peer_name, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | QuicByteCount queue_capacity) |
| 19 | : Endpoint(simulator, name), |
| 20 | peer_name_(peer_name), |
| 21 | egress_queue_(simulator, |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 22 | quiche::QuicheStringPrintf("%s (TX Queue)", name.c_str()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | queue_capacity) { |
| 24 | egress_queue_.set_listener_interface(this); |
| 25 | } |
| 26 | |
| 27 | int SimulatedQuartcPacketTransport::Write(const char* buffer, |
| 28 | size_t buf_len, |
| 29 | const PacketInfo& info) { |
| 30 | if (!writable_) { |
| 31 | return 0; |
| 32 | } |
| 33 | if (egress_queue_.bytes_queued() + buf_len > egress_queue_.capacity()) { |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | last_packet_number_ = info.packet_number; |
| 38 | |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 39 | auto packet = std::make_unique<Packet>(); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 40 | packet->contents = std::string(buffer, buf_len); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 41 | packet->size = buf_len; |
| 42 | packet->tx_timestamp = clock_->Now(); |
| 43 | packet->source = name(); |
| 44 | packet->destination = peer_name_; |
| 45 | |
| 46 | egress_queue_.AcceptPacket(std::move(packet)); |
| 47 | return buf_len; |
| 48 | } |
| 49 | |
| 50 | void SimulatedQuartcPacketTransport::SetDelegate(Delegate* delegate) { |
| 51 | delegate_ = delegate; |
| 52 | Schedule(clock_->Now()); |
| 53 | } |
| 54 | |
| 55 | UnconstrainedPortInterface* SimulatedQuartcPacketTransport::GetRxPort() { |
| 56 | return this; |
| 57 | } |
| 58 | |
| 59 | void SimulatedQuartcPacketTransport::SetTxPort(ConstrainedPortInterface* port) { |
| 60 | egress_queue_.set_tx_port(port); |
| 61 | Schedule(clock_->Now()); |
| 62 | } |
| 63 | |
| 64 | void SimulatedQuartcPacketTransport::AcceptPacket( |
| 65 | std::unique_ptr<Packet> packet) { |
| 66 | // Simulated switches broadcast packets to all ports if the cannot determine |
| 67 | // the recipient, so we need to drop packets that aren't intended for us. |
| 68 | if (packet->destination != name()) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | if (delegate_) { |
| 73 | delegate_->OnTransportReceived(packet->contents.data(), packet->size); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void SimulatedQuartcPacketTransport::OnPacketDequeued() { |
| 78 | if (delegate_ && writable_) { |
| 79 | delegate_->OnTransportCanWrite(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void SimulatedQuartcPacketTransport::Act() { |
| 84 | if (delegate_ && writable_) { |
| 85 | delegate_->OnTransportCanWrite(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void SimulatedQuartcPacketTransport::SetWritable(bool writable) { |
| 90 | writable_ = writable; |
| 91 | if (writable_) { |
| 92 | // May need to call |Delegate::OnTransportCanWrite|. |
| 93 | Schedule(clock_->Now()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | } // namespace simulator |
| 98 | } // namespace quic |