aboutsummaryrefslogtreecommitdiff
path: root/osp/impl/quic/quic_connection.h
blob: e00e25a044dcaeb2b7e86d7cce25b7a97bf06ad7 (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
// Copyright 2018 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 OSP_IMPL_QUIC_QUIC_CONNECTION_H_
#define OSP_IMPL_QUIC_QUIC_CONNECTION_H_

#include <memory>
#include <vector>

#include "platform/api/udp_socket.h"

namespace openscreen {
namespace osp {

class QuicStream {
 public:
  class Delegate {
   public:
    virtual ~Delegate() = default;

    virtual void OnReceived(QuicStream* stream,
                            const char* data,
                            size_t data_size) = 0;
    virtual void OnClose(uint64_t stream_id) = 0;
  };

  QuicStream(Delegate* delegate, uint64_t id) : delegate_(delegate), id_(id) {}
  virtual ~QuicStream() = default;

  uint64_t id() const { return id_; }
  virtual void Write(const uint8_t* data, size_t data_size) = 0;
  virtual void CloseWriteEnd() = 0;

 protected:
  Delegate* const delegate_;
  uint64_t id_;
};

class QuicConnection : public UdpSocket::Client {
 public:
  class Delegate {
   public:
    virtual ~Delegate() = default;

    // Called when the QUIC handshake has successfully completed.
    virtual void OnCryptoHandshakeComplete(uint64_t connection_id) = 0;

    // Called when a new stream on this connection is initiated by the other
    // endpoint.  |stream| will use a delegate returned by NextStreamDelegate.
    virtual void OnIncomingStream(uint64_t connection_id,
                                  std::unique_ptr<QuicStream> stream) = 0;

    // Called when the QUIC connection was closed.  The QuicConnection should
    // not be destroyed immediately, because the QUIC implementation will still
    // reference it briefly.  Instead, it should be destroyed during the next
    // event loop.
    // TODO(btolsch): Hopefully this can be changed with future QUIC
    // implementations.
    virtual void OnConnectionClosed(uint64_t connection_id) = 0;

    // This is used to get a QuicStream::Delegate for an incoming stream, which
    // will be returned via OnIncomingStream immediately after this call.
    virtual QuicStream::Delegate* NextStreamDelegate(uint64_t connection_id,
                                                     uint64_t stream_id) = 0;
  };

  explicit QuicConnection(Delegate* delegate) : delegate_(delegate) {}
  virtual ~QuicConnection() = default;

  virtual std::unique_ptr<QuicStream> MakeOutgoingStream(
      QuicStream::Delegate* delegate) = 0;
  virtual void Close() = 0;

 protected:
  Delegate* const delegate_;
};

}  // namespace osp
}  // namespace openscreen

#endif  // OSP_IMPL_QUIC_QUIC_CONNECTION_H_