00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef ASIO_DETAIL_WINSOCK_INIT_HPP
00012 #define ASIO_DETAIL_WINSOCK_INIT_HPP
00013
00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00015 # pragma once
00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
00017
00018 #include "asio/detail/push_options.hpp"
00019
00020 #include "asio/detail/push_options.hpp"
00021 #include <boost/config.hpp>
00022 #include "asio/detail/pop_options.hpp"
00023
00024 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
00025
00026 #include "asio/detail/push_options.hpp"
00027 #include <boost/shared_ptr.hpp>
00028 #include <boost/throw_exception.hpp>
00029 #include "asio/detail/pop_options.hpp"
00030
00031 #include "asio/system_error.hpp"
00032 #include "asio/detail/noncopyable.hpp"
00033 #include "asio/detail/socket_types.hpp"
00034
00035 namespace asio {
00036 namespace detail {
00037
00038 template <int Major = 2, int Minor = 0>
00039 class winsock_init
00040 : private noncopyable
00041 {
00042 private:
00043
00044 struct do_init
00045 {
00046 do_init()
00047 {
00048 WSADATA wsa_data;
00049 result_ = ::WSAStartup(MAKEWORD(Major, Minor), &wsa_data);
00050 }
00051
00052 ~do_init()
00053 {
00054 ::WSACleanup();
00055 }
00056
00057 int result() const
00058 {
00059 return result_;
00060 }
00061
00062
00063
00064
00065
00066
00067 static boost::shared_ptr<do_init> instance()
00068 {
00069 static boost::shared_ptr<do_init> init(new do_init);
00070 return init;
00071 }
00072
00073 private:
00074 int result_;
00075 };
00076
00077 public:
00078
00079 winsock_init()
00080 : ref_(do_init::instance())
00081 {
00082
00083
00084
00085 if (this != &instance_ && ref_->result() != 0)
00086 {
00087 asio::system_error e(
00088 asio::error_code(ref_->result(), asio::native_ecat),
00089 "winsock");
00090 boost::throw_exception(e);
00091 }
00092 }
00093
00094
00095 ~winsock_init()
00096 {
00097 }
00098
00099 private:
00100
00101 static winsock_init instance_;
00102
00103
00104
00105 boost::shared_ptr<do_init> ref_;
00106 };
00107
00108 template <int Major, int Minor>
00109 winsock_init<Major, Minor> winsock_init<Major, Minor>::instance_;
00110
00111 }
00112 }
00113
00114 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
00115
00116 #include "asio/detail/pop_options.hpp"
00117
00118 #endif // ASIO_DETAIL_WINSOCK_INIT_HPP