Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetBrokerClient.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright (C) 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: NetBrokerClient.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Client for Starshatter.com GameNet Broker
13 */
14 
15 
16 #include "MemDebug.h"
17 #include "NetBrokerClient.h"
18 
19 #include "HttpClient.h"
20 #include "HttpServlet.h"
21 #include "NetLayer.h"
22 
23 #include "Game.h"
24 
25 extern const char* versionInfo;
26 const char* HOSTNAME = "www.starshatter.com";
27 const WORD HTTPPORT = 80;
28 
31 
32 // +--------------------------------------------------------------------+
33 
34 bool
35 NetBrokerClient::GameOn(const char* name,
36 const char* type,
37 const char* addr,
38 WORD port,
39 const char* password)
40 {
41  bool result = false;
42 
43  if (!broker_available)
44  return result;
45 
46  Text msg;
47  char buffer[8];
48  NetAddr broker(HOSTNAME, HTTPPORT);
49 
50  if (broker.IPAddr() == 0)
51  return result;
52 
53  sprintf_s(buffer, "%d", port);
54 
55  msg = "GET http://";
56  msg += HOSTNAME;
57  msg += "/GameNet/GameOn.php?name=";
58  msg += HttpRequest::EncodeParam(name);
59  msg += "&addr=";
60  msg += HttpRequest::EncodeParam(addr);
61  msg += "&game=";
62  msg += HttpRequest::EncodeParam(type);
63  msg += "&vers=";
65  msg += "&port=";
66  msg += buffer;
67  msg += "&pass=";
68  msg += HttpRequest::EncodeParam(password);
69  msg += " HTTP/1.1\n\n";
70 
71  HttpClient client(broker);
72  HttpRequest request(msg);
73  request.SetHeader("Host", HOSTNAME);
74 
75  HttpResponse* response = client.DoRequest(request);
76 
77  if (response && response->Status() == 200) {
78  broker_found = true;
79  result = true;
80  }
81  else {
82  ::Print("NetBrokerClient unable to contact GameNet!\n");
83 
84  if (response) {
85  ::Print(" Response Status: %d\n", response->Status());
86  ::Print(" Response Content: %s\n", response->Content().data());
87  }
88  else {
89  ::Print(" No response.\n");
90  }
91 
92  if (!broker_found)
93  broker_available = false;
94  }
95 
96  delete response;
97  return result;
98 }
99 
100 bool
101 NetBrokerClient::GameList(const char* type, List<NetServerInfo>& server_list)
102 {
103  bool result = false;
104 
105  if (!broker_available)
106  return result;
107 
108  Text msg;
109  NetAddr broker(HOSTNAME, HTTPPORT);
110 
111  if (broker.IPAddr() == 0)
112  return result;
113 
114  msg = "GET http://";
115  msg += HOSTNAME;
116  msg += "/GameNet/GameList.php?game=";
117  msg += HttpRequest::EncodeParam(type);
118  msg += "&vers=";
120  msg += " HTTP/1.1\n\n";
121 
122  HttpClient client(broker);
123  HttpRequest request(msg);
124  request.SetHeader("Host", HOSTNAME);
125 
126  HttpResponse* response = client.DoRequest(request);
127 
128  if (response && response->Status() == 200) {
129  result = true;
130 
131  Text name;
132  Text type;
133  Text addr;
134  int port;
135  Text pass;
136  Text vers;
137  char buff[1024];
138 
139  const char* p = response->Content();
140 
141  // skip size
142  while (*p && strncmp(p, "name:", 5))
143  p++;
144 
145  while (*p) {
146  if (!strncmp(p, "name:", 5)) {
147  p += 5;
148  ZeroMemory(buff, sizeof(buff));
149  char* d = buff;
150  while (*p && *p != '\n') *d++ = *p++;
151  if (*p) p++;
152 
153  name = buff;
154  }
155  else if (!strncmp(p, "addr:", 5)) {
156  p += 5;
157  ZeroMemory(buff, sizeof(buff));
158  char* d = buff;
159  while (*p && *p != '\n') *d++ = *p++;
160  if (*p) p++;
161 
162  addr = buff;
163  }
164  else if (!strncmp(p, "port:", 5)) {
165  p += 5;
166  ZeroMemory(buff, sizeof(buff));
167  char* d = buff;
168  while (*p && *p != '\n') *d++ = *p++;
169  if (*p) p++;
170 
171  sscanf_s(buff, "%d", &port);
172  }
173  else if (!strncmp(p, "pass:", 5)) {
174  p += 5;
175  ZeroMemory(buff, sizeof(buff));
176  char* d = buff;
177  while (*p && *p != '\n') *d++ = *p++;
178  if (*p) p++;
179 
180  pass = buff;
181  }
182  else if (!strncmp(p, "game:", 5)) {
183  p += 5;
184  ZeroMemory(buff, sizeof(buff));
185  char* d = buff;
186  while (*p && *p != '\n') *d++ = *p++;
187  if (*p) p++;
188 
189  type = buff;
190  type.setSensitive(false);
191 
192  if (type.contains("lan"))
193  type = "LAN";
194  else
195  type = "Public";
196  }
197  else if (!strncmp(p, "vers:", 5)) {
198  p += 5;
199  ZeroMemory(buff, sizeof(buff));
200  char* d = buff;
201  while (*p && *p != '\n') *d++ = *p++;
202  if (*p) p++;
203 
204  vers = buff;
205  }
206  else if (!strncmp(p, "time:", 5)) {
207  while (*p && *p != '\n') p++;
208  if (*p) p++;
209  }
210 
211  else if (!strncmp(p, "###", 3)) {
212  NetServerInfo* server = new(__FILE__,__LINE__) NetServerInfo;
213  server->name = name;
214  server->hostname = addr;
215  server->type = type;
216  server->addr = NetAddr(addr, port);
217  server->port = port;
218  server->password = pass;
219  server->version = vers;
220  server->save = false;
221 
222  server_list.append(server);
223 
224  while (*p && strncmp(p, "name:", 5))
225  p++;
226  }
227  else {
228  while (*p && *p != '\n') p++;
229  if (*p) p++;
230  }
231  }
232  }
233  else if (!broker_found) {
234  broker_available = false;
235  }
236 
237  delete response;
238  return result;
239 }