QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017 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/quartc_dispatcher.h" |
| 6 | |
| 7 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 8 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
| 9 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 10 | #include "net/third_party/quiche/src/quic/quartc/quartc_factory.h" |
| 11 | |
| 12 | namespace quic { |
| 13 | |
| 14 | QuartcDispatcher::QuartcDispatcher( |
| 15 | std::unique_ptr<QuicConfig> config, |
| 16 | std::unique_ptr<QuicCryptoServerConfig> crypto_config, |
| 17 | QuicStringPiece crypto_config_serialized, |
| 18 | QuicVersionManager* version_manager, |
| 19 | std::unique_ptr<QuicConnectionHelperInterface> helper, |
| 20 | std::unique_ptr<QuicCryptoServerStream::Helper> session_helper, |
| 21 | std::unique_ptr<QuicAlarmFactory> alarm_factory, |
| 22 | std::unique_ptr<QuartcPacketWriter> packet_writer, |
| 23 | Delegate* delegate) |
| 24 | : QuicDispatcher(config.get(), |
| 25 | crypto_config.get(), |
| 26 | version_manager, |
| 27 | std::move(helper), |
| 28 | std::move(session_helper), |
| 29 | std::move(alarm_factory), |
| 30 | kQuicDefaultConnectionIdLength), |
| 31 | owned_quic_config_(std::move(config)), |
| 32 | owned_crypto_config_(std::move(crypto_config)), |
| 33 | crypto_config_(crypto_config_serialized), |
| 34 | delegate_(delegate), |
| 35 | packet_writer_(packet_writer.get()) { |
| 36 | // QuicDispatcher takes ownership of the writer. |
| 37 | QuicDispatcher::InitializeWithWriter(packet_writer.release()); |
| 38 | // NB: This must happen *after* InitializeWithWriter. It can call us back |
| 39 | // with OnTransportCanWrite() immediately, and the dispatcher needs to be |
| 40 | // fully initialized to handle that. |
| 41 | packet_writer_->SetPacketTransportDelegate(this); |
| 42 | } |
| 43 | |
| 44 | QuartcDispatcher::~QuartcDispatcher() { |
| 45 | packet_writer_->SetPacketTransportDelegate(nullptr); |
| 46 | } |
| 47 | |
| 48 | QuartcSession* QuartcDispatcher::CreateQuicSession( |
| 49 | QuicConnectionId connection_id, |
| 50 | const QuicSocketAddress& client_address, |
| 51 | QuicStringPiece alpn, |
| 52 | const ParsedQuicVersion& version) { |
| 53 | std::unique_ptr<QuicConnection> connection = CreateQuicConnection( |
| 54 | connection_id, client_address, helper(), alarm_factory(), writer(), |
| 55 | Perspective::IS_SERVER, ParsedQuicVersionVector{version}); |
| 56 | QuartcSession* session = new QuartcServerSession( |
| 57 | std::move(connection), /*visitor=*/this, config(), GetSupportedVersions(), |
| 58 | helper()->GetClock(), crypto_config(), compressed_certs_cache(), |
| 59 | session_helper()); |
| 60 | delegate_->OnSessionCreated(session); |
| 61 | return session; |
| 62 | } |
| 63 | |
| 64 | void QuartcDispatcher::OnTransportCanWrite() { |
| 65 | OnCanWrite(); |
| 66 | } |
| 67 | |
| 68 | void QuartcDispatcher::OnTransportReceived(const char* data, size_t data_len) { |
| 69 | // QuartcPacketTransport does not surface real peer addresses, so the |
| 70 | // dispatcher uses a dummy address when processing incoming packets. Note that |
| 71 | // the dispatcher refuses to process anything with port 0. |
| 72 | static const QuicSocketAddress* dummy_address = |
| 73 | new QuicSocketAddress(QuicIpAddress::Any4(), /*port=*/1); |
| 74 | |
| 75 | QuicReceivedPacket packet(data, data_len, helper()->GetClock()->Now()); |
| 76 | ProcessPacket(/*self_address=*/*dummy_address, |
| 77 | /*peer_address=*/*dummy_address, packet); |
| 78 | } |
| 79 | |
| 80 | } // namespace quic |