1 // Copyright (c) 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 THIRD_PARTY_CHROMIUM_QUIC_DEMO_DELEGATES_H_ 6 #define THIRD_PARTY_CHROMIUM_QUIC_DEMO_DELEGATES_H_ 7 8 #include <netinet/in.h> 9 #include <netinet/ip.h> 10 #include <sys/socket.h> 11 #include <sys/types.h> 12 13 #include <memory> 14 #include <vector> 15 16 #include "base/task/post_task.h" 17 #include "net/third_party/quic/quartc/quartc_packet_writer.h" 18 #include "net/third_party/quic/quartc/quartc_session.h" 19 #include "net/third_party/quic/quartc/quartc_stream.h" 20 21 class UdpTransport final : public quic::QuartcPacketTransport { 22 public: 23 UdpTransport(int fd, struct sockaddr_in peer_address); 24 ~UdpTransport() override; 25 26 int Write(const char* buffer, 27 size_t buf_len, 28 const PacketInfo& info) override; 29 30 private: 31 int fd_; 32 struct sockaddr_in peer_address_; 33 }; 34 35 class StreamDelegate final : public quic::QuartcStream::Delegate { 36 public: 37 StreamDelegate(); 38 ~StreamDelegate() override; 39 40 void OnReceived(quic::QuartcStream* stream, 41 const char* data, 42 size_t size) override; 43 void OnClose(quic::QuartcStream* stream) override; 44 void OnBufferChanged(quic::QuartcStream* stream) override; 45 closed()46 bool closed() const { return closed_; } 47 48 private: 49 bool closed_ = false; 50 }; 51 52 class SessionDelegate final : public quic::QuartcSession::Delegate { 53 public: 54 SessionDelegate(); 55 ~SessionDelegate() override; 56 57 void OnCryptoHandshakeComplete() override; 58 void OnIncomingStream(quic::QuartcStream* stream) override; 59 void OnConnectionClosed(quic::QuicErrorCode error_code, 60 const quic::QuicString& error_details, 61 quic::ConnectionCloseSource source) override; 62 last_stream_closed()63 bool last_stream_closed() const { 64 return stream_delegates_.empty() ? false 65 : stream_delegates_.back()->closed(); 66 } connection_closed()67 bool connection_closed() const { return connection_closed_; } 68 69 private: 70 std::vector<std::unique_ptr<StreamDelegate>> stream_delegates_; 71 bool connection_closed_ = false; 72 }; 73 74 struct FakeTask { 75 base::Location whence; 76 base::OnceClosure task; 77 base::TimeDelta delay; 78 }; 79 80 class FakeTaskRunner : public base::TaskRunner { 81 public: 82 explicit FakeTaskRunner(std::vector<FakeTask>* tasks); 83 84 bool PostDelayedTask(const base::Location& whence, 85 base::OnceClosure task, 86 base::TimeDelta delay) override; 87 88 bool RunsTasksInCurrentSequence() const override; 89 90 private: 91 ~FakeTaskRunner() override; 92 93 std::vector<FakeTask>* tasks_; 94 }; 95 96 #endif // THIRD_PARTY_CHROMIUM_QUIC_DEMO_DELEGATES_H_ 97