summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/quic_channel.h
blob: 64c346a93fd66c6d7b0c07fbac804cc9f6337afb (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
100
101
102
103
104
// 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 REMOTING_PROTOCOL_QUIC_CHANNEL_H_
#define REMOTING_PROTOCOL_QUIC_CHANNEL_H_

#include "net/quic/p2p/quic_p2p_stream.h"
#include "remoting/base/compound_buffer.h"
#include "remoting/protocol/p2p_stream_socket.h"

namespace net {
class DrainableIOBuffer;
}  // namespace net

namespace remoting {
namespace protocol {

// QuicChannel implements P2PStreamSocket interface for a QuicP2PStream.
class QuicChannel : public net::QuicP2PStream::Delegate,
                    public P2PStreamSocket {
 public:
  QuicChannel(net::QuicP2PStream* stream,
              const base::Closure& on_destroyed_callback);
  ~QuicChannel() override;

  const std::string& name() { return name_; }

  // P2PStreamSocket interface.
  int Read(const scoped_refptr<net::IOBuffer>& buffer,
           int buffer_len,
           const net::CompletionCallback& callback) override;
  int Write(const scoped_refptr<net::IOBuffer>& buffer,
            int buffer_len,
            const net::CompletionCallback& callback) override;

 protected:
  void SetName(const std::string& name);

  // Owned by QuicSession.
  net::QuicP2PStream* stream_;

 private:
  // net::QuicP2PStream::Delegate interface.
  void OnDataReceived(const char* data, int length) override;
  void OnClose(net::QuicErrorCode error) override;

  base::Closure on_destroyed_callback_;

  std::string name_;

  CompoundBuffer data_received_;

  net::CompletionCallback read_callback_;
  scoped_refptr<net::IOBuffer> read_buffer_;
  int read_buffer_size_ = 0;

  int error_ = 0;

  DISALLOW_COPY_AND_ASSIGN(QuicChannel);
};

// Client side of a channel. Sends the |name| specified in the constructor to
// the peer.
class QuicClientChannel : public QuicChannel {
 public:
  QuicClientChannel(net::QuicP2PStream* stream,
                    const base::Closure& on_destroyed_callback,
                    const std::string& name);
  ~QuicClientChannel() override;

 private:
  DISALLOW_COPY_AND_ASSIGN(QuicClientChannel);
};

// Host side of a channel. Receives name from the peer after ReceiveName is
// called. Read() can be called only after the name is received.
class QuicServerChannel : public QuicChannel {
 public:
  QuicServerChannel(net::QuicP2PStream* stream,
                    const base::Closure& on_destroyed_callback);
  ~QuicServerChannel() override;

  // Must be called after the constructor to receive channel name.
  // |name_received_callback| must use QuicChannel::name() to get the name.
  // Empty name() indicates failure to receive it.
  void ReceiveName(const base::Closure& name_received_callback);

 private:
  void OnNameSizeReadResult(int result);
  void ReadNameLoop(int result);
  void OnNameReadResult(int result);

  base::Closure name_received_callback_;
  uint8_t name_length_ = 0;
  scoped_refptr<net::DrainableIOBuffer> name_read_buffer_;

  DISALLOW_COPY_AND_ASSIGN(QuicServerChannel);
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_QUIC_CHANNEL_H_