Repository: conan-io/conan-center-index

Issue:
[request]  ValveSoftware/GameNetworkingSockets
### Package Details
  * Package Name/Version: **gamenetworkingsockets/1.3.0**
  * Source code: **https://github.com/ValveSoftware/GameNetworkingSockets**

### Description Of The Library / Tool

Reliable & unreliable messages over UDP. Robust message fragmentation & reassembly. P2P networking / NAT traversal. Encryption. 

5k6 stars + ~9k forks

related to #621



Discussion:

Resolving patch:
diff --git a/recipes/gamenetworkingsockets/all/conanfile.py b/recipes/gamenetworkingsockets/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/gamenetworkingsockets/all/conanfile.py
@@ -0,0 +1,126 @@
+from conans import ConanFile, CMake, tools
+from conans.errors import ConanInvalidConfiguration
+import os
+
+required_conan_version = ">=1.33.0"
+
+
+class GameNetworkingSocketsConan(ConanFile):
+    name = "gamenetworkingsockets"
+    description = "GameNetworkingSockets is a basic transport layer for games."
+    topics = ("networking", "game-development")
+    url = "https://github.com/conan-io/conan-center-index"
+    homepage = "https://github.com/ValveSoftware/GameNetworkingSockets"
+    license = "BSD-3-Clause"
+    generators = "cmake", "pkg_config"
+    settings = "os", "arch", "compiler", "build_type"
+    exports_sources = ["CMakeLists.txt", "patches/**"]
+
+    options = {
+        "shared": [True, False],
+        "fPIC": [True, False],
+        "encryption": ["openssl", "libsodium", "bcrypt"]
+    }
+
+    default_options = {
+        "shared": False,
+        "fPIC": True,
+        "encryption": "openssl"
+    }
+
+    _cmake = None
+
+    @property
+    def _source_subfolder(self):
+        return "source_subfolder"
+
+    @property
+    def _build_subfolder(self):
+        return "build_subfolder"
+
+    def config_options(self):
+        if self.settings.os == "Windows":
+            del self.options.fPIC
+
+    def configure(self):
+        if self.options.shared:
+            del self.options.fPIC
+
+    def validate(self):
+        if self.settings.compiler.get_safe("cppstd"):
+            tools.check_min_cppstd(self, 11)
+
+        if self.options.encryption == "bcrypt" and self.settings.os != "Windows":
+            raise ConanInvalidConfiguration("bcrypt is only valid on Windows")
+
+    def build_requirements(self):
+        self.build_requires("protobuf/3.17.1")
+
+    def requirements(self):
+        self.requires("protobuf/3.17.1")
+        if self.options.encryption == "openssl":
+            self.requires("openssl/1.1.1l")
+        elif self.options.encryption == "libsodium":
+            self.requires("libsodium/1.0.18")
+
+    def source(self):
+        tools.get(**self.conan_data["sources"][self.version], strip_root=True, destination=self._source_subfolder)
+
+    def _patch_sources(self):
+        for patch in self.conan_data.get("patches", {}).get(self.version, []):
+            tools.patch(**patch)
+
+    def build(self):
+        self._patch_sources()
+        cmake = self._configure_cmake()
+        cmake.build()
+
+    def _configure_cmake(self):
+        if self._cmake:
+            return self._cmake
+        self._cmake = CMake(self)
+        self._cmake.definitions["BUILD_STATIC"] = not self.options.shared
+        self._cmake.definitions["BUILD_SHARED"] = self.options.shared
+        self._cmake.definitions["GAMENETWORKINGSOCKETS_BUILD_EXAMPLES"] = False
+        self._cmake.definitions["GAMENETWORKINGSOCKETS_BUILD_TESTS"] = False
+        self._cmake.definitions["Protobuf_USE_STATIC_LIBS"] = not self.options["protobuf"].shared
+        crypto = {"openssl": "OpenSSL", "libsodium": "libsodium", "bcrypt": "BCrypt"}
+        self._cmake.definitions["USE_CRYPTO"] = crypto[str(self.options.encryption)]
+        crypto25519 = {"openssl": "OpenSSL", "libsodium": "libsodium", "bcrypt": "Reference"}
+        self._cmake.definitions["USE_CRYPTO25519"] = crypto25519[str(self.options.encryption)]
+        if self.options.encryption == "openssl":
+            self._cmake.definitions["OPENSSL_NEW_ENOUGH"] = True
+            self._cmake.definitions["OPENSSL_HAS_25519_RAW"] = True
+        self._cmake.configure(build_folder=self._build_subfolder)
+        return self._cmake
+
+    def package(self):
+        self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
+        cmake = self._configure_cmake()
+        cmake.install()
+        tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
+
+    def package_info(self):
+        self.cpp_info.names["cmake_find_package"] = "GameNetworkingSockets"
+        self.cpp_info.names["cmake_find_package_multi"] = "GameNetworkingSockets"
+        self.cpp_info.names["pkg_config"] = "GameNetworkingSockets"
+        self.cpp_info.includedirs.append(os.path.join("include", "GameNetworkingSockets"))
+        if self.options.shared:
+            self.cpp_info.libs = ["GameNetworkingSockets"]
+        else:
+            self.cpp_info.libs = ["GameNetworkingSockets_s"]
+            self.cpp_info.defines = ["STEAMNETWORKINGSOCKETS_STATIC_LINK"]
+
+        self.cpp_info.requires = ["protobuf::libprotobuf"]
+        if self.options.encryption == "openssl":
+            self.cpp_info.requires += ["openssl::crypto"]
+        elif self.options.encryption == "libsodium":
+            self.cpp_info.requires += ["libsodium::libsodium"]
+
+        if self.settings.os in ["Linux", "FreeBSD"]:
+            self.cpp_info.system_libs = ["pthread"]
+        elif self.settings.os == "Windows":
+            self.cpp_info.system_libs = ["ws2_32", "crypt32", "winmm"]
+            if self.options.encryption == "bcrypt":
+                self.cpp_info.system_libs += ["bcrypt"]
+        


