1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  *
7  */
8 
9 #include <quic/client/QuicClientAsyncTransport.h>
10 
11 #include <folly/Conv.h>
12 #include <folly/detail/SingletonStackTrace.h>
13 
14 namespace quic {
15 
QuicClientAsyncTransport(const std::shared_ptr<quic::QuicClientTransport> & clientSock)16 QuicClientAsyncTransport::QuicClientAsyncTransport(
17     const std::shared_ptr<quic::QuicClientTransport>& clientSock) {
18   setSocket(clientSock);
19   clientSock->start(this);
20 }
21 
onNewBidirectionalStream(StreamId)22 void QuicClientAsyncTransport::onNewBidirectionalStream(
23     StreamId /*id*/) noexcept {
24   CHECK(false);
25 }
onNewUnidirectionalStream(StreamId)26 void QuicClientAsyncTransport::onNewUnidirectionalStream(
27     StreamId /*id*/) noexcept {
28   CHECK(false);
29 }
30 
onStopSending(StreamId,ApplicationErrorCode)31 void QuicClientAsyncTransport::onStopSending(
32     StreamId /*id*/,
33     ApplicationErrorCode /*error*/) noexcept {}
34 
onConnectionEnd()35 void QuicClientAsyncTransport::onConnectionEnd() noexcept {
36   folly::AsyncSocketException ex(
37       folly::AsyncSocketException::UNKNOWN, "Quic connection ended");
38   // TODO: closeNow inside this callback may actually trigger gracefull close
39   closeNowImpl(std::move(ex));
40 }
41 
onConnectionError(std::pair<QuicErrorCode,std::string> code)42 void QuicClientAsyncTransport::onConnectionError(
43     std::pair<QuicErrorCode, std::string> code) noexcept {
44   folly::AsyncSocketException ex(
45       folly::AsyncSocketException::UNKNOWN,
46       folly::to<std::string>("Quic connection error", code.second));
47   // TODO: closeNow inside this callback may actually trigger gracefull close
48   closeNowImpl(std::move(ex));
49 }
50 
onTransportReady()51 void QuicClientAsyncTransport::onTransportReady() noexcept {
52   auto streamId = sock_->createBidirectionalStream();
53   if (!streamId) {
54     folly::AsyncSocketException ex(
55         folly::AsyncSocketException::UNKNOWN, "Quic failed to create stream");
56     closeNowImpl(std::move(ex));
57   }
58   setStreamId(streamId.value());
59 }
60 
61 } // namespace quic
62