OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/net/client_connection_manager.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "blimp/common/create_blimp_message.h" | |
12 #include "blimp/common/proto/blimp_message.pb.h" | |
13 #include "blimp/common/protocol_version.h" | |
14 #include "blimp/net/blimp_connection.h" | |
15 #include "blimp/net/blimp_message_processor.h" | |
16 #include "blimp/net/blimp_transport.h" | |
17 #include "blimp/net/browser_connection_handler.h" | |
18 #include "blimp/net/connection_handler.h" | |
19 #include "blimp/net/message_port.h" | |
20 #include "net/base/net_errors.h" | |
21 | |
22 namespace blimp { | |
23 | |
24 ClientConnectionManager::ClientConnectionManager( | |
25 ConnectionHandler* connection_handler) | |
26 : connection_handler_(connection_handler), weak_factory_(this) { | |
27 DCHECK(connection_handler_); | |
28 } | |
29 | |
30 ClientConnectionManager::~ClientConnectionManager() {} | |
31 | |
32 void ClientConnectionManager::AddTransport( | |
33 std::unique_ptr<BlimpTransport> transport) { | |
34 DCHECK(transport); | |
35 transports_.push_back(std::move(transport)); | |
36 } | |
37 | |
38 void ClientConnectionManager::Connect() { | |
39 // A |transport| added first is used first. When it fails to connect, | |
40 // the next transport is used. | |
41 DCHECK(!transports_.empty()); | |
42 Connect(0); | |
43 } | |
44 | |
45 void ClientConnectionManager::Connect(int transport_index) { | |
46 DVLOG(1) << "ClientConnectionManager::Connect(" << transport_index << ")"; | |
47 if (static_cast<size_t>(transport_index) < transports_.size()) { | |
48 transports_[transport_index]->Connect( | |
49 base::Bind(&ClientConnectionManager::OnConnectResult, | |
50 base::Unretained(this), transport_index)); | |
51 } else { | |
52 // TODO(haibinlu): add an error reporting path out for this. | |
53 LOG(WARNING) << "All transports failed to connect"; | |
54 } | |
55 } | |
56 | |
57 void ClientConnectionManager::OnConnectResult(int transport_index, int result) { | |
58 DCHECK_NE(result, net::ERR_IO_PENDING); | |
59 const auto& transport = transports_[transport_index]; | |
60 DVLOG(1) << "OnConnectResult; result = " << result; | |
61 if (result == net::OK) { | |
62 std::unique_ptr<BlimpConnection> connection = transport->MakeConnection(); | |
63 connection->AddConnectionErrorObserver(this); | |
64 SendAuthenticationMessage(std::move(connection)); | |
65 } else { | |
66 DVLOG(1) << "Transport " << transport->GetName() | |
67 << " failed to connect:" << net::ErrorToString(result); | |
68 Connect(transport_index + 1); | |
69 } | |
70 } | |
71 | |
72 void ClientConnectionManager::SendAuthenticationMessage( | |
73 std::unique_ptr<BlimpConnection> connection) { | |
74 DVLOG(1) << "Sending authentication message."; | |
75 connection->GetOutgoingMessageProcessor()->ProcessMessage( | |
76 CreateStartConnectionMessage(client_auth_token_, kProtocolVersion), | |
77 base::Bind(&ClientConnectionManager::OnAuthenticationMessageSent, | |
78 weak_factory_.GetWeakPtr(), | |
79 base::Passed(std::move(connection)))); | |
80 } | |
81 | |
82 void ClientConnectionManager::OnAuthenticationMessageSent( | |
83 std::unique_ptr<BlimpConnection> connection, | |
84 int result) { | |
85 DVLOG(1) << "AuthenticationMessageSent, result=" << result; | |
86 if (result != net::OK) { | |
87 // If a write error occurred, just throw away |connection|. | |
88 // We don't need to propagate the error code here because the connection | |
89 // will already have done so via the ErrorObserver object. | |
90 return; | |
91 } | |
92 connection_handler_->HandleConnection(std::move(connection)); | |
93 } | |
94 | |
95 void ClientConnectionManager::OnConnectionError(int error) { | |
96 // TODO(kmarshall): implement reconnection logic. | |
97 VLOG(0) << "Connection dropped, error=" << error; | |
98 } | |
99 | |
100 } // namespace blimp | |
OLD | NEW |