OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "blimp/net/client_connection_manager.h" | 5 #include "blimp/net/client_connection_manager.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "blimp/common/create_blimp_message.h" | 8 #include "blimp/common/create_blimp_message.h" |
9 #include "blimp/common/proto/blimp_message.pb.h" | 9 #include "blimp/common/proto/blimp_message.pb.h" |
10 #include "blimp/common/protocol_version.h" | 10 #include "blimp/common/protocol_version.h" |
11 #include "blimp/net/blimp_connection.h" | 11 #include "blimp/net/blimp_connection.h" |
12 #include "blimp/net/blimp_message_processor.h" | 12 #include "blimp/net/blimp_message_processor.h" |
13 #include "blimp/net/blimp_transport.h" | 13 #include "blimp/net/blimp_transport.h" |
14 #include "blimp/net/connection_handler.h" | 14 #include "blimp/net/connection_handler.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 | 16 |
17 namespace blimp { | 17 namespace blimp { |
18 | 18 |
19 ClientConnectionManager::ClientConnectionManager( | 19 ClientConnectionManager::ClientConnectionManager( |
20 ConnectionHandler* connection_handler) | 20 ConnectionHandler* connection_handler) |
21 : connection_handler_(connection_handler) { | 21 : connection_handler_(connection_handler), weak_factory_(this) { |
22 DCHECK(connection_handler_); | 22 DCHECK(connection_handler_); |
23 } | 23 } |
24 | 24 |
25 ClientConnectionManager::~ClientConnectionManager() {} | 25 ClientConnectionManager::~ClientConnectionManager() {} |
26 | 26 |
27 void ClientConnectionManager::AddTransport( | 27 void ClientConnectionManager::AddTransport( |
28 scoped_ptr<BlimpTransport> transport) { | 28 scoped_ptr<BlimpTransport> transport) { |
29 DCHECK(transport); | 29 DCHECK(transport); |
30 transports_.push_back(std::move(transport)); | 30 transports_.push_back(std::move(transport)); |
31 } | 31 } |
32 | 32 |
33 void ClientConnectionManager::Connect() { | 33 void ClientConnectionManager::Connect() { |
34 // A |transport| added first is used first. When it fails to connect, | 34 // A |transport| added first is used first. When it fails to connect, |
35 // the next transport is used. | 35 // the next transport is used. |
36 DCHECK(!transports_.empty()); | 36 DCHECK(!transports_.empty()); |
37 Connect(0); | 37 Connect(0); |
38 } | 38 } |
39 | 39 |
40 void ClientConnectionManager::Connect(int transport_index) { | 40 void ClientConnectionManager::Connect(int transport_index) { |
41 DVLOG(1) << "ClientConnectionManager::Connect(" << transport_index << ")"; | |
41 if (static_cast<size_t>(transport_index) < transports_.size()) { | 42 if (static_cast<size_t>(transport_index) < transports_.size()) { |
42 transports_[transport_index]->Connect( | 43 transports_[transport_index]->Connect( |
43 base::Bind(&ClientConnectionManager::OnConnectResult, | 44 base::Bind(&ClientConnectionManager::OnConnectResult, |
44 base::Unretained(this), transport_index)); | 45 base::Unretained(this), transport_index)); |
45 } else { | 46 } else { |
46 // TODO(haibinlu): add an error reporting path out for this. | 47 // TODO(haibinlu): add an error reporting path out for this. |
47 LOG(WARNING) << "All transports failed to connect"; | 48 LOG(WARNING) << "All transports failed to connect"; |
48 } | 49 } |
49 } | 50 } |
50 | 51 |
51 void ClientConnectionManager::OnConnectResult(int transport_index, int result) { | 52 void ClientConnectionManager::OnConnectResult(int transport_index, int result) { |
52 DCHECK_NE(result, net::ERR_IO_PENDING); | 53 DCHECK_NE(result, net::ERR_IO_PENDING); |
53 const auto& transport = transports_[transport_index]; | 54 const auto& transport = transports_[transport_index]; |
54 if (result == net::OK) { | 55 if (result == net::OK) { |
55 scoped_ptr<BlimpConnection> connection = transport->TakeConnection(); | 56 scoped_ptr<BlimpConnection> connection = transport->TakeConnection(); |
56 SendAuthenticationMessage(connection.get()); | 57 connection->SetConnectionErrorObserver(this); |
haibinlu
2015/12/29 00:51:46
ClientConnectionManager will then rely on error ob
Kevin M
2015/12/30 23:08:49
Good point. ClientConnectionManager will retain th
| |
57 connection_handler_->HandleConnection(std::move(connection)); | 58 SendAuthenticationMessage(std::move(connection)); |
58 } else { | 59 } else { |
59 DVLOG(1) << "Transport " << transport->GetName() | 60 DVLOG(1) << "Transport " << transport->GetName() |
60 << " failed to connect:" << net::ErrorToString(result); | 61 << " failed to connect:" << net::ErrorToString(result); |
61 Connect(transport_index + 1); | 62 Connect(transport_index + 1); |
62 } | 63 } |
63 } | 64 } |
64 | 65 |
65 void ClientConnectionManager::SendAuthenticationMessage( | 66 void ClientConnectionManager::SendAuthenticationMessage( |
66 BlimpConnection* connection) { | 67 scoped_ptr<BlimpConnection> connection) { |
67 // TODO(haibinlu): get client token. | 68 // TODO(haibinlu): get client token. |
haibinlu
2015/12/29 00:51:46
remove this TODO
Kevin M
2015/12/30 23:08:49
Done.
| |
68 const char* client_token = ""; | 69 DVLOG(1) << "Sending authentication message."; |
69 connection->GetOutgoingMessageProcessor()->ProcessMessage( | 70 connection->GetOutgoingMessageProcessor()->ProcessMessage( |
70 CreateStartConnectionMessage(client_token, kProtocolVersion), | 71 CreateStartConnectionMessage(client_token_, kProtocolVersion), |
71 net::CompletionCallback()); | 72 base::Bind(&ClientConnectionManager::AuthenticationMessageSent, |
haibinlu
2015/12/29 00:51:46
OnAuthenticationMessageSent?
Kevin M
2015/12/30 23:08:49
Done.
| |
73 weak_factory_.GetWeakPtr(), | |
74 base::Passed(std::move(connection)))); | |
haibinlu
2015/12/29 00:51:46
No need for std::move with base::Passed
Kevin M
2015/12/30 23:08:49
Passed requires a move-only type as its input, whi
| |
75 } | |
76 | |
77 void ClientConnectionManager::AuthenticationMessageSent( | |
78 scoped_ptr<BlimpConnection> connection, | |
79 int result) { | |
80 DVLOG(1) << "AuthenticationMessageSent, result=" << result; | |
81 if (result != net::OK) { | |
82 // If a write error occurred, just throw away |connection|. | |
83 // We don't need to propagate the error code here because the connection | |
84 // will already have done so via the ErrorObserver object. | |
85 return; | |
86 } | |
87 connection_handler_->HandleConnection(std::move(connection)); | |
88 } | |
89 | |
90 void ClientConnectionManager::OnConnectionError(int error) { | |
91 // TODO(kmarshall): Replace this with actual reconnection logic. | |
92 VLOG(0) << "Connection dropped, error=" << error; | |
93 Connect(); | |
72 } | 94 } |
73 | 95 |
74 } // namespace blimp | 96 } // namespace blimp |
OLD | NEW |