NetworkingUtils.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. oscpack -- Open Sound Control packet manipulation library
  3. http://www.audiomulch.com/~rossb/oscpack
  4. Copyright (c) 2004-2005 Ross Bencina <[email protected]>
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. Any person wishing to distribute modifications to the Software is
  15. requested to send the modifications to the original developer so that
  16. they can be incorporated into the canonical version.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  21. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  22. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "ip/NetworkingUtils.h"
  26. #include <winsock2.h> // this must come first to prevent errors with MSVC7
  27. #include <windows.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. static LONG initCount_ = 0;
  31. static bool winsockInitialized_ = false;
  32. NetworkInitializer::NetworkInitializer()
  33. {
  34. if( InterlockedIncrement( &initCount_ ) == 1 ){
  35. // there is a race condition here if one thread tries to access
  36. // the library while another is still initializing it.
  37. // i can't think of an easy way to fix it so i'm telling you here
  38. // incase you need to init the library from two threads at once.
  39. // this is why the header file advises to instantiate one of these
  40. // in main() so that the initialization happens globally
  41. // initialize winsock
  42. WSAData wsaData;
  43. int nCode = WSAStartup(MAKEWORD(1, 1), &wsaData);
  44. if( nCode != 0 ){
  45. //std::cout << "WSAStartup() failed with error code " << nCode << "\n";
  46. }else{
  47. winsockInitialized_ = true;
  48. }
  49. }
  50. }
  51. NetworkInitializer::~NetworkInitializer()
  52. {
  53. if( InterlockedDecrement( &initCount_ ) == 0 ){
  54. if( winsockInitialized_ ){
  55. WSACleanup();
  56. winsockInitialized_ = false;
  57. }
  58. }
  59. }
  60. unsigned long GetHostByName( const char *name )
  61. {
  62. NetworkInitializer networkInitializer;
  63. unsigned long result = 0;
  64. struct hostent *h = gethostbyname( name );
  65. if( h ){
  66. struct in_addr a;
  67. memcpy( &a, h->h_addr_list[0], h->h_length );
  68. result = ntohl(a.s_addr);
  69. }
  70. return result;
  71. }