annotate external/oscpack/ip/win32/NetworkingUtils.cpp @ 509:0284d2152e17

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