Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetClientConfig.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars
6  FILE: NetClientConfig.cpp
7  AUTHOR: John DiCamillo
8 
9 */
10 
11 #include "MemDebug.h"
12 
13 #include "NetClientConfig.h"
14 #include "NetLobbyClient.h"
15 #include "NetBrokerClient.h"
16 
17 #include "NetLayer.h"
18 #include "NetAddr.h"
19 #include "NetHost.h"
20 
21 #include "Token.h"
22 #include "Game.h"
23 #include "DataLoader.h"
24 #include "ParseUtil.h"
25 #include "Resource.h"
26 
27 // +--------------------------------------------------------------------+
28 
29 NetClientConfig* NetClientConfig::instance = 0;
30 
31 // +--------------------------------------------------------------------+
32 
34 : server_index(-1), host_request(false), conn(0)
35 {
36  instance = this;
37  Load();
38 }
39 
41 {
42  Logout();
43 
44  instance = 0;
45  servers.destroy();
46 }
47 
48 // +--------------------------------------------------------------------+
49 
50 void
52 {
53  if (!instance)
54  instance = new(__FILE__,__LINE__) NetClientConfig();
55 }
56 
57 void
59 {
60  delete instance;
61  instance = 0;
62 }
63 
64 // +--------------------------------------------------------------------+
65 
66 void
67 NetClientConfig::AddServer(const char* name, const char* addr, WORD port, const char* pass, bool save)
68 {
69  if (!addr || !*addr || port < 1024 || port > 48000)
70  return;
71 
72  char buffer[1024];
73  if (name && *name)
74  strcpy_s(buffer, name);
75  else
76  sprintf_s(buffer, "%s:%d", addr, port);
77 
78  NetServerInfo* server = new(__FILE__,__LINE__) NetServerInfo;
79  server->name = buffer;
80  server->hostname = addr;
81  server->addr = NetAddr(addr, port);
82  server->port = port;
83  server->password = pass;
84  server->save = save;
85 
86  if (server->addr.IPAddr() == 0) {
87  Print("NetClientConfig::AddServer(%s, %s, %d) failed to resolve IP Addr\n",
88  name, addr, port);
89  }
90 
91  servers.append(server);
92 }
93 
94 void
96 {
97  if (index >= 0 && index < servers.size()) {
98  delete servers.removeIndex(index);
99  }
100 }
101 
102 // +--------------------------------------------------------------------+
103 
106 {
107  if (n >= 0 && n < servers.size())
108  return servers.at(n);
109 
110  return 0;
111 }
112 
113 // +--------------------------------------------------------------------+
114 
117 {
118  if (server_index >= 0 && server_index < servers.size())
119  return servers.at(server_index);
120 
121  return 0;
122 }
123 
124 // +--------------------------------------------------------------------+
125 
126 void
128 {
129  Load();
130 
131  List<NetServerInfo> list;
132  if (NetBrokerClient::GameList("Starshatter", list)) {
133  servers.append(list);
134  }
135 }
136 
137 // +--------------------------------------------------------------------+
138 
139 void
141 {
142  server_index = -1;
143 
144  // read the config file:
145  BYTE* block = 0;
146  int blocklen = 0;
147 
148  char filename[64];
149  strcpy_s(filename, "client.cfg");
150 
151  FILE* f;
152  ::fopen_s(&f, filename, "rb");
153 
154  if (f) {
155  ::fseek(f, 0, SEEK_END);
156  blocklen = ftell(f);
157  ::fseek(f, 0, SEEK_SET);
158 
159  block = new(__FILE__,__LINE__) BYTE[blocklen+1];
160  block[blocklen] = 0;
161 
162  ::fread(block, blocklen, 1, f);
163  ::fclose(f);
164  }
165 
166  if (blocklen == 0)
167  return;
168 
169  servers.destroy();
170 
171  Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block, blocklen));
172  Term* term = parser.ParseTerm();
173 
174  if (!term) {
175  Print("ERROR: could not parse '%s'.\n", filename);
176  return;
177  }
178  else {
179  TermText* file_type = term->isText();
180  if (!file_type || file_type->value() != "CLIENT_CONFIG") {
181  Print("WARNING: invalid '%s' file. Using defaults\n", filename);
182  return;
183  }
184  }
185 
186  do {
187  delete term;
188 
189  term = parser.ParseTerm();
190 
191  if (term) {
192  TermDef* def = term->isDef();
193  if (def) {
194  if (def->name()->value() == "server") {
195  if (!def->term() || !def->term()->isStruct()) {
196  Print("WARNING: server struct missing in '%s'\n", filename);
197  }
198  else {
199  TermStruct* val = def->term()->isStruct();
200 
201  Text name;
202  Text addr;
203  Text pass;
204  int port;
205 
206  for (int i = 0; i < val->elements()->size(); i++) {
207  TermDef* pdef = val->elements()->at(i)->isDef();
208  if (pdef) {
209  if (pdef->name()->value() == "name")
210  GetDefText(name, pdef, filename);
211  else if (pdef->name()->value() == "addr")
212  GetDefText(addr, pdef, filename);
213  else if (pdef->name()->value() == "pass")
214  GetDefText(pass, pdef, filename);
215  else if (pdef->name()->value() == "port")
216  GetDefNumber(port, pdef, filename);
217  }
218  }
219 
220  AddServer(name, addr, (WORD) port, pass, true);
221  }
222  }
223  else {
224  Print("WARNING: unknown label '%s' in '%s'\n",
225  def->name()->value().data(), filename);
226  }
227  }
228  }
229  }
230  while (term);
231 
232  delete [] block;
233 }
234 
235 // +--------------------------------------------------------------------+
236 
237 void
239 {
240  FILE* f;
241  fopen_s(&f, "client.cfg", "w");
242  if (f) {
243  fprintf(f, "CLIENT_CONFIG\n\n");
244 
245  ListIter<NetServerInfo> iter = servers;
246  while (++iter) {
247  NetServerInfo* server = iter.value();
248 
249  if (server->save) {
250  int port = (int) server->port;
251  fprintf(f, "server: {\n");
252  fprintf(f, " name: \"%s\",\n", (const char*) server->name);
253  fprintf(f, " addr: \"%s\",\n", (const char*) server->hostname);
254  fprintf(f, " port: %d,\n", port);
255 
256  if (server->password.length())
257  fprintf(f, " pass: \"%s\",\n", (const char*) server->password);
258 
259  fprintf(f, "}\n\n");
260  }
261  }
262 
263  fclose(f);
264  }
265 }
266 
267 // +--------------------------------------------------------------------+
268 
269 void
271 {
273 
274  if (s) {
275  NetAddr addr = s->addr;
276 
277  if (conn) {
278  if (conn->GetServerAddr().IPAddr() != addr.IPAddr() ||
279  conn->GetServerAddr().Port() != addr.Port()) {
280  conn->Logout();
281  DropConnection();
282  }
283  }
284 
285  if (addr.IPAddr() && addr.Port() && !conn) {
286  conn = new(__FILE__,__LINE__) NetLobbyClient; // (addr);
287  }
288  }
289 
290  else if (conn) {
291  conn->Logout();
292  DropConnection();
293  }
294 }
295 
298 {
299  return conn;
300 }
301 
302 bool
304 {
305  bool result = false;
306 
307  if (!conn)
309 
310  if (conn)
311  result = conn->Login(host_request);
312 
313  return result;
314 }
315 
316 bool
318 {
319  bool result = false;
320 
321  if (conn) {
322  result = conn->Logout();
323  DropConnection();
324  }
325 
326  return result;
327 }
328 
329 void
331 {
332  delete conn;
333  conn = 0;
334 }