77 lines
1.8 KiB
C++
77 lines
1.8 KiB
C++
#include "game/Connexion.h"
|
|
#include "protocol/PacketDispatcher.h"
|
|
#include "protocol/PacketFactory.h"
|
|
#include "game/server/Server.h"
|
|
#include "misc/Compression.h"
|
|
|
|
#include "misc/Time.h"
|
|
|
|
#include <iostream>
|
|
#include <chrono>
|
|
|
|
namespace td {
|
|
namespace protocol {
|
|
|
|
Connexion::Connexion() : protocol::PacketHandler(nullptr){
|
|
|
|
}
|
|
|
|
Connexion::Connexion(Connexion&& move) : m_Socket(std::move(move.m_Socket)), protocol::PacketHandler(&m_Dispatcher){
|
|
|
|
}
|
|
|
|
Connexion::Connexion(protocol::PacketDispatcher* dispatcher) : protocol::PacketHandler(dispatcher){
|
|
|
|
}
|
|
|
|
Connexion::Connexion(protocol::PacketDispatcher* dispatcher, network::TCPSocket& socket) : protocol::PacketHandler(dispatcher), m_Socket(std::move(socket)){
|
|
|
|
}
|
|
|
|
bool Connexion::updateSocket(){
|
|
if(m_Socket.GetStatus() != network::Socket::Connected)
|
|
return false;
|
|
|
|
DataBuffer buffer;
|
|
m_Socket.Receive(buffer, sizeof(std::size_t));
|
|
if (buffer.GetSize() > 0){
|
|
std::size_t packetLenght;
|
|
buffer >> packetLenght;
|
|
|
|
m_Socket.Receive(buffer, packetLenght);
|
|
|
|
DataBuffer decompressed = utils::Decompress(buffer, packetLenght);
|
|
|
|
protocol::PacketType packetType;
|
|
decompressed >> packetType;
|
|
|
|
protocol::Packet* packet = protocol::PacketFactory::createPacket(packetType, decompressed);
|
|
GetDispatcher()->Dispatch(packet);
|
|
delete packet;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool Connexion::connect(const std::string& address, std::uint16_t port){
|
|
if(!m_Socket.Connect(address, port)){
|
|
return false;
|
|
}
|
|
m_Socket.SetBlocking(false);
|
|
return true;
|
|
}
|
|
|
|
void Connexion::sendPacket(protocol::Packet* packet){
|
|
network::SendPacket(packet->Serialize(), m_Socket);
|
|
}
|
|
|
|
void Connexion::closeConnection(){
|
|
m_Socket.Disconnect();
|
|
}
|
|
|
|
Connexion::~Connexion(){
|
|
|
|
}
|
|
|
|
} // namespace server
|
|
} // namespace td
|