File: tcp_stream_attempt.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (135 lines) | stat: -rw-r--r-- 4,145 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/socket/tcp_stream_attempt.h"

#include <memory>
#include <string_view>

#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/values.h"
#include "net/base/address_list.h"
#include "net/base/net_errors.h"
#include "net/base/tracing.h"
#include "net/socket/client_socket_factory.h"
#include "net/socket/socket_performance_watcher.h"
#include "net/socket/socket_performance_watcher_factory.h"
#include "net/socket/transport_client_socket.h"

namespace net {

// static
std::string_view TcpStreamAttempt::StateToString(State state) {
  switch (state) {
    case State::kNone:
      return "None";
    case State::kConnecting:
      return "Connecting";
  }
}

TcpStreamAttempt::TcpStreamAttempt(const StreamAttemptParams* params,
                                   IPEndPoint ip_endpoint,
                                   perfetto::Track track,
                                   const NetLogWithSource* net_log)
    : StreamAttempt(params,
                    ip_endpoint,
                    track,
                    NetLogSourceType::TCP_STREAM_ATTEMPT,
                    NetLogEventType::TCP_STREAM_ATTEMPT_ALIVE,
                    net_log) {}

TcpStreamAttempt::~TcpStreamAttempt() {
  MaybeRecordConnectEnd(ERR_ABORTED);
}

LoadState TcpStreamAttempt::GetLoadState() const {
  switch (next_state_) {
    case State::kNone:
      return LOAD_STATE_IDLE;
    case State::kConnecting:
      return LOAD_STATE_CONNECTING;
  }
}

base::Value::Dict TcpStreamAttempt::GetInfoAsValue() const {
  base::Value::Dict dict;
  dict.Set("next_state", StateToString(next_state_));
  return dict;
}

int TcpStreamAttempt::StartInternal() {
  next_state_ = State::kConnecting;

  std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher;
  if (params().socket_performance_watcher_factory) {
    socket_performance_watcher =
        params()
            .socket_performance_watcher_factory->CreateSocketPerformanceWatcher(
                SocketPerformanceWatcherFactory::PROTOCOL_TCP,
                ip_endpoint().address());
  }

  std::unique_ptr<TransportClientSocket> stream_socket =
      params().client_socket_factory->CreateTransportClientSocket(
          AddressList(ip_endpoint()), std::move(socket_performance_watcher),
          params().network_quality_estimator, net_log().net_log(),
          net_log().source());

  TransportClientSocket* socket_ptr = stream_socket.get();
  SetStreamSocket(std::move(stream_socket));

  mutable_connect_timing().connect_start = base::TimeTicks::Now();
  CHECK(!timeout_timer_.IsRunning());
  timeout_timer_.Start(
      FROM_HERE, kTcpHandshakeTimeout,
      base::BindOnce(&TcpStreamAttempt::OnTimeout, base::Unretained(this)));

  TRACE_EVENT_BEGIN("net.stream", "TcpConnect", track());
  net_log().AddEventReferencingSource(
      NetLogEventType::TCP_STREAM_ATTEMPT_CONNECT,
      socket_ptr->NetLog().source());
  int rv = socket_ptr->Connect(
      base::BindOnce(&TcpStreamAttempt::OnIOComplete, base::Unretained(this)));
  if (rv != ERR_IO_PENDING) {
    HandleCompletion(rv);
  }
  return rv;
}

base::Value::Dict TcpStreamAttempt::GetNetLogStartParams() {
  base::Value::Dict dict;
  dict.Set("ip_endpoint", ip_endpoint().ToString());
  return dict;
}

void TcpStreamAttempt::HandleCompletion(int rv) {
  MaybeRecordConnectEnd(rv);
  next_state_ = State::kNone;
  timeout_timer_.Stop();
  mutable_connect_timing().connect_end = base::TimeTicks::Now();
}

void TcpStreamAttempt::OnIOComplete(int rv) {
  CHECK_NE(rv, ERR_IO_PENDING);
  HandleCompletion(rv);
  NotifyOfCompletion(rv);
}

void TcpStreamAttempt::OnTimeout() {
  SetStreamSocket(nullptr);
  // TODO(bashi): The error code should be ERR_CONNECTION_TIMED_OUT but use
  // ERR_TIMED_OUT for consistency with ConnectJobs.
  OnIOComplete(ERR_TIMED_OUT);
}

void TcpStreamAttempt::MaybeRecordConnectEnd(int rv) {
  if (!timeout_timer_.IsRunning()) {
    return;
  }
  TRACE_EVENT_END("net.stream", track(), "result", rv);
}

}  // namespace net