xref: /aosp_15_r20/external/openscreen/osp/impl/quic/quic_connection.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2018 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 OSP_IMPL_QUIC_QUIC_CONNECTION_H_
6 #define OSP_IMPL_QUIC_QUIC_CONNECTION_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "platform/api/udp_socket.h"
12 
13 namespace openscreen {
14 namespace osp {
15 
16 class QuicStream {
17  public:
18   class Delegate {
19    public:
20 
21     virtual void OnReceived(QuicStream* stream,
22                             const char* data,
23                             size_t data_size) = 0;
24     virtual void OnClose(uint64_t stream_id) = 0;
25 
26    protected:
27     virtual ~Delegate() = default;
28   };
29 
QuicStream(Delegate * delegate,uint64_t id)30   QuicStream(Delegate* delegate, uint64_t id) : delegate_(delegate), id_(id) {}
31   virtual ~QuicStream() = default;
32 
id()33   uint64_t id() const { return id_; }
34   virtual void Write(const uint8_t* data, size_t data_size) = 0;
35   virtual void CloseWriteEnd() = 0;
36 
37  protected:
38   Delegate* const delegate_;
39   uint64_t id_;
40 };
41 
42 class QuicConnection : public UdpSocket::Client {
43  public:
44   class Delegate {
45    public:
46 
47     // Called when the QUIC handshake has successfully completed.
48     virtual void OnCryptoHandshakeComplete(uint64_t connection_id) = 0;
49 
50     // Called when a new stream on this connection is initiated by the other
51     // endpoint.  |stream| will use a delegate returned by NextStreamDelegate.
52     virtual void OnIncomingStream(uint64_t connection_id,
53                                   std::unique_ptr<QuicStream> stream) = 0;
54 
55     // Called when the QUIC connection was closed.  The QuicConnection should
56     // not be destroyed immediately, because the QUIC implementation will still
57     // reference it briefly.  Instead, it should be destroyed during the next
58     // event loop.
59     // TODO(btolsch): Hopefully this can be changed with future QUIC
60     // implementations.
61     virtual void OnConnectionClosed(uint64_t connection_id) = 0;
62 
63     // This is used to get a QuicStream::Delegate for an incoming stream, which
64     // will be returned via OnIncomingStream immediately after this call.
65     virtual QuicStream::Delegate* NextStreamDelegate(uint64_t connection_id,
66                                                      uint64_t stream_id) = 0;
67 
68    protected:
69     virtual ~Delegate() = default;
70   };
71 
QuicConnection(Delegate * delegate)72   explicit QuicConnection(Delegate* delegate) : delegate_(delegate) {}
73   virtual ~QuicConnection() = default;
74 
75   virtual std::unique_ptr<QuicStream> MakeOutgoingStream(
76       QuicStream::Delegate* delegate) = 0;
77   virtual void Close() = 0;
78 
79  protected:
80   Delegate* const delegate_;
81 };
82 
83 }  // namespace osp
84 }  // namespace openscreen
85 
86 #endif  // OSP_IMPL_QUIC_QUIC_CONNECTION_H_
87