AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
LibcurlHTTP2Connection.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LIBCURLUTILS_LIBCURLHTTP2CONNECTION_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LIBCURLUTILS_LIBCURLHTTP2CONNECTION_H_
18 
19 #include <condition_variable>
20 #include <deque>
21 #include <map>
22 #include <memory>
23 #include <thread>
24 
26 #include "CurlMultiHandleWrapper.h"
28 
29 namespace alexaClientSDK {
30 namespace avsCommon {
31 namespace utils {
32 namespace libcurlUtils {
33 
34 class LibcurlHTTP2Request;
35 
37 public:
45  static std::shared_ptr<LibcurlHTTP2Connection> create(
46  const std::shared_ptr<LibcurlSetCurlOptionsCallbackInterface>& setCurlOptionsCallback = nullptr);
47 
52 
55  std::shared_ptr<avsCommon::utils::http2::HTTP2RequestInterface> createAndSendRequest(
56  const http2::HTTP2RequestConfig& config) override;
57  void disconnect() override;
58  void addObserver(std::shared_ptr<avsCommon::utils::http2::HTTP2ConnectionObserverInterface> observer) override;
59  void removeObserver(std::shared_ptr<avsCommon::utils::http2::HTTP2ConnectionObserverInterface> observer) override;
61 
62 protected:
70  const std::shared_ptr<LibcurlSetCurlOptionsCallbackInterface>& setCurlOptionsCallback = nullptr);
71 
75  using ActiveStreamMap = std::map<CURL*, std::shared_ptr<LibcurlHTTP2Request>>;
82  bool addStream(std::shared_ptr<LibcurlHTTP2Request> stream);
83 
88  bool createMultiHandle();
89 
95  bool releaseStream(ActiveStreamMap::iterator& iterator);
96 
100  void networkLoop();
101 
107  bool isStopping();
108 
112  void setIsStopping();
113 
117  void cleanupFinishedStreams();
118 
124 
130  bool areStreamsPaused();
131 
135  void unPauseActiveStreams();
136 
143  bool cancelActiveStream(ActiveStreamMap::iterator& iterator);
144 
148  void cancelActiveStreams();
149 
153  void cancelPendingStreams();
154 
158  void cancelAllStreams();
159 
165  std::shared_ptr<LibcurlHTTP2Request> dequeueRequest();
166 
170  void processNextRequest();
171 
176 
178  std::thread m_networkThread;
179 
181  std::shared_ptr<avsCommon::utils::libcurlUtils::CurlMultiHandleWrapper> m_multi;
182 
184  std::mutex m_mutex;
185 
188  std::condition_variable m_cv;
189 
193 
195  std::deque<std::shared_ptr<LibcurlHTTP2Request>> m_requestQueue;
196 
198  std::mutex m_observersMutex;
199 
201  std::unordered_set<std::shared_ptr<avsCommon::utils::http2::HTTP2ConnectionObserverInterface>> m_observers;
202 
205 
207  std::shared_ptr<LibcurlSetCurlOptionsCallbackInterface> m_setCurlOptionsCallback;
208 };
209 
210 } // namespace libcurlUtils
211 } // namespace utils
212 } // namespace avsCommon
213 } // namespace alexaClientSDK
214 
215 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LIBCURLUTILS_LIBCURLHTTP2CONNECTION_H_
std::deque< std::shared_ptr< LibcurlHTTP2Request > > m_requestQueue
Queue of requests send. Serialized by m_mutex.
Definition: LibcurlHTTP2Connection.h:195
bool addStream(std::shared_ptr< LibcurlHTTP2Request > stream)
std::shared_ptr< avsCommon::utils::libcurlUtils::CurlMultiHandleWrapper > m_multi
Represents a CURL multi handle. Intended to only be accessed by the network loop thread and in addStr...
Definition: LibcurlHTTP2Connection.h:181
void removeObserver(std::shared_ptr< avsCommon::utils::http2::HTTP2ConnectionObserverInterface > observer) override
bool cancelActiveStream(ActiveStreamMap::iterator &iterator)
std::condition_variable m_cv
Definition: LibcurlHTTP2Connection.h:188
ActiveStreamMap m_activeStreams
Definition: LibcurlHTTP2Connection.h:192
std::shared_ptr< avsCommon::utils::http2::HTTP2RequestInterface > createAndSendRequest(const http2::HTTP2RequestConfig &config) override
std::unordered_set< std::shared_ptr< avsCommon::utils::http2::HTTP2ConnectionObserverInterface > > m_observers
Observers.
Definition: LibcurlHTTP2Connection.h:201
static std::shared_ptr< LibcurlHTTP2Connection > create(const std::shared_ptr< LibcurlSetCurlOptionsCallbackInterface > &setCurlOptionsCallback=nullptr)
std::shared_ptr< LibcurlSetCurlOptionsCallbackInterface > m_setCurlOptionsCallback
The LibcurlSetCurlOptionsCallbackInterface used for this connection.
Definition: LibcurlHTTP2Connection.h:207
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
std::thread m_networkThread
Main thread for this class.
Definition: LibcurlHTTP2Connection.h:178
LibcurlHTTP2Connection(const std::shared_ptr< LibcurlSetCurlOptionsCallbackInterface > &setCurlOptionsCallback=nullptr)
std::mutex m_mutex
Serializes concurrent access to the m_requestQueue and m_isStopping members.
Definition: LibcurlHTTP2Connection.h:184
std::mutex m_observersMutex
Mutex for observers.
Definition: LibcurlHTTP2Connection.h:198
bool m_isStopping
Set to true when we want to exit the network loop.
Definition: LibcurlHTTP2Connection.h:204
void addObserver(std::shared_ptr< avsCommon::utils::http2::HTTP2ConnectionObserverInterface > observer) override
std::map< CURL *, std::shared_ptr< LibcurlHTTP2Request > > ActiveStreamMap
Definition: LibcurlHTTP2Connection.h:75

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0