浏览代码

Added WSAStartup call in PeerConnection and cleaned up includes

Paul-Louis Ageneau 5 年之前
父节点
当前提交
9441f78494
共有 9 个文件被更改,包括 30 次插入62 次删除
  1. 1 0
      include/rtc/include.hpp
  2. 6 0
      src/datachannel.cpp
  3. 7 5
      src/icetransport.cpp
  4. 15 1
      src/peerconnection.cpp
  5. 0 4
      src/sctptransport.cpp
  6. 0 8
      src/sctptransport.hpp
  7. 1 16
      test/main.cpp
  8. 0 14
      test/p2p/answerer.cpp
  9. 0 14
      test/p2p/offerer.cpp

+ 1 - 0
include/rtc/include.hpp

@@ -20,6 +20,7 @@
 #define RTC_INCLUDE_H
 #define RTC_INCLUDE_H
 
 
 #ifdef _WIN32
 #ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
 #ifndef _WIN32_WINNT
 #ifndef _WIN32_WINNT
 #define _WIN32_WINNT 0x0602
 #define _WIN32_WINNT 0x0602
 #endif
 #endif

+ 6 - 0
src/datachannel.cpp

@@ -21,6 +21,12 @@
 #include "peerconnection.hpp"
 #include "peerconnection.hpp"
 #include "sctptransport.hpp"
 #include "sctptransport.hpp"
 
 
