blob: 8e82d98b62d56ef51cf45f82ea76d03a4e636bc2 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
8namespace quic {
9namespace simulator {
10
11SimulatedQuartcPacketTransport::SimulatedQuartcPacketTransport(
12 Simulator* simulator,
vasilvvc48c8712019-03-11 13:38:16 -070013 const std::string& name,
14 const std::string& peer_name,
QUICHE teama6ef0a62019-03-07 20:34:33 -050015 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
24int 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>();
vasilvvc48c8712019-03-11 13:38:16 -070037 packet->contents = std::string(buffer, buf_len);
QUICHE teama6ef0a62019-03-07 20:34:33 -050038 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
47void SimulatedQuartcPacketTransport::SetDelegate(Delegate* delegate) {
48 delegate_ = delegate;
49 Schedule(clock_->Now());
50}
51
52UnconstrainedPortInterface* SimulatedQuartcPacketTransport::GetRxPort() {
53 return this;
54}
55
56void SimulatedQuartcPacketTransport::SetTxPort(ConstrainedPortInterface* port) {
57 egress_queue_.set_tx_port(port);
58 Schedule(clock_->Now());
59}
60
61void 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
74void SimulatedQuartcPacketTransport::OnPacketDequeued() {
75 if (delegate_ && writable_) {
76 delegate_->OnTransportCanWrite();
77 }
78}
79
80void SimulatedQuartcPacketTransport::Act() {
81 if (delegate_ && writable_) {
82 delegate_->OnTransportCanWrite();
83 }
84}
85
86void 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