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