wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 1 | // Copyright (c) 2019 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 QUICHE_QUIC_QBONE_QBONE_CONTROL_STREAM_H_ |
| 6 | #define QUICHE_QUIC_QBONE_QBONE_CONTROL_STREAM_H_ |
| 7 | |
| 8 | #include "net/third_party/quiche/src/quic/core/quic_stream.h" |
dmcardle | c7679cd | 2020-01-06 08:34:22 -0800 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/qbone/qbone_control.pb.h" |
| 11 | |
| 12 | namespace quic { |
| 13 | |
| 14 | class QboneSessionBase; |
| 15 | |
| 16 | class QUIC_EXPORT_PRIVATE QboneControlStreamBase : public QuicStream { |
| 17 | public: |
| 18 | explicit QboneControlStreamBase(QuicSession* session); |
| 19 | |
| 20 | void OnDataAvailable() override; |
| 21 | |
renjietang | 546c714 | 2020-03-05 14:12:10 -0800 | [diff] [blame] | 22 | void OnStreamReset(const QuicRstStreamFrame& frame) override; |
| 23 | |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 24 | protected: |
QUICHE team | b80d7c3 | 2020-02-23 23:44:20 -0800 | [diff] [blame] | 25 | virtual void OnMessage(const std::string& data) = 0; |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 26 | bool SendMessage(const proto2::Message& proto); |
| 27 | |
| 28 | private: |
| 29 | uint16_t pending_message_size_; |
QUICHE team | b80d7c3 | 2020-02-23 23:44:20 -0800 | [diff] [blame] | 30 | std::string buffer_; |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | template <class T> |
| 34 | class QUIC_EXPORT_PRIVATE QboneControlHandler { |
| 35 | public: |
| 36 | virtual ~QboneControlHandler() { } |
| 37 | |
| 38 | virtual void OnControlRequest(const T& request) = 0; |
| 39 | virtual void OnControlError() = 0; |
| 40 | }; |
| 41 | |
| 42 | template <class Incoming, class Outgoing> |
| 43 | class QUIC_EXPORT_PRIVATE QboneControlStream : public QboneControlStreamBase { |
| 44 | public: |
| 45 | using Handler = QboneControlHandler<Incoming>; |
| 46 | |
| 47 | QboneControlStream(QuicSession* session, Handler* handler) |
| 48 | : QboneControlStreamBase(session), handler_(handler) {} |
| 49 | |
| 50 | bool SendRequest(const Outgoing& request) { return SendMessage(request); } |
| 51 | |
| 52 | protected: |
QUICHE team | b80d7c3 | 2020-02-23 23:44:20 -0800 | [diff] [blame] | 53 | void OnMessage(const std::string& data) override { |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 54 | Incoming request; |
| 55 | if (!request.ParseFromString(data)) { |
| 56 | QUIC_LOG(ERROR) << "Failed to parse incoming request"; |
| 57 | if (handler_ != nullptr) { |
| 58 | handler_->OnControlError(); |
| 59 | } |
| 60 | return; |
| 61 | } |
| 62 | if (handler_ != nullptr) { |
| 63 | handler_->OnControlRequest(request); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | Handler* handler_; |
| 69 | }; |
| 70 | |
| 71 | using QboneServerControlStream = |
| 72 | QboneControlStream<QboneServerRequest, QboneClientRequest>; |
| 73 | using QboneClientControlStream = |
| 74 | QboneControlStream<QboneClientRequest, QboneServerRequest>; |
| 75 | |
| 76 | } // namespace quic |
| 77 | |
| 78 | #endif // QUICHE_QUIC_QBONE_QBONE_CONTROL_STREAM_H_ |