+#ifdef _WIN32
+#include <winsock2.h>
+#else
+#include <arpa/inet.h>
+#endif
+
 namespace rtc {
 namespace rtc {
 
 
 using std::shared_ptr;
 using std::shared_ptr;

+ 7 - 5
src/icetransport.cpp

@@ -19,9 +19,15 @@
 #include "icetransport.hpp"
 #include "icetransport.hpp"
 #include "configuration.hpp"
 #include "configuration.hpp"
 
 
+#include <iostream>
+#include <random>
+#include <sstream>
+
 #ifdef _WIN32
 #ifdef _WIN32
 #include <winsock2.h>
 #include <winsock2.h>
-#elif __linux__
+#include <ws2tcpip.h>
+#else
+#include <arpa/inet.h>
 #include <netdb.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
 #include <sys/socket.h>
@@ -29,10 +35,6 @@
 
 
 #include <sys/types.h>
 #include <sys/types.h>
 
 
-#include <iostream>
-#include <random>
-#include <sstream>
-
 using namespace std::chrono_literals;
 using namespace std::chrono_literals;
 
 
 using std::shared_ptr;
 using std::shared_ptr;

+ 15 - 1
src/peerconnection.cpp

@@ -25,6 +25,10 @@
 
 
 #include <iostream>
 #include <iostream>
 
 
+#ifdef _WIN32
+#include <winsock2.h>
+#endif
+
 namespace rtc {
 namespace rtc {
 
 
 using namespace std::placeholders;
 using namespace std::placeholders;
@@ -32,7 +36,13 @@ using namespace std::placeholders;
 using std::shared_ptr;
 using std::shared_ptr;
 using std::weak_ptr;
 using std::weak_ptr;
 
 
-PeerConnection::PeerConnection() : PeerConnection(Configuration()) {}
+PeerConnection::PeerConnection() : PeerConnection(Configuration()) {
+#ifdef _WIN32
+	WSADATA wsaData;
+	if (WSAStartup(MAKEWORD(2, 2), &wsaData))
+		throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
+#endif
+}
 
 
 PeerConnection::PeerConnection(const Configuration &config)
 PeerConnection::PeerConnection(const Configuration &config)
     : mConfig(config), mCertificate(make_certificate("libdatachannel")), mState(State::New) {}
     : mConfig(config), mCertificate(make_certificate("libdatachannel")), mState(State::New) {}
@@ -43,6 +53,10 @@ PeerConnection::~PeerConnection() {
 	mSctpTransport.reset();
 	mSctpTransport.reset();
 	mDtlsTransport.reset();
 	mDtlsTransport.reset();
 	mIceTransport.reset();
 	mIceTransport.reset();
+
+#ifdef _WIN32
+	WSACleanup();
+#endif
 }
 }
 
 
 void PeerConnection::close() {
 void PeerConnection::close() {

+ 0 - 4
src/sctptransport.cpp

@@ -23,10 +23,6 @@
 #include <iostream>
 #include <iostream>
 #include <vector>
 #include <vector>
 
 
-#ifdef __linux__
-#include <arpa/inet.h>
-#endif
-
 #ifdef USE_JUICE
 #ifdef USE_JUICE
 #ifndef __APPLE__
 #ifndef __APPLE__
 // libjuice enables Linux path MTU discovery or sets the DF flag
 // libjuice enables Linux path MTU discovery or sets the DF flag

+ 0 - 8
src/sctptransport.hpp

@@ -29,14 +29,6 @@
 #include <map>
 #include <map>
 #include <mutex>
 #include <mutex>
 
 
-#ifdef _WIN32
-#include <winsock2.h>
-#elif __linux__
-#include <sys/socket.h>
-#endif
-
-#include <sys/types.h>
-
 #include "usrsctp.h"
 #include "usrsctp.h"
 
 
 namespace rtc {
 namespace rtc {

+ 1 - 16
test/main.cpp

@@ -23,10 +23,6 @@
 #include <memory>
 #include <memory>
 #include <thread>
 #include <thread>
 
 
-#ifdef _WIN32
-#include <winsock2.h>
-#endif
-
 using namespace rtc;
 using namespace rtc;
 using namespace std;
 using namespace std;
 
 
@@ -35,12 +31,6 @@ template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 int main(int argc, char **argv) {
 int main(int argc, char **argv) {
 	InitLogger(LogLevel::Debug);
 	InitLogger(LogLevel::Debug);
 
 
-#ifdef _WIN32
-	WSADATA wsaData;
-	if (WSAStartup(MAKEWORD(2, 2), &wsaData))
-		throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
-#endif
-
 	Configuration config;
 	Configuration config;
 	// config.iceServers.emplace_back("stun:stun.l.google.com:19302");
 	// config.iceServers.emplace_back("stun:stun.l.google.com:19302");
 	// config.iceServers.emplace_back(IceServer("TURN_SERVER_URL", "PORT", "USERNAME", "PASSWORD",
 	// config.iceServers.emplace_back(IceServer("TURN_SERVER_URL", "PORT", "USERNAME", "PASSWORD",
@@ -137,10 +127,5 @@ int main(int argc, char **argv) {
 	} else {
 	} else {
 		cout << "Failure" << endl;
 		cout << "Failure" << endl;
 	}
 	}
-
-#ifdef _WIN32
-		WSACleanup();
-#endif
-
-	    return success ? 0 : 1;
+	return success ? 0 : 1;
 }
 }

+ 0 - 14
test/p2p/answerer.cpp

@@ -23,10 +23,6 @@
 #include <memory>
 #include <memory>
 #include <thread>
 #include <thread>
 
 
-#ifdef _WIN32
-#include <winsock2.h>
-#endif
-
 using namespace rtc;
 using namespace rtc;
 using namespace std;
 using namespace std;
 
 
@@ -35,12 +31,6 @@ template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 int main(int argc, char **argv) {
 int main(int argc, char **argv) {
 	InitLogger(LogLevel::Warning);
 	InitLogger(LogLevel::Warning);
 
 
-#ifdef _WIN32
-	WSADATA wsaData;
-	if (WSAStartup(MAKEWORD(2, 2), &wsaData))
-		throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
-#endif
-
 	Configuration config;
 	Configuration config;
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
 
 
@@ -141,8 +131,4 @@ int main(int argc, char **argv) {
 		dc->close();
 		dc->close();
 	if (pc)
 	if (pc)
 		pc->close();
 		pc->close();
-
-#ifdef _WIN32
-	WSACleanup();
-#endif
 }
 }

+ 0 - 14
test/p2p/offerer.cpp

@@ -23,10 +23,6 @@
 #include <memory>
 #include <memory>
 #include <thread>
 #include <thread>
 
 
-#ifdef _WIN32
-#include <winsock2.h>
-#endif
-
 using namespace rtc;
 using namespace rtc;
 using namespace std;
 using namespace std;
 
 
@@ -35,12 +31,6 @@ template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 int main(int argc, char **argv) {
 int main(int argc, char **argv) {
 	InitLogger(LogLevel::Warning);
 	InitLogger(LogLevel::Warning);
 
 
-#ifdef _WIN32
-	WSADATA wsaData;
-	if (WSAStartup(MAKEWORD(2, 2), &wsaData))
-		throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
-#endif
-
 	Configuration config;
 	Configuration config;
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
 
 
@@ -141,8 +131,4 @@ int main(int argc, char **argv) {
 		dc->close();
 		dc->close();
 	if (pc)
 	if (pc)
 		pc->close();
 		pc->close();
-
-#ifdef _WIN32
-	WSACleanup();
-#endif
 }
 }