1 | // |
2 | // Net.cpp |
3 | // |
4 | // Library: Net |
5 | // Package: NetCore |
6 | // Module: NetCore |
7 | // |
8 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Net/Net.h" |
16 | |
17 | |
18 | #include "Poco/Net/SocketDefs.h" |
19 | #include "Poco/Net/NetException.h" |
20 | |
21 | |
22 | namespace Poco { |
23 | namespace Net { |
24 | |
25 | |
26 | void Net_API initializeNetwork() |
27 | { |
28 | #if defined(POCO_OS_FAMILY_WINDOWS) |
29 | WORD version = MAKEWORD(2, 2); |
30 | WSADATA data; |
31 | if (WSAStartup(version, &data) != 0) |
32 | throw NetException("Failed to initialize network subsystem" ); |
33 | #endif |
34 | } |
35 | |
36 | |
37 | void Net_API uninitializeNetwork() |
38 | { |
39 | #if defined(POCO_OS_FAMILY_WINDOWS) |
40 | WSACleanup(); |
41 | #endif |
42 | } |
43 | |
44 | |
45 | } } // namespace Poco::Net |
46 | |
47 | |
48 | #if defined(POCO_OS_FAMILY_WINDOWS) && !defined(POCO_NO_AUTOMATIC_LIB_INIT) |
49 | |
50 | struct NetworkInitializer |
51 | /// Network initializer for windows statically |
52 | /// linked library. |
53 | { |
54 | NetworkInitializer() |
55 | /// Calls Poco::Net::initializeNetwork(); |
56 | { |
57 | Poco::Net::initializeNetwork(); |
58 | } |
59 | |
60 | ~NetworkInitializer() |
61 | /// Calls Poco::Net::uninitializeNetwork(); |
62 | { |
63 | try |
64 | { |
65 | Poco::Net::uninitializeNetwork(); |
66 | } |
67 | catch (...) |
68 | { |
69 | poco_unexpected(); |
70 | } |
71 | } |
72 | }; |
73 | |
74 | const NetworkInitializer pocoNetworkInitializer; |
75 | |
76 | #endif |
77 | |