summaryrefslogtreecommitdiffhomepage
path: root/Stars45/NetClientConfig.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Stars45/NetClientConfig.cpp')
-rw-r--r--Stars45/NetClientConfig.cpp406
1 files changed, 203 insertions, 203 deletions
diff --git a/Stars45/NetClientConfig.cpp b/Stars45/NetClientConfig.cpp
index f4986c8..4e116ed 100644
--- a/Stars45/NetClientConfig.cpp
+++ b/Stars45/NetClientConfig.cpp
@@ -1,10 +1,10 @@
/* Project Starshatter 4.5
- Destroyer Studios LLC
- Copyright © 1997-2004. All Rights Reserved.
+ Destroyer Studios LLC
+ Copyright © 1997-2004. All Rights Reserved.
- SUBSYSTEM: Stars
- FILE: NetClientConfig.cpp
- AUTHOR: John DiCamillo
+ SUBSYSTEM: Stars
+ FILE: NetClientConfig.cpp
+ AUTHOR: John DiCamillo
*/
@@ -31,18 +31,18 @@ NetClientConfig* NetClientConfig::instance = 0;
// +--------------------------------------------------------------------+
NetClientConfig::NetClientConfig()
- : server_index(-1), host_request(false), conn(0)
+: server_index(-1), host_request(false), conn(0)
{
- instance = this;
- Load();
+ instance = this;
+ Load();
}
NetClientConfig::~NetClientConfig()
{
- Logout();
+ Logout();
- instance = 0;
- servers.destroy();
+ instance = 0;
+ servers.destroy();
}
// +--------------------------------------------------------------------+
@@ -50,15 +50,15 @@ NetClientConfig::~NetClientConfig()
void
NetClientConfig::Initialize()
{
- if (!instance)
- instance = new(__FILE__,__LINE__) NetClientConfig();
+ if (!instance)
+ instance = new(__FILE__,__LINE__) NetClientConfig();
}
void
NetClientConfig::Close()
{
- delete instance;
- instance = 0;
+ delete instance;
+ instance = 0;
}
// +--------------------------------------------------------------------+
@@ -66,37 +66,37 @@ NetClientConfig::Close()
void
NetClientConfig::AddServer(const char* name, const char* addr, WORD port, const char* pass, bool save)
{
- if (!addr || !*addr || port < 1024 || port > 48000)
- return;
-
- char buffer[1024];
- if (name && *name)
- strcpy(buffer, name);
- else
- sprintf(buffer, "%s:%d", addr, port);
-
- NetServerInfo* server = new(__FILE__,__LINE__) NetServerInfo;
- server->name = buffer;
- server->hostname = addr;
- server->addr = NetAddr(addr, port);
- server->port = port;
- server->password = pass;
- server->save = save;
-
- if (server->addr.IPAddr() == 0) {
- Print("NetClientConfig::AddServer(%s, %s, %d) failed to resolve IP Addr\n",
- name, addr, port);
- }
-
- servers.append(server);
+ if (!addr || !*addr || port < 1024 || port > 48000)
+ return;
+
+ char buffer[1024];
+ if (name && *name)
+ strcpy(buffer, name);
+ else
+ sprintf(buffer, "%s:%d", addr, port);
+
+ NetServerInfo* server = new(__FILE__,__LINE__) NetServerInfo;
+ server->name = buffer;
+ server->hostname = addr;
+ server->addr = NetAddr(addr, port);
+ server->port = port;
+ server->password = pass;
+ server->save = save;
+
+ if (server->addr.IPAddr() == 0) {
+ Print("NetClientConfig::AddServer(%s, %s, %d) failed to resolve IP Addr\n",
+ name, addr, port);
+ }
+
+ servers.append(server);
}
void
NetClientConfig::DelServer(int index)
{
- if (index >= 0 && index < servers.size()) {
- delete servers.removeIndex(index);
- }
+ if (index >= 0 && index < servers.size()) {
+ delete servers.removeIndex(index);
+ }
}
// +--------------------------------------------------------------------+
@@ -104,10 +104,10 @@ NetClientConfig::DelServer(int index)
NetServerInfo*
NetClientConfig::GetServerInfo(int n)
{
- if (n >= 0 && n < servers.size())
- return servers.at(n);
+ if (n >= 0 && n < servers.size())
+ return servers.at(n);
- return 0;
+ return 0;
}
// +--------------------------------------------------------------------+
@@ -115,10 +115,10 @@ NetClientConfig::GetServerInfo(int n)
NetServerInfo*
NetClientConfig::GetSelectedServer()
{
- if (server_index >= 0 && server_index < servers.size())
- return servers.at(server_index);
+ if (server_index >= 0 && server_index < servers.size())
+ return servers.at(server_index);
- return 0;
+ return 0;
}
// +--------------------------------------------------------------------+
@@ -126,12 +126,12 @@ NetClientConfig::GetSelectedServer()
void
NetClientConfig::Download()
{
- Load();
+ Load();
- List<NetServerInfo> list;
- if (NetBrokerClient::GameList("Starshatter", list)) {
- servers.append(list);
- }
+ List<NetServerInfo> list;
+ if (NetBrokerClient::GameList("Starshatter", list)) {
+ servers.append(list);
+ }
}
// +--------------------------------------------------------------------+
@@ -139,96 +139,96 @@ NetClientConfig::Download()
void
NetClientConfig::Load()
{
- server_index = -1;
-
- // read the config file:
- BYTE* block = 0;
- int blocklen = 0;
-
- char filename[64];
- strcpy(filename, "client.cfg");
-
- FILE* f = ::fopen(filename, "rb");
-
- if (f) {
- ::fseek(f, 0, SEEK_END);
- blocklen = ftell(f);
- ::fseek(f, 0, SEEK_SET);
-
- block = new(__FILE__,__LINE__) BYTE[blocklen+1];
- block[blocklen] = 0;
-
- ::fread(block, blocklen, 1, f);
- ::fclose(f);
- }
-
- if (blocklen == 0)
- return;
-
- servers.destroy();
-
- Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block, blocklen));
- Term* term = parser.ParseTerm();
-
- if (!term) {
- Print("ERROR: could not parse '%s'.\n", filename);
- return;
- }
- else {
- TermText* file_type = term->isText();
- if (!file_type || file_type->value() != "CLIENT_CONFIG") {
- Print("WARNING: invalid '%s' file. Using defaults\n", filename);
- return;
- }
- }
-
- do {
- delete term;
-
- term = parser.ParseTerm();
-
- if (term) {
- TermDef* def = term->isDef();
- if (def) {
- if (def->name()->value() == "server") {
- if (!def->term() || !def->term()->isStruct()) {
- Print("WARNING: server struct missing in '%s'\n", filename);
- }
- else {
- TermStruct* val = def->term()->isStruct();
-
- Text name;
- Text addr;
- Text pass;
- int port;
-
- for (int i = 0; i < val->elements()->size(); i++) {
- TermDef* pdef = val->elements()->at(i)->isDef();
- if (pdef) {
- if (pdef->name()->value() == "name")
- GetDefText(name, pdef, filename);
- else if (pdef->name()->value() == "addr")
- GetDefText(addr, pdef, filename);
- else if (pdef->name()->value() == "pass")
- GetDefText(pass, pdef, filename);
- else if (pdef->name()->value() == "port")
- GetDefNumber(port, pdef, filename);
- }
- }
-
- AddServer(name, addr, (WORD) port, pass, true);
- }
- }
- else {
- Print("WARNING: unknown label '%s' in '%s'\n",
- def->name()->value().data(), filename);
- }
- }
- }
- }
- while (term);
-
- delete [] block;
+ server_index = -1;
+
+ // read the config file:
+ BYTE* block = 0;
+ int blocklen = 0;
+
+ char filename[64];
+ strcpy(filename, "client.cfg");
+
+ FILE* f = ::fopen(filename, "rb");
+
+ if (f) {
+ ::fseek(f, 0, SEEK_END);
+ blocklen = ftell(f);
+ ::fseek(f, 0, SEEK_SET);
+
+ block = new(__FILE__,__LINE__) BYTE[blocklen+1];
+ block[blocklen] = 0;
+
+ ::fread(block, blocklen, 1, f);
+ ::fclose(f);
+ }
+
+ if (blocklen == 0)
+ return;
+
+ servers.destroy();
+
+ Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block, blocklen));
+ Term* term = parser.ParseTerm();
+
+ if (!term) {
+ Print("ERROR: could not parse '%s'.\n", filename);
+ return;
+ }
+ else {
+ TermText* file_type = term->isText();
+ if (!file_type || file_type->value() != "CLIENT_CONFIG") {
+ Print("WARNING: invalid '%s' file. Using defaults\n", filename);
+ return;
+ }
+ }
+
+ do {
+ delete term;
+
+ term = parser.ParseTerm();
+
+ if (term) {
+ TermDef* def = term->isDef();
+ if (def) {
+ if (def->name()->value() == "server") {
+ if (!def->term() || !def->term()->isStruct()) {
+ Print("WARNING: server struct missing in '%s'\n", filename);
+ }
+ else {
+ TermStruct* val = def->term()->isStruct();
+
+ Text name;
+ Text addr;
+ Text pass;
+ int port;
+
+ for (int i = 0; i < val->elements()->size(); i++) {
+ TermDef* pdef = val->elements()->at(i)->isDef();
+ if (pdef) {
+ if (pdef->name()->value() == "name")
+ GetDefText(name, pdef, filename);
+ else if (pdef->name()->value() == "addr")
+ GetDefText(addr, pdef, filename);
+ else if (pdef->name()->value() == "pass")
+ GetDefText(pass, pdef, filename);
+ else if (pdef->name()->value() == "port")
+ GetDefNumber(port, pdef, filename);
+ }
+ }
+
+ AddServer(name, addr, (WORD) port, pass, true);
+ }
+ }
+ else {
+ Print("WARNING: unknown label '%s' in '%s'\n",
+ def->name()->value().data(), filename);
+ }
+ }
+ }
+ }
+ while (term);
+
+ delete [] block;
}
// +--------------------------------------------------------------------+
@@ -236,30 +236,30 @@ NetClientConfig::Load()
void
NetClientConfig::Save()
{
- FILE* f = fopen("client.cfg", "w");
- if (f) {
- fprintf(f, "CLIENT_CONFIG\n\n");
-
- ListIter<NetServerInfo> iter = servers;
- while (++iter) {
- NetServerInfo* server = iter.value();
-
- if (server->save) {
- int port = (int) server->port;
- fprintf(f, "server: {\n");
- fprintf(f, " name: \"%s\",\n", (const char*) server->name);
- fprintf(f, " addr: \"%s\",\n", (const char*) server->hostname);
- fprintf(f, " port: %d,\n", port);
-
- if (server->password.length())
- fprintf(f, " pass: \"%s\",\n", (const char*) server->password);
-
- fprintf(f, "}\n\n");
- }
- }
-
- fclose(f);
- }
+ FILE* f = fopen("client.cfg", "w");
+ if (f) {
+ fprintf(f, "CLIENT_CONFIG\n\n");
+
+ ListIter<NetServerInfo> iter = servers;
+ while (++iter) {
+ NetServerInfo* server = iter.value();
+
+ if (server->save) {
+ int port = (int) server->port;
+ fprintf(f, "server: {\n");
+ fprintf(f, " name: \"%s\",\n", (const char*) server->name);
+ fprintf(f, " addr: \"%s\",\n", (const char*) server->hostname);
+ fprintf(f, " port: %d,\n", port);
+
+ if (server->password.length())
+ fprintf(f, " pass: \"%s\",\n", (const char*) server->password);
+
+ fprintf(f, "}\n\n");
+ }
+ }
+
+ fclose(f);
+ }
}
// +--------------------------------------------------------------------+
@@ -267,66 +267,66 @@ NetClientConfig::Save()
void
NetClientConfig::CreateConnection()
{
- NetServerInfo* s = GetSelectedServer();
-
- if (s) {
- NetAddr addr = s->addr;
-
- if (conn) {
- if (conn->GetServerAddr().IPAddr() != addr.IPAddr() ||
- conn->GetServerAddr().Port() != addr.Port()) {
- conn->Logout();
- DropConnection();
- }
- }
-
- if (addr.IPAddr() && addr.Port() && !conn) {
- conn = new(__FILE__,__LINE__) NetLobbyClient; // (addr);
- }
- }
-
- else if (conn) {
- conn->Logout();
- DropConnection();
- }
+ NetServerInfo* s = GetSelectedServer();
+
+ if (s) {
+ NetAddr addr = s->addr;
+
+ if (conn) {
+ if (conn->GetServerAddr().IPAddr() != addr.IPAddr() ||
+ conn->GetServerAddr().Port() != addr.Port()) {
+ conn->Logout();
+ DropConnection();
+ }
+ }
+
+ if (addr.IPAddr() && addr.Port() && !conn) {
+ conn = new(__FILE__,__LINE__) NetLobbyClient; // (addr);
+ }
+ }
+
+ else if (conn) {
+ conn->Logout();
+ DropConnection();
+ }
}
NetLobbyClient*
NetClientConfig::GetConnection()
{
- return conn;
+ return conn;
}
bool
NetClientConfig::Login()
{
- bool result = false;
+ bool result = false;
- if (!conn)
- CreateConnection();
+ if (!conn)
+ CreateConnection();
- if (conn)
- result = conn->Login(host_request);
+ if (conn)
+ result = conn->Login(host_request);
- return result;
+ return result;
}
bool
NetClientConfig::Logout()
{
- bool result = false;
+ bool result = false;
- if (conn) {
- result = conn->Logout();
- DropConnection();
- }
+ if (conn) {
+ result = conn->Logout();
+ DropConnection();
+ }
- return result;
+ return result;
}
void
NetClientConfig::DropConnection()
{
- delete conn;
- conn = 0;
+ delete conn;
+ conn = 0;
}