Changeset 3661 for trunk/oscam-reader.c
- Timestamp:
- 10/21/10 17:10:31 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/oscam-reader.c
r3653 r3661 114 114 struct timeval tv; 115 115 if(timeout&MSTIMEOUT) { tv.tv_sec=0; tv.tv_usec=(timeout&~MSTIMEOUT)*1000; } 116 else { tv.tv_sec= timeout; tv.tv_usec=0; }116 else { tv.tv_sec=0; tv.tv_usec=timeout*1000; } 117 117 int r=select(sd+1,forRead ? &fds:0,forRead ? 0:&fds,0,&tv); 118 118 if(r>0) return 1; … … 185 185 } 186 186 187 void clear_block_delay(struct s_reader *rdr) { 188 rdr->tcp_block_delay = 100; 189 cs_ftime(&rdr->tcp_block_connect_till); 190 } 191 187 192 int network_tcp_connection_open() 188 193 { 189 194 struct s_client *cl = cur_client(); 190 cs_log("connecting to %s", cur_client()->reader->device); 191 192 if (!hostResolve(cur_client()->reader)) 195 struct s_reader *rdr = cl->reader; 196 struct timeb cur_time; 197 cs_log("connecting to %s", rdr->device); 198 199 in_addr_t last_ip = cl->ip; 200 if (!hostResolve(rdr)) 193 201 return -1; 194 202 203 if (last_ip != cl->ip) //clean blocking delay on ip change: 204 clear_block_delay(rdr); 205 cs_ftime(&cur_time); 206 if (comp_timeb(&cur_time, &rdr->tcp_block_connect_till) < 0) { //inside of blocking delay, do not connect! 207 cs_log("tcp connect blocking delay asserted for %s", rdr->label); 208 return -1; 209 } 210 195 211 int sd = cl->udp_fd; 196 if (connect(sd, (struct sockaddr *)&cl->udp_sa, sizeof(cl->udp_sa)) == 0) 212 int fl = fcntl(sd, F_GETFL); 213 fcntl(sd, F_SETFL, O_NONBLOCK); //set to nonblocking mode to avoid "endless" connecting loops and pipe-overflows: 214 int res =connect(sd, (struct sockaddr *)&cl->udp_sa, sizeof(cl->udp_sa)); 215 if (res == 0) { 216 fcntl(sd, F_SETFL, fl); //connect sucessfull, restore blocking mode 197 217 return sd; 198 218 } 219 220 cs_log("connect error %d for %s", errno, rdr->label); 221 199 222 if (errno == EINPROGRESS || errno == EALREADY) { 200 if (network_select(0, DEFAULT_CONNECT_TIMEOUT) > 0) { 223 if (network_select(0, DEFAULT_CONNECT_TIMEOUT) > 0) { //if connect is in progress, wait apr. 500ms 201 224 int r = -1; 202 225 uint l = sizeof(r); 203 226 if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &r, (socklen_t*)&l) == 0) { 204 if (r == 0) return sd; 227 if (r == 0) { 228 fcntl(sd, F_SETFL, fl); 229 return sd; //now we are connected 230 } 205 231 } 206 232 } 207 233 } 208 else if (errno == EBADF || errno == ENOTSOCK) { 209 cs_log("connect failed: bad socket/descriptor %d", sd); 210 return -1; 211 } 234 //else we are not connected - or already connected: 212 235 else if (errno == EISCONN) { 213 236 cs_log("already connected!"); 237 fcntl(sd, F_SETFL, fl); 214 238 return sd; 239 } 240 241 if (errno == EBADF || errno == ENOTSOCK) { 242 cs_log("connect failed: bad socket/descriptor %d", sd); 215 243 } 216 244 else if (errno == ETIMEDOUT) { 217 245 cs_log("connect failed: timeout"); 218 return -1;219 246 } 220 247 else if (errno == ECONNREFUSED) { 221 248 cs_log("connection refused"); 222 return -1;223 249 } 224 250 else if (errno == ENETUNREACH) { 225 251 cs_log("connect failed: network unreachable!"); 226 return -1;227 252 } 228 253 else if (errno == EADDRINUSE) { 229 254 cs_log("connect failed: address in use!"); 230 return -1; 231 } 232 233 cs_log("connect(fd=%d) failed: (errno=%d)", sd, errno); 255 } 256 else 257 cs_log("connect(fd=%d) failed: (errno=%d)", sd, errno); 258 259 fcntl(sd, F_SETFL, fl); //restore blocking mode 260 261 //connect has failed. Block connect for a while: 262 if (!rdr->tcp_block_delay) 263 rdr->tcp_block_delay = 100; 264 cs_debug_mask(D_TRACE, "tcp connect blocking delay set to %d", rdr->tcp_block_delay); 265 rdr->tcp_block_connect_till = cur_time; 266 rdr->tcp_block_connect_till.time += rdr->tcp_block_delay / 1000; 267 rdr->tcp_block_connect_till.millitm += rdr->tcp_block_delay % 1000; 268 rdr->tcp_block_delay *= 2; //increment timeouts 269 if (rdr->tcp_block_delay >= 60*1000) 270 rdr->tcp_block_delay = 60*1000; //max 60s 271 234 272 return -1; 235 273 } … … 242 280 close(fd); 243 281 cl->udp_fd = 0; 282 clear_block_delay(reader); 244 283 245 284 if (cl->typ != 'c')
Note:
See TracChangeset
for help on using the changeset viewer.