Project import generated by Copybara.

PiperOrigin-RevId: 237361882
Change-Id: I109a68f44db867b20f8c6a7732b0ce657133e52a
diff --git a/quic/quartc/quartc_dispatcher.cc b/quic/quartc/quartc_dispatcher.cc
new file mode 100644
index 0000000..8070203
--- /dev/null
+++ b/quic/quartc/quartc_dispatcher.cc
@@ -0,0 +1,80 @@
+// Copyright (c) 2017 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 "net/third_party/quiche/src/quic/quartc/quartc_dispatcher.h"
+
+#include "net/third_party/quiche/src/quic/core/quic_utils.h"
+#include "net/third_party/quiche/src/quic/core/quic_versions.h"
+#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
+#include "net/third_party/quiche/src/quic/quartc/quartc_factory.h"
+
+namespace quic {
+
+QuartcDispatcher::QuartcDispatcher(
+    std::unique_ptr<QuicConfig> config,
+    std::unique_ptr<QuicCryptoServerConfig> crypto_config,
+    QuicStringPiece crypto_config_serialized,
+    QuicVersionManager* version_manager,
+    std::unique_ptr<QuicConnectionHelperInterface> helper,
+    std::unique_ptr<QuicCryptoServerStream::Helper> session_helper,
+    std::unique_ptr<QuicAlarmFactory> alarm_factory,
+    std::unique_ptr<QuartcPacketWriter> packet_writer,
+    Delegate* delegate)
+    : QuicDispatcher(config.get(),
+                     crypto_config.get(),
+                     version_manager,
+                     std::move(helper),
+                     std::move(session_helper),
+                     std::move(alarm_factory),
+                     kQuicDefaultConnectionIdLength),
+      owned_quic_config_(std::move(config)),
+      owned_crypto_config_(std::move(crypto_config)),
+      crypto_config_(crypto_config_serialized),
+      delegate_(delegate),
+      packet_writer_(packet_writer.get()) {
+  // QuicDispatcher takes ownership of the writer.
+  QuicDispatcher::InitializeWithWriter(packet_writer.release());
+  // NB: This must happen *after* InitializeWithWriter.  It can call us back
+  // with OnTransportCanWrite() immediately, and the dispatcher needs to be
+  // fully initialized to handle that.
+  packet_writer_->SetPacketTransportDelegate(this);
+}
+
+QuartcDispatcher::~QuartcDispatcher() {
+  packet_writer_->SetPacketTransportDelegate(nullptr);
+}
+
+QuartcSession* QuartcDispatcher::CreateQuicSession(
+    QuicConnectionId connection_id,
+    const QuicSocketAddress& client_address,
+    QuicStringPiece alpn,
+    const ParsedQuicVersion& version) {
+  std::unique_ptr<QuicConnection> connection = CreateQuicConnection(
+      connection_id, client_address, helper(), alarm_factory(), writer(),
+      Perspective::IS_SERVER, ParsedQuicVersionVector{version});
+  QuartcSession* session = new QuartcServerSession(
+      std::move(connection), /*visitor=*/this, config(), GetSupportedVersions(),
+      helper()->GetClock(), crypto_config(), compressed_certs_cache(),
+      session_helper());
+  delegate_->OnSessionCreated(session);
+  return session;
+}
+
+void QuartcDispatcher::OnTransportCanWrite() {
+  OnCanWrite();
+}
+
+void QuartcDispatcher::OnTransportReceived(const char* data, size_t data_len) {
+  // QuartcPacketTransport does not surface real peer addresses, so the
+  // dispatcher uses a dummy address when processing incoming packets. Note that
+  // the dispatcher refuses to process anything with port 0.
+  static const QuicSocketAddress* dummy_address =
+      new QuicSocketAddress(QuicIpAddress::Any4(), /*port=*/1);
+
+  QuicReceivedPacket packet(data, data_len, helper()->GetClock()->Now());
+  ProcessPacket(/*self_address=*/*dummy_address,
+                /*peer_address=*/*dummy_address, packet);
+}
+
+}  // namespace quic