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