Improve RPC port handling

- Use port from remote-server.json in dfhack-run
- Make DFHACK_RUN environment variable take priority over remote-server.json
- Log current port to stderr
develop
lethosor 2018-07-02 23:53:56 -04:00
parent 81a7ddcf92
commit c543a17250
4 changed files with 38 additions and 16 deletions

@ -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)

@ -55,9 +55,10 @@ POSSIBILITY OF SUCH DAMAGE.
#include <memory>
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;
}

@ -58,12 +58,11 @@ POSSIBILITY OF SUCH DAMAGE.
#include <memory>
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))

@ -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; };