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 #ifndef BLIMP_NET_BLIMP_MESSAGE_OUTPUT_BUFFER_H_ | |
6 #define BLIMP_NET_BLIMP_MESSAGE_OUTPUT_BUFFER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <list> | |
11 #include <queue> | |
12 #include <utility> | |
13 | |
14 #include "base/macros.h" | |
15 #include "blimp/net/blimp_message_checkpoint_observer.h" | |
16 #include "blimp/net/blimp_message_processor.h" | |
17 #include "blimp/net/blimp_net_export.h" | |
18 #include "net/base/completion_callback.h" | |
19 | |
20 namespace blimp { | |
21 | |
22 // Provides a FIFO buffer for reliable, ordered message delivery. | |
23 // Messages are retained for redelivery until they are acknowledged by the | |
24 // receiving end (via BlimpMessageCheckpointObserver). | |
25 // Messages can be paired with callbacks that are invoked on successful | |
26 // message acknowledgment. | |
27 // (Redelivery will be used in a future CL to implement Fast Recovery | |
28 // of dropped connections.) | |
29 // BlimpMessageOutputBuffer is created on the UI thread, and then used and | |
30 // destroyed on the IO thread. | |
31 class BLIMP_NET_EXPORT BlimpMessageOutputBuffer | |
32 : public BlimpMessageProcessor, | |
33 public BlimpMessageCheckpointObserver { | |
34 public: | |
35 explicit BlimpMessageOutputBuffer(int max_buffer_size_bytes); | |
36 ~BlimpMessageOutputBuffer() override; | |
37 | |
38 // Sets the processor that will be used for writing buffered messages. | |
39 void SetOutputProcessor(BlimpMessageProcessor* processor); | |
40 | |
41 // Marks all messages in buffer for retransmission. | |
42 void RetransmitBufferedMessages(); | |
43 | |
44 // BlimpMessageProcessor implementation. | |
45 // |callback|, if set, will be called once the remote end has acknowledged the | |
46 // receipt of |message|. | |
47 void ProcessMessage(std::unique_ptr<BlimpMessage> message, | |
48 const net::CompletionCallback& callback) override; | |
49 | |
50 // MessageCheckpointObserver implementation. | |
51 void OnMessageCheckpoint(int64_t message_id) override; | |
52 | |
53 int GetBufferByteSizeForTest() const; | |
54 int GetUnacknowledgedMessageCountForTest() const; | |
55 | |
56 private: | |
57 struct BufferEntry { | |
58 BufferEntry(std::unique_ptr<BlimpMessage> message, | |
59 net::CompletionCallback callback); | |
60 ~BufferEntry(); | |
61 | |
62 const std::unique_ptr<BlimpMessage> message; | |
63 const net::CompletionCallback callback; | |
64 }; | |
65 | |
66 typedef std::list<std::unique_ptr<BufferEntry>> MessageBuffer; | |
67 | |
68 // Writes the next message in the buffer if an output processor is attached | |
69 // and the buffer contains a message. | |
70 void WriteNextMessageIfReady(); | |
71 | |
72 // Receives the completion status of a write operation. | |
73 void OnWriteComplete(int result); | |
74 | |
75 BlimpMessageProcessor* output_processor_ = nullptr; | |
76 net::CancelableCompletionCallback write_complete_cb_; | |
77 | |
78 // Maximum serialized footprint of buffered messages. | |
79 int max_buffer_size_bytes_; | |
80 | |
81 // Serialized footprint of the messages contained in the write and ack | |
82 // buffers. | |
83 int current_buffer_size_bytes_ = 0; | |
84 | |
85 // The ID used by the last outgoing message. | |
86 int64_t prev_message_id_ = 0; | |
87 | |
88 // List of unsent messages. | |
89 MessageBuffer write_buffer_; | |
90 | |
91 // List of messages that are sent and awaiting acknowledgment. | |
92 // The messages in |ack_buffer_| are contiguous with the messages in | |
93 // |write_buffer_|. | |
94 MessageBuffer ack_buffer_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(BlimpMessageOutputBuffer); | |
97 }; | |
98 | |
99 } // namespace blimp | |
100 | |
101 #endif // BLIMP_NET_BLIMP_MESSAGE_OUTPUT_BUFFER_H_ | |
OLD | NEW |