You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.2KB

  1. /*
  2. oscpack -- Open Sound Control (OSC) packet manipulation library
  3. http://www.rossbencina.com/code/oscpack
  4. Copyright (c) 2004-2013 Ross Bencina <rossb@audiomulch.com>
  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. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  18. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. /*
  23. The text above constitutes the entire oscpack license; however,
  24. the oscpack developer(s) also make the following non-binding requests:
  25. Any person wishing to distribute modifications to the Software is
  26. requested to send the modifications to the original developer so that
  27. they can be incorporated into the canonical version. It is also
  28. requested that these non-binding requests be included whenever the
  29. above license is reproduced.
  30. */
  31. #include "../NetworkingUtils.h"
  32. #include <winsock2.h> // this must come first to prevent errors with MSVC7
  33. #include <windows.h>
  34. #include <cstring>
  35. static LONG initCount_ = 0;
  36. static bool winsockInitialized_ = false;
  37. NetworkInitializer::NetworkInitializer()
  38. {
  39. if( InterlockedIncrement( &initCount_ ) == 1 ){
  40. // there is a race condition here if one thread tries to access
  41. // the library while another is still initializing it.
  42. // i can't think of an easy way to fix it so i'm telling you here
  43. // incase you need to init the library from two threads at once.
  44. // this is why the header file advises to instantiate one of these
  45. // in main() so that the initialization happens globally
  46. // initialize winsock
  47. WSAData wsaData;
  48. int nCode = WSAStartup(MAKEWORD(1, 1), &wsaData);
  49. if( nCode != 0 ){
  50. //std::cout << "WSAStartup() failed with error code " << nCode << "\n";
  51. }else{
  52. winsockInitialized_ = true;
  53. }
  54. }
  55. }
  56. NetworkInitializer::~NetworkInitializer()
  57. {
  58. if( InterlockedDecrement( &initCount_ ) == 0 ){
  59. if( winsockInitialized_ ){
  60. WSACleanup();
  61. winsockInitialized_ = false;
  62. }
  63. }
  64. }
  65. unsigned long GetHostByName( const char *name )
  66. {
  67. NetworkInitializer networkInitializer;
  68. unsigned long result = 0;
  69. struct hostent *h = gethostbyname( name );
  70. if( h ){
  71. struct in_addr a;
  72. std::memcpy( &a, h->h_addr_list[0], h->h_length );
  73. result = ntohl(a.s_addr);
  74. }
  75. return result;
  76. }