SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WSAStartupSingleton.cpp
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2016-2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
16 #include "slikenet/WSAStartupSingleton.h"
17
18
19
20
21
22 #if defined(_WIN32) && !defined(WINDOWS_STORE_RT)
23 #include <winsock2.h>
24 #include <ws2tcpip.h>
25
26
27
28
29
30 #endif
31 #include "slikenet/defines.h"
32 #include <stdio.h>
33
34 #ifdef _WIN32
35 #include <tchar.h>
36 #else
37 #ifndef _T
38 #define _T(x) (x)
39 #endif
40 #endif
41
42 int WSAStartupSingleton::refCount=0;
43
44 WSAStartupSingleton::WSAStartupSingleton() {}
45 WSAStartupSingleton::~WSAStartupSingleton() {}
46 void WSAStartupSingleton::AddRef(void)
47 {
48 #if defined(_WIN32) && !defined(WINDOWS_STORE_RT)
49
50 refCount++;
51
52 if (refCount!=1)
53 return;
54
55
56
57
58
59 WSADATA winsockInfo;
60 if ( WSAStartup( MAKEWORD( 2, 2 ), &winsockInfo ) != 0 )
61 {
62 #if defined(_DEBUG) && !defined(WINDOWS_PHONE_8)
63 DWORD dwIOError = GetLastError();
64 LPTSTR messageBuffer;
65 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
66 NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
67 ( LPTSTR ) & messageBuffer, 0, NULL );
68 // something has gone wrong here...
69 RAKNET_DEBUG_TPRINTF( _T("WSAStartup failed:Error code - %lu\n%s"), dwIOError, messageBuffer );
70 //Free the buffer.
71 LocalFree( messageBuffer );
72 #endif
73 }
74
75 #endif
76 }
77 void WSAStartupSingleton::Deref(void)
78 {
79 #if defined(_WIN32) && !defined(WINDOWS_STORE_RT)
80 if (refCount==0)
81 return;
82
83 if (refCount>1)
84 {
85 refCount--;
86 return;
87 }
88
89 WSACleanup();
90
91
92
93
94
95
96 refCount=0;
97 #endif
98 }