2019-01-09 20:44:58 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: Client.cpp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Created: 25/01/2018 12:26:43
|
|
|
|
*
|
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#include <gk/core/input/GamePad.hpp>
|
|
|
|
#include <gk/core/input/InputHandler.hpp>
|
|
|
|
#include <gk/core/GameClock.hpp>
|
|
|
|
#include <gk/core/Exception.hpp>
|
|
|
|
|
|
|
|
#include "Client.hpp"
|
|
|
|
|
|
|
|
void Client::connect(sf::IpAddress serverAddress, u16 serverPort) {
|
|
|
|
m_serverAddress = serverAddress;
|
|
|
|
m_serverPort = serverPort;
|
|
|
|
|
|
|
|
m_tcpSocket.reset(new sf::TcpSocket);
|
|
|
|
if (serverAddress.toInteger() == 0 || m_tcpSocket->connect(serverAddress, serverPort, sf::seconds(5)) != sf::Socket::Done)
|
|
|
|
throw EXCEPTION("Network error: Unable to connect to server", serverAddress.toString() + ":" + std::to_string(serverPort));
|
|
|
|
|
|
|
|
if (m_socket.bind(0) != sf::Socket::Done)
|
|
|
|
throw EXCEPTION("Network error: Bind failed");
|
|
|
|
|
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::ClientConnect << sf::IpAddress::getLocalAddress().toString() << m_socket.getLocalPort();
|
|
|
|
m_tcpSocket->send(packet);
|
|
|
|
|
|
|
|
sf::Packet answer;
|
|
|
|
m_tcpSocket->receive(answer);
|
|
|
|
|
|
|
|
Network::Command command;
|
|
|
|
answer >> command;
|
|
|
|
if (command == Network::Command::ClientRefused)
|
2019-01-15 21:54:11 +01:00
|
|
|
throw EXCEPTION("Server error: You can't join an already started game."); // FIXME
|
2019-01-09 20:44:58 +01:00
|
|
|
|
|
|
|
if (command != Network::Command::ClientOk)
|
|
|
|
throw EXCEPTION("Network error: Expected 'ClientOk' packet.");
|
|
|
|
answer >> m_id;
|
|
|
|
|
|
|
|
m_tcpSocket->setBlocking(false);
|
|
|
|
m_socket.setBlocking(false);
|
|
|
|
|
|
|
|
m_isConnected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::disconnect() {
|
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::ClientDisconnect;
|
|
|
|
m_tcpSocket->send(packet);
|
|
|
|
}
|
|
|
|
|
2019-01-16 22:24:57 +01:00
|
|
|
void Client::send(sf::Packet &packet) {
|
|
|
|
if (m_tcpSocket)
|
|
|
|
m_tcpSocket->send(packet);
|
|
|
|
else
|
|
|
|
throw EXCEPTION("Network error: Trying to send a packet without being connected");
|
|
|
|
}
|
|
|
|
|
2019-01-09 20:44:58 +01:00
|
|
|
void Client::sendKeyState() {
|
|
|
|
if (!m_keyUpdateTimer.isStarted())
|
|
|
|
m_keyUpdateTimer.start();
|
|
|
|
|
|
|
|
if (m_keyUpdateTimer.time() > 15) {
|
|
|
|
gk::InputHandler *inputHandler = gk::GamePad::getInputHandler();
|
|
|
|
if (inputHandler) {
|
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::KeyState << gk::GameClock::getTicks() << m_id;
|
|
|
|
for (auto &it : inputHandler->keysPressed()) {
|
|
|
|
packet << static_cast<u8>(it.first) << it.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_socket.send(packet, m_serverAddress, m_serverPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_keyUpdateTimer.reset();
|
|
|
|
m_keyUpdateTimer.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 01:28:24 +01:00
|
|
|
void Client::update() {
|
2019-01-09 20:44:58 +01:00
|
|
|
sf::Packet packet;
|
|
|
|
sf::IpAddress senderAddress;
|
|
|
|
u16 senderPort;
|
|
|
|
while (m_socket.receive(packet, senderAddress, senderPort) == sf::Socket::Done) {
|
|
|
|
Network::Command command;
|
|
|
|
packet >> command;
|
|
|
|
|
2019-01-12 13:52:30 +01:00
|
|
|
// std::cout << "UDP Message of type '" << Network::commandToString(command) << "' received from: " << senderAddress << ":" << senderPort << std::endl;
|
2019-01-09 20:44:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (m_tcpSocket->receive(packet) == sf::Socket::Done) {
|
|
|
|
Network::Command command;
|
|
|
|
packet >> command;
|
|
|
|
|
2019-01-16 22:24:57 +01:00
|
|
|
// DEBUG("TCP message received:", Network::commandToString(command));
|
2019-01-12 13:52:30 +01:00
|
|
|
|
2019-04-07 15:40:37 +02:00
|
|
|
auto it = m_commands.find(command);
|
|
|
|
if (it != m_commands.end())
|
|
|
|
it->second(packet);
|
2019-01-09 20:44:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|