aboutsummaryrefslogtreecommitdiff
path: root/osp/impl/quic/testing/fake_quic_connection_factory.cc
blob: 47f3ab8757dd6c8cbbe728836420cccc22de70c9 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// 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_factory.h"

#include <algorithm>
#include <memory>

#include "util/logging.h"

namespace openscreen {
namespace osp {

FakeQuicConnectionFactoryBridge::FakeQuicConnectionFactoryBridge(
    const IPEndpoint& controller_endpoint)
    : controller_endpoint_(controller_endpoint) {}

void FakeQuicConnectionFactoryBridge::OnConnectionClosed(
    QuicConnection* connection) {
  if (connection == connections_.controller) {
    connections_.controller = nullptr;
    return;
  } else if (connection == connections_.receiver) {
    connections_.receiver = nullptr;
    return;
  }
  OSP_DCHECK(false) << "reporting an unknown connection as closed";
}

void FakeQuicConnectionFactoryBridge::OnOutgoingStream(
    QuicConnection* connection,
    QuicStream* stream) {
  FakeQuicConnection* remote_connection = nullptr;
  if (connection == connections_.controller) {
    remote_connection = connections_.receiver;
  } else if (connection == connections_.receiver) {
    remote_connection = connections_.controller;
  }

  if (remote_connection) {
    remote_connection->delegate()->OnIncomingStream(
        remote_connection->id(), remote_connection->MakeIncomingStream());
  }
}

void FakeQuicConnectionFactoryBridge::SetServerDelegate(
    QuicConnectionFactory::ServerDelegate* delegate,
    const IPEndpoint& endpoint) {
  OSP_DCHECK(!delegate_ || !delegate);
  delegate_ = delegate;
  receiver_endpoint_ = endpoint;
}

void FakeQuicConnectionFactoryBridge::RunTasks(bool is_client) {
  bool* idle_flag = is_client ? &client_idle_ : &server_idle_;
  *idle_flag = true;

  if (!connections_.controller || !connections_.receiver) {
    return;
  }

  if (connections_pending_) {
    *idle_flag = false;
    connections_.receiver->delegate()->OnCryptoHandshakeComplete(
        connections_.receiver->id());
    connections_.controller->delegate()->OnCryptoHandshakeComplete(
        connections_.controller->id());
    connections_pending_ = false;
    return;
  }

  const size_t num_streams = connections_.controller->streams().size();
  OSP_CHECK_EQ(num_streams, connections_.receiver->streams().size());

  auto stream_it_pair =
      std::make_pair(connections_.controller->streams().begin(),
                     connections_.receiver->streams().begin());

  for (size_t i = 0; i < num_streams; ++i) {
    auto* controller_stream = stream_it_pair.first->second;
    auto* receiver_stream = stream_it_pair.second->second;

    std::vector<uint8_t> written_data = controller_stream->TakeWrittenData();
    OSP_DCHECK(controller_stream->TakeReceivedData().empty());

    if (!written_data.empty()) {
      *idle_flag = false;
      receiver_stream->delegate()->OnReceived(
          receiver_stream, reinterpret_cast<const char*>(written_data.data()),
          written_data.size());
    }

    written_data = receiver_stream->TakeWrittenData();
    OSP_DCHECK(receiver_stream->TakeReceivedData().empty());

    if (written_data.size()) {
      *idle_flag = false;
      controller_stream->delegate()->OnReceived(
          controller_stream, reinterpret_cast<const char*>(written_data.data()),
          written_data.size());
    }

    // Close the read end for closed write ends
    if (controller_stream->write_end_closed()) {
      receiver_stream->CloseReadEnd();
    }
    if (receiver_stream->write_end_closed()) {
      controller_stream->CloseReadEnd();
    }

    if (controller_stream->both_ends_closed() &&
        receiver_stream->both_ends_closed()) {
      controller_stream->delegate()->OnClose(controller_stream->id());
      receiver_stream->delegate()->OnClose(receiver_stream->id());

      controller_stream->delegate()->OnReceived(controller_stream, nullptr, 0);
      receiver_stream->delegate()->OnReceived(receiver_stream, nullptr, 0);

      stream_it_pair.first =
          connections_.controller->streams().erase(stream_it_pair.first);
      stream_it_pair.second =
          connections_.receiver->streams().erase(stream_it_pair.second);
    } else {
      // The stream pair must always be advanced at the same time.
      ++stream_it_pair.first;
      ++stream_it_pair.second;
    }
  }
}

std::unique_ptr<QuicConnection> FakeQuicConnectionFactoryBridge::Connect(
    const IPEndpoint& endpoint,
    QuicConnection::Delegate* connection_delegate) {
  if (endpoint != receiver_endpoint_) {
    return nullptr;
  }

  OSP_DCHECK(!connections_.controller);
  OSP_DCHECK(!connections_.receiver);
  auto controller_connection = std::make_unique<FakeQuicConnection>(
      this, next_connection_id_++, connection_delegate);
  connections_.controller = controller_connection.get();

  auto receiver_connection = std::make_unique<FakeQuicConnection>(
      this, next_connection_id_++,
      delegate_->NextConnectionDelegate(controller_endpoint_));
  connections_.receiver = receiver_connection.get();
  delegate_->OnIncomingConnection(std::move(receiver_connection));
  return controller_connection;
}

FakeClientQuicConnectionFactory::FakeClientQuicConnectionFactory(
    FakeQuicConnectionFactoryBridge* bridge)
    : bridge_(bridge) {}
FakeClientQuicConnectionFactory::~FakeClientQuicConnectionFactory() = default;

void FakeClientQuicConnectionFactory::SetServerDelegate(
    ServerDelegate* delegate,
    const std::vector<IPEndpoint>& endpoints) {
  OSP_DCHECK(false) << "don't call SetServerDelegate from QuicClient side";
}

std::unique_ptr<QuicConnection> FakeClientQuicConnectionFactory::Connect(
    const IPEndpoint& endpoint,
    QuicConnection::Delegate* connection_delegate) {
  return bridge_->Connect(endpoint, connection_delegate);
}

void FakeClientQuicConnectionFactory::OnError(UdpSocket* socket, Error error) {
  OSP_UNIMPLEMENTED();
}

void FakeClientQuicConnectionFactory::OnSendError(UdpSocket* socket,
                                                  Error error) {
  OSP_UNIMPLEMENTED();
}

void FakeClientQuicConnectionFactory::OnRead(UdpSocket* socket,
                                             ErrorOr<UdpPacket> packet) {
  bridge_->RunTasks(true);
  idle_ = bridge_->client_idle();
}

FakeServerQuicConnectionFactory::FakeServerQuicConnectionFactory(
    FakeQuicConnectionFactoryBridge* bridge)
    : bridge_(bridge) {}
FakeServerQuicConnectionFactory::~FakeServerQuicConnectionFactory() = default;

void FakeServerQuicConnectionFactory::SetServerDelegate(
    ServerDelegate* delegate,
    const std::vector<IPEndpoint>& endpoints) {
  if (delegate) {
    OSP_DCHECK_EQ(1u, endpoints.size())
        << "fake bridge doesn't support multiple server endpoints";
  }
  bridge_->SetServerDelegate(delegate,
                             endpoints.empty() ? IPEndpoint{} : endpoints[0]);
}

std::unique_ptr<QuicConnection> FakeServerQuicConnectionFactory::Connect(
    const IPEndpoint& endpoint,
    QuicConnection::Delegate* connection_delegate) {
  OSP_DCHECK(false) << "don't call Connect() from QuicServer side";
  return nullptr;
}

void FakeServerQuicConnectionFactory::OnError(UdpSocket* socket, Error error) {
  OSP_UNIMPLEMENTED();
}

void FakeServerQuicConnectionFactory::OnSendError(UdpSocket* socket,
                                                  Error error) {
  OSP_UNIMPLEMENTED();
}

void FakeServerQuicConnectionFactory::OnRead(UdpSocket* socket,
                                             ErrorOr<UdpPacket> packet) {
  bridge_->RunTasks(false);
  idle_ = bridge_->server_idle();
}

}  // namespace osp
}  // namespace openscreen