Commit cd107de6 authored by Hubert Denkmair's avatar Hubert Denkmair

use environment variables for hostnames and ports

parent ad7c86e4
......@@ -11,16 +11,18 @@ int RelayServer::Run()
uWS::Hub h;
EPoll epoll;
_clientSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(9010);
inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
if (connect(_clientSocket, (struct sockaddr*)&serv_addr, sizeof(serv_addr))<0)
const char* gameserverHost = getEnvOrDefault(ENV_GAMESERVER_HOST, ENV_GAMESERVER_HOST_DEFAULT);
const char* gameserverPort = getEnvOrDefault(ENV_GAMESERVER_PORT, ENV_GAMESERVER_PORT_DEFAULT);
const char* websocketPort = getEnvOrDefault(ENV_WEBSOCKET_PORT, ENV_WEBSOCKET_PORT_DEFAULT);
fprintf(stderr, "connecting to gameserver on %s port %s...\n", gameserverHost , gameserverPort);
_clientSocket = connectTcpSocket(gameserverHost , gameserverPort);
if (_clientSocket < 0)
{
perror("connect to server failed");
return -1;
}
fprintf(stderr, "connected.\n");
_tcpProtocol.SetFrameCompleteCallback(
[this, &h](uint64_t frame_id)
......@@ -62,7 +64,7 @@ int RelayServer::Run()
res->end(response.data(), response.length());
});
if (!h.listen(9009))
if (!h.listen(atoi(websocketPort)))
{
return -1;
}
......@@ -86,3 +88,47 @@ int RelayServer::Run()
);
}
}
int RelayServer::connectTcpSocket(const char *hostname, const char *port)
{
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
struct addrinfo *result;
int s = getaddrinfo(hostname, port, &hints, &result);
if (s != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
return s;
}
struct addrinfo* rp = nullptr;
int retval = -1;
for (rp=result; rp!=nullptr; rp=rp->ai_next)
{
int fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (fd == -1) { continue; }
if (connect(fd, rp->ai_addr, rp->ai_addrlen) == 0)
{
retval = fd;
break;
}
close(fd);
}
freeaddrinfo(result);
return retval;
}
const char *RelayServer::getEnvOrDefault(const char *envVar, const char *defaultValue)
{
const char* value = getenv(envVar);
if (value == nullptr)
{
value = defaultValue;
}
return value;
}
......@@ -12,4 +12,13 @@ class RelayServer
private:
int _clientSocket;
TcpProtocol _tcpProtocol;
static constexpr const char* ENV_GAMESERVER_HOST = "GAMESERVER_HOST";
static constexpr const char* ENV_GAMESERVER_HOST_DEFAULT = "localhost";
static constexpr const char* ENV_GAMESERVER_PORT = "GAMESERVER_PORT";
static constexpr const char* ENV_GAMESERVER_PORT_DEFAULT = "9010";
static constexpr const char* ENV_WEBSOCKET_PORT = "WEBSOCKET_PORT";
static constexpr const char* ENV_WEBSOCKET_PORT_DEFAULT = "9009";
static int connectTcpSocket(const char* hostname, const char* port);
static const char* getEnvOrDefault(const char* envVar, const char* defaultValue);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment