Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1208)

Side by Side Diff: net/socket/tcp_client_socket_pool.cc

Issue 151118: Refactor ConnectJob and TCPConnectJob. (Closed)
Patch Set: Add comments. Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/socket/tcp_client_socket_pool.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/socket/tcp_client_socket_pool.h" 5 #include "net/socket/tcp_client_socket_pool.h"
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
12 #include "net/socket/client_socket_factory.h" 12 #include "net/socket/client_socket_factory.h"
13 #include "net/socket/client_socket_handle.h" 13 #include "net/socket/client_socket_handle.h"
14 #include "net/socket/client_socket_pool_base.h" 14 #include "net/socket/client_socket_pool_base.h"
15 #include "net/socket/tcp_client_socket.h" 15 #include "net/socket/tcp_client_socket.h"
16 16
17 using base::TimeDelta; 17 using base::TimeDelta;
18 18
19 namespace net { 19 namespace net {
20 20
21 TCPConnectJob::TCPConnectJob( 21 TCPConnectJob::TCPConnectJob(
22 const std::string& group_name, 22 const std::string& group_name,
23 const HostResolver::RequestInfo& resolve_info, 23 const HostResolver::RequestInfo& resolve_info,
24 const ClientSocketHandle* handle, 24 const ClientSocketHandle* handle,
25 ClientSocketFactory* client_socket_factory, 25 ClientSocketFactory* client_socket_factory,
26 HostResolver* host_resolver, 26 HostResolver* host_resolver,
27 Delegate* delegate) 27 Delegate* delegate)
28 : group_name_(group_name), 28 : ConnectJob(group_name, handle, delegate),
29 resolve_info_(resolve_info), 29 resolve_info_(resolve_info),
30 handle_(handle),
31 client_socket_factory_(client_socket_factory), 30 client_socket_factory_(client_socket_factory),
32 ALLOW_THIS_IN_INITIALIZER_LIST( 31 ALLOW_THIS_IN_INITIALIZER_LIST(
33 callback_(this, 32 callback_(this,
34 &TCPConnectJob::OnIOComplete)), 33 &TCPConnectJob::OnIOComplete)),
35 delegate_(delegate),
36 resolver_(host_resolver) {} 34 resolver_(host_resolver) {}
37 35
38 TCPConnectJob::~TCPConnectJob() { 36 TCPConnectJob::~TCPConnectJob() {
39 // We don't worry about cancelling the host resolution and TCP connect, since 37 // We don't worry about cancelling the host resolution and TCP connect, since
40 // ~SingleRequestHostResolver and ~ClientSocket will take care of it. 38 // ~SingleRequestHostResolver and ~ClientSocket will take care of it.
41 } 39 }
42 40
43 int TCPConnectJob::Connect() { 41 int TCPConnectJob::Connect() {
44 set_load_state(LOAD_STATE_RESOLVING_HOST); 42 next_state_ = kStateResolveHost;
45 int rv = resolver_.Resolve(resolve_info_, &addresses_, &callback_); 43 return DoLoop(OK);
44 }
45
46 void TCPConnectJob::OnIOComplete(int result) {
47 int rv = DoLoop(result);
46 if (rv != ERR_IO_PENDING) 48 if (rv != ERR_IO_PENDING)
47 rv = OnIOCompleteInternal(rv, true /* synchronous */); 49 delegate()->OnConnectJobComplete(rv, this); // Deletes |this|
50 }
51
52 int TCPConnectJob::DoLoop(int result) {
53 DCHECK_NE(next_state_, kStateNone);
54
55 int rv = result;
56 do {
57 State state = next_state_;
58 next_state_ = kStateNone;
59 switch (state) {
60 case kStateResolveHost:
61 DCHECK_EQ(OK, rv);
62 rv = DoResolveHost();
63 break;
64 case kStateResolveHostComplete:
65 rv = DoResolveHostComplete(rv);
66 break;
67 case kStateTCPConnect:
68 DCHECK_EQ(OK, rv);
69 rv = DoTCPConnect();
70 break;
71 case kStateTCPConnectComplete:
72 rv = DoTCPConnectComplete(rv);
73 break;
74 default:
75 NOTREACHED();
76 rv = ERR_FAILED;
77 break;
78 }
79 } while (rv != ERR_IO_PENDING && next_state_ != kStateNone);
80
48 return rv; 81 return rv;
49 } 82 }
50 83
51 void TCPConnectJob::OnIOComplete(int result) { 84 int TCPConnectJob::DoResolveHost() {
52 OnIOCompleteInternal(result, false /* asynchronous */); 85 set_load_state(LOAD_STATE_RESOLVING_HOST);
86 next_state_ = kStateResolveHostComplete;
87 return resolver_.Resolve(resolve_info_, &addresses_, &callback_);
53 } 88 }
54 89
55 int TCPConnectJob::OnIOCompleteInternal( 90 int TCPConnectJob::DoResolveHostComplete(int result) {
56 int result, bool synchronous) { 91 DCHECK_EQ(LOAD_STATE_RESOLVING_HOST, load_state());
57 CHECK(result != ERR_IO_PENDING); 92 if (result == OK)
93 next_state_ = kStateTCPConnect;
94 return result;
95 }
58 96
59 if (result == OK && load_state() == LOAD_STATE_RESOLVING_HOST) { 97 int TCPConnectJob::DoTCPConnect() {
60 set_load_state(LOAD_STATE_CONNECTING); 98 next_state_ = kStateTCPConnectComplete;
61 socket_.reset(client_socket_factory_->CreateTCPClientSocket(addresses_)); 99 set_load_state(LOAD_STATE_CONNECTING);
62 connect_start_time_ = base::TimeTicks::Now(); 100 set_socket(client_socket_factory_->CreateTCPClientSocket(addresses_));
63 result = socket_->Connect(&callback_); 101 connect_start_time_ = base::TimeTicks::Now();
64 if (result == ERR_IO_PENDING) 102 return socket()->Connect(&callback_);
65 return result; 103 }
66 }
67 104
105 int TCPConnectJob::DoTCPConnectComplete(int result) {
106 DCHECK_EQ(load_state(), LOAD_STATE_CONNECTING);
68 if (result == OK) { 107 if (result == OK) {
69 DCHECK_EQ(load_state(), LOAD_STATE_CONNECTING); 108 DCHECK(connect_start_time_ != base::TimeTicks());
70 CHECK(connect_start_time_ != base::TimeTicks());
71 base::TimeDelta connect_duration = 109 base::TimeDelta connect_duration =
72 base::TimeTicks::Now() - connect_start_time_; 110 base::TimeTicks::Now() - connect_start_time_;
73 111
74 UMA_HISTOGRAM_CLIPPED_TIMES("Net.TCP_Connection_Latency", 112 UMA_HISTOGRAM_CLIPPED_TIMES("Net.TCP_Connection_Latency",
75 connect_duration, 113 connect_duration,
76 base::TimeDelta::FromMilliseconds(1), 114 base::TimeDelta::FromMilliseconds(1),
77 base::TimeDelta::FromMinutes(10), 115 base::TimeDelta::FromMinutes(10),
78 100); 116 100);
79 } 117 }
80 118
81 // Now, we either succeeded at Connect()'ing, or we failed at host resolution
82 // or Connect()'ing. Either way, we'll run the callback to alert the client.
83
84 delegate_->OnConnectJobComplete(
85 group_name_,
86 handle_,
87 result == OK ? socket_.release() : NULL,
88 result,
89 !synchronous);
90
91 // |this| is deleted after this point.
92 return result; 119 return result;
93 } 120 }
94 121
95 ConnectJob* TCPClientSocketPool::TCPConnectJobFactory::NewConnectJob( 122 ConnectJob* TCPClientSocketPool::TCPConnectJobFactory::NewConnectJob(
96 const std::string& group_name, 123 const std::string& group_name,
97 const ClientSocketPoolBase::Request& request, 124 const ClientSocketPoolBase::Request& request,
98 ConnectJob::Delegate* delegate) const { 125 ConnectJob::Delegate* delegate) const {
99 return new TCPConnectJob( 126 return new TCPConnectJob(
100 group_name, request.resolve_info, request.handle, 127 group_name, request.resolve_info, request.handle,
101 client_socket_factory_, host_resolver_, delegate); 128 client_socket_factory_, host_resolver_, delegate);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 const std::string& group_name) const { 168 const std::string& group_name) const {
142 return base_->IdleSocketCountInGroup(group_name); 169 return base_->IdleSocketCountInGroup(group_name);
143 } 170 }
144 171
145 LoadState TCPClientSocketPool::GetLoadState( 172 LoadState TCPClientSocketPool::GetLoadState(
146 const std::string& group_name, const ClientSocketHandle* handle) const { 173 const std::string& group_name, const ClientSocketHandle* handle) const {
147 return base_->GetLoadState(group_name, handle); 174 return base_->GetLoadState(group_name, handle);
148 } 175 }
149 176
150 } // namespace net 177 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/tcp_client_socket_pool.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698