| 439 | |
| 440 | template<typename Protocol> |
| 441 | bool server_endpoint_impl<Protocol>::flush(endpoint_type _key) { |
| 442 | |
| 443 | bool has_queued(true); |
| 444 | bool is_current_train(true); |
| 445 | |
| 446 | std::scoped_lock its_lock(mutex_); |
| 447 | |
| 448 | auto it = targets_.find(_key); |
| 449 | if (it == targets_.end()) |
| 450 | return false; |
| 451 | |
| 452 | auto& its_data = it->second; |
| 453 | auto its_train(its_data.train_); |
| 454 | if (!its_data.dispatched_trains_.empty()) { |
| 455 | |
| 456 | auto its_dispatched = its_data.dispatched_trains_.begin(); |
| 457 | if (its_dispatched->first <= its_train->departure_) { |
| 458 | |
| 459 | is_current_train = false; |
| 460 | if (!its_dispatched->second.empty()) { |
| 461 | its_train = its_dispatched->second.front(); |
| 462 | its_dispatched->second.pop_front(); |
| 463 | if (its_dispatched->second.empty()) { |
| 464 | |
| 465 | its_data.dispatched_trains_.erase(its_dispatched); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if (!its_train->buffer_->empty()) { |
| 472 | |
| 473 | queue_train(it, its_train); |
| 474 | |
| 475 | // Reset current train if necessary |
| 476 | if (is_current_train) { |
| 477 | its_train->reset(); |
| 478 | } |
| 479 | } else { |
| 480 | has_queued = false; |
| 481 | } |
| 482 | |
| 483 | if (!is_current_train || !its_data.dispatched_trains_.empty()) { |
| 484 | |
| 485 | auto its_now(std::chrono::steady_clock::now()); |
| 486 | start_dispatch_timer(it, its_now); |
| 487 | } |
| 488 | |
| 489 | return has_queued; |
| 490 | } |
| 491 | |
| 492 | template<typename Protocol> |
| 493 | void server_endpoint_impl<Protocol>::connect_cbk(boost::system::error_code const& _error) { |