WSAStartupSingleton.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * 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. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "WSAStartupSingleton.h"
  11. #if defined(_WIN32) && !defined(WINDOWS_STORE_RT)
  12. #include <winsock2.h>
  13. #include <ws2tcpip.h>
  14. #endif
  15. #include "RakNetDefines.h"
  16. #include <stdio.h>
  17. int WSAStartupSingleton::refCount=0;
  18. WSAStartupSingleton::WSAStartupSingleton() {}
  19. WSAStartupSingleton::~WSAStartupSingleton() {}
  20. void WSAStartupSingleton::AddRef(void)
  21. {
  22. #if defined(_WIN32) && !defined(WINDOWS_STORE_RT)
  23. refCount++;
  24. if (refCount!=1)
  25. return;
  26. WSADATA winsockInfo;
  27. if ( WSAStartup( MAKEWORD( 2, 2 ), &winsockInfo ) != 0 )
  28. {
  29. #if defined(_DEBUG) && !defined(WINDOWS_PHONE_8)
  30. DWORD dwIOError = GetLastError();
  31. LPVOID messageBuffer;
  32. FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  33. NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language
  34. ( LPTSTR ) & messageBuffer, 0, NULL );
  35. // something has gone wrong here...
  36. RAKNET_DEBUG_PRINTF( "WSAStartup failed:Error code - %d\n%s", dwIOError, messageBuffer );
  37. //Free the buffer.
  38. LocalFree( messageBuffer );
  39. #endif
  40. }
  41. #endif
  42. }
  43. void WSAStartupSingleton::Deref(void)
  44. {
  45. #if defined(_WIN32) && !defined(WINDOWS_STORE_RT)
  46. if (refCount==0)
  47. return;
  48. if (refCount>1)
  49. {
  50. refCount--;
  51. return;
  52. }
  53. WSACleanup();
  54. refCount=0;
  55. #endif
  56. }
粤ICP备19079148号