diff options
Diffstat (limited to '')
| -rw-r--r-- | src/socket.cpp | 66 |
1 files changed, 53 insertions, 13 deletions
diff --git a/src/socket.cpp b/src/socket.cpp index 1874fd2..73d52e4 100644 --- a/src/socket.cpp +++ b/src/socket.cpp | |||
| @@ -179,22 +179,32 @@ size_t Bu::Socket::read( void *pBuf, size_t nBytes ) | |||
| 179 | size_t Bu::Socket::read( void *pBuf, size_t nBytes, | 179 | size_t Bu::Socket::read( void *pBuf, size_t nBytes, |
| 180 | uint32_t nSec, uint32_t nUSec ) | 180 | uint32_t nSec, uint32_t nUSec ) |
| 181 | { | 181 | { |
| 182 | struct timeval tv, nt, ct; | ||
| 183 | size_t nRead = 0; | ||
| 184 | |||
| 182 | fd_set rfds; | 185 | fd_set rfds; |
| 183 | FD_ZERO(&rfds); | 186 | FD_ZERO(&rfds); |
| 184 | FD_SET(nSocket, &rfds); | 187 | FD_SET(nSocket, &rfds); |
| 185 | struct timeval tv = { nSec, nUSec }; | 188 | |
| 186 | int retval = select( nSocket+1, &rfds, NULL, NULL, &tv ); | 189 | gettimeofday( &nt, NULL ); |
| 187 | if( retval == -1 ) | 190 | nt.tv_sec += nSec; |
| 188 | throw ConnectionException( | 191 | nt.tv_usec += nUSec; |
| 189 | excodeBadReadError, | 192 | |
| 190 | "Bad Read error" | 193 | for(;;) |
| 191 | ); | 194 | { |
| 192 | if( !FD_ISSET( nSocket, &rfds ) ) | 195 | tv.tv_sec = nSec; |
| 193 | throw ConnectionException( | 196 | tv.tv_usec = nUSec; |
| 194 | excodeSocketTimeout, | 197 | select( nSocket+1, &rfds, NULL, NULL, &tv ); |
| 195 | "Socket timout on read" | 198 | nRead += read( ((char *)pBuf)+nRead, nBytes-nRead ); |
| 196 | ); | 199 | if( nRead >= nBytes ) |
| 197 | return read( pBuf, nBytes ); | 200 | break; |
| 201 | gettimeofday( &ct, NULL ); | ||
| 202 | if( (ct.tv_sec > nt.tv_sec) || | ||
| 203 | (ct.tv_sec == nt.tv_sec && | ||
| 204 | ct.tv_usec >= nt.tv_usec) ) | ||
| 205 | break; | ||
| 206 | } | ||
| 207 | return nRead; | ||
| 198 | } | 208 | } |
| 199 | 209 | ||
| 200 | size_t Bu::Socket::write( const void *pBuf, size_t nBytes ) | 210 | size_t Bu::Socket::write( const void *pBuf, size_t nBytes ) |
| @@ -208,6 +218,36 @@ size_t Bu::Socket::write( const void *pBuf, size_t nBytes ) | |||
| 208 | return nWrote; | 218 | return nWrote; |
| 209 | } | 219 | } |
| 210 | 220 | ||
| 221 | size_t Bu::Socket::write( const void *pBuf, size_t nBytes, uint32_t nSec, uint32_t nUSec ) | ||
| 222 | { | ||
| 223 | struct timeval tv, nt, ct; | ||
| 224 | size_t nWrote = 0; | ||
| 225 | |||
| 226 | fd_set wfds; | ||
| 227 | FD_ZERO(&wfds); | ||
| 228 | FD_SET(nSocket, &wfds); | ||
| 229 | |||
| 230 | gettimeofday( &nt, NULL ); | ||
| 231 | nt.tv_sec += nSec; | ||
| 232 | nt.tv_usec += nUSec; | ||
| 233 | |||
| 234 | for(;;) | ||
| 235 | { | ||
| 236 | tv.tv_sec = nSec; | ||
| 237 | tv.tv_usec = nUSec; | ||
| 238 | select( nSocket+1, NULL, &wfds, NULL, &tv ); | ||
| 239 | nWrote += write( ((char *)pBuf)+nWrote, nBytes-nWrote ); | ||
| 240 | if( nWrote >= nBytes ) | ||
| 241 | break; | ||
| 242 | gettimeofday( &ct, NULL ); | ||
| 243 | if( (ct.tv_sec > nt.tv_sec) || | ||
| 244 | (ct.tv_sec == nt.tv_sec && | ||
| 245 | ct.tv_usec >= nt.tv_usec) ) | ||
| 246 | break; | ||
| 247 | } | ||
| 248 | return nWrote; | ||
| 249 | } | ||
| 250 | |||
| 211 | long Bu::Socket::tell() | 251 | long Bu::Socket::tell() |
| 212 | { | 252 | { |
| 213 | throw UnsupportedException(); | 253 | throw UnsupportedException(); |
