Skip to content

Commit 9a89baa

Browse files
committed
Server: Socket::ReceiveData() (vector): Return empty vector from function on ping message
This will allow `Win7::PlayerSocket::ProcessMessages()` to time out the client in states not involving participation in a match.
1 parent da1b6e3 commit 9a89baa

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

InternetGamesServer/Socket.cpp

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -262,53 +262,50 @@ Socket::ReceiveData()
262262
{
263263
char receivedBuf[DEFAULT_BUFLEN];
264264

265-
while (true)
265+
const int receivedLen = recv(m_socket, receivedBuf, DEFAULT_BUFLEN, 0);
266+
if (receivedLen > 0)
266267
{
267-
const int receivedLen = recv(m_socket, receivedBuf, DEFAULT_BUFLEN, 0);
268-
if (receivedLen > 0)
268+
const std::string received(receivedBuf, receivedLen);
269+
std::istringstream receivedStream(received);
270+
271+
std::vector<std::vector<std::string>> receivedEntries;
272+
std::string receivedLine;
273+
while (std::getline(receivedStream, receivedLine))
269274
{
270-
const std::string received(receivedBuf, receivedLen);
271-
std::istringstream receivedStream(received);
275+
if (receivedLine.empty())
276+
continue;
272277

273-
std::vector<std::vector<std::string>> receivedEntries;
274-
std::string receivedLine;
275-
while (std::getline(receivedStream, receivedLine))
278+
if (receivedLine.back() == '\r')
276279
{
280+
receivedLine.pop_back(); // Remove carriage return
277281
if (receivedLine.empty())
278282
continue;
279-
280-
if (receivedLine.back() == '\r')
281-
{
282-
receivedLine.pop_back(); // Remove carriage return
283-
if (receivedLine.empty())
284-
continue;
285-
}
286-
287-
receivedEntries.push_back(StringSplit(std::move(receivedLine), "&")); // Split data by "&" for easier parsing in certain cases
288283
}
289284

290-
if (!s_logPingMessages && receivedEntries.empty())
291-
continue;
285+
receivedEntries.push_back(StringSplit(std::move(receivedLine), "&")); // Split data by "&" for easier parsing in certain cases
286+
}
292287

293-
m_log << "[RECEIVED]: " << std::endl;
294-
for (const std::vector<std::string>& receivedLineEntries : receivedEntries)
295-
{
296-
for (const std::string& entry : receivedLineEntries)
297-
{
298-
m_log << entry << std::endl;
299-
}
300-
m_log << std::endl;
301-
}
302-
m_log << '\n' << std::endl;
288+
if (!s_logPingMessages && receivedEntries.empty())
289+
return {};
303290

304-
return receivedEntries;
305-
}
306-
else if (receivedLen == 0)
291+
m_log << "[RECEIVED]: " << std::endl;
292+
for (const std::vector<std::string>& receivedLineEntries : receivedEntries)
307293
{
308-
throw ClientDisconnected();
294+
for (const std::string& entry : receivedLineEntries)
295+
{
296+
m_log << entry << std::endl;
297+
}
298+
m_log << std::endl;
309299
}
310-
throw std::runtime_error("\"recv\" failed: " + std::to_string(WSAGetLastError()));
300+
m_log << '\n' << std::endl;
301+
302+
return receivedEntries;
303+
}
304+
else if (receivedLen == 0)
305+
{
306+
throw ClientDisconnected();
311307
}
308+
throw std::runtime_error("\"recv\" failed: " + std::to_string(WSAGetLastError()));
312309
}
313310

314311

InternetGamesServer/Win7/PlayerSocket.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ PlayerSocket::ProcessMessages()
3232
{
3333
while (true)
3434
{
35-
// Receive and send back data
3635
const std::vector<std::vector<std::string>> receivedData = m_socket.ReceiveData();
37-
assert(!receivedData.empty());
3836

3937
bool skipLines = false;
4038
for (const std::vector<std::string>& receivedLineData : receivedData)

0 commit comments

Comments
 (0)