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" |
| 6 | #include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h" |
| 7 | |
| 8 | namespace quic { |
| 9 | namespace simulator { |
| 10 | |
| 11 | SimulatedQuartcPacketTransport::SimulatedQuartcPacketTransport( |
| 12 | Simulator* simulator, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 13 | const std::string& name, |
| 14 | const std::string& peer_name, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | QuicByteCount queue_capacity) |
| 16 | : Endpoint(simulator, name), |
| 17 | peer_name_(peer_name), |
| 18 | egress_queue_(simulator, |
| 19 | QuicStringPrintf("%s (TX Queue)", name.c_str()), |
| 20 | queue_capacity) { |
| 21 | egress_queue_.set_listener_interface(this); |
| 22 | } |
| 23 | |
| 24 | int SimulatedQuartcPacketTransport::Write(const char* buffer, |
| 25 | size_t buf_len, |
| 26 | const PacketInfo& info) { |
| 27 | if (!writable_) { |
| 28 | return 0; |
| 29 | } |
| 30 | if (egress_queue_.bytes_queued() + buf_len > egress_queue_.capacity()) { |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | last_packet_number_ = info.packet_number; |
| 35 | |
| 36 | auto packet = QuicMakeUnique<Packet>(); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 37 | packet->contents = std::string(buffer, buf_len); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 38 | packet->size = buf_len; |
| 39 | packet->tx_timestamp = clock_->Now(); |
| 40 | packet->source = name(); |
| 41 | packet->destination = peer_name_; |
| 42 | |
| 43 | egress_queue_.AcceptPacket(std::move(packet)); |
| 44 | return buf_len; |
| 45 | } |
| 46 | |
| 47 | void SimulatedQuartcPacketTransport::SetDelegate(Delegate* delegate) { |
| 48 | delegate_ = delegate; |
| 49 | Schedule(clock_->Now()); |
| 50 | } |
| 51 | |
| 52 | UnconstrainedPortInterface* SimulatedQuartcPacketTransport::GetRxPort() { |
| 53 | return this; |
| 54 | } |
| 55 | |
| 56 | void SimulatedQuartcPacketTransport::SetTxPort(ConstrainedPortInterface* port) { |
| 57 | egress_queue_.set_tx_port(port); |
| 58 | Schedule(clock_->Now()); |
| 59 | } |
| 60 | |
| 61 | void SimulatedQuartcPacketTransport::AcceptPacket( |
| 62 | std::unique_ptr<Packet> packet) { |
| 63 | // Simulated switches broadcast packets to all ports if the cannot determine |
| 64 | // the recipient, so we need to drop packets that aren't intended for us. |
| 65 | if (packet->destination != name()) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if (delegate_) { |
| 70 | delegate_->OnTransportReceived(packet->contents.data(), packet->size); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void SimulatedQuartcPacketTransport::OnPacketDequeued() { |
| 75 | if (delegate_ && writable_) { |
| 76 | delegate_->OnTransportCanWrite(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void SimulatedQuartcPacketTransport::Act() { |
| 81 | if (delegate_ && writable_) { |
| 82 | delegate_->OnTransportCanWrite(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void SimulatedQuartcPacketTransport::SetWritable(bool writable) { |
| 87 | writable_ = writable; |
| 88 | if (writable_) { |
| 89 | // May need to call |Delegate::OnTransportCanWrite|. |
| 90 | Schedule(clock_->Now()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace simulator |
| 95 | } // namespace quic |