Skip to content

Commit

Permalink
[ServerConnectState] Now prints connection error messages instead of …
Browse files Browse the repository at this point in the history
…closing the client.
  • Loading branch information
Unarelith committed May 5, 2020
1 parent 9f09a0a commit a22bbf9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
10 changes: 5 additions & 5 deletions source/client/network/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ void Client::connect(sf::IpAddress serverAddress, u16 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));
throw ClientConnectException("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");
throw ClientConnectException("Network error: Bind failed");

sf::Packet packet;
packet << Network::Command::ClientConnect << sf::IpAddress::getLocalAddress().toString() << m_socket.getLocalPort();
Expand All @@ -52,15 +52,15 @@ void Client::connect(sf::IpAddress serverAddress, u16 serverPort) {
Network::Command command;
answer >> command;
if (command == Network::Command::ClientRefused)
throw EXCEPTION("Server error: Connection refused. Server probably reached max player amount.");
throw ClientConnectException("Server error: Connection refused. Server probably reached max player amount.");

bool isSingleplayer;
if (command != Network::Command::ClientOk)
throw EXCEPTION("Network error: Expected 'ClientOk' packet.");
throw ClientConnectException("Network error: Expected 'ClientOk' packet.");

answer >> m_id >> isSingleplayer;
if (m_isSingleplayer != isSingleplayer)
throw EXCEPTION("Client error: The server is not valid");
throw ClientConnectException("Client error: The server is not valid");

m_tcpSocket->setBlocking(false);
m_socket.setBlocking(false);
Expand Down
13 changes: 13 additions & 0 deletions source/client/network/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@

#include "Network.hpp"

class ClientConnectException {
public:
ClientConnectException(const std::string &str)
: m_str(str) {}

virtual const char *what() const noexcept {
return m_str.c_str();
}

private:
std::string m_str;
};

class Client {
using CommandCallback = std::function<void(sf::Packet &packet)>;

Expand Down
6 changes: 3 additions & 3 deletions source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@
GameState::GameState()
: m_textureAtlas(gk::ResourceHandler::getInstance().get<TextureAtlas>("atlas-blocks"))
{
gk::Mouse::setCursorVisible(false);
gk::Mouse::setCursorGrabbed(true);

initShaders();

m_clientCommandHandler.setupCallbacks();
Expand All @@ -69,6 +66,9 @@ void GameState::init() {
void GameState::connect(const std::string &host, int port) {
m_client.connect(host, port);
m_player.setClientID(m_client.id());

gk::Mouse::setCursorVisible(false);
gk::Mouse::setCursorGrabbed(true);
}

void GameState::onEvent(const SDL_Event &event) {
Expand Down
32 changes: 29 additions & 3 deletions source/client/states/ServerConnectState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,25 @@ ServerConnectState::ServerConnectState(gk::ApplicationState *parent) : Interface
}

auto &game = m_stateStack->push<GameState>();
game.connect(host, port);

auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, true, this);
serverLoadingState.setTexturePack(m_texturePack);
try {
game.connect(host, port);

auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, true, this);
serverLoadingState.setTexturePack(m_texturePack);
}
catch (ClientConnectException &e) {
gkError() << e.what();

m_stateStack->pop();

m_errorText.setText(e.what());
m_errorText.updateVertexBuffer();
m_errorText.setPosition(
Config::screenWidth / 2.0f - m_errorText.getSize().x * Config::guiScale / 2.0f,
Config::screenHeight / 2.0f - 30 * Config::guiScale
);
}
});

m_cancelButton.setText("Cancel");
Expand All @@ -69,6 +84,9 @@ ServerConnectState::ServerConnectState(gk::ApplicationState *parent) : Interface
m_cancelButton.setCallback([this](TextButton &) {
m_stateStack->pop();
});

m_errorText.setColor(gk::Color::Red);
m_errorText.setScale(Config::guiScale, Config::guiScale);
}

void ServerConnectState::onEvent(const SDL_Event &event) {
Expand All @@ -80,6 +98,11 @@ void ServerConnectState::onEvent(const SDL_Event &event) {

m_connectButton.setPosition(Config::screenWidth / 2.0f - m_connectButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 340);
m_cancelButton.setPosition(Config::screenWidth / 2.0f - m_cancelButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 261);

m_errorText.setPosition(
Config::screenWidth / 2.0f - m_errorText.getSize().x * Config::guiScale / 2.0f,
Config::screenHeight / 2.0f - 30 * Config::guiScale
);
}

if (!m_stateStack->empty() && &m_stateStack->top() == this) {
Expand All @@ -104,6 +127,9 @@ void ServerConnectState::draw(gk::RenderTarget &target, gk::RenderStates states)

target.draw(m_connectButton, states);
target.draw(m_cancelButton, states);

if (!m_errorText.text().empty())
target.draw(m_errorText, states);
}
}

2 changes: 2 additions & 0 deletions source/client/states/ServerConnectState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class ServerConnectState : public InterfaceState {
TextButton m_connectButton;
TextButton m_cancelButton;

Text m_errorText;

std::string m_texturePack;
};

Expand Down
7 changes: 6 additions & 1 deletion source/client/states/TitleScreenState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ void TitleScreenState::startSingleplayer(bool showLoadingState) {

void TitleScreenState::startMultiplayer(const std::string &host) {
auto &game = m_stateStack->push<GameState>();
game.connect(host, m_port);
try {
game.connect(host, m_port);
}
catch (ClientConnectException &e) {
throw EXCEPTION(e.what());
}

auto &serverLoadingState = m_stateStack->push<ServerLoadingState>(game, false, this);
serverLoadingState.setTexturePack(m_texturePack);
Expand Down

0 comments on commit a22bbf9

Please sign in to comment.