quic_client_message_loop_network_helper.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (c) 2012 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. #include "net/tools/quic/quic_client_message_loop_network_helper.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/logging.h"
  8. #include "base/run_loop.h"
  9. #include "base/threading/thread_task_runner_handle.h"
  10. #include "net/base/net_errors.h"
  11. #include "net/http/http_request_info.h"
  12. #include "net/http/http_response_info.h"
  13. #include "net/log/net_log_source.h"
  14. #include "net/log/net_log_with_source.h"
  15. #include "net/quic/address_utils.h"
  16. #include "net/quic/quic_chromium_alarm_factory.h"
  17. #include "net/quic/quic_chromium_connection_helper.h"
  18. #include "net/quic/quic_chromium_packet_reader.h"
  19. #include "net/quic/quic_chromium_packet_writer.h"
  20. #include "net/socket/udp_client_socket.h"
  21. #include "net/spdy/spdy_http_utils.h"
  22. #include "net/third_party/quiche/src/quiche/quic/core/crypto/quic_random.h"
  23. #include "net/third_party/quiche/src/quiche/quic/core/http/spdy_utils.h"
  24. #include "net/third_party/quiche/src/quiche/quic/core/quic_connection.h"
  25. #include "net/third_party/quiche/src/quiche/quic/core/quic_packets.h"
  26. #include "net/third_party/quiche/src/quiche/quic/core/quic_server_id.h"
  27. #include "net/third_party/quiche/src/quiche/quic/platform/api/quic_flags.h"
  28. #include "net/third_party/quiche/src/quiche/spdy/core/http2_header_block.h"
  29. using std::string;
  30. namespace net {
  31. QuicClientMessageLooplNetworkHelper::QuicClientMessageLooplNetworkHelper(
  32. quic::QuicChromiumClock* clock,
  33. quic::QuicClientBase* client)
  34. : clock_(clock), client_(client) {}
  35. QuicClientMessageLooplNetworkHelper::~QuicClientMessageLooplNetworkHelper() =
  36. default;
  37. bool QuicClientMessageLooplNetworkHelper::CreateUDPSocketAndBind(
  38. quic::QuicSocketAddress server_address,
  39. quic::QuicIpAddress bind_to_address,
  40. int bind_to_port) {
  41. auto socket = std::make_unique<UDPClientSocket>(DatagramSocket::DEFAULT_BIND,
  42. nullptr, NetLogSource());
  43. if (bind_to_address.IsInitialized()) {
  44. client_address_ =
  45. quic::QuicSocketAddress(bind_to_address, client_->local_port());
  46. } else if (server_address.host().address_family() ==
  47. quic::IpAddressFamily::IP_V4) {
  48. client_address_ =
  49. quic::QuicSocketAddress(quic::QuicIpAddress::Any4(), bind_to_port);
  50. } else {
  51. client_address_ =
  52. quic::QuicSocketAddress(quic::QuicIpAddress::Any6(), bind_to_port);
  53. }
  54. int rc = socket->Connect(ToIPEndPoint(server_address));
  55. if (rc != OK) {
  56. LOG(ERROR) << "Connect failed: " << ErrorToShortString(rc);
  57. return false;
  58. }
  59. rc = socket->SetReceiveBufferSize(quic::kDefaultSocketReceiveBuffer);
  60. if (rc != OK) {
  61. LOG(ERROR) << "SetReceiveBufferSize() failed: " << ErrorToShortString(rc);
  62. return false;
  63. }
  64. rc = socket->SetSendBufferSize(quic::kDefaultSocketReceiveBuffer);
  65. if (rc != OK) {
  66. LOG(ERROR) << "SetSendBufferSize() failed: " << ErrorToShortString(rc);
  67. return false;
  68. }
  69. IPEndPoint address;
  70. rc = socket->GetLocalAddress(&address);
  71. if (rc != OK) {
  72. LOG(ERROR) << "GetLocalAddress failed: " << ErrorToShortString(rc);
  73. return false;
  74. }
  75. client_address_ = ToQuicSocketAddress(address);
  76. socket_.swap(socket);
  77. packet_reader_ = std::make_unique<QuicChromiumPacketReader>(
  78. socket_.get(), clock_, this, kQuicYieldAfterPacketsRead,
  79. quic::QuicTime::Delta::FromMilliseconds(
  80. kQuicYieldAfterDurationMilliseconds),
  81. NetLogWithSource());
  82. if (socket != nullptr) {
  83. socket->Close();
  84. }
  85. return true;
  86. }
  87. void QuicClientMessageLooplNetworkHelper::CleanUpAllUDPSockets() {
  88. client_->reset_writer();
  89. packet_reader_.reset();
  90. packet_reader_started_ = false;
  91. }
  92. void QuicClientMessageLooplNetworkHelper::StartPacketReaderIfNotStarted() {
  93. if (!packet_reader_started_) {
  94. packet_reader_->StartReading();
  95. packet_reader_started_ = true;
  96. }
  97. }
  98. void QuicClientMessageLooplNetworkHelper::RunEventLoop() {
  99. StartPacketReaderIfNotStarted();
  100. base::RunLoop().RunUntilIdle();
  101. }
  102. quic::QuicPacketWriter*
  103. QuicClientMessageLooplNetworkHelper::CreateQuicPacketWriter() {
  104. // This is always called once per QuicSession before
  105. // StartPacketReaderIfNotStarted. However if the QuicClient is creating
  106. // multiple sessions it needs to restart the packet reader for the second one
  107. // so we set packet_reader_started_ to false to ensure that.
  108. packet_reader_started_ = false;
  109. return new QuicChromiumPacketWriter(
  110. socket_.get(), base::ThreadTaskRunnerHandle::Get().get());
  111. }
  112. bool QuicClientMessageLooplNetworkHelper::OnReadError(
  113. int result,
  114. const DatagramClientSocket* socket) {
  115. LOG(ERROR) << "QuicSimpleClient read failed: " << ErrorToShortString(result);
  116. client_->Disconnect();
  117. return false;
  118. }
  119. quic::QuicSocketAddress
  120. QuicClientMessageLooplNetworkHelper::GetLatestClientAddress() const {
  121. return client_address_;
  122. }
  123. bool QuicClientMessageLooplNetworkHelper::OnPacket(
  124. const quic::QuicReceivedPacket& packet,
  125. const quic::QuicSocketAddress& local_address,
  126. const quic::QuicSocketAddress& peer_address) {
  127. client_->session()->connection()->ProcessUdpPacket(local_address,
  128. peer_address, packet);
  129. if (!client_->session()->connection()->connected()) {
  130. return false;
  131. }
  132. return true;
  133. }
  134. } // namespace net