• 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_SERVICE_COMMON_H_
6 #define OSP_IMPL_QUIC_QUIC_SERVICE_COMMON_H_
7 
8 #include <cstdint>
9 #include <map>
10 #include <memory>
11 #include <vector>
12 
13 #include "osp/impl/quic/quic_connection.h"
14 #include "osp/public/protocol_connection.h"
15 
16 namespace openscreen {
17 namespace osp {
18 
19 class ServiceConnectionDelegate;
20 
21 class QuicProtocolConnection final : public ProtocolConnection {
22  public:
23   class Owner {
24    public:
25     virtual ~Owner() = default;
26 
27     // Called right before |connection| is destroyed (destructor runs).
28     virtual void OnConnectionDestroyed(QuicProtocolConnection* connection) = 0;
29   };
30 
31   static std::unique_ptr<QuicProtocolConnection> FromExisting(
32       Owner* owner,
33       QuicConnection* connection,
34       ServiceConnectionDelegate* delegate,
35       uint64_t endpoint_id);
36 
37   QuicProtocolConnection(Owner* owner,
38                          uint64_t endpoint_id,
39                          uint64_t connection_id);
40   ~QuicProtocolConnection() override;
41 
42   // ProtocolConnection overrides.
43   void Write(const uint8_t* data, size_t data_size) override;
44   void CloseWriteEnd() override;
45 
stream()46   QuicStream* stream() { return stream_; }
set_stream(QuicStream * stream)47   void set_stream(QuicStream* stream) { stream_ = stream; }
48 
49   void OnClose();
50 
51  private:
52   Owner* const owner_;
53   QuicStream* stream_ = nullptr;
54 };
55 
56 struct ServiceStreamPair {
57   ServiceStreamPair(std::unique_ptr<QuicStream> stream,
58                     QuicProtocolConnection* protocol_connection);
59   ~ServiceStreamPair();
60   ServiceStreamPair(ServiceStreamPair&&) noexcept;
61   ServiceStreamPair& operator=(ServiceStreamPair&&) noexcept;
62 
63   std::unique_ptr<QuicStream> stream;
64   uint64_t connection_id;
65   QuicProtocolConnection* protocol_connection;
66 };
67 
68 class ServiceConnectionDelegate final : public QuicConnection::Delegate,
69                                         public QuicStream::Delegate {
70  public:
71   class ServiceDelegate : public QuicProtocolConnection::Owner {
72    public:
73     ~ServiceDelegate() override = default;
74 
75     virtual uint64_t OnCryptoHandshakeComplete(
76         ServiceConnectionDelegate* delegate,
77         uint64_t connection_id) = 0;
78     virtual void OnIncomingStream(
79         std::unique_ptr<QuicProtocolConnection> connection) = 0;
80     virtual void OnConnectionClosed(uint64_t endpoint_id,
81                                     uint64_t connection_id) = 0;
82     virtual void OnDataReceived(uint64_t endpoint_id,
83                                 uint64_t connection_id,
84                                 const uint8_t* data,
85                                 size_t data_size) = 0;
86   };
87 
88   ServiceConnectionDelegate(ServiceDelegate* parent,
89                             const IPEndpoint& endpoint);
90   ~ServiceConnectionDelegate() override;
91 
92   void AddStreamPair(ServiceStreamPair&& stream_pair);
93   void DropProtocolConnection(QuicProtocolConnection* connection);
94 
95   // This should be called at the end of each event loop that effects this
96   // connection so streams that were closed by the other endpoint can be
97   // destroyed properly.
98   void DestroyClosedStreams();
99 
endpoint()100   const IPEndpoint& endpoint() const { return endpoint_; }
101 
has_streams()102   bool has_streams() const { return !streams_.empty(); }
103 
104   // QuicConnection::Delegate overrides.
105   void OnCryptoHandshakeComplete(uint64_t connection_id) override;
106   void OnIncomingStream(uint64_t connection_id,
107                         std::unique_ptr<QuicStream> stream) override;
108   void OnConnectionClosed(uint64_t connection_id) override;
109   QuicStream::Delegate* NextStreamDelegate(uint64_t connection_id,
110                                            uint64_t stream_id) override;
111 
112   // QuicStream::Delegate overrides.
113   void OnReceived(QuicStream* stream,
114                   const char* data,
115                   size_t data_size) override;
116   void OnClose(uint64_t stream_id) override;
117 
118  private:
119   ServiceDelegate* const parent_;
120   IPEndpoint endpoint_;
121   uint64_t endpoint_id_;
122   std::unique_ptr<QuicProtocolConnection> pending_connection_;
123   std::map<uint64_t, ServiceStreamPair> streams_;
124   std::vector<ServiceStreamPair> closed_streams_;
125 };
126 
127 struct ServiceConnectionData {
128   explicit ServiceConnectionData(
129       std::unique_ptr<QuicConnection> connection,
130       std::unique_ptr<ServiceConnectionDelegate> delegate);
131   ServiceConnectionData(ServiceConnectionData&&) noexcept;
132   ~ServiceConnectionData();
133   ServiceConnectionData& operator=(ServiceConnectionData&&) noexcept;
134 
135   std::unique_ptr<QuicConnection> connection;
136   std::unique_ptr<ServiceConnectionDelegate> delegate;
137 };
138 
139 }  // namespace osp
140 }  // namespace openscreen
141 
142 #endif  // OSP_IMPL_QUIC_QUIC_SERVICE_COMMON_H_
143