Magic_Game/src/kiwano-network/HttpModule.h

145 lines
3.6 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>
2020-02-14 17:56:50 +08:00
#include <kiwano/core/Module.h>
#include <mutex>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
namespace network
{
/**
* \~chinese
2020-02-10 17:32:04 +08:00
* \defgroup Network
2020-01-21 10:09:55 +08:00
*/
/**
* \addtogroup Network
* @{
*/
/**
* \~chinese
2020-02-14 17:56:50 +08:00
* @brief HTTP模块
2020-01-21 10:09:55 +08:00
*/
2020-02-14 17:56:50 +08:00
class KGE_API HttpModule
: public Singleton<HttpModule>
, public Module
2020-01-21 10:09:55 +08:00
{
2020-02-14 17:56:50 +08:00
friend Singleton<HttpModule>;
2020-01-21 10:09:55 +08:00
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 发送HTTP请求
/// @param[in] request HTTP请求
/// @details 发送请求后,无论结束或失败都将调用请求的响应回调函数
2020-01-21 10:09:55 +08:00
void Send(HttpRequestPtr request);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置连接超时时长
2020-01-21 10:09:55 +08:00
void SetTimeoutForConnect(Duration timeout);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取连接超时时长
2020-01-21 10:09:55 +08:00
Duration GetTimeoutForConnect() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置读取超时时长
2020-01-21 10:09:55 +08:00
void SetTimeoutForRead(Duration timeout);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取读取超时时长
2020-01-21 10:09:55 +08:00
Duration GetTimeoutForRead() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置SSL证书地址
2020-01-21 10:09:55 +08:00
void SetSSLVerification(String const& root_certificate_path);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取SSL证书地址
2020-01-21 10:09:55 +08:00
String const& GetSSLVerification() const;
2020-01-21 10:09:55 +08:00
public:
2020-02-14 17:56:50 +08:00
virtual void SetupModule() override;
2020-02-14 17:56:50 +08:00
virtual void DestroyModule() override;
2020-01-21 10:09:55 +08:00
private:
2020-02-14 17:56:50 +08:00
HttpModule();
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-02-14 17:56:50 +08:00
inline void HttpModule::SetTimeoutForConnect(Duration timeout)
2020-01-21 10:09:55 +08:00
{
timeout_for_connect_ = timeout;
}
2019-11-13 14:33:15 +08:00
2020-02-14 17:56:50 +08:00
inline Duration HttpModule::GetTimeoutForConnect() const
2020-01-21 10:09:55 +08:00
{
return timeout_for_connect_;
}
2019-11-13 14:33:15 +08:00
2020-02-14 17:56:50 +08:00
inline void HttpModule::SetTimeoutForRead(Duration timeout)
2020-01-21 10:09:55 +08:00
{
timeout_for_read_ = timeout;
}
2019-11-13 14:33:15 +08:00
2020-02-14 17:56:50 +08:00
inline Duration HttpModule::GetTimeoutForRead() const
2020-01-21 10:09:55 +08:00
{
return timeout_for_read_;
}
2019-11-13 14:33:15 +08:00
2020-02-14 17:56:50 +08:00
inline void HttpModule::SetSSLVerification(String const& root_certificate_path)
2020-01-21 10:09:55 +08:00
{
ssl_verification_ = root_certificate_path;
}
2019-11-13 14:33:15 +08:00
2020-02-14 17:56:50 +08:00
inline String const& HttpModule::GetSSLVerification() const
2020-01-21 10:09:55 +08:00
{
return ssl_verification_;
}
2020-01-21 10:09:55 +08:00
} // namespace network
} // namespace kiwano