| 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 |
template<class C> |
| 30 |
bool ReceiveOnSocket( Socket& s , Pointer( C ) data , size_t dataSize ) |
| 31 |
{ |
| 32 |
#ifdef ARRAY_DEBUG |
| 33 |
if( dataSize>data.maximum()*sizeof( C ) ) MK_THROW( "Size of socket read exceeds source maximum: " , dataSize , " > " , data.maximum()*sizeof( C ) ); |
| 34 |
#endif // ARRAY_DEBUG |
| 35 |
unsigned long long rec=0; |
| 36 |
while( rec!=dataSize ) |
| 37 |
{ |
| 38 |
int tmp = socket_receive( s , ( ( Pointer( char ) ) data) + rec , dataSize-rec ); |
| 39 |
if( tmp<=0 ) |
| 40 |
{ |
| 41 |
if( !tmp ) MK_THROW( "Connection Closed" ); |
| 42 |
else MK_THROW( "socket_receive from client failed: " , LastSocketError() ); |
| 43 |
return false; |
| 44 |
} |
| 45 |
rec+=tmp; |
| 46 |
} |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
template<class C> |
| 51 |
bool SendOnSocket( Socket& s , ConstPointer( C ) data , size_t dataSize ) |
| 52 |
{ |
| 53 |
#ifdef ARRAY_DEBUG |
| 54 |
if( dataSize>data.maximum()*sizeof( C ) ) MK_THROW( "Size of socket write exceeds source maximum: " , dataSize , " > " , data.maximum()*sizeof( C ) ); |
| 55 |
#endif // ARRAY_DEBUG |
| 56 |
if( socket_send( s , ( ConstPointer( char ) )data , dataSize )<0 ) |
| 57 |
{ |
| 58 |
MK_THROW( "socket_send to client failed (" , s , "): " , LastSocketError() ); |
| 59 |
return false; |
| 60 |
} |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
template<class C> |
| 65 |
bool SendOnSocket( Socket& s , Pointer( C ) data , size_t dataSize ){ return SendOnSocket( ( ConstPointer( C ) )data , dataSize ); } |
| 66 |
|
| 67 |
template<class C> |
| 68 |
void ReceiveOnSocket( Socket& s , Pointer( C ) data , size_t dataSize , const char* errorMessage , ... ) |
| 69 |
{ |
| 70 |
#ifdef ARRAY_DEBUG |
| 71 |
if( dataSize>data.maximum()*sizeof( C ) ) MK_THROW( "Size of socket read exceeds source maximum: " , dataSize , " > " , data.maximum()*sizeof( C ) ); |
| 72 |
#endif // ARRAY_DEBUG |
| 73 |
unsigned long long rec=0; |
| 74 |
while( rec!=dataSize ) |
| 75 |
{ |
| 76 |
int tmp = socket_receive( s , ( ( Pointer( char ) ) data) + rec , dataSize-rec ); |
| 77 |
if( tmp<=0 ) |
| 78 |
{ |
| 79 |
if( !tmp ) MK_THROW( "Connection Closed" ); |
| 80 |
else MK_THROW( "socket_receive from client failed: " , LastSocketError() ); |
| 81 |
{ |
| 82 |
fprintf( stderr , "\t" ); |
| 83 |
va_list args; |
| 84 |
va_start( args , errorMessage ); |
| 85 |
vfprintf( stderr , errorMessage , args ); |
| 86 |
va_end( args ); |
| 87 |
fprintf( stderr , "\n" ); |
| 88 |
} |
| 89 |
exit( EXIT_FAILURE ); |
| 90 |
} |
| 91 |
rec+=tmp; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
template<class C> |
| 96 |
void SendOnSocket( Socket& s , ConstPointer( C ) data , size_t dataSize , const char* errorMessage , ... ) |
| 97 |
{ |
| 98 |
#ifdef ARRAY_DEBUG |
| 99 |
if( dataSize>data.maximum()*sizeof( C ) ) MK_THROW( "Size of socket write exceeds source maximum: " , dataSize , " > " , data.maximum()*sizeof( C ) ); |
| 100 |
#endif // ARRAY_DEBUG |
| 101 |
if( socket_send( s , ( ConstPointer( char ) )data , dataSize )<0 ) |
| 102 |
MK_THROW( "socket_send to client failed: " , LastSocketError() ); |
| 103 |
} |
| 104 |
|
| 105 |
template<class C> |
| 106 |
void SendOnSocket( Socket& s , Pointer( C ) data , size_t dataSize , const char* errorMessage , ... ) |
| 107 |
{ |
| 108 |
#ifdef ARRAY_DEBUG |
| 109 |
if( dataSize>data.maximum()*sizeof( C ) ) MK_THROW( "Size of socket write exceeds source maximum: " , dataSize , " > " , data.maximum()*sizeof( C ) ); |
| 110 |
#endif // ARRAY_DEBUG |
| 111 |
if( socket_send( s , ( ConstPointer( char ) )data , dataSize )<0 ) |
| 112 |
MK_THROW( "socket_send to client failed: " , LastSocketError() ); |
| 113 |
} |
| 114 |
|
| 115 |
inline bool GetHostEndpointAddress( EndpointAddress* address , const char* prefix ) |
| 116 |
{ |
| 117 |
boost::asio::ip::tcp::resolver resolver( io_service ); |
| 118 |
boost::asio::ip::tcp::resolver::query query( boost::asio::ip::host_name() , std::string( "" ) , boost::asio::ip::resolver_query_base::numeric_service ); |
| 119 |
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve( query ) , end; |
| 120 |
for( int count=0 ; iterator!=end ; ) |
| 121 |
{ |
| 122 |
if( (*iterator).endpoint().address().is_v4() ) |
| 123 |
{ |
| 124 |
std::string addrss_string = (*iterator).endpoint().address().to_string(); |
| 125 |
const char* _address = addrss_string.c_str(); |
| 126 |
if( !prefix || strstr( _address , prefix ) ) |
| 127 |
{ |
| 128 |
*address = (*iterator).endpoint().address(); |
| 129 |
return true; |
| 130 |
} |
| 131 |
} |
| 132 |
iterator++; |
| 133 |
} |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
inline bool GetHostAddress( char* address , const char* prefix ) |
| 138 |
{ |
| 139 |
EndpointAddress _address; |
| 140 |
if( !GetHostEndpointAddress( &_address , prefix ) ) return false; |
| 141 |
strcpy( address , _address.to_string().c_str() ); |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
inline int GetLocalSocketPort( Socket& s ) |
| 146 |
{ |
| 147 |
return s->local_endpoint().port(); |
| 148 |
} |
| 149 |
|
| 150 |
inline EndpointAddress GetLocalSocketEndpointAddress( Socket& s ) |
| 151 |
{ |
| 152 |
return s->local_endpoint().address(); |
| 153 |
} |
| 154 |
|
| 155 |
inline int GetPeerSocketPort( Socket& s ) |
| 156 |
{ |
| 157 |
return s->remote_endpoint().port(); |
| 158 |
} |
| 159 |
|
| 160 |
inline EndpointAddress GetPeerSocketEndpointAddress( Socket& s ) |
| 161 |
{ |
| 162 |
return s->remote_endpoint().address(); |
| 163 |
} |
| 164 |
|
| 165 |
inline Socket GetConnectSocket( const char* address , int port , int ms , bool progress ) |
| 166 |
{ |
| 167 |
char _port[128]; |
| 168 |
sprintf( _port , "%d" , port ); |
| 169 |
boost::asio::ip::tcp::resolver resolver( io_service ); |
| 170 |
boost::asio::ip::tcp::resolver::query query( address , _port ); |
| 171 |
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve( query ); |
| 172 |
Socket s = new boost::asio::ip::tcp::socket( io_service ); |
| 173 |
boost::system::error_code ec; |
| 174 |
long long sleepCount = 0; |
| 175 |
do |
| 176 |
{ |
| 177 |
boost::asio::connect( *s , resolver.resolve(query) , ec ); |
| 178 |
sleepCount++; |
| 179 |
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) ); |
| 180 |
if( progress && !(sleepCount%ms) ) printf( "." ); |
| 181 |
} |
| 182 |
while( ec ); |
| 183 |
if( progress ) printf( "\n" ) , fflush( stdout ); |
| 184 |
return s; |
| 185 |
} |
| 186 |
|
| 187 |
inline Socket GetConnectSocket( EndpointAddress address , int port , int ms , bool progress ) |
| 188 |
{ |
| 189 |
char _port[128]; |
| 190 |
sprintf( _port , "%d" , port ); |
| 191 |
boost::asio::ip::tcp::resolver resolver( io_service ); |
| 192 |
boost::asio::ip::tcp::resolver::query query( address.to_string().c_str() , _port ); |
| 193 |
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve( query ); |
| 194 |
Socket s = new boost::asio::ip::tcp::socket( io_service ); |
| 195 |
boost::system::error_code ec; |
| 196 |
long long sleepCount = 0; |
| 197 |
do |
| 198 |
{ |
| 199 |
boost::asio::connect( *s , resolver.resolve(query) , ec ); |
| 200 |
sleepCount++; |
| 201 |
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) ); |
| 202 |
if( progress && !(sleepCount%ms) ) std::cout << "."; |
| 203 |
} |
| 204 |
while( ec ); |
| 205 |
if( progress ) std::cout << std::endl; |
| 206 |
return s; |
| 207 |
} |
| 208 |
|
| 209 |
inline Socket AcceptSocket( AcceptorSocket listen ) |
| 210 |
{ |
| 211 |
Socket s = new boost::asio::ip::tcp::socket( io_service ); |
| 212 |
listen->accept( *s ); |
| 213 |
return s; |
| 214 |
} |
| 215 |
|
| 216 |
inline AcceptorSocket GetListenSocket( int &port ) |
| 217 |
{ |
| 218 |
AcceptorSocket s = new boost::asio::ip::tcp::acceptor( io_service , boost::asio::ip::tcp::endpoint( boost::asio::ip::tcp::v4() , port ) ); |
| 219 |
port = s->local_endpoint().port(); |
| 220 |
return s; |
| 221 |
} |
| 222 |
|
| 223 |
inline void CloseSocket( Socket& s ) |
| 224 |
{ |
| 225 |
delete s; |
| 226 |
s = _INVALID_SOCKET_; |
| 227 |
} |
| 228 |
|
| 229 |
inline void CloseAcceptorSocket( AcceptorSocket& s ) |
| 230 |
{ |
| 231 |
delete s; |
| 232 |
s = _INVALID_ACCEPTOR_SOCKET_; |
| 233 |
} |