2 // detail/winsock_init.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_WINSOCK_INIT_HPP
12 #define BOOST_ASIO_DETAIL_WINSOCK_INIT_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
22 #include <boost/asio/detail/push_options.hpp>
28 class winsock_init_base
31 // Structure to track result of initialisation and number of uses. POD is used
32 // to ensure that the values are zero-initialised prior to any code being run.
39 BOOST_ASIO_DECL static void startup(data& d,
40 unsigned char major, unsigned char minor);
42 BOOST_ASIO_DECL static void manual_startup(data& d);
44 BOOST_ASIO_DECL static void cleanup(data& d);
46 BOOST_ASIO_DECL static void manual_cleanup(data& d);
48 BOOST_ASIO_DECL static void throw_on_error(data& d);
51 template <int Major = 2, int Minor = 0>
52 class winsock_init : private winsock_init_base
55 winsock_init(bool allow_throw = true)
57 startup(data_, Major, Minor);
59 throw_on_error(data_);
62 winsock_init(const winsock_init&)
64 startup(data_, Major, Minor);
65 throw_on_error(data_);
73 // This class may be used to indicate that user code will manage Winsock
74 // initialisation and cleanup. This may be required in the case of a DLL, for
75 // example, where it is not safe to initialise Winsock from global object
78 // To prevent asio from initialising Winsock, the object must be constructed
79 // before any Asio's own global objects. With MSVC, this may be accomplished
80 // by adding the following code to the DLL:
82 // #pragma warning(push)
83 // #pragma warning(disable:4073)
84 // #pragma init_seg(lib)
85 // boost::asio::detail::winsock_init<>::manual manual_winsock_init;
86 // #pragma warning(pop)
92 manual_startup(data_);
97 manual_startup(data_);
102 manual_cleanup(data_);
111 template <int Major, int Minor>
112 winsock_init_base::data winsock_init<Major, Minor>::data_;
114 // Static variable to ensure that winsock is initialised before main, and
115 // therefore before any other threads can get started.
116 static const winsock_init<>& winsock_init_instance = winsock_init<>(false);
118 } // namespace detail
122 #include <boost/asio/detail/pop_options.hpp>
124 #if defined(BOOST_ASIO_HEADER_ONLY)
125 # include <boost/asio/detail/impl/winsock_init.ipp>
126 #endif // defined(BOOST_ASIO_HEADER_ONLY)
128 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
130 #endif // BOOST_ASIO_DETAIL_WINSOCK_INIT_HPP