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/blimp_message_checkpointer.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "blimp/common/create_blimp_message.h" | |
9 #include "blimp/common/proto/blimp_message.pb.h" | |
10 #include "blimp/common/proto/protocol_control.pb.h" | |
11 #include "blimp/net/blimp_message_checkpoint_observer.h" | |
12 #include "net/base/net_errors.h" | |
13 | |
14 namespace blimp { | |
15 | |
16 namespace { | |
17 const int kDeferCheckpointAckSeconds = 1; | |
18 } | |
19 | |
20 BlimpMessageCheckpointer::BlimpMessageCheckpointer( | |
21 BlimpMessageProcessor* incoming_processor, | |
22 BlimpMessageProcessor* outgoing_processor, | |
23 BlimpMessageCheckpointObserver* checkpoint_observer) | |
24 : incoming_processor_(incoming_processor), | |
25 outgoing_processor_(outgoing_processor), | |
26 checkpoint_observer_(checkpoint_observer), | |
27 weak_factory_(this) { | |
28 DCHECK(incoming_processor_); | |
29 DCHECK(outgoing_processor_); | |
30 DCHECK(checkpoint_observer_); | |
31 } | |
32 | |
33 BlimpMessageCheckpointer::~BlimpMessageCheckpointer() {} | |
34 | |
35 void BlimpMessageCheckpointer::ProcessMessage( | |
36 std::unique_ptr<BlimpMessage> message, | |
37 const net::CompletionCallback& callback) { | |
38 if (message->has_protocol_control()) { | |
39 if (message->protocol_control().has_checkpoint_ack() && | |
40 message->protocol_control().checkpoint_ack().has_checkpoint_id()) { | |
41 checkpoint_observer_->OnMessageCheckpoint( | |
42 message->protocol_control().checkpoint_ack().checkpoint_id()); | |
43 callback.Run(net::OK); | |
44 } else { | |
45 DLOG(WARNING) << "Invalid checkpoint ACK. Dropping connection."; | |
46 callback.Run(net::ERR_FAILED); | |
47 } | |
48 | |
49 return; | |
50 } | |
51 | |
52 // TODO(wez): Provide independent checkpoints for each message->type()? | |
53 DCHECK(message->has_message_id()); | |
54 | |
55 // Store the message-Id to include in the checkpoint ACK. | |
56 checkpoint_id_ = message->message_id(); | |
57 | |
58 // Kick the timer, if not running, to ACK after a short delay. | |
59 if (!defer_timer_.IsRunning()) { | |
60 defer_timer_.Start(FROM_HERE, | |
61 base::TimeDelta::FromSeconds(kDeferCheckpointAckSeconds), | |
62 this, &BlimpMessageCheckpointer::SendCheckpointAck); | |
63 } | |
64 | |
65 // Pass the message along for actual processing. | |
66 incoming_processor_->ProcessMessage( | |
67 std::move(message), | |
68 base::Bind(&BlimpMessageCheckpointer::InvokeCompletionCallback, | |
69 weak_factory_.GetWeakPtr(), callback)); | |
70 } | |
71 | |
72 void BlimpMessageCheckpointer::InvokeCompletionCallback( | |
73 const net::CompletionCallback& callback, | |
74 int result) { | |
75 callback.Run(result); | |
76 } | |
77 | |
78 void BlimpMessageCheckpointer::SendCheckpointAck() { | |
79 outgoing_processor_->ProcessMessage( | |
80 CreateCheckpointAckMessage(checkpoint_id_), net::CompletionCallback()); | |
81 } | |
82 | |
83 } // namespace blimp | |
OLD | NEW |