#pragma once #include #include #include #include #include #include "io/network/server.hpp" #include "communication/bolt/v1/bolt.hpp" namespace bolt { template class Server : public io::Server> { public: Server(io::Socket&& socket) : io::Server>(std::forward(socket)) {} void start(size_t n) { workers.reserve(n); for(size_t i = 0; i < n; ++i) { workers.push_back(std::make_shared(bolt)); workers.back()->start(alive); } while(alive) { this->wait_and_process_events(); } } void shutdown() { alive.store(false); for(auto& worker : workers) worker->thread.join(); } void on_connect() { assert(idx < workers.size()); if(UNLIKELY(!workers[idx]->accept(this->socket))) return; idx = idx == workers.size() - 1 ? 0 : idx + 1; } void on_wait_timeout() {} private: Bolt bolt; std::vector workers; std::atomic alive {true}; int idx {0}; }; }