aboutsummaryrefslogtreecommitdiff
path: root/osp/impl/quic/testing/fake_quic_connection.cc
blob: 52f7241cb3a5987446d751a860c8d350bb1cdaaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "osp/impl/quic/testing/fake_quic_connection.h"

#include <memory>

#include "osp/impl/quic/testing/fake_quic_connection_factory.h"
#include "util/logging.h"

namespace openscreen {
namespace osp {

FakeQuicStream::FakeQuicStream(Delegate* delegate, uint64_t id)
    : QuicStream(delegate, id) {}

FakeQuicStream::~FakeQuicStream() = default;

void FakeQuicStream::ReceiveData(const uint8_t* data, size_t size) {
  OSP_DCHECK(!read_end_closed_);
  read_buffer_.insert(read_buffer_.end(), data, data + size);
}

void FakeQuicStream::CloseReadEnd() {
  read_end_closed_ = true;
}

std::vector<uint8_t> FakeQuicStream::TakeReceivedData() {
  return std::move(read_buffer_);
}

std::vector<uint8_t> FakeQuicStream::TakeWrittenData() {
  return std::move(write_buffer_);
}

void FakeQuicStream::Write(const uint8_t* data, size_t size) {
  OSP_DCHECK(!write_end_closed_);
  write_buffer_.insert(write_buffer_.end(), data, data + size);
}

void FakeQuicStream::CloseWriteEnd() {
  write_end_closed_ = true;
}

FakeQuicConnection::FakeQuicConnection(
    FakeQuicConnectionFactoryBridge* parent_factory,
    uint64_t connection_id,
    Delegate* delegate)
    : QuicConnection(delegate),
      parent_factory_(parent_factory),
      connection_id_(connection_id) {}

FakeQuicConnection::~FakeQuicConnection() = default;

std::unique_ptr<FakeQuicStream> FakeQuicConnection::MakeIncomingStream() {
  uint64_t stream_id = next_stream_id_++;
  auto result = std::make_unique<FakeQuicStream>(
      delegate()->NextStreamDelegate(id(), stream_id), stream_id);
  streams_.emplace(result->id(), result.get());
  return result;
}

void FakeQuicConnection::OnRead(UdpSocket* socket, ErrorOr<UdpPacket> data) {
  OSP_NOTREACHED() << "data should go directly to fake streams";
}

void FakeQuicConnection::OnSendError(UdpSocket* socket, Error error) {
  OSP_NOTREACHED() << "data should go directly to fake streams";
}

void FakeQuicConnection::OnError(UdpSocket* socket, Error error) {
  OSP_NOTREACHED() << "data should go directly to fake streams";
}

std::unique_ptr<QuicStream> FakeQuicConnection::MakeOutgoingStream(
    QuicStream::Delegate* delegate) {
  auto result = std::make_unique<FakeQuicStream>(delegate, next_stream_id_++);
  streams_.emplace(result->id(), result.get());
  parent_factory_->OnOutgoingStream(this, result.get());
  return result;
}

void FakeQuicConnection::Close() {
  parent_factory_->OnConnectionClosed(this);
  delegate()->OnConnectionClosed(connection_id_);
  for (auto& stream : streams_) {
    stream.second->delegate()->OnClose(stream.first);
    stream.second->delegate()->OnReceived(stream.second, nullptr, 0);
  }
}

}  // namespace osp
}  // namespace openscreen