Test patch:
diff --git a/recipes/gamenetworkingsockets/all/test_package/CMakeLists.txt b/recipes/gamenetworkingsockets/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/gamenetworkingsockets/all/test_package/CMakeLists.txt
@@ -0,0 +1,11 @@
+cmake_minimum_required(VERSION 3.1)
+project(test_package CXX)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup(TARGETS)
+
+find_package(GameNetworkingSockets REQUIRED CONFIG)
+
+add_executable(${PROJECT_NAME} test_package.cpp)
+set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11) 
+target_link_libraries(${PROJECT_NAME} GameNetworkingSockets::GameNetworkingSockets)
diff --git a/recipes/gamenetworkingsockets/all/test_package/conanfile.py b/recipes/gamenetworkingsockets/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/gamenetworkingsockets/all/test_package/conanfile.py
@@ -0,0 +1,17 @@
+from conans import ConanFile, CMake, tools
+import os
+
+
+class TestPackageConan(ConanFile):
+    settings = "os", "arch", "compiler", "build_type"
+    generators = "cmake", "cmake_find_package_multi"
+
+    def build(self):
+        cmake = CMake(self)
+        cmake.configure()
+        cmake.build()
+
+    def test(self):
+        if not tools.cross_building(self):
+            bin_path = os.path.join("bin", "test_package")
+            self.run(bin_path, run_environment=True)
diff --git a/recipes/gamenetworkingsockets/all/test_package/test_package.cpp b/recipes/gamenetworkingsockets/all/test_package/test_package.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/gamenetworkingsockets/all/test_package/test_package.cpp
@@ -0,0 +1,55 @@
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <steam/isteamnetworkingutils.h>
+#include <steam/steamnetworkingsockets.h>
+
+static FILE* g_fpLog = NULL;
+static SteamNetworkingMicroseconds g_logTimeZero;
+
+static void DebugOutput(ESteamNetworkingSocketsDebugOutputType eType, char const* pszMsg)
+{
+    SteamNetworkingMicroseconds time = SteamNetworkingUtils()->GetLocalTimestamp() - g_logTimeZero;
+    if (g_fpLog) {
+        fprintf(g_fpLog, "%10.6f %s\n", time * 1e-6, pszMsg);
+    }
+
+    if (eType <= k_ESteamNetworkingSocketsDebugOutputType_Msg) {
+        printf("%10.6f %s\n", time * 1e-6, pszMsg);
+        fflush(stdout);
+    }
+
+    if (eType == k_ESteamNetworkingSocketsDebugOutputType_Bug) {
+        fflush(stdout);
+        fflush(stderr);
+        if (g_fpLog) {
+            fflush(g_fpLog);
+        }
+
+        if (strstr(pszMsg, "SteamNetworkingGlobalLock held for")) {
+            return;
+        }
+
+        assert(!"TEST FAILED");
+    }
+}
+
+int main()
+{
+    g_fpLog = stderr;
+    g_logTimeZero = SteamNetworkingUtils()->GetLocalTimestamp();
+
+    SteamNetworkingUtils()->SetDebugOutputFunction(k_ESteamNetworkingSocketsDebugOutputType_Debug, DebugOutput);
+    SteamNetworkingUtils()->SetDebugOutputFunction(k_ESteamNetworkingSocketsDebugOutputType_Verbose, DebugOutput);
+    SteamNetworkingUtils()->SetDebugOutputFunction(k_ESteamNetworkingSocketsDebugOutputType_Msg, DebugOutput);
+
+    SteamDatagramErrMsg errMsg;
+    if (!GameNetworkingSockets_Init(NULL, errMsg)) {
+        fprintf(stderr, "GameNetworkingSockets_Init failed. %s", errMsg);
+        return 1;
+    }
+
+    GameNetworkingSockets_Kill();
+    return 0;
+}