summaryrefslogtreecommitdiffstats
path: root/blimp/net/blimp_message_output_buffer.h
blob: 0a7bddf3ae7e580ca441480f59e7015661bf11ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BLIMP_NET_BLIMP_MESSAGE_OUTPUT_BUFFER_H_
#define BLIMP_NET_BLIMP_MESSAGE_OUTPUT_BUFFER_H_

#include <list>
#include <queue>
#include <utility>

#include "base/macros.h"
#include "blimp/net/blimp_message_checkpoint_observer.h"
#include "blimp/net/blimp_message_processor.h"
#include "blimp/net/blimp_net_export.h"
#include "net/base/completion_callback.h"

namespace blimp {

class BlimpConnection;

// Provides a FIFO buffer for reliable, ordered message delivery.
// Messages are retained for redelivery until they are acknowledged by the
// receiving end (via BlimpMessageCheckpointObserver).
// Messages can be paired with callbacks that are invoked on successful
// message acknowledgement.
// (Redelivery will be used in a future CL to implement Fast Recovery
// of dropped connections.)
class BLIMP_NET_EXPORT BlimpMessageOutputBuffer
    : public BlimpMessageProcessor,
      public BlimpMessageCheckpointObserver {
 public:
  explicit BlimpMessageOutputBuffer(int max_buffer_size_bytes);
  ~BlimpMessageOutputBuffer() override;

  // Sets the processor that will be used for writing buffered messages.
  void SetOutputProcessor(BlimpMessageProcessor* processor);

  // Marks all messages in buffer for retransmission.
  void RetransmitBufferedMessages();

  // BlimpMessageProcessor implementation.
  // |callback|, if set, will be called once the remote end has acknowledged the
  // receipt of |message|.
  void ProcessMessage(scoped_ptr<BlimpMessage> message,
                      const net::CompletionCallback& callback) override;

  // MessageCheckpointObserver implementation.
  void OnMessageCheckpoint(int64 message_id) override;

  int GetBufferByteSizeForTest() const;
  int GetUnacknowledgedMessageCountForTest() const;

 private:
  struct BufferEntry {
    BufferEntry(scoped_ptr<BlimpMessage> message,
                net::CompletionCallback callback);
    ~BufferEntry();

    const scoped_ptr<BlimpMessage> message;
    const net::CompletionCallback callback;
  };

  typedef std::list<scoped_ptr<BufferEntry>> MessageBuffer;

  // Writes the next message in the buffer if an output processor is attached
  // and the buffer contains a message.
  void WriteNextMessageIfReady();

  // Receives the completion status of a write operation.
  void OnWriteComplete(int result);

  BlimpMessageProcessor* output_processor_ = nullptr;
  net::CancelableCompletionCallback write_complete_cb_;

  // Maximum serialized footprint of buffered messages.
  int max_buffer_size_bytes_;

  // Serialized footprint of the messages contained in the write and ack
  // buffers.
  int current_buffer_size_bytes_ = 0;

  // The ID used by the last outgoing message.
  int64 prev_message_id_ = 0;

  // List of unsent messages.
  MessageBuffer write_buffer_;

  // List of messages that are sent and awaiting acknoweldgement.
  // The messages in |ack_buffer_| are contiguous with the messages in
  // |write_buffer_|.
  MessageBuffer ack_buffer_;

  DISALLOW_COPY_AND_ASSIGN(BlimpMessageOutputBuffer);
};

}  // namespace blimp

#endif  // BLIMP_NET_BLIMP_MESSAGE_OUTPUT_BUFFER_H_