-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
246 lines (229 loc) · 6.79 KB
/
player.cpp
File metadata and controls
246 lines (229 loc) · 6.79 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Game server for Alien Front Online.
Copyright (C) 2025 Flyinghead
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "player.h"
#include "game.h"
#include "discord.h"
void GameConnection::start()
{
// Wait for a valid login packet for 10 sec then close the connection.
timeoutTimer.expires_after(asio::chrono::seconds(10));
timeoutTimer.async_wait([self = shared_from_this()](const std::error_code& ec) {
if (!ec && self->player != nullptr) {
ERROR_LOG("[%s] Connection timed out", self->player->getIp().c_str());
self->player->disconnect();
}
});
receive();
}
void GameConnection::receive() {
asio::async_read_until(socket, recvBuffer, packetMatcher,
std::bind(&GameConnection::onReceive, shared_from_this(), asio::placeholders::error, asio::placeholders::bytes_transferred));
}
void GameConnection::sendPacket(uint8_t opcode, const uint8_t *payload, unsigned size)
{
*(uint16_t *)&sendBuffer[sendIdx] = size + 3;
sendBuffer[sendIdx + 2] = opcode;
if (size != 0)
memcpy(&sendBuffer[sendIdx + 3], payload, size);
sendIdx += size + 3;
send();
}
void GameConnection::send()
{
if (sending)
return;
sending = true;
uint16_t packetSize = *(uint16_t *)&sendBuffer[0];
asio::async_write(socket, asio::buffer(sendBuffer, packetSize),
std::bind(&GameConnection::onSent, shared_from_this(),
asio::placeholders::error,
asio::placeholders::bytes_transferred));
}
void GameConnection::onReceive(const std::error_code& ec, size_t len)
{
if (ec || len < 3)
{
std::string addr;
if (player)
addr = player->getIp();
else
addr = "?.?.?.?";
if (ec && ec != asio::error::eof && ec != asio::error::operation_aborted
&& ec != asio::error::bad_descriptor)
ERROR_LOG("[%s] onReceive: %s", addr.c_str(), ec.message().c_str());
else if (len != 0)
ERROR_LOG("[%s] onReceive: small packet: %zd", addr.c_str(), len);
if (player)
player->disconnect();
return;
}
// Check for (too) large packets
uint16_t pktlen = *(uint16_t *)recvBuffer.bytes();
if (pktlen > MAX_PKT_LEN)
{
std::string addr;
if (player)
addr = player->getIp();
else
addr = "?.?.?.?";
ERROR_LOG("[%s] onReceive: jumbo packet: %zd", addr.c_str(), pktlen);
if (player)
player->disconnect();
return;
}
// Grab data and process if correct.
if (player->receiveTcp(recvBuffer.bytes(), len))
timeoutTimer.cancel();
recvBuffer.consume(len);
receive();
}
void GameConnection::onSent(const std::error_code& ec, size_t len)
{
if (ec)
{
if (ec != asio::error::eof && ec != asio::error::bad_descriptor)
ERROR_LOG("[%s] onSent: %s", player != nullptr ? player->getIp().c_str() : "?.?.?.?",
ec.message().c_str());
if (player)
player->disconnect();
return;
}
sending = false;
assert(len <= sendIdx);
sendIdx -= len;
if (sendIdx != 0) {
memmove(&sendBuffer[0], &sendBuffer[len], sendIdx);
send();
}
}
void GameConnection::close()
{
std::error_code ec;
socket.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
player = nullptr;
}
bool Player::receiveTcp(const uint8_t *data, size_t len)
{
switch (data[2])
{
case 0: // Login
{
if (data[7] == 0)
{
// Spectator
NOTICE_LOG("Adding spectator: %s", endpoint.address().to_string().c_str());
game->addSpectator(shared_from_this());
//uint8_t data[] { 0, 255, 0 };
//connection->sendPacket(0, data, sizeof(data));
uint8_t data[] { 1, 0, 0 }; // 1 occupied slot at 0?
connection->sendPacket(0, data, sizeof(data));
game->sendPlayerList();
return true;
}
if (len < 43) {
WARN_LOG("%s [%s] TCP packet 0 is too short: %zd", name.c_str(), getIp().c_str(), len);
dumpData(data, len);
break;
}
// port is assumed to be 7980 (offset 5)
// offset 9 might be PID (6 used, 18 total)
name = (const char *)&data[27];
setExtraData(&data[27 + 8]);
bool alien = (bool)data[36];
assignSlot(alien);
uint8_t data[] { 1, (uint8_t)slotNum, 0 };
connection->sendPacket(0, data, sizeof(data));
game->sendPlayerList();
int armySlots = 0, alienSlots = 0;
std::vector<std::string> players;
for (int i = 0; i < 8; i++)
{
if (game->getSlotType(i) == Game::Open || game->getSlotType(i) == Game::Open_CPU)
{
if (i >= 4)
alienSlots++;
else
armySlots++;
}
else if (game->getPlayer(i) != nullptr) {
players.push_back(game->getPlayer(i)->getName());
}
}
if (players.size() == 1)
discordGameCreated(game->getType(), game->getName(), name, armySlots, alienSlots);
else
discordGameJoined(game->getType(), game->getName(), name, players, armySlots, alienSlots);
return true;
}
case 1: // Update extra data?
{
if (len < 20) {
WARN_LOG("%s [%s] TCP packet 1 is too short: %zd", name.c_str(), getIp().c_str(), len);
}
else
{
DEBUG_LOG("%s [%s][slot %d] Packet1 updateState", name.c_str(), getIp().c_str(), slotNum);
setExtraData(&data[12]);
// broadcast as 14 00 02 ...
std::array<uint8_t, 20> out;
memcpy(out.data(), data, sizeof(out));
out[2] = 2;
game->tcpSendToAll(out.data(), sizeof(out), shared_from_this());
}
break;
}
case 7: // Player list ack'ed ?
DEBUG_LOG("%s [%s][slot %d] Player list ack'ed", name.c_str(), getIp().c_str(), slotNum);
break;
case 0x78: // ???
DEBUG_LOG("%s [%s][slot %d] Packet 78", name.c_str(), getIp().c_str(), slotNum);
connection->sendPacket(0x78, &data[3], len - 3);
// broadcast to other players
if (slotNum != -1)
game->tcpSendToAll(data, len, shared_from_this());
break;
default:
WARN_LOG("%s [%s] Unhandled game packet: %02x", name.c_str(), getIp().c_str(), data[2]);
dumpData(data, len);
break;
}
return false;
}
void Player::sendTcp(const uint8_t *data, size_t len) {
connection->sendPacket(data[2], &data[3], len - 3);
}
int Player::assignSlot(bool alien)
{
slotNum = game->assignSlot(shared_from_this(), alien);
DEBUG_LOG("Player %s [%s] assigned slot %d", name.c_str(), getIp().c_str(), slotNum);
return slotNum;
}
void Player::disconnect()
{
if (connection != nullptr) {
connection->close();
connection = nullptr;
}
if (game != nullptr)
{
// avoid endless recursivity
auto lgame = game;
game = nullptr;
if (slotNum != -1)
lgame->disconnect(shared_from_this());
else
lgame->removeSpectator(shared_from_this());
}
}