diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 22ceb5ea5..390efdcfe 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -369,7 +369,7 @@ ENDIF() TARGET_LINK_LIBRARIES(dfhack protobuf-lite clsocket lua jsoncpp_lib_static dfhack-version ${PROJECT_LIBS}) SET_TARGET_PROPERTIES(dfhack PROPERTIES INTERFACE_LINK_LIBRARIES "") -TARGET_LINK_LIBRARIES(dfhack-client protobuf-lite clsocket) +TARGET_LINK_LIBRARIES(dfhack-client protobuf-lite clsocket jsoncpp_lib_static) TARGET_LINK_LIBRARIES(dfhack-run dfhack-client) if(APPLE) diff --git a/library/RemoteClient.cpp b/library/RemoteClient.cpp index 07f9c6961..25355530f 100644 --- a/library/RemoteClient.cpp +++ b/library/RemoteClient.cpp @@ -55,9 +55,10 @@ POSSIBILITY OF SUCH DAMAGE. #include -using namespace DFHack; - +#include "json/json.h" #include "tinythread.h" + +using namespace DFHack; using namespace tthread; using dfproto::CoreTextNotification; @@ -132,14 +133,34 @@ bool readFullBuffer(CSimpleSocket *socket, void *buf, int size) int RemoteClient::GetDefaultPort() { - const char *port = getenv("DFHACK_PORT"); - if (!port) port = "0"; + int port = DEFAULT_PORT; - int portval = atoi(port); - if (portval <= 0) - return 5000; + const char *port_env = getenv("DFHACK_PORT"); + if (port_env) + { + int port_val = atoi(port_env); + if (port_val > 0) + port = port_val; + } else - return portval; + { + for (const char *filename : {"dfhack-config/remote-server.json", "../dfhack-config/remote-server.json"}) + { + std::ifstream in_file(filename, std::ios_base::in); + if (in_file) + { + Json::Value config; + in_file >> config; + in_file.close(); + if (config.isMember("port")) { + port = config["port"].asInt(); + break; + } + } + } + } + + return port; } bool RemoteClient::connect(int port) @@ -157,7 +178,7 @@ bool RemoteClient::connect(int port) if (!socket->Open("localhost", port)) { - default_output().printerr("Could not connect to localhost: %d\n", port); + default_output().printerr("Could not connect to localhost:%d\n", port); return false; } diff --git a/library/RemoteServer.cpp b/library/RemoteServer.cpp index fb10990f8..f110cc8b4 100644 --- a/library/RemoteServer.cpp +++ b/library/RemoteServer.cpp @@ -58,12 +58,11 @@ POSSIBILITY OF SUCH DAMAGE. #include -using namespace DFHack; - +#include "json/json.h" #include "tinythread.h" -using namespace tthread; -#include "json/json.h" +using namespace DFHack; +using namespace tthread; using dfproto::CoreTextNotification; using dfproto::CoreTextFragment; @@ -395,11 +394,11 @@ bool ServerMain::listen(int port) inFile.close(); allow_remote = configJson.get("allow_remote", "false").asBool(); - port = configJson.get("port", port).asInt(); } + // rewrite/normalize config file configJson["allow_remote"] = allow_remote; - configJson["port"] = port; + configJson["port"] = configJson.get("port", RemoteClient::DEFAULT_PORT); std::ofstream outFile(filename, std::ios_base::trunc); @@ -409,6 +408,7 @@ bool ServerMain::listen(int port) outFile.close(); } + std::cerr << "Listening on port " << port << (allow_remote ? " (remote enabled)" : "") << std::endl; if (allow_remote) { if (!socket->Listen(NULL, port)) diff --git a/library/include/RemoteClient.h b/library/include/RemoteClient.h index e1a2c512a..cf07bba8b 100644 --- a/library/include/RemoteClient.h +++ b/library/include/RemoteClient.h @@ -233,6 +233,7 @@ namespace DFHack RemoteClient(color_ostream *default_output = NULL); ~RemoteClient(); + static constexpr int DEFAULT_PORT = 5000; static int GetDefaultPort(); color_ostream &default_output() { return *p_default_output; };