diff options
Diffstat (limited to 'src/stable/tcpsocket.cpp')
| -rw-r--r-- | src/stable/tcpsocket.cpp | 554 |
1 files changed, 277 insertions, 277 deletions
diff --git a/src/stable/tcpsocket.cpp b/src/stable/tcpsocket.cpp index fa79a36..b77f439 100644 --- a/src/stable/tcpsocket.cpp +++ b/src/stable/tcpsocket.cpp | |||
| @@ -32,392 +32,392 @@ | |||
| 32 | namespace Bu { subExceptionDef( TcpSocketException ) } | 32 | namespace Bu { subExceptionDef( TcpSocketException ) } |
| 33 | 33 | ||
| 34 | Bu::TcpSocket::TcpSocket( handle nTcpSocket ) : | 34 | Bu::TcpSocket::TcpSocket( handle nTcpSocket ) : |
| 35 | nTcpSocket( nTcpSocket ), | 35 | nTcpSocket( nTcpSocket ), |
| 36 | bActive( true ), | 36 | bActive( true ), |
| 37 | bBlocking( true ) | 37 | bBlocking( true ) |
| 38 | { | 38 | { |
| 39 | #ifdef WIN32 | 39 | #ifdef WIN32 |
| 40 | Bu::Winsock2::getInstance(); | 40 | Bu::Winsock2::getInstance(); |
| 41 | #endif | 41 | #endif |
| 42 | setAddress(); | 42 | setAddress(); |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | Bu::TcpSocket::TcpSocket( const Bu::String &sAddr, int nPort, int nTimeout, | 45 | Bu::TcpSocket::TcpSocket( const Bu::String &sAddr, int nPort, int nTimeout, |
| 46 | bool bBlocking ) : | 46 | bool bBlocking ) : |
| 47 | nTcpSocket( 0 ), | 47 | nTcpSocket( 0 ), |
| 48 | bActive( false ), | 48 | bActive( false ), |
| 49 | bBlocking( true ) | 49 | bBlocking( true ) |
| 50 | { | 50 | { |
| 51 | #ifdef WIN32 | 51 | #ifdef WIN32 |
| 52 | Bu::Winsock2::getInstance(); | 52 | Bu::Winsock2::getInstance(); |
| 53 | #endif | 53 | #endif |
| 54 | 54 | ||
| 55 | /* Create the socket. */ | 55 | /* Create the socket. */ |
| 56 | nTcpSocket = bu_socket( PF_INET, SOCK_STREAM, 0 ); | 56 | nTcpSocket = bu_socket( PF_INET, SOCK_STREAM, 0 ); |
| 57 | 57 | ||
| 58 | #ifdef WIN32 | 58 | #ifdef WIN32 |
| 59 | if( nTcpSocket == INVALID_SOCKET ) | 59 | if( nTcpSocket == INVALID_SOCKET ) |
| 60 | #else | 60 | #else |
| 61 | if( nTcpSocket < 0 ) | 61 | if( nTcpSocket < 0 ) |
| 62 | #endif | 62 | #endif |
| 63 | { | 63 | { |
| 64 | throw ExceptionBase("Couldn't create socket.\n"); | 64 | throw ExceptionBase("Couldn't create socket.\n"); |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | setBlocking( false ); | 67 | setBlocking( false ); |
| 68 | 68 | ||
| 69 | /* Connect to the server. */ | 69 | /* Connect to the server. */ |
| 70 | //printf("Resolving hostname (%s)...\n", sAddr ); | 70 | //printf("Resolving hostname (%s)...\n", sAddr ); |
| 71 | { | 71 | { |
| 72 | struct addrinfo *pAddr = NULL; | 72 | struct addrinfo *pAddr = NULL; |
| 73 | struct addrinfo aiHints; | 73 | struct addrinfo aiHints; |
| 74 | memset( &aiHints, 0, sizeof(addrinfo) ); | 74 | memset( &aiHints, 0, sizeof(addrinfo) ); |
| 75 | aiHints.ai_flags = AI_CANONNAME; | 75 | aiHints.ai_flags = AI_CANONNAME; |
| 76 | aiHints.ai_family = AF_INET; | 76 | aiHints.ai_family = AF_INET; |
| 77 | aiHints.ai_socktype = SOCK_STREAM; | 77 | aiHints.ai_socktype = SOCK_STREAM; |
| 78 | char ibuf[10]; | 78 | char ibuf[10]; |
| 79 | sprintf( ibuf, "%d", nPort ); | 79 | sprintf( ibuf, "%d", nPort ); |
| 80 | 80 | ||
| 81 | int ret; | 81 | int ret; |
| 82 | if( (ret = bu_getaddrinfo( | 82 | if( (ret = bu_getaddrinfo( |
| 83 | sAddr.getStr(), ibuf, &aiHints, &pAddr )) != 0 ) | 83 | sAddr.getStr(), ibuf, &aiHints, &pAddr )) != 0 ) |
| 84 | { | 84 | { |
| 85 | close(); | 85 | close(); |
| 86 | throw Bu::TcpSocketException("Couldn't resolve hostname %s (%s).\n", | 86 | throw Bu::TcpSocketException("Couldn't resolve hostname %s (%s).\n", |
| 87 | sAddr.getStr(), bu_gai_strerror(ret)); | 87 | sAddr.getStr(), bu_gai_strerror(ret)); |
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | bu_connect( | 90 | bu_connect( |
| 91 | nTcpSocket, | 91 | nTcpSocket, |
| 92 | pAddr->ai_addr, | 92 | pAddr->ai_addr, |
| 93 | pAddr->ai_addrlen | 93 | pAddr->ai_addrlen |
| 94 | ); | 94 | ); |
| 95 | 95 | ||
| 96 | sAddress = pAddr->ai_canonname; | 96 | sAddress = pAddr->ai_canonname; |
| 97 | 97 | ||
| 98 | bu_freeaddrinfo( pAddr ); | 98 | bu_freeaddrinfo( pAddr ); |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | bActive = true; | 101 | bActive = true; |
| 102 | 102 | ||
| 103 | if( nTimeout > 0 ) | 103 | if( nTimeout > 0 ) |
| 104 | { | 104 | { |
| 105 | fd_set rfds, wfds, efds; | 105 | fd_set rfds, wfds, efds; |
| 106 | int retval; | 106 | int retval; |
| 107 | 107 | ||
| 108 | FD_ZERO(&rfds); | 108 | FD_ZERO(&rfds); |
| 109 | FD_SET(nTcpSocket, &rfds); | 109 | FD_SET(nTcpSocket, &rfds); |
| 110 | FD_ZERO(&wfds); | 110 | FD_ZERO(&wfds); |
| 111 | FD_SET(nTcpSocket, &wfds); | 111 | FD_SET(nTcpSocket, &wfds); |
| 112 | FD_ZERO(&efds); | 112 | FD_ZERO(&efds); |
| 113 | FD_SET(nTcpSocket, &efds); | 113 | FD_SET(nTcpSocket, &efds); |
| 114 | 114 | ||
| 115 | struct timeval tv; | 115 | struct timeval tv; |
| 116 | tv.tv_sec = nTimeout; | 116 | tv.tv_sec = nTimeout; |
| 117 | tv.tv_usec = 0; | 117 | tv.tv_usec = 0; |
| 118 | 118 | ||
| 119 | retval = bu_select( nTcpSocket+1, &rfds, &wfds, &efds, &tv ); | 119 | retval = bu_select( nTcpSocket+1, &rfds, &wfds, &efds, &tv ); |
| 120 | 120 | ||
| 121 | if( retval == 0 ) | 121 | if( retval == 0 ) |
| 122 | { | 122 | { |
| 123 | close(); | 123 | close(); |
| 124 | throw ExceptionBase("Connection timeout.\n"); | 124 | throw ExceptionBase("Connection timeout.\n"); |
| 125 | } | 125 | } |
| 126 | read( NULL, 0 ); // See if we can get any errors out of the way early. | 126 | read( NULL, 0 ); // See if we can get any errors out of the way early. |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | if( bBlocking ) | 129 | if( bBlocking ) |
| 130 | setBlocking( bBlocking ); | 130 | setBlocking( bBlocking ); |
| 131 | } | 131 | } |
| 132 | 132 | ||
| 133 | Bu::TcpSocket::~TcpSocket() | 133 | Bu::TcpSocket::~TcpSocket() |
| 134 | { | 134 | { |
| 135 | close(); | 135 | close(); |
| 136 | } | 136 | } |
| 137 | 137 | ||
| 138 | void Bu::TcpSocket::close() | 138 | void Bu::TcpSocket::close() |
| 139 | { | 139 | { |
| 140 | if( bActive ) | 140 | if( bActive ) |
| 141 | { | 141 | { |
| 142 | #ifndef WIN32 | 142 | #ifndef WIN32 |
| 143 | fsync( nTcpSocket ); | 143 | fsync( nTcpSocket ); |
| 144 | #endif | 144 | #endif |
| 145 | #ifdef WIN32 | 145 | #ifdef WIN32 |
| 146 | #ifndef SHUT_RDWR | 146 | #ifndef SHUT_RDWR |
| 147 | #define SHUT_RDWR (SD_BOTH) | 147 | #define SHUT_RDWR (SD_BOTH) |
| 148 | #endif | 148 | #endif |
| 149 | #endif | 149 | #endif |
| 150 | bu_shutdown( nTcpSocket, SHUT_RDWR ); | 150 | bu_shutdown( nTcpSocket, SHUT_RDWR ); |
| 151 | ::close( nTcpSocket ); | 151 | ::close( nTcpSocket ); |
| 152 | } | 152 | } |
| 153 | bActive = false; | 153 | bActive = false; |
| 154 | } | 154 | } |
| 155 | 155 | ||
| 156 | Bu::size Bu::TcpSocket::read( void *pBuf, Bu::size nBytes ) | 156 | Bu::size Bu::TcpSocket::read( void *pBuf, Bu::size nBytes ) |
| 157 | { | 157 | { |
| 158 | fd_set rfds; | 158 | fd_set rfds; |
| 159 | FD_ZERO(&rfds); | 159 | FD_ZERO(&rfds); |
| 160 | FD_SET(nTcpSocket, &rfds); | 160 | FD_SET(nTcpSocket, &rfds); |
| 161 | struct timeval tv = {0, 0}; | 161 | struct timeval tv = {0, 0}; |
| 162 | if( bu_select( nTcpSocket+1, &rfds, NULL, NULL, &tv ) < 0 ) | 162 | if( bu_select( nTcpSocket+1, &rfds, NULL, NULL, &tv ) < 0 ) |
| 163 | { | 163 | { |
| 164 | int iErr = errno; | 164 | int iErr = errno; |
| 165 | close(); | 165 | close(); |
| 166 | throw TcpSocketException( TcpSocketException::cRead, strerror(iErr) ); | 166 | throw TcpSocketException( TcpSocketException::cRead, strerror(iErr) ); |
| 167 | } | 167 | } |
| 168 | if( FD_ISSET( nTcpSocket, &rfds ) || bBlocking ) | 168 | if( FD_ISSET( nTcpSocket, &rfds ) || bBlocking ) |
| 169 | { | 169 | { |
| 170 | int nRead = TEMP_FAILURE_RETRY( | 170 | int nRead = TEMP_FAILURE_RETRY( |
| 171 | bu_recv( nTcpSocket, (char *) pBuf, nBytes, 0 ) ); | 171 | bu_recv( nTcpSocket, (char *) pBuf, nBytes, 0 ) ); |
| 172 | if( nRead == 0 && nBytes > 0 ) | 172 | if( nRead == 0 && nBytes > 0 ) |
| 173 | { | 173 | { |
| 174 | close(); | 174 | close(); |
| 175 | throw TcpSocketException( TcpSocketException::cClosed, "TcpSocket closed."); | 175 | throw TcpSocketException( TcpSocketException::cClosed, "TcpSocket closed."); |
| 176 | } | 176 | } |
| 177 | if( nRead < 0 ) | 177 | if( nRead < 0 ) |
| 178 | { | 178 | { |
| 179 | #ifdef WIN32 | 179 | #ifdef WIN32 |
| 180 | int iWSAError = bu_WSAGetLastError(); | 180 | int iWSAError = bu_WSAGetLastError(); |
| 181 | if( iWSAError == WSAEWOULDBLOCK ) | 181 | if( iWSAError == WSAEWOULDBLOCK ) |
| 182 | return 0; | 182 | return 0; |
| 183 | #else | 183 | #else |
| 184 | if( errno == ENETRESET || errno == ECONNRESET ) | 184 | if( errno == ENETRESET || errno == ECONNRESET ) |
| 185 | { | 185 | { |
| 186 | close(); | 186 | close(); |
| 187 | throw TcpSocketException( TcpSocketException::cClosed, | 187 | throw TcpSocketException( TcpSocketException::cClosed, |
| 188 | strerror(errno) ); | 188 | strerror(errno) ); |
| 189 | } | 189 | } |
| 190 | if( errno == EAGAIN ) | 190 | if( errno == EAGAIN ) |
| 191 | return 0; | 191 | return 0; |
| 192 | int iErr = errno; | 192 | int iErr = errno; |
| 193 | close(); | 193 | close(); |
| 194 | throw TcpSocketException( TcpSocketException::cRead, strerror(iErr) ); | 194 | throw TcpSocketException( TcpSocketException::cRead, strerror(iErr) ); |
| 195 | #endif | 195 | #endif |
| 196 | } | 196 | } |
| 197 | return nRead; | 197 | return nRead; |
| 198 | } | 198 | } |
| 199 | return 0; | 199 | return 0; |
| 200 | } | 200 | } |
| 201 | 201 | ||
| 202 | Bu::size Bu::TcpSocket::read( void *pBuf, Bu::size nBytes, | 202 | Bu::size Bu::TcpSocket::read( void *pBuf, Bu::size nBytes, |
| 203 | uint32_t nSec, uint32_t nUSec ) | 203 | uint32_t nSec, uint32_t nUSec ) |
| 204 | { | 204 | { |
| 205 | struct timeval tv; | 205 | struct timeval tv; |
| 206 | Bu::size nRead = 0; | 206 | Bu::size nRead = 0; |
| 207 | 207 | ||
| 208 | fd_set rfds; | 208 | fd_set rfds; |
| 209 | FD_ZERO(&rfds); | 209 | FD_ZERO(&rfds); |
| 210 | FD_SET(nTcpSocket, &rfds); | 210 | FD_SET(nTcpSocket, &rfds); |
| 211 | 211 | ||
| 212 | #ifdef WIN32 | 212 | #ifdef WIN32 |
| 213 | DWORD dwStart = GetTickCount(); | 213 | DWORD dwStart = GetTickCount(); |
| 214 | uint64_t uOver = dwStart + ((nUSec / 1000) * (nSec * 1000)); | 214 | uint64_t uOver = dwStart + ((nUSec / 1000) * (nSec * 1000)); |
| 215 | DWORD dwEnd = uOver>4294967295U?uOver-4294967295U:uOver; | 215 | DWORD dwEnd = uOver>4294967295U?uOver-4294967295U:uOver; |
| 216 | #else | 216 | #else |
| 217 | struct timeval nt, ct; | 217 | struct timeval nt, ct; |
| 218 | gettimeofday( &nt, NULL ); | 218 | gettimeofday( &nt, NULL ); |
| 219 | nt.tv_sec += nSec; | 219 | nt.tv_sec += nSec; |
| 220 | nt.tv_usec += nUSec; | 220 | nt.tv_usec += nUSec; |
| 221 | #endif | 221 | #endif |
| 222 | 222 | ||
| 223 | for(;;) | 223 | for(;;) |
| 224 | { | 224 | { |
| 225 | tv.tv_sec = nSec; | 225 | tv.tv_sec = nSec; |
| 226 | tv.tv_usec = nUSec; | 226 | tv.tv_usec = nUSec; |
| 227 | bu_select( nTcpSocket+1, &rfds, NULL, NULL, &tv ); | 227 | bu_select( nTcpSocket+1, &rfds, NULL, NULL, &tv ); |
| 228 | nRead += read( ((char *)pBuf)+nRead, nBytes-nRead ); | 228 | nRead += read( ((char *)pBuf)+nRead, nBytes-nRead ); |
| 229 | if( nRead >= nBytes ) | 229 | if( nRead >= nBytes ) |
| 230 | break; | 230 | break; |
| 231 | #ifdef WIN32 | 231 | #ifdef WIN32 |
| 232 | DWORD dwNow = GetTickCount(); | 232 | DWORD dwNow = GetTickCount(); |
| 233 | if( dwNow > dwEnd ) | 233 | if( dwNow > dwEnd ) |
| 234 | break; | 234 | break; |
| 235 | #else | 235 | #else |
| 236 | gettimeofday( &ct, NULL ); | 236 | gettimeofday( &ct, NULL ); |
| 237 | if( (ct.tv_sec > nt.tv_sec) || | 237 | if( (ct.tv_sec > nt.tv_sec) || |
| 238 | (ct.tv_sec == nt.tv_sec && | 238 | (ct.tv_sec == nt.tv_sec && |
| 239 | ct.tv_usec >= nt.tv_usec) ) | 239 | ct.tv_usec >= nt.tv_usec) ) |
| 240 | break; | 240 | break; |
| 241 | #endif | 241 | #endif |
| 242 | } | 242 | } |
| 243 | return nRead; | 243 | return nRead; |
| 244 | } | 244 | } |
| 245 | 245 | ||
| 246 | Bu::size Bu::TcpSocket::write( const void *pBuf, Bu::size nBytes ) | 246 | Bu::size Bu::TcpSocket::write( const void *pBuf, Bu::size nBytes ) |
| 247 | { | 247 | { |
| 248 | //#ifdef WIN32 | 248 | //#ifdef WIN32 |
| 249 | int nWrote = TEMP_FAILURE_RETRY( | 249 | int nWrote = TEMP_FAILURE_RETRY( |
| 250 | bu_send( nTcpSocket, (const char *) pBuf, nBytes, 0 ) ); | 250 | bu_send( nTcpSocket, (const char *) pBuf, nBytes, 0 ) ); |
| 251 | //#else | 251 | //#else |
| 252 | // int nWrote = TEMP_FAILURE_RETRY( ::write( nTcpSocket, pBuf, nBytes ) ); | 252 | // int nWrote = TEMP_FAILURE_RETRY( ::write( nTcpSocket, pBuf, nBytes ) ); |
| 253 | //#endif | 253 | //#endif |
| 254 | if( nWrote < 0 ) | 254 | if( nWrote < 0 ) |
| 255 | { | 255 | { |
| 256 | #ifdef WIN32 | 256 | #ifdef WIN32 |
| 257 | int iWSAError = bu_WSAGetLastError(); | 257 | int iWSAError = bu_WSAGetLastError(); |
| 258 | if( iWSAError == WSAEWOULDBLOCK ) | 258 | if( iWSAError == WSAEWOULDBLOCK ) |
| 259 | return 0; | 259 | return 0; |
| 260 | #else | 260 | #else |
| 261 | if( errno == EAGAIN ) return 0; | 261 | if( errno == EAGAIN ) return 0; |
| 262 | #endif | 262 | #endif |
| 263 | throw TcpSocketException( TcpSocketException::cWrite, strerror(errno) ); | 263 | throw TcpSocketException( TcpSocketException::cWrite, strerror(errno) ); |
| 264 | } | 264 | } |
| 265 | return nWrote; | 265 | return nWrote; |
| 266 | } | 266 | } |
| 267 | 267 | ||
| 268 | Bu::size Bu::TcpSocket::write( const void *pBuf, Bu::size nBytes, uint32_t nSec, uint32_t nUSec ) | 268 | Bu::size Bu::TcpSocket::write( const void *pBuf, Bu::size nBytes, uint32_t nSec, uint32_t nUSec ) |
| 269 | { | 269 | { |
| 270 | struct timeval tv; | 270 | struct timeval tv; |
| 271 | Bu::size nWrote = 0; | 271 | Bu::size nWrote = 0; |
| 272 | 272 | ||
| 273 | fd_set wfds; | 273 | fd_set wfds; |
| 274 | FD_ZERO(&wfds); | 274 | FD_ZERO(&wfds); |
| 275 | FD_SET(nTcpSocket, &wfds); | 275 | FD_SET(nTcpSocket, &wfds); |
| 276 | 276 | ||
| 277 | #ifdef WIN32 | 277 | #ifdef WIN32 |
| 278 | DWORD dwStart = GetTickCount(); | 278 | DWORD dwStart = GetTickCount(); |
| 279 | uint64_t uOver = dwStart + ((nUSec / 1000) * (nSec * 1000)); | 279 | uint64_t uOver = dwStart + ((nUSec / 1000) * (nSec * 1000)); |
| 280 | DWORD dwEnd = uOver>4294967295U?uOver-4294967295U:uOver; | 280 | DWORD dwEnd = uOver>4294967295U?uOver-4294967295U:uOver; |
| 281 | #else | 281 | #else |
| 282 | struct timeval nt, ct; | 282 | struct timeval nt, ct; |
| 283 | gettimeofday( &nt, NULL ); | 283 | gettimeofday( &nt, NULL ); |
| 284 | nt.tv_sec += nSec; | 284 | nt.tv_sec += nSec; |
| 285 | nt.tv_usec += nUSec; | 285 | nt.tv_usec += nUSec; |
| 286 | #endif | 286 | #endif |
| 287 | 287 | ||
| 288 | for(;;) | 288 | for(;;) |
| 289 | { | 289 | { |
| 290 | tv.tv_sec = nSec; | 290 | tv.tv_sec = nSec; |
| 291 | tv.tv_usec = nUSec; | 291 | tv.tv_usec = nUSec; |
| 292 | bu_select( nTcpSocket+1, NULL, &wfds, NULL, &tv ); | 292 | bu_select( nTcpSocket+1, NULL, &wfds, NULL, &tv ); |
| 293 | nWrote += write( ((char *)pBuf)+nWrote, nBytes-nWrote ); | 293 | nWrote += write( ((char *)pBuf)+nWrote, nBytes-nWrote ); |
| 294 | if( nWrote >= nBytes ) | 294 | if( nWrote >= nBytes ) |
| 295 | break; | 295 | break; |
| 296 | #ifdef WIN32 | 296 | #ifdef WIN32 |
| 297 | DWORD dwNow = GetTickCount(); | 297 | DWORD dwNow = GetTickCount(); |
| 298 | if( dwNow > dwEnd ) | 298 | if( dwNow > dwEnd ) |
| 299 | break; | 299 | break; |
| 300 | #else | 300 | #else |
| 301 | gettimeofday( &ct, NULL ); | 301 | gettimeofday( &ct, NULL ); |
| 302 | if( (ct.tv_sec > nt.tv_sec) || | 302 | if( (ct.tv_sec > nt.tv_sec) || |
| 303 | (ct.tv_sec == nt.tv_sec && | 303 | (ct.tv_sec == nt.tv_sec && |
| 304 | ct.tv_usec >= nt.tv_usec) ) | 304 | ct.tv_usec >= nt.tv_usec) ) |
| 305 | break; | 305 | break; |
| 306 | #endif | 306 | #endif |
| 307 | } | 307 | } |
| 308 | return nWrote; | 308 | return nWrote; |
| 309 | } | 309 | } |
| 310 | 310 | ||
| 311 | Bu::size Bu::TcpSocket::tell() | 311 | Bu::size Bu::TcpSocket::tell() |
| 312 | { | 312 | { |
| 313 | throw UnsupportedException(); | 313 | throw UnsupportedException(); |
| 314 | } | 314 | } |
| 315 | 315 | ||
| 316 | void Bu::TcpSocket::seek( Bu::size ) | 316 | void Bu::TcpSocket::seek( Bu::size ) |
| 317 | { | 317 | { |
| 318 | throw UnsupportedException(); | 318 | throw UnsupportedException(); |
| 319 | } | 319 | } |
| 320 | 320 | ||
| 321 | void Bu::TcpSocket::setPos( Bu::size ) | 321 | void Bu::TcpSocket::setPos( Bu::size ) |
| 322 | { | 322 | { |
| 323 | throw UnsupportedException(); | 323 | throw UnsupportedException(); |
| 324 | } | 324 | } |
| 325 | 325 | ||
| 326 | void Bu::TcpSocket::setPosEnd( Bu::size ) | 326 | void Bu::TcpSocket::setPosEnd( Bu::size ) |
| 327 | { | 327 | { |
| 328 | throw UnsupportedException(); | 328 | throw UnsupportedException(); |
| 329 | } | 329 | } |
| 330 | 330 | ||
| 331 | bool Bu::TcpSocket::isEos() | 331 | bool Bu::TcpSocket::isEos() |
| 332 | { | 332 | { |
| 333 | return !bActive; | 333 | return !bActive; |
| 334 | } | 334 | } |
| 335 | 335 | ||
| 336 | bool Bu::TcpSocket::canRead() | 336 | bool Bu::TcpSocket::canRead() |
| 337 | { | 337 | { |
| 338 | fd_set rfds; | 338 | fd_set rfds; |
| 339 | FD_ZERO(&rfds); | 339 | FD_ZERO(&rfds); |
| 340 | FD_SET(nTcpSocket, &rfds); | 340 | FD_SET(nTcpSocket, &rfds); |
| 341 | struct timeval tv = { 0, 0 }; | 341 | struct timeval tv = { 0, 0 }; |
| 342 | int retval = bu_select( nTcpSocket+1, &rfds, NULL, NULL, &tv ); | 342 | int retval = bu_select( nTcpSocket+1, &rfds, NULL, NULL, &tv ); |
| 343 | if( retval == -1 ) | 343 | if( retval == -1 ) |
| 344 | throw TcpSocketException( | 344 | throw TcpSocketException( |
| 345 | TcpSocketException::cBadRead, | 345 | TcpSocketException::cBadRead, |
| 346 | "Bad Read error" | 346 | "Bad Read error" |
| 347 | ); | 347 | ); |
| 348 | 348 | ||
| 349 | if( !FD_ISSET( nTcpSocket, &rfds ) ) | 349 | if( !FD_ISSET( nTcpSocket, &rfds ) ) |
| 350 | return false; | 350 | return false; |
| 351 | return true; | 351 | return true; |
| 352 | } | 352 | } |
| 353 | 353 | ||
| 354 | bool Bu::TcpSocket::canWrite() | 354 | bool Bu::TcpSocket::canWrite() |
| 355 | { | 355 | { |
| 356 | fd_set wfds; | 356 | fd_set wfds; |
| 357 | FD_ZERO(&wfds); | 357 | FD_ZERO(&wfds); |
| 358 | FD_SET(nTcpSocket, &wfds); | 358 | FD_SET(nTcpSocket, &wfds); |
| 359 | struct timeval tv = { 0, 0 }; | 359 | struct timeval tv = { 0, 0 }; |
| 360 | int retval = bu_select( nTcpSocket+1, NULL, &wfds, NULL, &tv ); | 360 | int retval = bu_select( nTcpSocket+1, NULL, &wfds, NULL, &tv ); |
| 361 | if( retval == -1 ) | 361 | if( retval == -1 ) |
| 362 | throw TcpSocketException( | 362 | throw TcpSocketException( |
| 363 | TcpSocketException::cBadRead, | 363 | TcpSocketException::cBadRead, |
| 364 | "Bad Read error" | 364 | "Bad Read error" |
| 365 | ); | 365 | ); |
| 366 | if( !FD_ISSET( nTcpSocket, &wfds ) ) | 366 | if( !FD_ISSET( nTcpSocket, &wfds ) ) |
| 367 | return false; | 367 | return false; |
| 368 | return true; | 368 | return true; |
| 369 | } | 369 | } |
| 370 | 370 | ||
| 371 | bool Bu::TcpSocket::isReadable() | 371 | bool Bu::TcpSocket::isReadable() |
| 372 | { | 372 | { |
| 373 | return true; | 373 | return true; |
| 374 | } | 374 | } |
| 375 | 375 | ||
| 376 | bool Bu::TcpSocket::isWritable() | 376 | bool Bu::TcpSocket::isWritable() |
| 377 | { | 377 | { |
| 378 | return true; | 378 | return true; |
| 379 | } | 379 | } |
| 380 | 380 | ||
| 381 | bool Bu::TcpSocket::isSeekable() | 381 | bool Bu::TcpSocket::isSeekable() |
| 382 | { | 382 | { |
| 383 | return false; | 383 | return false; |
| 384 | } | 384 | } |
| 385 | 385 | ||
| 386 | bool Bu::TcpSocket::isBlocking() | 386 | bool Bu::TcpSocket::isBlocking() |
| 387 | { | 387 | { |
| 388 | #ifndef WIN32 | 388 | #ifndef WIN32 |
| 389 | return ((fcntl( nTcpSocket, F_GETFL, 0 ) & O_NONBLOCK) != O_NONBLOCK); | 389 | return ((fcntl( nTcpSocket, F_GETFL, 0 ) & O_NONBLOCK) != O_NONBLOCK); |
| 390 | #else | 390 | #else |
| 391 | return false; | 391 | return false; |
| 392 | #endif | 392 | #endif |
| 393 | } | 393 | } |
| 394 | 394 | ||
| 395 | void Bu::TcpSocket::setBlocking( bool bBlocking ) | 395 | void Bu::TcpSocket::setBlocking( bool bBlocking ) |
| 396 | { | 396 | { |
| 397 | this->bBlocking = bBlocking; | 397 | this->bBlocking = bBlocking; |
| 398 | #ifndef WIN32 | 398 | #ifndef WIN32 |
| 399 | if( bBlocking ) | 399 | if( bBlocking ) |
| 400 | { | 400 | { |
| 401 | fcntl( nTcpSocket, F_SETFL, fcntl( nTcpSocket, F_GETFL, 0 ) & (~O_NONBLOCK) ); | 401 | fcntl( nTcpSocket, F_SETFL, fcntl( nTcpSocket, F_GETFL, 0 ) & (~O_NONBLOCK) ); |
| 402 | } | 402 | } |
| 403 | else | 403 | else |
| 404 | { | 404 | { |
| 405 | fcntl( nTcpSocket, F_SETFL, fcntl( nTcpSocket, F_GETFL, 0 ) | O_NONBLOCK ); | 405 | fcntl( nTcpSocket, F_SETFL, fcntl( nTcpSocket, F_GETFL, 0 ) | O_NONBLOCK ); |
| 406 | } | 406 | } |
| 407 | #else | 407 | #else |
| 408 | u_long iMode; | 408 | u_long iMode; |
| 409 | if( bBlocking ) | 409 | if( bBlocking ) |
| 410 | iMode = 0; | 410 | iMode = 0; |
| 411 | else | 411 | else |
| 412 | iMode = 1; | 412 | iMode = 1; |
| 413 | //------------------------- | 413 | //------------------------- |
| 414 | // Set the socket I/O mode: In this case FIONBIO | 414 | // Set the socket I/O mode: In this case FIONBIO |
| 415 | // enables or disables the blocking mode for the | 415 | // enables or disables the blocking mode for the |
| 416 | // socket based on the numerical value of iMode. | 416 | // socket based on the numerical value of iMode. |
| 417 | // If iMode = 0, blocking is enabled; | 417 | // If iMode = 0, blocking is enabled; |
| 418 | // If iMode != 0, non-blocking mode is enabled. | 418 | // If iMode != 0, non-blocking mode is enabled. |
| 419 | bu_ioctlsocket(nTcpSocket, FIONBIO, &iMode); | 419 | bu_ioctlsocket(nTcpSocket, FIONBIO, &iMode); |
| 420 | #endif | 420 | #endif |
| 421 | } | 421 | } |
| 422 | 422 | ||
| 423 | void Bu::TcpSocket::setSize( Bu::size ) | 423 | void Bu::TcpSocket::setSize( Bu::size ) |
| @@ -430,53 +430,53 @@ void Bu::TcpSocket::flush() | |||
| 430 | 430 | ||
| 431 | bool Bu::TcpSocket::isOpen() | 431 | bool Bu::TcpSocket::isOpen() |
| 432 | { | 432 | { |
| 433 | return bActive; | 433 | return bActive; |
| 434 | } | 434 | } |
| 435 | 435 | ||
| 436 | void Bu::TcpSocket::setAddress() | 436 | void Bu::TcpSocket::setAddress() |
| 437 | { | 437 | { |
| 438 | struct sockaddr_in addr; | 438 | struct sockaddr_in addr; |
| 439 | socklen_t len = sizeof(addr); | 439 | socklen_t len = sizeof(addr); |
| 440 | addr.sin_family = AF_INET; | 440 | addr.sin_family = AF_INET; |
| 441 | bu_getpeername( nTcpSocket, (sockaddr *)(&addr), &len ); | 441 | bu_getpeername( nTcpSocket, (sockaddr *)(&addr), &len ); |
| 442 | sAddress = bu_inet_ntoa( addr.sin_addr ); | 442 | sAddress = bu_inet_ntoa( addr.sin_addr ); |
| 443 | } | 443 | } |
| 444 | 444 | ||
| 445 | Bu::String Bu::TcpSocket::getAddress() const | 445 | Bu::String Bu::TcpSocket::getAddress() const |
| 446 | { | 446 | { |
| 447 | return sAddress; | 447 | return sAddress; |
| 448 | } | 448 | } |
| 449 | 449 | ||
| 450 | Bu::TcpSocket::operator Bu::TcpSocket::handle() const | 450 | Bu::TcpSocket::operator Bu::TcpSocket::handle() const |
| 451 | { | 451 | { |
| 452 | return nTcpSocket; | 452 | return nTcpSocket; |
| 453 | } | 453 | } |
| 454 | 454 | ||
| 455 | Bu::TcpSocket::handle Bu::TcpSocket::getHandle() const | 455 | Bu::TcpSocket::handle Bu::TcpSocket::getHandle() const |
| 456 | { | 456 | { |
| 457 | return nTcpSocket; | 457 | return nTcpSocket; |
| 458 | } | 458 | } |
| 459 | 459 | ||
| 460 | Bu::TcpSocket::handle Bu::TcpSocket::takeHandle() | 460 | Bu::TcpSocket::handle Bu::TcpSocket::takeHandle() |
| 461 | { | 461 | { |
| 462 | handle nRet = nTcpSocket; | 462 | handle nRet = nTcpSocket; |
| 463 | bActive = false; | 463 | bActive = false; |
| 464 | nTcpSocket = 0; | 464 | nTcpSocket = 0; |
| 465 | return nRet; | 465 | return nRet; |
| 466 | } | 466 | } |
| 467 | 467 | ||
| 468 | Bu::size Bu::TcpSocket::getSize() const | 468 | Bu::size Bu::TcpSocket::getSize() const |
| 469 | { | 469 | { |
| 470 | throw UnsupportedException(); | 470 | throw UnsupportedException(); |
| 471 | } | 471 | } |
| 472 | 472 | ||
| 473 | Bu::size Bu::TcpSocket::getBlockSize() const | 473 | Bu::size Bu::TcpSocket::getBlockSize() const |
| 474 | { | 474 | { |
| 475 | return 1500; //TODO: Fix this, it's stupid. | 475 | return 1500; //TODO: Fix this, it's stupid. |
| 476 | } | 476 | } |
| 477 | 477 | ||
| 478 | Bu::String Bu::TcpSocket::getLocation() const | 478 | Bu::String Bu::TcpSocket::getLocation() const |
| 479 | { | 479 | { |
| 480 | return getAddress(); | 480 | return getAddress(); |
| 481 | } | 481 | } |
| 482 | 482 | ||
