#include <sys/wait.h>
#include <unistd.h>
uint16_t Engine::id_t = 0;
Engine::Engine(const char *file) : file(file) {
if (pipe(fd_to) < 0 || pipe(fd_from) < 0) {
Engine::Pipes::Pipes() {
if (pipe(fd) < 0 || pipe(fd) < 0) {
logger::error("pipe");
throw std::runtime_error("pipe failed");
}
}
if ((engine_pid = fork()) > 0) {
void Engine::Pipes::close() {
if (::close(fd[0]) < 0 || ::close(fd[1])) {
logger::error("close");
throw std::runtime_error("close failed");
}
closed = true;
}
uint16_t Engine::id_t = 0;
Engine::Engine(const char *file) : file(file) {
if ((engine_pid = fork()) == 0) {
start_engine();
} else if (engine_pid < 0) {
logger::error("fork");
throw std::runtime_error("fork failed");
}
if (close(fd_to[0]) < 0 || close(fd_from[1])) {
logger::error("close");
throw std::runtime_error("close failed");
}
send("uci");
logger::log(std::format("Engine {}: waiting for uciok from {}", id, file));