108 lines
2.8 KiB
C++
108 lines
2.8 KiB
C++
#include "connector.h"
|
|
|
|
Connector::Connector() {}
|
|
|
|
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;
|
|
addr.sin_port = htons(CLI_PORT);
|
|
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;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int Connector::Connect(const char* srv_addr, std::string name) {
|
|
sockaddr_in add;
|
|
m_srvsockaddr.sin_family = AF_INET;
|
|
m_srvsockaddr.sin_port = htons(SRV_PORT);
|
|
|
|
if (inet_pton(AF_INET, srv_addr, &m_srvsockaddr.sin_addr) <= 0) {
|
|
std::cout << "Addresse serveur invalide." << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
if (connect(m_sock_tcp, (sockaddr*)&m_srvsockaddr, sizeof(m_srvsockaddr)) < 0) {
|
|
std::cout << "Échec de la connexion." << std::endl;
|
|
}
|
|
|
|
char* buf = new char[150];
|
|
uint32_t buflen = 150;
|
|
netprot::LoginInfo log;
|
|
memcpy(&log.name, name.c_str(), name.size() + 1);
|
|
|
|
netprot::Serialize(&log, &buf, &buflen);
|
|
|
|
int se = send(m_sock_tcp, buf, buflen, 0);
|
|
|
|
delete[] buf;
|
|
buf = new char[150] {0};
|
|
buflen = 150;
|
|
int rpack = 0;
|
|
int errors = 0;
|
|
while (rpack < 2) {
|
|
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;
|
|
}
|
|
++rpack;
|
|
break;
|
|
case netprot::PACKET_TYPE::GAMEINFO:
|
|
if (!netprot::Deserialize(&m_gameinfo, buf, buflen)) {
|
|
std::cout << "Packet GameInfo invalide." << std::endl;
|
|
return 3;
|
|
}
|
|
++rpack;
|
|
break;
|
|
case netprot::PACKET_TYPE::PLAYINF:
|
|
// TODO: Populer un remotePlayer avec ça.
|
|
break;
|
|
default:
|
|
++errors;
|
|
//std::cout << "Packet invalide." << std::endl;
|
|
break;
|
|
}
|
|
if (errors > 100)
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
uint64_t Connector::getId() const { return m_loginfo.sid; }
|
|
|
|
unsigned int Connector::getSeed() const { return m_gameinfo.seed; }
|