• 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_IMPL_H_
6 #define OSP_IMPL_QUIC_QUIC_CONNECTION_IMPL_H_
7 
8 #include <list>
9 #include <memory>
10 
11 #include "osp/impl/quic/quic_connection.h"
12 #include "platform/api/udp_socket.h"
13 #include "platform/base/ip_address.h"
14 #include "third_party/chromium_quic/src/base/callback.h"
15 #include "third_party/chromium_quic/src/base/location.h"
16 #include "third_party/chromium_quic/src/base/task_runner.h"
17 #include "third_party/chromium_quic/src/base/time/time.h"
18 #include "third_party/chromium_quic/src/net/third_party/quic/quartc/quartc_packet_writer.h"
19 #include "third_party/chromium_quic/src/net/third_party/quic/quartc/quartc_session.h"
20 #include "third_party/chromium_quic/src/net/third_party/quic/quartc/quartc_stream.h"
21 
22 namespace openscreen {
23 namespace osp {
24 
25 class QuicConnectionFactoryImpl;
26 
27 class UdpTransport final : public ::quic::QuartcPacketTransport {
28  public:
29   UdpTransport(UdpSocket* socket, const IPEndpoint& destination);
30   UdpTransport(UdpTransport&&) noexcept;
31   ~UdpTransport() override;
32 
33   UdpTransport& operator=(UdpTransport&&) noexcept;
34 
35   // ::quic::QuartcPacketTransport overrides.
36   int Write(const char* buffer,
37             size_t buffer_length,
38             const PacketInfo& info) override;
39 
socket()40   UdpSocket* socket() const { return socket_; }
41 
42  private:
43   UdpSocket* socket_;
44   IPEndpoint destination_;
45 };
46 
47 class QuicStreamImpl final : public QuicStream,
48                              public ::quic::QuartcStream::Delegate {
49  public:
50   QuicStreamImpl(QuicStream::Delegate* delegate, ::quic::QuartcStream* stream);
51   ~QuicStreamImpl() override;
52 
53   // QuicStream overrides.
54   void Write(const uint8_t* data, size_t size) override;
55   void CloseWriteEnd() override;
56 
57   // ::quic::QuartcStream::Delegate overrides.
58   void OnReceived(::quic::QuartcStream* stream,
59                   const char* data,
60                   size_t data_size) override;
61   void OnClose(::quic::QuartcStream* stream) override;
62   void OnBufferChanged(::quic::QuartcStream* stream) override;
63 
64  private:
65   ::quic::QuartcStream* const stream_;
66 };
67 
68 class QuicConnectionImpl final : public QuicConnection,
69                                  public ::quic::QuartcSession::Delegate {
70  public:
71   QuicConnectionImpl(QuicConnectionFactoryImpl* parent_factory,
72                      QuicConnection::Delegate* delegate,
73                      std::unique_ptr<UdpTransport> udp_transport,
74                      std::unique_ptr<::quic::QuartcSession> session);
75 
76   ~QuicConnectionImpl() override;
77 
78   // UdpSocket::Client overrides.
79   void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> data) override;
80   void OnError(UdpSocket* socket, Error error) override;
81   void OnSendError(UdpSocket* socket, Error error) override;
82 
83   // QuicConnection overrides.
84   std::unique_ptr<QuicStream> MakeOutgoingStream(
85       QuicStream::Delegate* delegate) override;
86   void Close() override;
87 
88   // ::quic::QuartcSession::Delegate overrides.
89   void OnCryptoHandshakeComplete() override;
90   void OnIncomingStream(::quic::QuartcStream* stream) override;
91   void OnConnectionClosed(::quic::QuicErrorCode error_code,
92                           const ::quic::QuicString& error_details,
93                           ::quic::ConnectionCloseSource source) override;
94 
95  private:
96   QuicConnectionFactoryImpl* const parent_factory_;
97   const std::unique_ptr<::quic::QuartcSession> session_;
98   const std::unique_ptr<UdpTransport> udp_transport_;
99   std::vector<QuicStream*> streams_;
100 };
101 
102 }  // namespace osp
103 }  // namespace openscreen
104 
105 #endif  // OSP_IMPL_QUIC_QUIC_CONNECTION_IMPL_H_
106