2023-09-24 08:45:40 -04:00
|
|
|
#include "connection.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-09-27 17:34:25 -04:00
|
|
|
Connection::Connection(SOCKET sock,
|
|
|
|
sockaddr_in sockaddr,
|
2023-10-25 12:16:14 -04:00
|
|
|
LoginInfo log,
|
|
|
|
PlayerInfo play):
|
2023-09-27 17:34:25 -04:00
|
|
|
m_sock(sock),
|
|
|
|
m_addr(sockaddr),
|
|
|
|
m_loginfo(log),
|
|
|
|
m_playinfo(play) {
|
2023-09-24 08:45:40 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-25 12:16:14 -04:00
|
|
|
Connection::~Connection() { closesocket(m_sock); }
|
2023-09-24 08:45:40 -04:00
|
|
|
|
2023-09-27 17:34:25 -04:00
|
|
|
uint64_t Connection::GetHash(bool self) const { return self? m_loginfo.sid: m_playinfo.id; }
|
2023-09-24 08:45:40 -04:00
|
|
|
|
2023-09-27 17:34:25 -04:00
|
|
|
uint64_t Connection::GetTeamHash() const { return m_loginfo.tid; }
|
2023-09-24 08:45:40 -04:00
|
|
|
|
2023-09-27 17:34:25 -04:00
|
|
|
std::string Connection::GetName() const { return m_loginfo.name; }
|
2023-09-24 08:45:40 -04:00
|
|
|
|
2023-11-27 13:11:24 -05:00
|
|
|
void Connection::AddInput(Input in) { m_input_manifest.insert({ in.timestamp, in }); m_input_vector.push_back(in); }
|
2023-09-24 11:07:03 -04:00
|
|
|
|
2023-10-25 12:16:14 -04:00
|
|
|
Output* Connection::getOutput(Timestamp time) {
|
2023-09-24 11:07:03 -04:00
|
|
|
auto out = m_output_manifest.find(time);
|
|
|
|
if (out != m_output_manifest.end())
|
|
|
|
return &out->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-10-25 12:16:14 -04:00
|
|
|
Sync Connection::getSync(Timestamp time) {
|
|
|
|
Sync sync;
|
2023-09-24 11:07:03 -04:00
|
|
|
auto out = m_output_manifest.find(time);
|
|
|
|
if (out != m_output_manifest.end()) {
|
|
|
|
sync.timestamp = out->second.timestamp;
|
|
|
|
sync.position = out->second.position;
|
2023-09-27 17:34:25 -04:00
|
|
|
sync.sid = m_loginfo.sid;
|
2023-09-24 11:07:03 -04:00
|
|
|
}
|
|
|
|
return sync;
|
|
|
|
}
|
|
|
|
|
2023-10-25 12:16:14 -04:00
|
|
|
SOCKET Connection::getSock() const { return m_sock; }
|
|
|
|
|
|
|
|
PlayerInfo* Connection::getInfo() const { return (PlayerInfo*)&m_playinfo; }
|
|
|
|
|
2023-10-26 17:57:42 -04:00
|
|
|
sockaddr_in* Connection::getAddr() const { return (sockaddr_in*)&m_addr; }
|
|
|
|
|
2023-11-06 17:59:57 -05:00
|
|
|
void Connection::getPacks(SOCKET sock) {
|
2023-11-06 16:43:52 -05:00
|
|
|
std::vector<char*> lsPck;
|
|
|
|
Input in;
|
2023-11-24 15:32:53 -05:00
|
|
|
Sync sync;
|
2023-11-06 17:59:57 -05:00
|
|
|
lsPck = recvPacksFrom(sock, &m_buf, m_addr);
|
2023-11-06 16:43:52 -05:00
|
|
|
for (auto& pck : lsPck) {
|
2023-11-06 17:59:57 -05:00
|
|
|
uint32_t bsize = m_buf.len - (pck - m_buf.ptr);
|
2023-11-06 16:43:52 -05:00
|
|
|
switch (netprot::getType(pck, 1)) {
|
|
|
|
using enum netprot::PACKET_TYPE;
|
|
|
|
case INPUT:
|
2023-11-24 13:42:53 -05:00
|
|
|
if (Deserialize(&in, pck, &bsize)) {
|
2023-11-06 17:59:57 -05:00
|
|
|
m_input_manifest[in.timestamp] = in;
|
2023-11-24 14:15:40 -05:00
|
|
|
m_input_vector.push_back(in);
|
2023-11-24 13:42:53 -05:00
|
|
|
}
|
2023-11-06 16:43:52 -05:00
|
|
|
break;
|
2023-11-24 15:32:53 -05:00
|
|
|
case SYNC:
|
|
|
|
if (Deserialize(&sync, pck, &bsize))
|
|
|
|
m_nsync = true;
|
|
|
|
break;
|
2023-11-06 16:43:52 -05:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lsPck.clear();
|
|
|
|
}
|
|
|
|
|
2023-11-06 17:59:57 -05:00
|
|
|
void Connection::sendPacks(SOCKET sock, std::unordered_map<uint64_t, Connection*> conns) {
|
2023-11-24 15:56:58 -05:00
|
|
|
while (!m_output_vector.empty()) {
|
|
|
|
Output out = m_output_vector.front();
|
2023-11-06 17:59:57 -05:00
|
|
|
for (auto& [key, conn] : conns) {
|
2023-11-24 14:33:48 -05:00
|
|
|
if (m_playinfo.id == conn->GetHash(false))
|
2023-11-06 17:59:57 -05:00
|
|
|
continue;
|
2023-11-27 13:11:24 -05:00
|
|
|
//std::cout << m_playinfo.id << ": " << m_playinfo.name << ": " << conn->GetName() << std::endl;
|
|
|
|
std::cout << out.id << ": " << out.position << std::endl;
|
2023-11-27 14:24:11 -05:00
|
|
|
|
2023-11-06 17:59:57 -05:00
|
|
|
sendPackTo<Output>(sock, &out, &m_bufout, conn->getAddr());
|
|
|
|
}
|
2023-11-24 15:56:58 -05:00
|
|
|
m_output_vector.pop_front();
|
2023-11-06 17:59:57 -05:00
|
|
|
}
|
|
|
|
}
|
2023-11-06 16:43:52 -05:00
|
|
|
|
|
|
|
void Connection::Run(World* world) {
|
|
|
|
Input in, last;
|
|
|
|
Output out;
|
|
|
|
float el;
|
|
|
|
|
|
|
|
if (m_input_manifest.size() < 2)
|
|
|
|
return;
|
|
|
|
|
2023-11-24 14:15:40 -05:00
|
|
|
while (m_last_in < m_input_vector.size() - 1) {
|
|
|
|
in = m_input_vector.at(m_last_in + 1);
|
|
|
|
last = m_input_vector.at(m_last_in);
|
2023-11-06 17:59:57 -05:00
|
|
|
|
|
|
|
el = (float)(in.timestamp - last.timestamp) / 1000.;
|
|
|
|
player.get()->SetDirection(in.direction);
|
|
|
|
player.get()->ApplyPhysics(player.get()->GetInput(in.keys.forward,
|
|
|
|
in.keys.backward,
|
|
|
|
in.keys.left,
|
|
|
|
in.keys.right,
|
|
|
|
in.keys.jump, false, el), world, el);
|
2023-11-06 16:43:52 -05:00
|
|
|
|
2023-11-24 12:43:09 -05:00
|
|
|
out.position = player.get()->GetPositionAbs();
|
2023-11-27 14:24:11 -05:00
|
|
|
player.get()->GetPositionAbs().Afficher();
|
2023-11-06 17:59:57 -05:00
|
|
|
out.direction = in.direction;
|
|
|
|
out.timestamp = in.timestamp;
|
|
|
|
out.id = m_playinfo.id;
|
2023-11-06 16:43:52 -05:00
|
|
|
|
2023-11-06 17:59:57 -05:00
|
|
|
m_output_manifest[out.timestamp] = out;
|
2023-11-24 14:15:40 -05:00
|
|
|
m_output_vector.push_back(out);
|
2023-11-27 14:24:11 -05:00
|
|
|
|
2023-11-06 17:59:57 -05:00
|
|
|
++m_last_in;
|
|
|
|
}
|
2023-11-06 16:43:52 -05:00
|
|
|
}
|
|
|
|
|
2023-09-24 11:07:03 -04:00
|
|
|
void Connection::CleanInputManifest(Timestamp time) {
|
|
|
|
auto wat = m_input_manifest.find(time);
|
|
|
|
|
|
|
|
while (wat != m_input_manifest.begin())
|
|
|
|
m_input_manifest.erase(wat--);
|
2023-09-24 08:45:40 -04:00
|
|
|
}
|