Changeset 11190 for trunk/FACT++/src/Connection.cc
- Timestamp:
- 06/27/11 13:00:22 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/Connection.cc
r11164 r11190 3 3 4 4 @brief Maintains an ansynchronous TCP/IP client connection 5 6 @todo 7 Unify with ConnectionUSB 5 8 6 9 */ … … 44 47 } 45 48 49 /* 46 50 void Connection::AsyncWait(ba::deadline_timer &timer, int millisec, 47 51 void (Connection::*handler)(const bs::error_code&)) … … 59 63 timer.async_wait(boost::bind(handler, this, dummy::error)); 60 64 } 65 */ 61 66 62 67 void Connection::AsyncConnect(tcp::resolver::iterator iterator) … … 66 71 // AsyncConnect + Deadline 67 72 async_connect(endpoint, 68 boost::bind(&Connection::ConnectImp, 69 this, ba::placeholders::error, 70 iterator)); 73 boost::bind(&Connection::ConnectIterImp, 74 this, iterator, ba::placeholders::error)); 75 76 // We will get a "Connection timeout anyway" 77 //AsyncWait(fConnectTimeout, 5, &Connection::HandleConnectTimeout); 78 } 79 80 void Connection::AsyncConnect() 81 { 82 // AsyncConnect + Deadline 83 async_connect(fEndpoint, 84 boost::bind(&Connection::ConnectAddrImp, 85 this, fEndpoint, ba::placeholders::error)); 71 86 72 87 // We will get a "Connection timeout anyway" … … 275 290 } 276 291 277 void Connection::ConnectImp(const bs::error_code& error, 278 tcp::resolver::iterator iterator) 279 { 280 tcp::endpoint endpoint = *iterator; 281 292 void Connection::ConnectAddrImp(const tcp::endpoint &endpoint, const bs::error_code& error) 293 { 282 294 const string host = endpoint.port()==0 ? "" : 283 295 endpoint.address().to_string()+":"+lexical_cast<string>(endpoint.port()); … … 325 337 326 338 fConnectionStatus = kConnecting; 327 339 /* 328 340 // Go on with the next 329 341 if (++iterator != tcp::resolver::iterator()) … … 332 344 return; 333 345 } 334 346 */ 335 347 // No more entries to try, if we would not put anything else 336 348 // into the queue anymore it would now return (run() would return) … … 343 355 } 344 356 357 void Connection::ConnectIterImp(tcp::resolver::iterator iterator, const bs::error_code& error) 358 { 359 ConnectAddrImp(*iterator, error); 360 /* 361 tcp::endpoint endpoint = *iterator; 362 363 const string host = endpoint.port()==0 ? "" : 364 endpoint.address().to_string()+":"+lexical_cast<string>(endpoint.port()); 365 366 // Connection established 367 if (!error) 368 { 369 set_option(socket_base::keep_alive(true)); 370 371 const int optval = 10; 372 // First keep alive after 10s 373 setsockopt(native(), SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(optval)); 374 // New keep alive after 10s 375 setsockopt(native(), SOL_TCP, TCP_KEEPINTVL, &optval, sizeof(optval)); 376 377 Info("Connection established to "+host+"..."); 378 379 fConnectionStatus = kConnected; 380 381 ConnectionEstablished(); 382 return; 383 } 384 385 // If returning from run will lead to deletion of this 386 // instance, close() is not needed (maybe implicitly called). 387 // If run is called again, close() is needed here. Otherwise: 388 // Software caused connection abort when we try to resolve 389 // the endpoint again. 390 CloseImp(false); 391 392 ostringstream msg; 393 if (!host.empty()) 394 msg << "Connecting to " << host << ": " << error.message() << " (" << error << ")"; 395 396 if (fErrConnect!=msg.str()) 397 { 398 if (error!=ba::error::basic_errors::connection_refused) 399 fMsgConnect = ""; 400 fErrConnect = msg.str(); 401 Warn(fErrConnect); 402 } 403 404 if (error==ba::error::basic_errors::operation_aborted) 405 return; 406 407 fConnectionStatus = kConnecting; 408 */ 409 // Go on with the next 410 if (++iterator != tcp::resolver::iterator()) 411 { 412 AsyncConnect(iterator); 413 return; 414 } 415 /* 416 // No more entries to try, if we would not put anything else 417 // into the queue anymore it would now return (run() would return) 418 419 // Since we don't want to block the main loop, we wait using an 420 // asnychronous timer 421 422 // FIXME: Should we move this before AsyncConnect() ? 423 AsyncWait(fConnectionTimer, 250, &Connection::HandleConnectionTimer); 424 */ 425 } 426 345 427 // FIXME: Async connect should get address and port as an argument 346 428 void Connection::StartConnect() 347 429 { 348 430 fConnectionStatus = kConnecting; 431 432 if (fEndpoint!=tcp::endpoint()) 433 { 434 ostringstream msg; 435 msg << "Trying to connect to " << fEndpoint << "..."; 436 Info(msg); 437 438 AsyncConnect(); 439 return; 440 } 349 441 350 442 tcp::resolver resolver(get_io_service());
Note:
See TracChangeset
for help on using the changeset viewer.