summaryrefslogtreecommitdiff
path: root/chromium/net/third_party/quiche/src/quic/qbone/qbone_control_stream.cc
blob: 0b453a7a954dd7591de74d5034c713a933d2e43b (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
// Copyright (c) 2019 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.

#include "net/third_party/quiche/src/quic/qbone/qbone_control_stream.h"

#include "net/third_party/quiche/src/quic/core/quic_session.h"
#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
#include "net/third_party/quiche/src/quic/qbone/qbone_constants.h"
#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"

namespace quic {

namespace {
static constexpr size_t kRequestSizeBytes = sizeof(uint16_t);
}  // namespace

QboneControlStreamBase::QboneControlStreamBase(QuicSession* session)
    : QuicStream(
          QboneConstants::GetControlStreamId(session->transport_version()),
          session,
          /*is_static=*/true,
          BIDIRECTIONAL),
      pending_message_size_(0) {}

void QboneControlStreamBase::OnDataAvailable() {
  sequencer()->Read(&buffer_);
  while (true) {
    if (pending_message_size_ == 0) {
      // Start of a message.
      if (buffer_.size() < kRequestSizeBytes) {
        return;
      }
      memcpy(&pending_message_size_, buffer_.data(), kRequestSizeBytes);
      buffer_.erase(0, kRequestSizeBytes);
    }
    // Continuation of a message.
    if (buffer_.size() < pending_message_size_) {
      return;
    }
    std::string tmp = buffer_.substr(0, pending_message_size_);
    buffer_.erase(0, pending_message_size_);
    pending_message_size_ = 0;
    OnMessage(tmp);
  }
}

bool QboneControlStreamBase::SendMessage(const proto2::Message& proto) {
  std::string tmp;
  if (!proto.SerializeToString(&tmp)) {
    QUIC_BUG << "Failed to serialize QboneControlRequest";
    return false;
  }
  if (tmp.size() > kuint16max) {
    QUIC_BUG << "QboneControlRequest too large: " << tmp.size() << " > "
             << kuint16max;
    return false;
  }
  uint16_t size = tmp.size();
  char size_str[kRequestSizeBytes];
  memcpy(size_str, &size, kRequestSizeBytes);
  WriteOrBufferData(quiche::QuicheStringPiece(size_str, kRequestSizeBytes),
                    false, nullptr);
  WriteOrBufferData(tmp, false, nullptr);
  return true;
}

void QboneControlStreamBase::OnStreamReset(
    const QuicRstStreamFrame& /*frame*/) {
  stream_delegate()->OnStreamError(QUIC_INVALID_STREAM_ID,
                                   "Attempt to reset control stream");
}

}  // namespace quic