Magic_Game/src/kiwano-network/HttpClient.h

145 lines
3.5 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2020-01-21 10:09:55 +08:00
#include <condition_variable>
2020-01-17 16:55:47 +08:00
#include <kiwano/core/Common.h>
2019-11-13 14:33:15 +08:00
#include <kiwano/core/Component.h>
#include <mutex>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
namespace network
{
/**
* \~chinese
* \defgroup Network <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>
*/
/**
* \addtogroup Network
* @{
*/
/**
* \~chinese
* @brief HTTP<EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD>
*/
class KGE_API HttpClient
: public Singleton<HttpClient>
, public ComponentBase
{
friend Singleton<HttpClient>;
2020-01-21 10:09:55 +08:00
public:
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>HTTP<54><50><EFBFBD><EFBFBD>
/// @param[in] request HTTP<54><50><EFBFBD><EFBFBD>
/// @details <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>۽<EFBFBD><DBBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܶ<EFBFBD><DCB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
void Send(HttpRequestPtr request);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>ʱʱ<CAB1><CAB1>
void SetTimeoutForConnect(Duration timeout);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD>ӳ<EFBFBD>ʱʱ<CAB1><CAB1>
Duration GetTimeoutForConnect() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>ö<EFBFBD>ȡ<EFBFBD><C8A1>ʱʱ<CAB1><CAB1>
void SetTimeoutForRead(Duration timeout);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1>ȡ<EFBFBD><C8A1>ʱʱ<CAB1><CAB1>
Duration GetTimeoutForRead() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>SSL֤<4C><D6A4><EFBFBD><EFBFBD>ַ
void SetSSLVerification(String const& root_certificate_path);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡSSL֤<4C><D6A4><EFBFBD><EFBFBD>ַ
String const& GetSSLVerification() const;
2020-01-21 10:09:55 +08:00
public:
virtual void SetupComponent() override;
2020-01-21 10:09:55 +08:00
virtual void DestroyComponent() override;
2020-01-21 10:09:55 +08:00
private:
HttpClient();
2020-01-21 10:09:55 +08:00
void NetworkThread();
2020-01-21 10:09:55 +08:00
void Perform(HttpRequestPtr request, HttpResponsePtr response);
2020-01-21 10:09:55 +08:00
void DispatchResponseCallback();
2020-01-21 10:09:55 +08:00
private:
Duration timeout_for_connect_;
Duration timeout_for_read_;
2020-01-21 10:09:55 +08:00
String ssl_verification_;
2020-01-21 10:09:55 +08:00
std::mutex request_mutex_;
Queue<HttpRequestPtr> request_queue_;
2019-11-13 14:33:15 +08:00
2020-01-21 10:09:55 +08:00
std::mutex response_mutex_;
Queue<HttpResponsePtr> response_queue_;
2019-12-31 10:37:29 +08:00
2020-01-21 10:09:55 +08:00
std::condition_variable_any sleep_condition_;
};
2019-11-13 14:33:15 +08:00
2020-01-21 10:09:55 +08:00
/** @} */
2019-11-13 14:33:15 +08:00
2020-01-21 10:09:55 +08:00
inline void HttpClient::SetTimeoutForConnect(Duration timeout)
{
timeout_for_connect_ = timeout;
}
2019-11-13 14:33:15 +08:00
2020-01-21 10:09:55 +08:00
inline Duration HttpClient::GetTimeoutForConnect() const
{
return timeout_for_connect_;
}
2019-11-13 14:33:15 +08:00
2020-01-21 10:09:55 +08:00
inline void HttpClient::SetTimeoutForRead(Duration timeout)
{
timeout_for_read_ = timeout;
}
2019-11-13 14:33:15 +08:00
2020-01-21 10:09:55 +08:00
inline Duration HttpClient::GetTimeoutForRead() const
{
return timeout_for_read_;
}
2019-11-13 14:33:15 +08:00
2020-01-21 10:09:55 +08:00
inline void HttpClient::SetSSLVerification(String const& root_certificate_path)
{
ssl_verification_ = root_certificate_path;
}
2019-11-13 14:33:15 +08:00
2020-01-21 10:09:55 +08:00
inline String const& HttpClient::GetSSLVerification() const
{
return ssl_verification_;
}
2020-01-21 10:09:55 +08:00
} // namespace network
} // namespace kiwano