Changeset c4f5530cc3500542dc8c5e08efa54997edf279b9
- Timestamp:
- 03/02/08 20:09:45
(8 months ago)
- Author:
- Rémi Denis-Courmont <rem@videolan.org>
- git-committer:
- Rémi Denis-Courmont <rem@videolan.org> 1202065785 +0000
- git-parent:
[518f3a21a2e4ca352dd8a9392be85e70fa4357bb]
- git-author:
- Rémi Denis-Courmont <rem@videolan.org> 1202065785 +0000
- Message:
Use waitpipe when establishing an outgoing network connection
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| racb0c8e |
rc4f5530 |
|
| 82 | 82 | int i_realport, i_val, i_handle = -1; |
|---|
| 83 | 83 | |
|---|
| | 84 | int evfd = vlc_object_waitpipe (p_this); |
|---|
| | 85 | if (evfd == -1) |
|---|
| | 86 | return -1; |
|---|
| | 87 | |
|---|
| 84 | 88 | if( i_port == 0 ) |
|---|
| 85 | 89 | i_port = 80; /* historical VLC thing */ |
|---|
| | 90 | |
|---|
| 86 | 91 | |
|---|
| 87 | 92 | memset( &hints, 0, sizeof( hints ) ); |
|---|
| … | … | |
| 159 | 164 | if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) ) |
|---|
| 160 | 165 | { |
|---|
| 161 | | socklen_t i_val_size = sizeof( i_val ); |
|---|
| 162 | | div_t d; |
|---|
| 163 | | vlc_value_t timeout; |
|---|
| | 166 | int timeout, val; |
|---|
| 164 | 167 | |
|---|
| 165 | 168 | if( net_errno != EINPROGRESS ) |
|---|
| … | … | |
| 170 | 173 | msg_Dbg( p_this, "connection: %m" ); |
|---|
| 171 | 174 | |
|---|
| 172 | | var_Create( p_this, "ipv4-timeout", |
|---|
| 173 | | VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); |
|---|
| 174 | | var_Get( p_this, "ipv4-timeout", &timeout ); |
|---|
| 175 | | if( timeout.i_int < 0 ) |
|---|
| | 175 | timeout = var_CreateGetInteger (p_this, "ipv4-timeout"); |
|---|
| | 176 | if (timeout < 0) |
|---|
| 176 | 177 | { |
|---|
| 177 | 178 | msg_Err( p_this, "invalid negative value for ipv4-timeout" ); |
|---|
| 178 | | timeout.i_int = 0; |
|---|
| | 179 | timeout = 0; |
|---|
| 179 | 180 | } |
|---|
| 180 | | d = div( timeout.i_int, 100 ); |
|---|
| 181 | | |
|---|
| 182 | | for (;;) |
|---|
| | 181 | |
|---|
| | 182 | struct pollfd ufd[2] = { |
|---|
| | 183 | { .fd = fd, .events = POLLOUT }, |
|---|
| | 184 | { .fd = evfd, .events = POLLIN }, |
|---|
| | 185 | }; |
|---|
| | 186 | |
|---|
| | 187 | do |
|---|
| | 188 | /* NOTE: timeout screwed up if we catch a signal (EINTR) */ |
|---|
| | 189 | val = poll (ufd, sizeof (ufd) / sizeof (ufd[0]), timeout); |
|---|
| | 190 | while ((val == -1) && (net_errno == EINTR)); |
|---|
| | 191 | |
|---|
| | 192 | switch (val) |
|---|
| 183 | 193 | { |
|---|
| 184 | | struct pollfd ufd = { .fd = fd, .events = POLLOUT }; |
|---|
| 185 | | int i_ret; |
|---|
| 186 | | |
|---|
| 187 | | if( p_this->b_die ) |
|---|
| 188 | | { |
|---|
| 189 | | msg_Dbg( p_this, "connection aborted" ); |
|---|
| 190 | | net_Close( fd ); |
|---|
| 191 | | vlc_freeaddrinfo( res ); |
|---|
| 192 | | return -1; |
|---|
| 193 | | } |
|---|
| 194 | | |
|---|
| 195 | | /* |
|---|
| 196 | | * We'll wait 0.1 second if nothing happens |
|---|
| 197 | | * NOTE: |
|---|
| 198 | | * time out will be shortened if we catch a signal (EINTR) |
|---|
| 199 | | */ |
|---|
| 200 | | i_ret = poll (&ufd, 1, (d.quot > 0) ? 100 : d.rem); |
|---|
| 201 | | if( i_ret == 1 ) |
|---|
| 202 | | break; |
|---|
| 203 | | |
|---|
| 204 | | if( ( i_ret == -1 ) && ( net_errno != EINTR ) ) |
|---|
| 205 | | { |
|---|
| 206 | | msg_Err( p_this, "connection polling error: %m" ); |
|---|
| 207 | | goto next_ai; |
|---|
| 208 | | } |
|---|
| 209 | | |
|---|
| 210 | | if( d.quot <= 0 ) |
|---|
| 211 | | { |
|---|
| 212 | | msg_Warn( p_this, "connection timed out" ); |
|---|
| 213 | | goto next_ai; |
|---|
| 214 | | } |
|---|
| 215 | | |
|---|
| 216 | | d.quot--; |
|---|
| | 194 | case -1: /* error */ |
|---|
| | 195 | msg_Err (p_this, "connection polling error: %m"); |
|---|
| | 196 | goto next_ai; |
|---|
| | 197 | |
|---|
| | 198 | case 0: /* timeout */ |
|---|
| | 199 | msg_Warn (p_this, "connection timed out"); |
|---|
| | 200 | goto next_ai; |
|---|
| | 201 | |
|---|
| | 202 | default: /* something happended */ |
|---|
| | 203 | if (ufd[1].revents) |
|---|
| | 204 | goto next_ai; /* LibVLC object killed */ |
|---|
| 217 | 205 | } |
|---|
| 218 | 206 | |
|---|
| 219 | | #if !defined( SYS_BEOS ) && !defined( UNDER_CE ) |
|---|
| 220 | | if( getsockopt( fd, SOL_SOCKET, SO_ERROR, (void*)&i_val, |
|---|
| 221 | | &i_val_size ) == -1 || i_val != 0 ) |
|---|
| | 207 | /* There is NO WAY around checking SO_ERROR. |
|---|
| | 208 | * Don't ifdef it out!!! */ |
|---|
| | 209 | if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &val, |
|---|
| | 210 | &(socklen_t){ sizeof (val) }) || val) |
|---|
| 222 | 211 | { |
|---|
| 223 | | errno = i_val; |
|---|
| 224 | | msg_Err( p_this, "connection failed: %m" ); |
|---|
| | 212 | errno = val; |
|---|
| | 213 | msg_Err (p_this, "connection failed: %m"); |
|---|
| 225 | 214 | goto next_ai; |
|---|
| 226 | 215 | } |
|---|
| 227 | | #endif |
|---|
| 228 | 216 | } |
|---|
| 229 | 217 | |
|---|
| … | … | |
| 288 | 276 | { |
|---|
| 289 | 277 | int timeout = (i_wait < 0) ? -1 : i_wait / 1000; |
|---|
| 290 | | int evfd; |
|---|
| | 278 | int evfd = vlc_object_waitpipe (p_this); |
|---|
| | 279 | |
|---|
| | 280 | if (evfd == -1) |
|---|
| | 281 | return -1; |
|---|
| 291 | 282 | |
|---|
| 292 | 283 | assert( pi_fd != NULL ); |
|---|
| 293 | | |
|---|
| 294 | | evfd = vlc_object_waitpipe (p_this); |
|---|
| 295 | 284 | |
|---|
| 296 | 285 | for (;;) |
|---|