| 224 | } |
| 225 | |
| 226 | bool Socket::ConnectBlocking( const char* addr, uint16_t port ) |
| 227 | { |
| 228 | assert( !IsValid() ); |
| 229 | assert( !m_ptr ); |
| 230 | |
| 231 | struct addrinfo hints; |
| 232 | struct addrinfo *res, *ptr; |
| 233 | |
| 234 | memset( &hints, 0, sizeof( hints ) ); |
| 235 | hints.ai_family = AF_UNSPEC; |
| 236 | hints.ai_socktype = SOCK_STREAM; |
| 237 | |
| 238 | char portbuf[32]; |
| 239 | sprintf( portbuf, "%" PRIu16, port ); |
| 240 | |
| 241 | if( getaddrinfo( addr, portbuf, &hints, &res ) != 0 ) return false; |
| 242 | int sock = 0; |
| 243 | for( ptr = res; ptr; ptr = ptr->ai_next ) |
| 244 | { |
| 245 | if( ( sock = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol ) ) == -1 ) continue; |
| 246 | #if defined __APPLE__ |
| 247 | int val = 1; |
| 248 | setsockopt( sock, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof( val ) ); |
| 249 | #endif |
| 250 | if( connect( sock, ptr->ai_addr, ptr->ai_addrlen ) == -1 ) |
| 251 | { |
| 252 | #ifdef _WIN32 |
| 253 | closesocket( sock ); |
| 254 | #else |
| 255 | close( sock ); |
| 256 | #endif |
| 257 | continue; |
| 258 | } |
| 259 | break; |
| 260 | } |
| 261 | freeaddrinfo( res ); |
| 262 | if( !ptr ) return false; |
| 263 | |
| 264 | m_sock.store( sock, std::memory_order_relaxed ); |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | void Socket::Close() |
| 269 | { |