2023-09-25 08:23:52 -04:00
|
|
|
|
#include "connector.h"
|
|
|
|
|
|
2023-09-25 17:17:17 -04:00
|
|
|
|
Connector::Connector() {}
|
2023-09-25 08:23:52 -04:00
|
|
|
|
|
2023-09-25 17:17:17 -04:00
|
|
|
|
Connector::~Connector() {}
|
|
|
|
|
|
|
|
|
|
int Connector::Init() {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &m_wsaData) != 0) { /* Initialisation de l'environnement reseau (Windows only) */
|
|
|
|
|
std::cout << "Initialisation WinSock." << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
m_sock_udp = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
if (m_sock_udp == INVALID_SOCKET) { /* Creation du socket UDP */
|
|
|
|
|
std::cout << "Creation Socket UDP." << std::endl;
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_sock_tcp = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
|
if (m_sock_tcp == INVALID_SOCKET) { /* Creation du socket TCP */
|
|
|
|
|
std::cout << "Creation Socket TCP." << std::endl;
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Creation structure donnes descripteur du socket serveur */
|
|
|
|
|
sockaddr_in addr;
|
|
|
|
|
addr.sin_family = AF_INET;
|
2023-09-27 11:24:41 -04:00
|
|
|
|
addr.sin_port = htons(CLI_PORT);
|
2023-09-25 17:17:17 -04:00
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
|
|
if (bind(m_sock_udp, (sockaddr*)&addr, sizeof(addr)) != 0) { /* Associer le socket UDP au port */
|
|
|
|
|
std::cout << "Association Socket UDP." << std::endl;
|
|
|
|
|
return 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bind(m_sock_tcp, (sockaddr*)&addr, sizeof(addr)) != 0) { /* Associer le socket TCP au port */
|
|
|
|
|
std::cout << "Association Socket TCP." << std::endl;
|
|
|
|
|
return 5;
|
|
|
|
|
}
|
2023-09-25 08:23:52 -04:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 15:41:07 -04:00
|
|
|
|
int Connector::Connect(const char* srv_addr, std::string name) {
|
2023-09-27 11:24:41 -04:00
|
|
|
|
sockaddr_in add;
|
2023-09-27 17:34:25 -04:00
|
|
|
|
m_srvsockaddr.sin_family = AF_INET;
|
|
|
|
|
m_srvsockaddr.sin_port = htons(SRV_PORT);
|
2023-09-27 11:24:41 -04:00
|
|
|
|
|
2023-09-27 17:34:25 -04:00
|
|
|
|
if (inet_pton(AF_INET, srv_addr, &m_srvsockaddr.sin_addr) <= 0) {
|
2023-09-27 11:24:41 -04:00
|
|
|
|
std::cout << "Addresse serveur invalide." << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2023-09-27 17:34:25 -04:00
|
|
|
|
|
|
|
|
|
if (connect(m_sock_tcp, (sockaddr*)&m_srvsockaddr, sizeof(m_srvsockaddr)) < 0) {
|
2023-09-27 11:24:41 -04:00
|
|
|
|
std::cout << "<EFBFBD>chec de la connexion." << std::endl;
|
|
|
|
|
}
|
2023-09-27 17:34:25 -04:00
|
|
|
|
|
2023-09-27 11:24:41 -04:00
|
|
|
|
char* buf = new char[150];
|
|
|
|
|
uint32_t buflen = 150;
|
2023-09-27 17:34:25 -04:00
|
|
|
|
netprot::LoginInfo log;
|
2023-09-27 11:24:41 -04:00
|
|
|
|
memcpy(&log.name, name.c_str(), name.size() + 1);
|
|
|
|
|
|
|
|
|
|
netprot::Serialize(&log, &buf, &buflen);
|
2023-09-27 17:34:25 -04:00
|
|
|
|
|
2023-09-27 11:24:41 -04:00
|
|
|
|
int se = send(m_sock_tcp, buf, buflen, 0);
|
|
|
|
|
|
|
|
|
|
delete[] buf;
|
|
|
|
|
buf = new char[150] {0};
|
|
|
|
|
buflen = 150;
|
2023-10-27 14:37:53 -04:00
|
|
|
|
bool ready = false;
|
2023-10-18 09:59:34 -04:00
|
|
|
|
int errors = 0;
|
2023-10-27 14:37:53 -04:00
|
|
|
|
netprot::PlayerInfo pinfo{};
|
|
|
|
|
while (!ready) {
|
2023-09-27 17:34:25 -04:00
|
|
|
|
recv(m_sock_tcp, buf, buflen, 0);
|
|
|
|
|
|
|
|
|
|
switch (netprot::getType(buf, buflen)) {
|
|
|
|
|
case netprot::PACKET_TYPE::LOGINF:
|
|
|
|
|
if (!netprot::Deserialize(&m_loginfo, buf, buflen)) {
|
|
|
|
|
std::cout << "Packet LoginInfo invalide." << std::endl;
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case netprot::PACKET_TYPE::GAMEINFO:
|
|
|
|
|
if (!netprot::Deserialize(&m_gameinfo, buf, buflen)) {
|
|
|
|
|
std::cout << "Packet GameInfo invalide." << std::endl;
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2023-10-25 12:16:14 -04:00
|
|
|
|
case netprot::PACKET_TYPE::PLAYINF:
|
2023-10-27 14:37:53 -04:00
|
|
|
|
if (!netprot::Deserialize(&pinfo, buf, buflen)) {
|
|
|
|
|
std::cout << "Packet PlayerInfo invalide." << std::endl;
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
m_players[pinfo.id] = pinfo;
|
|
|
|
|
break;
|
|
|
|
|
case netprot::PACKET_TYPE::SYNC:
|
|
|
|
|
if (!netprot::Deserialize(&m_origin, buf, buflen)) {
|
|
|
|
|
std::cout << "Packet Sync invalide." << std::endl;
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
ready = true;
|
2023-10-25 12:16:14 -04:00
|
|
|
|
break;
|
2023-09-27 17:34:25 -04:00
|
|
|
|
default:
|
2023-10-18 09:59:34 -04:00
|
|
|
|
++errors;
|
|
|
|
|
//std::cout << "Packet invalide." << std::endl;
|
2023-09-27 17:34:25 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
2023-10-18 09:59:34 -04:00
|
|
|
|
if (errors > 100)
|
|
|
|
|
return 1;
|
2023-09-27 11:24:41 -04:00
|
|
|
|
}
|
2023-09-25 08:23:52 -04:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 17:34:25 -04:00
|
|
|
|
uint64_t Connector::getId() const { return m_loginfo.sid; }
|
2023-09-25 08:23:52 -04:00
|
|
|
|
|
2023-09-27 17:34:25 -04:00
|
|
|
|
unsigned int Connector::getSeed() const { return m_gameinfo.seed; }
|
2023-10-27 14:37:53 -04:00
|
|
|
|
|
|
|
|
|
netprot::Sync Connector::getOrigin() const { return m_origin; }
|