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