#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[1500]; uint32_t buflen = 1500; 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; bool ready = false; int errors = 0; netprot::PlayerInfo pinfo{}; while (!ready) { uint32_t bytes = recv(m_sock_tcp, buf, buflen, 0); uint32_t curr = 0; char* currbuf = buf; while (bytes > 0) { switch (netprot::getType(currbuf, buflen)) { case netprot::PACKET_TYPE::LOGINF: if (!netprot::Deserialize(&m_loginfo, currbuf, buflen)) { std::cout << "Packet LoginInfo invalide." << std::endl; return 2; } curr = sizeof(netprot::LoginInfo); break; case netprot::PACKET_TYPE::GAMEINFO: if (!netprot::Deserialize(&m_gameinfo, currbuf, buflen)) { std::cout << "Packet GameInfo invalide." << std::endl; return 3; } curr = sizeof(netprot::GameInfo); break; case netprot::PACKET_TYPE::PLAYINF: if (!netprot::Deserialize(&pinfo, currbuf, buflen)) { std::cout << "Packet PlayerInfo invalide." << std::endl; return 3; } m_players[pinfo.id] = pinfo; curr = sizeof(netprot::PlayerInfo); break; case netprot::PACKET_TYPE::SYNC: if (!netprot::Deserialize(&m_origin, currbuf, buflen)) { std::cout << "Packet Sync invalide." << std::endl; return 3; } curr = sizeof(netprot::Sync); ready = true; break; default: ++errors; //std::cout << "Packet invalide." << std::endl; break; } currbuf = &currbuf[curr]; bytes -= curr; 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; } netprot::Sync Connector::getOrigin() const { return m_origin; }