Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.0KB

  1. //
  2. // detail/winsock_init.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_WINSOCK_INIT_HPP
  11. #define ASIO_DETAIL_WINSOCK_INIT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  17. #include "asio/detail/push_options.hpp"
  18. namespace asio {
  19. namespace detail {
  20. class winsock_init_base
  21. {
  22. protected:
  23. // Structure to track result of initialisation and number of uses. POD is used
  24. // to ensure that the values are zero-initialised prior to any code being run.
  25. struct data
  26. {
  27. long init_count_;
  28. long result_;
  29. };
  30. ASIO_DECL static void startup(data& d,
  31. unsigned char major, unsigned char minor);
  32. ASIO_DECL static void manual_startup(data& d);
  33. ASIO_DECL static void cleanup(data& d);
  34. ASIO_DECL static void manual_cleanup(data& d);
  35. ASIO_DECL static void throw_on_error(data& d);
  36. };
  37. template <int Major = 2, int Minor = 0>
  38. class winsock_init : private winsock_init_base
  39. {
  40. public:
  41. winsock_init(bool allow_throw = true)
  42. {
  43. startup(data_, Major, Minor);
  44. if (allow_throw)
  45. throw_on_error(data_);
  46. }
  47. winsock_init(const winsock_init&)
  48. {
  49. startup(data_, Major, Minor);
  50. throw_on_error(data_);
  51. }
  52. ~winsock_init()
  53. {
  54. cleanup(data_);
  55. }
  56. // This class may be used to indicate that user code will manage Winsock
  57. // initialisation and cleanup. This may be required in the case of a DLL, for
  58. // example, where it is not safe to initialise Winsock from global object
  59. // constructors.
  60. //
  61. // To prevent asio from initialising Winsock, the object must be constructed
  62. // before any Asio's own global objects. With MSVC, this may be accomplished
  63. // by adding the following code to the DLL:
  64. //
  65. // #pragma warning(push)
  66. // #pragma warning(disable:4073)
  67. // #pragma init_seg(lib)
  68. // asio::detail::winsock_init<>::manual manual_winsock_init;
  69. // #pragma warning(pop)
  70. class manual
  71. {
  72. public:
  73. manual()
  74. {
  75. manual_startup(data_);
  76. }
  77. manual(const manual&)
  78. {
  79. manual_startup(data_);
  80. }
  81. ~manual()
  82. {
  83. manual_cleanup(data_);
  84. }
  85. };
  86. private:
  87. friend class manual;
  88. static data data_;
  89. };
  90. template <int Major, int Minor>
  91. winsock_init_base::data winsock_init<Major, Minor>::data_;
  92. // Static variable to ensure that winsock is initialised before main, and
  93. // therefore before any other threads can get started.
  94. static const winsock_init<>& winsock_init_instance = winsock_init<>(false);
  95. } // namespace detail
  96. } // namespace asio
  97. #include "asio/detail/pop_options.hpp"
  98. #if defined(ASIO_HEADER_ONLY)
  99. # include "asio/detail/impl/winsock_init.ipp"
  100. #endif // defined(ASIO_HEADER_ONLY)
  101. #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  102. #endif // ASIO_DETAIL_WINSOCK_INIT_HPP