| 423 | } |
| 424 | |
| 425 | ServerMainImpl::ServerMainImpl(std::promise<bool> promise, int port) : |
| 426 | socket{} |
| 427 | { |
| 428 | socket.Initialize(); |
| 429 | |
| 430 | std::string filename("dfhack-config/remote-server.json"); |
| 431 | |
| 432 | Json::Value configJson; |
| 433 | |
| 434 | bool allow_remote = false; |
| 435 | |
| 436 | std::ifstream inFile(filename, std::ios_base::in); |
| 437 | try { |
| 438 | if (inFile.is_open()) |
| 439 | { |
| 440 | inFile >> configJson; |
| 441 | allow_remote = configJson.get("allow_remote", "false").asBool(); |
| 442 | } |
| 443 | } catch (const std::exception & e) { |
| 444 | std::cerr << "Error reading remote server config file: " << filename << ": " << e.what() << std::endl; |
| 445 | std::cerr << "Reverting to remote server config to defaults" << std::endl; |
| 446 | } |
| 447 | inFile.close(); |
| 448 | |
| 449 | // rewrite/normalize config file |
| 450 | configJson["allow_remote"] = allow_remote; |
| 451 | configJson["port"] = configJson.get("port", RemoteClient::DEFAULT_PORT); |
| 452 | |
| 453 | std::ofstream outFile(filename, std::ios_base::trunc); |
| 454 | |
| 455 | if (outFile.is_open()) |
| 456 | { |
| 457 | outFile << configJson; |
| 458 | outFile.close(); |
| 459 | } |
| 460 | |
| 461 | std::cerr << "Listening on port " << port << (allow_remote ? " (remote enabled)" : "") << std::endl; |
| 462 | const char* addr = allow_remote ? NULL : "127.0.0.1"; |
| 463 | if (!socket.Listen(addr, port)) { |
| 464 | promise.set_value(false); |
| 465 | return; |
| 466 | } |
| 467 | promise.set_value(true); |
| 468 | } |
| 469 | |
| 470 | ServerMainImpl::~ServerMainImpl() |
| 471 | { |