-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameServer.cpp
208 lines (175 loc) · 5.02 KB
/
GameServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/******************************************************************************
** (C) Chris Oldwood
**
** MODULE: GAMESERVER.CPP
** COMPONENT: The Application
** DESCRIPTION: CGameServer class definition.
**
*******************************************************************************
*/
#include "Common.hpp"
#include "GameServer.hpp"
#include "UTSvrBrowser.hpp"
#include <limits.h>
#include <NCL/UDPCltSocket.hpp>
#include <WCL/StrTok.hpp>
#include "QueryResponse.hpp"
#include <NCL/SocketException.hpp>
#include <Core/AnsiWide.hpp>
/******************************************************************************
**
** Constants.
**
*******************************************************************************
*/
const char* CGameServer::QUERY_INFO = "\\info\\";
const char* CGameServer::QUERY_STATUS = "\\status\\";
const tchar* CGameServer::END_OF_RESPONSE = TXT("\\final\\");
const tchar* CGameServer::FIELD_SEPS = TXT("\\");
/******************************************************************************
** Method: Constructor.
**
** Description: .
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
CGameServer::CGameServer(const tchar* pszAddress, int nPort)
: m_strAddress(pszAddress)
, m_nPort(nPort)
{
}
/******************************************************************************
** Method: Destructor.
**
** Description: .
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
CGameServer::~CGameServer()
{
}
/******************************************************************************
** Method: QueryInfo()
**
** Description: Queries the server for the basic server info. It is used as the
** ping request.
**
** Parameters: oResponse The servers response.
**
** Returns: true or false.
**
*******************************************************************************
*/
bool CGameServer::QueryInfo(CQueryResponse& oResponse)
{
return ExecQuery(QUERY_INFO, oResponse);
}
/******************************************************************************
** Method: QueryStatus()
**
** Description: Queries the server for the full status of the current game.
**
** Parameters: oResponse The servers response.
**
** Returns: true or false.
**
*******************************************************************************
*/
bool CGameServer::QueryStatus(CQueryResponse& oResponse)
{
return ExecQuery(QUERY_STATUS, oResponse);
}
/******************************************************************************
** Method: ExecQuery()
**
** Description: Send a query to the server and extract the response.
**
** Parameters: pszQuery The query to execute.
** oResponse The servers response.
**
** Returns: true or false.
**
*******************************************************************************
*/
bool CGameServer::ExecQuery(const char* pszQuery, CQueryResponse& oResponse)
{
int nError = CServers::ERROR_FAILED;
int nTime = INT_MAX;
try
{
int nMaxAttempts = App.m_oPingOpts.m_nAttempts;
uint nPingTimeout = App.m_oPingOpts.m_nWaitTime;
// For n attempts...
for (int nAttempt = 0; ((nAttempt < nMaxAttempts) && (nError != CServers::ERROR_NONE)); ++nAttempt)
{
CUDPCltSocket oSocket;
CString strResponse;
// Open a connection.
oSocket.Connect(m_strAddress, m_nPort);
// Send the query.
oSocket.Send(pszQuery, strlen(pszQuery));
DWORD dwStart = ::GetTickCount();
// Until we find the response terminator.
while (strResponse.Find(END_OF_RESPONSE) == -1)
{
int nAvail, nRead;
// Anything to read?
if ((nAvail = oSocket.Available()) > 0)
{
CBuffer oBuffer(nAvail);
// Anything read?
if ((nRead = oSocket.Recv(oBuffer)) > 0)
{
const char* psz = static_cast<const char*>(oBuffer.Buffer());
// Append to response buffer.
strResponse += A2T(std::string(psz, psz+nRead));
}
}
// Abort query if it takes too long.
if ((::GetTickCount() - dwStart) > nPingTimeout)
{
nError = CServers::ERROR_TIMED_OUT;
break;
}
::Sleep(1);
}
// Calculate ping time.
nTime = ::GetTickCount() - dwStart;
// Close connection.
oSocket.Close();
// Got entire response?
if (strResponse.Find(END_OF_RESPONSE) != -1)
{
CStrArray astrFields;
// Discard leading '\\'.
strResponse.Delete(0);
// Split response into fields.
CStrTok::Split(strResponse, FIELD_SEPS, astrFields);
// Add field/value pairs to response...
for (size_t i = 0; i < (astrFields.Size() / 2); ++i)
{
oResponse.m_astrFields.Add(astrFields[(i*2)+0]);
oResponse.m_astrValues.Add(astrFields[(i*2)+1]);
}
// Success.
nError = CServers::ERROR_NONE;
}
}
}
catch (const CSocketException& /*e*/)
{
// TRACE3("SocketException (%s:%d): %s\n", m_strAddress, m_nPort, e.ErrorText());
}
// Complete response details.
oResponse.m_nError = nError;
oResponse.m_nTime = nTime;
return (nError == CServers::ERROR_NONE);
}