Magic_Game/src/kiwano-network/HttpModule.h

150 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-02-20 13:58:17 +08:00
#include <atomic>
#include <mutex>
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>
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>
2020-01-21 10:09:55 +08:00
*/
/**
* \addtogroup Network
* @{
*/
/**
* \~chinese
* @brief HTTPģ<EFBFBD><EFBFBD>
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
/// @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>
2020-01-21 10:09:55 +08:00
void Send(HttpRequestPtr request);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>ʱʱ<CAB1><CAB1>
2020-01-21 10:09:55 +08:00
void SetTimeoutForConnect(Duration timeout);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD>ӳ<EFBFBD>ʱʱ<CAB1><CAB1>
2020-01-21 10:09:55 +08:00
Duration GetTimeoutForConnect() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>ö<EFBFBD>ȡ<EFBFBD><C8A1>ʱʱ<CAB1><CAB1>
2020-01-21 10:09:55 +08:00
void SetTimeoutForRead(Duration timeout);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1>ȡ<EFBFBD><C8A1>ʱʱ<CAB1><CAB1>
2020-01-21 10:09:55 +08:00
Duration GetTimeoutForRead() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>SSL֤<4C><D6A4><EFBFBD><EFBFBD>ַ
2020-02-19 12:09:50 +08:00
void SetSSLVerification(const String& root_certificate_path);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20><>ȡSSL֤<4C><D6A4><EFBFBD><EFBFBD>ַ
2020-02-19 12:09:50 +08:00
const String& 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-02-20 13:58:17 +08:00
std::condition_variable sleep_cond_;
std::atomic<bool> quit_flag_;
std::mutex quit_mutex_;
std::condition_variable quit_cond_;
2020-01-21 10:09:55 +08:00
};
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-19 12:09:50 +08:00
inline void HttpModule::SetSSLVerification(const String& 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-19 12:09:50 +08:00
inline const String& 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