summaryrefslogtreecommitdiffstats
path: root/net/curvecp/sent_block_list.h
blob: c246204051622ce90017ae4f0bb845eb58e8faa6 (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
// Copyright (c) 2011 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 NET_CURVECP_SENT_BLOCK_LIST_H_
#define NET_CURVECP_SENT_BLOCK_LIST_H_

#include <vector>

#include "base/memory/ref_counted.h"
#include "base/time.h"

namespace net {

class IOBufferWithSize;

// A SentBlockList manages everything needed to track a set of of blocks
// which have been sent.  It can assign new ids to new blocks, find pending
// blocks by id, or discard blocks after they have been acknowledged by the
// remote.
class SentBlockList {
 public:
  SentBlockList();
  ~SentBlockList();

  // Creates a new block to be tracked.
  // On success, returns the unique position of this buffer within the stream,
  // which can be used to lookup this block later.
  // Returns -1 if the blocklist is already full.
  int64 CreateBlock(IOBufferWithSize* buffer);

  // Tracks that a block has been sent.
  // |position| is the identifier of the block being sent.
  // |message_id| is the id of the message sent containing the block.
  // Returns false if no block for |position| exists.
  bool MarkBlockSent(int64 position, int64 message_id);

  // Acknowledges ranges from |begin_range| to |end_range|.
  // Partial acknowledgements (e.g. where the ack range spans a partial
  // block) are not supported.
  // Removes blocks covered by these ranges from tracking.
  void AcknowledgeBlocks(int64 begin_range, int64 end_range);

  // Returns a new, unique message id.
  int64 GetNewMessageId();

  // Find a block based on its |position|.
  // Returns the block, if found, or NULL otherwise.
  IOBufferWithSize* FindBlockByPosition(int64 position) const;

  // Find the last send time of a block, based the id of the last
  // message to send it.
  base::TimeTicks FindLastSendTime(int64 last_message_id) const;

  // Returns the position of the oldest sent block.
  int64 FindPositionOfOldestSentBlock() const;

  // Returns the number of blocks in the list.
  size_t size() const { return list_.size(); }

  // Returns true if the list is full and cannot take new blocks.
  bool is_full() const;

 private:
  // An UnackedBlock is a block of application data which has been sent in a
  // message, but not acknowledged by the other side yet.  We track this
  // because we may need to retransmit.
  typedef struct {
    int64 position;                  // Offset of this block in the stream.
    int64 length;                    // Number of bytes in this block.
    int64 transmissions;             // Count of transmissions of this block.
    int64 last_message_id;           // ID of last message sending this block.
    base::TimeTicks last_sent_time;  // Time of last message sending this block.
    scoped_refptr<IOBufferWithSize> data;
  } Block;

  typedef std::vector<Block> BlockList;

  // Finds a block by position and returns it index within |list_|.
  int FindBlock(int64 position) const;

  // Debugging: Writes the entire blocklist to the log.
  void LogBlockList() const;

  int64 current_message_id_;    // The current message id.
  int64 send_sequence_number_;  // The sending sequence number.
  BlockList list_;

  DISALLOW_COPY_AND_ASSIGN(SentBlockList);
};

}  // namespace net

#endif  // NET_CURVECP_SENT_BLOCK_LIST_H_