summaryrefslogtreecommitdiffstats
path: root/net/curvecp/received_block_list.h
blob: 2cf111ee61f70961743cc99b867f6790fc5945d9 (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
// 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_RECEIVED_BLOCK_LIST_H_
#define NET_CURVECP_RECEIVED_BLOCK_LIST_H_

#include <deque>

#include "base/memory/ref_counted.h"

namespace net {

class DrainableIOBuffer;
class IOBuffer;

// A ReceivedBlockList manages everything needed to track a set of of incoming
// blocks.  It can coalesce continuous blocks and locate holes in the received
// data.
// Note:  this list does not expect to receive overlapping blocks such as:
//        Block 1: bytes 10-20
//        Block 2: bytes 15-25
class ReceivedBlockList {
 public:
  ReceivedBlockList();
  ~ReceivedBlockList();

  // Returns the total number of bytes received in order.
  int bytes_received() const { return bytes_received_; }

  // Returns the number of currently available bytes to read.
  int bytes_available() const;

  // Adds an incoming block into the list of received blocks.
  void AddBlock(int64 position, IOBuffer* data, int length);

  // Reads data from the buffer starting at |current_position()|.
  // |buffer| is the buffer to fill.
  // |size| is the maximum number of bytes to be filled into |buffer|.
  // Returns the number of bytes read.
  int ReadBytes(IOBuffer* buffer, int size);

  // Gets the current position of the received data in the stream.
  // This is for testing only.
  int current_position() const { return current_position_; }

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

 private:
  typedef struct {
    int64 position;                         // Position of this block.
    scoped_refptr<DrainableIOBuffer> data;  // The data.
  } Block;
  typedef std::deque<Block> BlockList;

  // Recomputes |bytes_received_|.
  void ComputeBytesReceived();

  int64 current_position_;
  int64 bytes_received_;
  BlockList list_;

  DISALLOW_COPY_AND_ASSIGN(ReceivedBlockList);
};

}  // namespace net

#endif  // NET_CURVECP_RECEIVED_BLOCK_LIST_H_