| 1 |
/* |
| 2 |
Copyright (c) 2008, Michael Kazhdan |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#ifndef SOCKET_INCLUDED |
| 30 |
#define SOCKET_INCLUDED |
| 31 |
|
| 32 |
#ifdef _WIN32 |
| 33 |
#ifndef _WIN32_WINNT |
| 34 |
#define _WIN32_WINNT 0x0601 |
| 35 |
#endif // _WIN32_WINNT |
| 36 |
#endif // _WIN32 |
| 37 |
|
| 38 |
#include <iostream> |
| 39 |
#include <boost/asio.hpp> |
| 40 |
#include <stdarg.h> |
| 41 |
#include <thread> |
| 42 |
#include "Array.h" |
| 43 |
#include "MyMiscellany.h" |
| 44 |
#include "Streams.h" |
| 45 |
|
| 46 |
namespace PoissonRecon |
| 47 |
{ |
| 48 |
|
| 49 |
static const unsigned int SOCKET_CONNECT_WAIT = 500; // Default time to wait on a socket (in ms) |
| 50 |
|
| 51 |
typedef boost::asio::ip::tcp::socket *Socket; |
| 52 |
typedef boost::asio::ip::tcp::acceptor *AcceptorSocket; |
| 53 |
typedef boost::asio::ip::address EndpointAddress; |
| 54 |
const Socket _INVALID_SOCKET_ = (Socket)NULL; |
| 55 |
const AcceptorSocket _INVALID_ACCEPTOR_SOCKET_ = (AcceptorSocket)NULL; |
| 56 |
static boost::asio::io_service io_service; |
| 57 |
|
| 58 |
template< class C > int socket_receive( Socket &s , C *destination , size_t len ) |
| 59 |
{ |
| 60 |
boost::system::error_code ec; |
| 61 |
int ret = (int)( boost::asio::read( *s , boost::asio::buffer( destination , len ) , ec ) ); |
| 62 |
if( ec ) MK_THROW( "Failed to read from socket" ); |
| 63 |
return ret; |
| 64 |
} |
| 65 |
|
| 66 |
template< class C > int socket_send( Socket& s , const C* source , size_t len ) |
| 67 |
{ |
| 68 |
boost::system::error_code ec; |
| 69 |
int ret = (int)( boost::asio::write( *s , boost::asio::buffer( source , len ) , ec ) ); |
| 70 |
if( ec ) MK_THROW( "Failed to write to socket" ); |
| 71 |
return ret; |
| 72 |
} |
| 73 |
|
| 74 |
inline bool AddressesEqual( const EndpointAddress& a1 , const EndpointAddress& a2 ){ return a1.to_string()==a2.to_string(); } |
| 75 |
inline const char *LastSocketError( void ){ return ""; } |
| 76 |
inline void PrintHostAddresses( FILE* fp ) |
| 77 |
{ |
| 78 |
boost::asio::ip::tcp::resolver resolver( io_service ); |
| 79 |
boost::asio::ip::tcp::resolver::query query( boost::asio::ip::host_name() , std::string( "" ) , boost::asio::ip::resolver_query_base::numeric_service ); |
| 80 |
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve( query ) , end; |
| 81 |
for( int count=0 ; iterator!=end ; ) |
| 82 |
{ |
| 83 |
if( (*iterator).endpoint().address().is_v4() ) fprintf( fp , "%d] %s\n" , count++ , (*iterator).endpoint().address().to_string().c_str() ); |
| 84 |
// else fprintf( fp , "%d]* %s\n" , count++ , (*iterator).endpoint().address().to_string().c_str() ); |
| 85 |
iterator++; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
#ifdef ARRAY_DEBUG |
| 90 |
template< class C > |
| 91 |
int socket_receive( Socket& s , Array< C > destination , size_t len ) |
| 92 |
{ |
| 93 |
if( len>destination.maximum()*sizeof( C ) ) |
| 94 |
MK_THROW( "Size of socket_receive exceeds destination maximum: " , len , " > " , destination.maximum()*sizeof( C ) ); |
| 95 |
return socket_receive( s , (char*)&destination[0] , len ); |
| 96 |
} |
| 97 |
template< class C > |
| 98 |
int socket_send( Socket s , ConstArray< C > source , size_t len ) |
| 99 |
{ |
| 100 |
if( len>source.maximum()*sizeof( C ) ) |
| 101 |
MK_THROW( "Size of socket_send exceeds source maximum: " , len , " > " , source.maximum()*sizeof( C ) ); |
| 102 |
return socket_send( s , (char*)&source[0] , len ); |
| 103 |
} |
| 104 |
#endif // ARRAY_DEBUG |
| 105 |
|
| 106 |
class ConnectionData |
| 107 |
{ |
| 108 |
public: |
| 109 |
EndpointAddress localAddr , peerAddr; |
| 110 |
int localPort , peerPort; |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
template< class C > bool ReceiveOnSocket( Socket &s , Pointer( C ) data , size_t dataSize ); |
| 115 |
template< class C > bool SendOnSocket ( Socket &s , ConstPointer( C ) data , size_t dataSize ); |
| 116 |
template< class C > bool SendOnSocket ( Socket &s , Pointer( C ) data , size_t dataSize ); |
| 117 |
template< class C > void ReceiveOnSocket( Socket &s , Pointer( C ) data , size_t dataSize , const char *errorMessage , ... ); |
| 118 |
template< class C > void SendOnSocket ( Socket &s , ConstPointer( C ) data , size_t dataSize , const char *errorMessage , ... ); |
| 119 |
template< class C > void SendOnSocket ( Socket &s , Pointer( C ) data , size_t dataSize , const char *errorMessage , ... ); |
| 120 |
|
| 121 |
AcceptorSocket GetListenSocket( int& port ); |
| 122 |
Socket AcceptSocket( AcceptorSocket listen ); |
| 123 |
Socket GetConnectSocket( const char* address , int port , int ms=5 , bool progress=false ); |
| 124 |
Socket GetConnectSocket( EndpointAddress , int port , int ms=5 , bool progress=false ); |
| 125 |
void CloseSocket( Socket& s ); |
| 126 |
void CloseAcceptorSocket( AcceptorSocket& s ); |
| 127 |
EndpointAddress GetLocalSocketEndpointAddress( Socket& s ); |
| 128 |
int GetLocalSocketPort ( Socket& s ); |
| 129 |
EndpointAddress GetLocalSocketEndpointAddress( Socket& s ); |
| 130 |
int GetPeerSocketPort ( Socket& s ); |
| 131 |
bool GetHostAddress( char* address , const char* prefix = NULL ); |
| 132 |
bool GetHostEndpointAddress( EndpointAddress* address , const char* prefix=NULL ); |
| 133 |
void PrintHostAddress( void ); |
| 134 |
|
| 135 |
struct SocketStream : public BinaryStream |
| 136 |
{ |
| 137 |
SocketStream( Socket socket=_INVALID_SOCKET_ ) : _socket(socket){} |
| 138 |
protected: |
| 139 |
Socket _socket; |
| 140 |
bool _read( Pointer( unsigned char ) ptr , size_t sz ){ return socket_receive( _socket , ptr , sizeof(unsigned char)*sz )==sz; } |
| 141 |
bool _write( ConstPointer( unsigned char ) ptr , size_t sz ){ return socket_send ( _socket , ptr , sizeof(unsigned char)*sz )==sz; } |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
#include "Socket.inl" |
| 146 |
} |
| 147 |
#endif // SOCKET_INCLUDED |