瀏覽代碼

Added optional preloading

Paul-Louis Ageneau 5 年之前
父節點
當前提交
22a1c56863
共有 5 個文件被更改,包括 12 次插入2 次删除
  1. 2 0
      include/rtc/init.hpp
  2. 1 1
      src/certificate.hpp
  3. 5 0
      src/init.cpp
  4. 1 1
      src/peerconnection.cpp
  5. 3 0
      test/benchmark.cpp

+ 2 - 0
include/rtc/init.hpp

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

+ 1 - 1
src/certificate.hpp

@@ -61,7 +61,7 @@ string make_fingerprint(X509 *x509);
 using certificate_ptr = std::shared_ptr<Certificate>;
 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();
 

+ 5 - 0
src/init.cpp

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

+ 1 - 1
src/peerconnection.cpp

@@ -39,7 +39,7 @@ using std::weak_ptr;
 PeerConnection::PeerConnection() : PeerConnection(Configuration()) {}
 
 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) {
 	PLOG_VERBOSE << "Creating PeerConnection";
 }

+ 3 - 0
test/benchmark.cpp

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