• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "third_party/chromium_quic/demo/delegates.h"
6 
7 #include <stdio.h>
8 
UdpTransport(int fd,struct sockaddr_in peer_address)9 UdpTransport::UdpTransport(int fd, struct sockaddr_in peer_address)
10     : fd_(fd), peer_address_(peer_address) {}
11 UdpTransport::~UdpTransport() = default;
12 
Write(const char * buffer,size_t buf_len,const PacketInfo & info)13 int UdpTransport::Write(const char* buffer,
14                         size_t buf_len,
15                         const PacketInfo& info) {
16   printf("writing...\n");
17   return sendto(fd_, buffer, buf_len, 0, (struct sockaddr*)&peer_address_,
18                 sizeof(peer_address_));
19 }
20 
21 StreamDelegate::StreamDelegate() = default;
22 StreamDelegate::~StreamDelegate() = default;
23 
OnReceived(quic::QuartcStream * stream,const char * data,size_t size)24 void StreamDelegate::OnReceived(quic::QuartcStream* stream,
25                                 const char* data,
26                                 size_t size) {
27   if (!size) {
28     printf("  -- (fin)\n");
29     if (!stream->fin_sent()) {
30       stream->WriteMemSlices(quic::QuicMemSliceSpan(quic::QuicMemSliceSpanImpl(
31                                  nullptr, nullptr, 0)),
32                              true);
33     }
34   } else {
35     printf("  :: %*s\n", (int)size, data);
36   }
37 }
38 
OnClose(quic::QuartcStream * stream)39 void StreamDelegate::OnClose(quic::QuartcStream* stream) {
40   closed_ = true;
41   printf("stream closed\n");
42 }
43 
OnBufferChanged(quic::QuartcStream * stream)44 void StreamDelegate::OnBufferChanged(quic::QuartcStream* stream) {}
45 
46 SessionDelegate::SessionDelegate() = default;
47 SessionDelegate::~SessionDelegate() = default;
48 
OnCryptoHandshakeComplete()49 void SessionDelegate::OnCryptoHandshakeComplete() {
50   printf("crypto handshake complete\n");
51 }
52 
OnIncomingStream(quic::QuartcStream * stream)53 void SessionDelegate::OnIncomingStream(quic::QuartcStream* stream) {
54   if (connection_closed_) {
55     return;
56   }
57   auto next_delegate = std::make_unique<StreamDelegate>();
58   stream->SetDelegate(next_delegate.get());
59   stream_delegates_.push_back(std::move(next_delegate));
60 }
61 
OnConnectionClosed(quic::QuicErrorCode error_code,const quic::QuicString & error_details,quic::ConnectionCloseSource source)62 void SessionDelegate::OnConnectionClosed(quic::QuicErrorCode error_code,
63                                          const quic::QuicString& error_details,
64                                          quic::ConnectionCloseSource source) {
65   connection_closed_ = true;
66   stream_delegates_.clear();
67   printf("connection closed: %s\n", error_details.c_str());
68 }
69 
FakeTaskRunner(std::vector<FakeTask> * tasks)70 FakeTaskRunner::FakeTaskRunner(std::vector<FakeTask>* tasks) : tasks_(tasks) {}
71 FakeTaskRunner::~FakeTaskRunner() = default;
72 
PostDelayedTask(const base::Location & whence,base::OnceClosure task,base::TimeDelta delay)73 bool FakeTaskRunner::PostDelayedTask(const base::Location& whence,
74                                      base::OnceClosure task,
75                                      base::TimeDelta delay) {
76   tasks_->push_back({whence, std::move(task), delay});
77   return true;
78 }
79 
RunsTasksInCurrentSequence() const80 bool FakeTaskRunner::RunsTasksInCurrentSequence() const {
81   return true;
82 }
83