Added optional preloading

This commit is contained in:
Paul-Louis Ageneau
2020-06-09 11:17:09 +02:00
parent 74c5cbcf9f
commit 22a1c56863
5 changed files with 12 additions and 2 deletions

View File

@ -31,6 +31,7 @@ using init_token = std::shared_ptr<Init>;
class Init { class Init {
public: public:
static init_token Token(); static init_token Token();
static void Preload();
static void Cleanup(); static void Cleanup();
~Init(); ~Init();
@ -43,6 +44,7 @@ private:
static std::mutex Mutex; static std::mutex Mutex;
}; };
inline void Preload() { Init::Preload(); }
inline void Cleanup() { Init::Cleanup(); } inline void Cleanup() { Init::Cleanup(); }
} // namespace rtc } // namespace rtc

View File

@ -61,7 +61,7 @@ string make_fingerprint(X509 *x509);
using certificate_ptr = std::shared_ptr<Certificate>; using certificate_ptr = std::shared_ptr<Certificate>;
using future_certificate_ptr = std::shared_future<certificate_ptr>; using future_certificate_ptr = std::shared_future<certificate_ptr>;
future_certificate_ptr make_certificate(string commonName); // cached future_certificate_ptr make_certificate(string commonName = "libdatachannel"); // cached
void CleanupCertificateCache(); void CleanupCertificateCache();

View File

@ -55,6 +55,11 @@ init_token Init::Token() {
return Global; return Global;
} }
void Init::Preload() {
Token(); // pre-init
make_certificate().wait(); // preload certificate
}
void Init::Cleanup() { Global.reset(); } void Init::Cleanup() { Global.reset(); }
Init::Init() { Init::Init() {

View File

@ -39,7 +39,7 @@ using std::weak_ptr;
PeerConnection::PeerConnection() : PeerConnection(Configuration()) {} PeerConnection::PeerConnection() : PeerConnection(Configuration()) {}
PeerConnection::PeerConnection(const Configuration &config) PeerConnection::PeerConnection(const Configuration &config)
: mConfig(config), mCertificate(make_certificate("libdatachannel")), mState(State::New), : mConfig(config), mCertificate(make_certificate()), mState(State::New),
mGatheringState(GatheringState::New) { mGatheringState(GatheringState::New) {
PLOG_VERBOSE << "Creating PeerConnection"; PLOG_VERBOSE << "Creating PeerConnection";
} }

View File

@ -183,10 +183,13 @@ size_t benchmark(milliseconds duration) {
#ifdef BENCHMARK_MAIN #ifdef BENCHMARK_MAIN
int main(int argc, char **argv) { int main(int argc, char **argv) {
try { try {
rtc::Preload();
size_t goodput = benchmark(30s); size_t goodput = benchmark(30s);
if (goodput == 0) if (goodput == 0)
throw runtime_error("No data received"); throw runtime_error("No data received");
rtc::Cleanup();
return 0; return 0;
} catch (const std::exception &e) { } catch (const std::exception &e) {